diff --git a/packages/preview/algorithmic/0.1.0/LICENSES/MIT.txt b/packages/preview/algorithmic/0.1.0/LICENSES/MIT.txt new file mode 100644 index 000000000..2071b23b0 --- /dev/null +++ b/packages/preview/algorithmic/0.1.0/LICENSES/MIT.txt @@ -0,0 +1,9 @@ +MIT License + +Copyright (c) + +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. diff --git a/packages/preview/algorithmic/0.1.0/README.md b/packages/preview/algorithmic/0.1.0/README.md new file mode 100644 index 000000000..285be76f1 --- /dev/null +++ b/packages/preview/algorithmic/0.1.0/README.md @@ -0,0 +1,135 @@ + + +# typst-algorithmic + +This is a package inspired by the LaTeX [`algorithmicx`][algorithmicx] package +for Typst. It's useful for writing pseudocode and typesetting it all nicely. + +[algorithmicx]: https://ctan.org/pkg/algorithmicx + +![screenshot of the typst-algorithmic output, showing line numbers, automatic +indentation, bolded keywords, and such](./docs/assets/demo-rendered.png) + +Example: + +```typst +#import "@preview/algorithmic:0.1.0" +#import algorithmic: algorithm + +#algorithm({ + import algorithmic: * + Function("Binary-Search", args: ("A", "n", "v"), { + Cmt[Initialize the search range] + Assign[$l$][$1$] + Assign[$r$][$n$] + State[] + While(cond: $l <= r$, { + Assign([mid], FnI[floor][$(l + r)/2$]) + If(cond: $A ["mid"] < v$, { + Assign[$l$][$m + 1$] + }) + ElsIf(cond: [$A ["mid"] > v$], { + Assign[$r$][$m - 1$] + }) + Else({ + Return[$m$] + }) + }) + Return[*null*] + }) +}) +``` + +This DSL is implemented using the same trick as [CeTZ] uses: a code block of +arrays gets those arrays joined together. + +[CeTZ]: https://github.com/johannes-wolf/typst-canvas + +Currently this library is not really customizable. Please vendor it and hack it +up as needed then file an issue for the customization option you're missing. + +## Reference + +#### stmt + +Statement-level contexts in `algorithmic` generally accept the type `body` in +the following: + +``` +body = (ast|content)[] | ast | content +ast = (change_indent: int, body: body) +``` + +#### inline + +Inline functions will generate plain content. + +#### `algorithmic(..bits)` + +Takes one or more lists of `ast` and creates an algorithmic block with line +numbers. + +### Control flow + +#### `Function`/`Procedure` (stmt) + +Defined as `f(name: string|content, args: content[]?, ..body)`. Body can be one or more `body` +values. + +#### `If`/`ElseIf`/`Else`/`For`/`While` (stmt) + +Defined as `f(cond: string|content, ..body)`. Body can be one or more `body` +values. + +Generates an indented block with the body, and the specified `cond` between the +two keywords as condition. + +### Statements + +#### `Assign` (stmt) + +Defined as `Assign(var: content, val: content)`. + +Generates `#var <- #val`. + +#### `CallI` (inline), `Call` (stmt) + +Defined as `f(name, args: content|content[])`. + +Calls a function with the function name styled in smallcaps and the args joined by +commas. + +#### `Cmt` (stmt) + +Defined as `Cmt(body: content)`. + +Makes a line comment. + +#### `FnI` (inline), `Fn` (stmt) + +Defined as `f(name, args: content|content[])`. + +Calls a function with the function name styled in bold and the args joined by +commas. + +#### `Ic` (inline) + +Defined as `Ic(body: content) -> content`. + +Makes an inline comment. + +#### `Return` (stmt) + +Defined as `Return(arg: content)`. + +Generates `return #arg`. + +#### `State` (stmt) + +Defined as `State(body: content)`. + +Turns any content into a line. diff --git a/packages/preview/algorithmic/0.1.0/algorithmic-demo.pdf b/packages/preview/algorithmic/0.1.0/algorithmic-demo.pdf new file mode 100644 index 000000000..b3618d86d Binary files /dev/null and b/packages/preview/algorithmic/0.1.0/algorithmic-demo.pdf differ diff --git a/packages/preview/algorithmic/0.1.0/algorithmic-demo.pdf.license b/packages/preview/algorithmic/0.1.0/algorithmic-demo.pdf.license new file mode 100644 index 000000000..2e3fe52fd --- /dev/null +++ b/packages/preview/algorithmic/0.1.0/algorithmic-demo.pdf.license @@ -0,0 +1,3 @@ +SPDX-FileCopyrightText: 2023 Jade Lovelace + +SPDX-License-Identifier: MIT diff --git a/packages/preview/algorithmic/0.1.0/algorithmic-demo.typ b/packages/preview/algorithmic/0.1.0/algorithmic-demo.typ new file mode 100644 index 000000000..e9ab472e2 --- /dev/null +++ b/packages/preview/algorithmic/0.1.0/algorithmic-demo.typ @@ -0,0 +1,29 @@ +// SPDX-FileCopyrightText: 2023 Jade Lovelace +// +// SPDX-License-Identifier: MIT + +#import "algorithmic.typ" +#import algorithmic: algorithm + +#algorithm({ + import algorithmic: * + Function("Binary-Search", args: ("A", "n", "v"), { + Cmt[Initialize the search range] + Assign[$l$][$1$] + Assign[$r$][$n$] + State[] + While(cond: $l <= r$, { + Assign([mid], FnI[floor][$(l + r)/2$]) + If(cond: $A ["mid"] < v$, { + Assign[$l$][$m + 1$] + }) + ElsIf(cond: [$A ["mid"] > v$], { + Assign[$r$][$m - 1$] + }) + Else({ + Return[$m$] + }) + }) + Return[*null*] + }) +}) diff --git a/packages/preview/algorithmic/0.1.0/algorithmic.typ b/packages/preview/algorithmic/0.1.0/algorithmic.typ new file mode 100644 index 000000000..9a9723992 --- /dev/null +++ b/packages/preview/algorithmic/0.1.0/algorithmic.typ @@ -0,0 +1,79 @@ +// SPDX-FileCopyrightText: 2023 Jade Lovelace +// +// SPDX-License-Identifier: MIT + +/* + * Generated AST: + * (change_indent: int, body: ((ast | content)[] | content | ast) + */ + +#let ast_to_content_list(indent, ast) = { + if type(ast) == "array" { + ast.map(d => ast_to_content_list(indent, d)) + } else if type(ast) == "content" { + (pad(left: indent * 0.5em, ast),) + } else if type(ast) == "dictionary" { + let new_indent = ast.at("change_indent", default: 0) + indent + ast_to_content_list(new_indent, ast.body) + } +} + +#let algorithm(..bits) = { + let content = bits.pos().map(b => ast_to_content_list(0, b)).flatten() + let table_bits = () + let lineno = 1 + + while lineno <= content.len() { + table_bits.push([#lineno:]) + table_bits.push(content.at(lineno - 1)) + lineno = lineno + 1 + } + table( + columns: (18pt, 100%), + // line spacing + inset: 0.3em, + stroke: none, + ..table_bits + ) +} + +#let iflike_block(kw1: "", kw2: "", cond: "", ..body) = ( + (strong(kw1) + " " + cond + " " + strong(kw2)), + // XXX: .pos annoys me here + (change_indent: 4, body: body.pos()) +) + +#let function_like(name, kw: "function", args: (), ..body) = ( + iflike_block(kw1: kw, cond: (smallcaps(name) + "(" + args.join(", ") + ")"), ..body) +) + +#let listify(v) = { + if type(v) == "list" { + v + } else { + (v,) + } +} + +#let Function = function_like.with(kw: "function") +#let Procedure = function_like.with(kw: "procedure") + +#let State(block) = ((body: block),) + +/// Inline call +#let CallI(name, args) = smallcaps(name) + "(" + listify(args).join(", ") + ")" +#let Call(..args) = (CallI(..args),) +#let FnI(f, args) = strong(f) + " (" + listify(args).join(", ") + ")" +#let Fn(..args) = (FnI(..args),) +#let Ic(c) = sym.triangle.stroked.r + " " + c +#let Cmt(c) = (Ic(c),) +// It kind of sucks that Else is a separate block but it's fine +#let If = iflike_block.with(kw1: "if", kw2: "then") +#let While = iflike_block.with(kw1: "while", kw2: "do") +#let For = iflike_block.with(kw1: "for", kw2: "do") +#let Assign(var, val) = (var + " " + $<-$ + " " + val,) + +#let Else = iflike_block.with(kw1: "else") +#let ElsIf = iflike_block.with(kw1: "else if", kw2: "then") +#let ElseIf = ElsIf +#let Return(arg) = (strong("return") + " " + arg,) diff --git a/packages/preview/algorithmic/0.1.0/docs/assets/demo-rendered.png b/packages/preview/algorithmic/0.1.0/docs/assets/demo-rendered.png new file mode 100644 index 000000000..d1e575e6e Binary files /dev/null and b/packages/preview/algorithmic/0.1.0/docs/assets/demo-rendered.png differ diff --git a/packages/preview/algorithmic/0.1.0/docs/assets/demo-rendered.png.license b/packages/preview/algorithmic/0.1.0/docs/assets/demo-rendered.png.license new file mode 100644 index 000000000..2e3fe52fd --- /dev/null +++ b/packages/preview/algorithmic/0.1.0/docs/assets/demo-rendered.png.license @@ -0,0 +1,3 @@ +SPDX-FileCopyrightText: 2023 Jade Lovelace + +SPDX-License-Identifier: MIT diff --git a/packages/preview/algorithmic/0.1.0/test.pdf b/packages/preview/algorithmic/0.1.0/test.pdf new file mode 100644 index 000000000..8bb2824af Binary files /dev/null and b/packages/preview/algorithmic/0.1.0/test.pdf differ diff --git a/packages/preview/algorithmic/0.1.0/test.typ b/packages/preview/algorithmic/0.1.0/test.typ new file mode 100644 index 000000000..68f28f170 --- /dev/null +++ b/packages/preview/algorithmic/0.1.0/test.typ @@ -0,0 +1,7 @@ +#import "algorithmic.typ": algorithm +#import "algorithmic.typ" + +#algorithm({ + import algorithmic: * + Cmt[blah] +}) diff --git a/packages/preview/algorithmic/0.1.0/typst.toml b/packages/preview/algorithmic/0.1.0/typst.toml new file mode 100644 index 000000000..8b627ab45 --- /dev/null +++ b/packages/preview/algorithmic/0.1.0/typst.toml @@ -0,0 +1,9 @@ +[package] +name = "algorithmic" +version = "0.1.0" +entrypoint = "algorithmic.typ" +authors = ["Jade Lovelace"] +license = "MIT" +repository = "https://github.com/lf-/typst-algorithmic" +exclude = ["docs/assets/*", "*.pdf"] +description = "Algorithm pseudocode typesetting for Typst, inspired by algorithmicx in LaTeX"