How to use 'regex' to remove excessive newlines and spaces?

#set smartquote(enabled: false)
#show sym.dash.en: "--"
#show sym.dash.em: "---"

#show quote.where(block: true): it => {
  let attribution = if it.attribution != none {
    align(end, [--- #it.attribution])
  }
  block(
    inset: 1em,
    width: 100%,
    if it.quotes == true [
      "#it.body"
      #attribution
    ] else [
      #it.body
      #attribution
    ]
  )
}

// --

#strong[Test 1]

Lorem ipsum --- dolor "sit" amet.

#strong[Test 2]

#quote(attribution: "John Doe", block: true)[
  Lorem ipsum dolor sit amet.
]

#strong[Test 3]

#quote(attribution: "John Doe", block: true, quotes: true)[
  Lorem ipsum dolor sit amet.
]

The example above is my attempt to disable typographic substitutions.

It almost works, but there is one minor issue: as you can see from the output of Test 3, there are excessive space characters near quotation marks:

" Lorem ipsum dolor sit amet. "
                                    --- John Doe

How to get rid of them? My guess is that the line

"#it.body"

should be changed to something like

#regex("(\")\n+", $1)#it.body#regex("\n+(\")", $1)

but I haven’t succeeded to make it work.

If you show repr(it) in your quote.where show rule you will see that the body is parsed with leading and trailing whitespace: ([], [Lorem ipsum dolor sit amet.], []).

It’s because of the spaces you have in markup: if you write instead #quote(...)[Lorem ipsum dolor sit amet.] these spaces in the body will disappear.

Of course you want to keep the spaces in markup. You can use a weak horizontal spacing to collapse these spurious spaces:

if it.quotes == true {
 ["] + h(0pt, weak: true) + it.body + h(0pt, weak: true) + ["]
  attribution
} else {
  it.body
  attribution
}

I think that’s actually what typst does internally, and it’s also a trick shown in the documentation in the example for attribution.

1 Like