diff options
| author | John MacFarlane <[email protected]> | 2025-10-13 12:10:43 +0200 |
|---|---|---|
| committer | John MacFarlane <[email protected]> | 2025-10-13 12:11:29 +0200 |
| commit | 3d6439ad156a24a75d04c90c9ab8aa1b2cd7d1b0 (patch) | |
| tree | 4767728b328992d90a7e95563364f62cfcfd17c2 | |
| parent | d3ed33bd005a7b54a11fbaf43b60ee5e28ab070d (diff) | |
Typst writer: Escape open paren after non-space.
This fixes an issue that occurs if an open paren comes
right after e.g. `#strong[test]`.
Closes #11210.
| -rw-r--r-- | src/Text/Pandoc/Writers/Typst.hs | 20 | ||||
| -rw-r--r-- | test/command/11210.md | 13 |
2 files changed, 32 insertions, 1 deletions
diff --git a/src/Text/Pandoc/Writers/Typst.hs b/src/Text/Pandoc/Writers/Typst.hs index 40e1f3ac8..e129fce69 100644 --- a/src/Text/Pandoc/Writers/Typst.hs +++ b/src/Text/Pandoc/Writers/Typst.hs @@ -409,7 +409,25 @@ listItemToTypst ind marker blocks = do return $ hang ind (marker <> space) contents inlinesToTypst :: PandocMonad m => [Inline] -> TW m (Doc Text) -inlinesToTypst ils = hcat <$> mapM inlineToTypst ils +inlinesToTypst ils = hcat <$> mapM inlineToTypst (escapeParens ils) + +-- Add an escape before a parenthesis right after a non-space element. +-- Otherwise we risk `#emph[test](3)` which will error. See #11210. +escapeParens :: [Inline] -> [Inline] +escapeParens [] = [] +escapeParens (s : x : xs) + | isSpacey s + = s : x : escapeParens xs +escapeParens (Str t : xs) + | Just ('(',_) <- T.uncons t + = RawInline (Format "typst") "\\" : Str t : escapeParens xs +escapeParens (x : xs) = x : escapeParens xs + +isSpacey :: Inline -> Bool +isSpacey Space = True +isSpacey SoftBreak = True +isSpacey LineBreak = True +isSpacey _ = False inlineToTypst :: PandocMonad m => Inline -> TW m (Doc Text) inlineToTypst inline = diff --git a/test/command/11210.md b/test/command/11210.md new file mode 100644 index 000000000..cfd3af7e8 --- /dev/null +++ b/test/command/11210.md @@ -0,0 +1,13 @@ +``` +% pandoc -t typst -f man +.PP +.IR login (1) +.PP +and a regular (paren) that should not be escaped. +^D +#emph[login]\(1) + +and a regular (paren) that should not be escaped. + +``` + |
