andreasKroepelin / lovelace

A Typst package for typesetting pseudocode
MIT License
43 stars 3 forks source link

Feature request: use raw text to simplify pseudocode syntax #2

Closed OrangeX4 closed 9 months ago

OrangeX4 commented 11 months ago

Lovelace is a unopinionated and flexible pseudocode package and i think it is great.

But I still think there's some points for improvement here, why don't we use a simpler raw text to identify Indent? Something like this:

#import "@preview/lovelace:0.1.0": *

#show: setup-lovelace

#let _typst2lovelace(typst-code, scope: (:)) = {
  let indent = 0
  let last-indent = 0
  let res = ()
  for line in typst-code.text.split("\n") {
    let whitespaces = line.find(regex("^\\s+"))
    let current-indent = if (whitespaces != none) { whitespaces.len() } else { 0 }
    if (indent == 0 and current-indent != 0) {
      indent = current-indent
    }
    if (current-indent > last-indent) {
      res = res + (ind,) * int((current-indent - last-indent) / indent)
    } else if (current-indent < last-indent) {
      res = res + (ded,) * int((last-indent - current-indent) / indent)
    }
    last-indent = current-indent
    let line-code = line.slice(current-indent)
    let match = line-code.match(regex("^<(.*)>\\s*$"))
    if (match != none) {
      res.push(label(match.captures.at(0)))
    } else {
      res.push(eval(line-code, mode: "markup", scope: (no-number: no-number, comment: comment) + scope))
    }
  }
  res
}

#pseudocode(.._typst2lovelace(
  ```typ
  *input:* integers $a$ and $b$
  *output:* greatest common divisor of $a$ and $b$
  *while* $a != b$ *do*
    *if* $a > b$ *then*
      $a <- a - b$
    *else*
      $b <- b - a$
    *end*
  *end*
  *return* $a$```
))
image

I think that might be better :-)

andreasKroepelin commented 11 months ago

Interesting idea! I'm not sure about the name yet but I'd be happy to review a PR implementing this.

OrangeX4 commented 11 months ago

I have opened a pr: https://github.com/andreasKroepelin/lovelace/pull/3

andreasKroepelin commented 9 months ago

Fixed by #3