Skip to content

Commit

Permalink
efilrst:0.3.1 (#1611)
Browse files Browse the repository at this point in the history
  • Loading branch information
jmigual authored Jan 23, 2025
1 parent 1d9436c commit e86fade
Show file tree
Hide file tree
Showing 6 changed files with 276 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/preview/efilrst/0.3.1/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
build/
21 changes: 21 additions & 0 deletions packages/preview/efilrst/0.3.1/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 Joan Marcè i Igual

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
94 changes: 94 additions & 0 deletions packages/preview/efilrst/0.3.1/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
# Typst efilrst
A simple referenceable list library for Typst. If you ever wanted to reference elements in a list by a key, this library is for you. The name comes from "reflist" but sorted alphabetically because we are not allowed to use descriptive names for packages in Typst 🤷🏻‍♂️.

## Example

```typst
#import "@preview/efilrst:0.1.0" as efilrst
#show ref: efilrst.show-rule
#let constraint = efilrst.reflist.with(
name: "Constraint",
list-style: "C1.1.1)",
ref-style: "C1.1.1")
#constraint(
counter-name: "continuable",
[My cool constraint A],<c:a>,
[My also cool constraint B],<c:b>,
[My non-referenceable constraint C],
)
See how my @c:a is better than @c:b but not as cool as @c:e.
#constraint(
counter-name: "continuable",
[We continue the list with D],<c:d>,
[And then add constraint E],<c:e>,
)
#constraint(
[This is a new list!],<c:f>,
(
[And it has a sublist!],<c:g>,
[With a constraint H],<c:h>,
)
)
#constraint(
[This is another list!],<c:i>,
)
```

This generates the following output:

![Example of the typst output. The last sentence reads "See how my Constraint C1 is better than Constraint C2"](img/image.png)

## Reference

```typst
reflist(
..children,
name: "",
list-style: "1.1.1)",
ref-style: "1.1.1",
counter-name: auto,
start: 1,
full: true,
ref-joiner: sym.space.nobreak,
)
```

- `children`: Elements that will be part of the list. They can be given as a `content` followed by a `label` to reference them. If the `label` is not provided, it cannot be referenced. If an array is provided (e.g., `([Content], <label1>, [Content], <label2>)`), the elements will be part of a sublist. Sublists can be nested but they will always require at least one parent element where the sublist will be attached to.
- `name`: Name of the list. This will be used to reference the list.
- `list-style`: Style of the list. It will be used to generate the list numbers.
- `ref-style`: Style of the references. It will be used to generate the numbers of the references.
- `counter-name`: Name of the counter that will be used to generate the numbers. If `auto` is provided, `efilrst` will choose a non-colliding name. If a name is provided and is the same as the name of a previous list, the counter will continue from the last number of the previous list.
- `start`: Number from which the list will start.
- `full`: If `true`, the full reference will be shown. If `false`, only the number will be shown.
- `ref-joiner`: Symbol used to join the name of the reference and the reference itself. By default, a non-breaking space is added to mimic the default behaviour of typst for references. E.g., for a reference named `Constraint` and a reference `C1` it generates `[Constraint~C1]`. To disable it, pass `none`.


## License

This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.

## Changelog

### 0.1.0

- Initial release

### 0.2.0

- Add continuation of lists through the `counter` function

### 0.3.0

- Add support for nested lists

### 0.3.1

- Fix bad numbering [#1](https://github.com/jmigual/typst-efilrst/issues/1)
- Add option to have a non-breaking space between the name of the reference and the reference itself [#2](https://github.com/jmigual/typst-efilrst/issues/2)
Binary file added packages/preview/efilrst/0.3.1/img/image.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
149 changes: 149 additions & 0 deletions packages/preview/efilrst/0.3.1/src/lib.typ
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
#let s = counter("_efilrst-counter")
#let counter-prefix = "_efilrst-"

#let _make-pairs(children) = {
let childrenPairs = ()

// Gather children in body-label pairs
for (n, val) in children.enumerate() {
if (type(val) == content) {
childrenPairs.push((val, none))
} else if (type(val) == label and n > 0) {
let (body, lbl) = childrenPairs.last()
childrenPairs.last() = (body, val)
} else if (type(val) == array) {
let body = _make-pairs(val)
childrenPairs.push((body, none))
}
}

return childrenPairs
}

#let _get-counter(counter-name, counter-prefix, start) = {
let private-counter-name = ""
if (counter-name == auto) {
private-counter-name = counter-prefix + str(s.get().at(0))
} else {
private-counter-name = counter-prefix + counter-name
}


let c = counter(private-counter-name)
let level = 0
let counter-val = c.get().at(level)
if counter-val == 0 {
counter-val = start
}
return (c, counter-val)
}

#let _make-children(
childrenPairs,
counter-val,
ref-style,
name,
list-style,
full,
ref-joiner,
numbers: (),
) = {
let children = ()
let n = 0
for (body, lbl) in childrenPairs {
if type(body) == content {
if type(lbl) == label {
let num-text = if full {
numbering(ref-style, ..numbers, counter-val + n)
} else {
numbering(ref-style, counter-val + n)
}
let m = metadata((efilrst-type: "efilrst", efilrst-n: num-text, efilrst-name: name, efilrst-joiner: ref-joiner))
children.push([#body#m#lbl])
} else {
children.push([
#body
])
}
n += 1
} else if type(body) == array {
if n > 0 {
children.last() += _make-children(
body,
counter-val,
ref-style,
name,
list-style,
full,
ref-nb-space,
numbers: numbers + (n,),
)
}
else {
error("A nested list first needs an element")
}
}
}

return enum(numbering: list-style, start: counter-val, full: full, ..children)
}


#let reflist(
..children,
name: "",
list-style: "1.1.1)",
ref-style: "1.1.1",
counter-name: auto,
start: 1,
full: true,
ref-joiner: sym.space.nobreak,
) = (
context {
let childrenPairs = _make-pairs(children.pos())
let (c, counter-val) = _get-counter(counter-name, counter-prefix, start)

// Insert a metadata to be labelled
_make-children(
childrenPairs,
counter-val,
ref-style,
name,
list-style,
full,
ref-joiner,
)
s.step()
c.update(counter-val + childrenPairs.len())
}
)


#let show-rule(it) = {
if (
it.element != none and it.element.func() == metadata and type(it.element.value) == dictionary and it
.element
.value
.at("efilrst-type", default: none) == "efilrst"
) {
let itv = it.element.value
let sup = if (it.supplement != auto) {
[#it.supplement]
} else {
[#itv.efilrst-name]
}

if itv.efilrst-joiner != none and sup != [] {
sup = [#sup#itv.efilrst-joiner#itv.efilrst-n]
} else {
sup = [#sup #itv.efilrst-n]
}

link(it.element.location(), sup)
} else {
it
}
}



11 changes: 11 additions & 0 deletions packages/preview/efilrst/0.3.1/typst.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[package]
name = "efilrst"
version = "0.3.1"
entrypoint = "src/lib.typ"
authors = ["Joan Marcè i Igual <@jmigual>"]
license = "MIT"
repository = "https://github.com/jmigual/typst-efilrst"
keywords = ["typst", "list", "referenceable"]
compiler = "0.12.0"
description = "A simple referenceable list library for typst."
exclude = ["examples/*", "img/*", "tests/*"]

0 comments on commit e86fade

Please sign in to comment.