How to force line breaks without extra spaces

Note that if you have markup that breaks up your text, like ab _cd_ then regex cannot/will not match across the different styles, it can only test and match on ab and cd separately.

I’m skeptical of the regex rules in general, they are really quite limited.


Apparently I continued hacking on this: here’s how we can really extend the raw block to support more markup. We use a show rule to evaluate markup on each line. Note the limitation - it evaluates one line at a time. And like you said, every solution might need performance evaluation. Eval markup for example brings back smartquotes, i.e automatic “” or «» as appropriate per language. (And, markup eval also removes the space-preserving property of raw blocks).

So this becomes a relatively nice way to typeset a poem or a song.

#let functions = (
  ul: std.underline,
)

#show raw.where(lang: "poem"): set text(font: "EB Garamond")
#show raw.where(lang: "poem"): set raw(theme: none)
#show raw.where(lang: "poem"): it => {
  show raw.line: it => {
    // Evaluate typst markup in the raw block
    // NOTE: can only see one line at a time, line-spanning markup not supported.
    // Add eval's scope argument to make custom functions available

    if it.number != 0 {
      // preserve initial space on the line
      let prefix = if it.text.starts-with(" ") {
        let space-width = 0.5em
        let space-prefix = it.text.position(regex("\S"))
        if space-prefix != none {
          h(space-width * space-prefix)
        }
      }
      raw.line(0, it.count, it.text, prefix + eval(it.text, mode: "markup", scope: functions))
    } else {
      it
    }
  }
  it
}


```poem
*Under the greenwood tree*
Under the greenwood tree #emoji.tree
Who loves to _lie with me,_
And turn his merry note
Unto the sweet bird's throat,
Come hither, come hither, come hither:
  Here shall he see
  No enemy
#ul[But winter and rough weather.]

Who doth ambition shun, #emoji.sloth
And loves to live i' the sun, $smash$
Seeking the food he eats,
And pleas'd with what he gets,
Come hither, come hither, come hither:
  Here shall he see
  No enemy
#ul[But winter and rough weather.]
```
3 Likes