Skip to content

Commit

Permalink
algorithmic:0.1.0 (#60)
Browse files Browse the repository at this point in the history
  • Loading branch information
lf- committed Aug 19, 2023
1 parent bfa4e8d commit 0694c9a
Show file tree
Hide file tree
Showing 11 changed files with 274 additions and 0 deletions.
9 changes: 9 additions & 0 deletions packages/preview/algorithmic/0.1.0/LICENSES/MIT.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
MIT License

Copyright (c) <year> <copyright holders>

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.
135 changes: 135 additions & 0 deletions packages/preview/algorithmic/0.1.0/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
<!--
SPDX-FileCopyrightText: 2023 Jade Lovelace
SPDX-License-Identifier: MIT
-->

# 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.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
SPDX-FileCopyrightText: 2023 Jade Lovelace

SPDX-License-Identifier: MIT
29 changes: 29 additions & 0 deletions packages/preview/algorithmic/0.1.0/algorithmic-demo.typ
Original file line number Diff line number Diff line change
@@ -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*]
})
})
79 changes: 79 additions & 0 deletions packages/preview/algorithmic/0.1.0/algorithmic.typ
Original file line number Diff line number Diff line change
@@ -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,)
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
SPDX-FileCopyrightText: 2023 Jade Lovelace

SPDX-License-Identifier: MIT
Binary file added packages/preview/algorithmic/0.1.0/test.pdf
Binary file not shown.
7 changes: 7 additions & 0 deletions packages/preview/algorithmic/0.1.0/test.typ
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#import "algorithmic.typ": algorithm
#import "algorithmic.typ"

#algorithm({
import algorithmic: *
Cmt[blah]
})
9 changes: 9 additions & 0 deletions packages/preview/algorithmic/0.1.0/typst.toml
Original file line number Diff line number Diff line change
@@ -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"

0 comments on commit 0694c9a

Please sign in to comment.