diff options
| author | John MacFarlane <jgm@berkeley.edu> | 2024-01-29 09:29:52 -0800 |
|---|---|---|
| committer | John MacFarlane <jgm@berkeley.edu> | 2024-01-29 10:53:51 -0800 |
| commit | b6ac32b1161eae21bbb9525415ed64d11adf7a54 (patch) | |
| tree | 876274f85b6e911dcf227b74dc5a04b9e99bc476 /src | |
| parent | d90444d4c869598434b3d4bee8386b16ff57b6f6 (diff) | |
Typst writer escaping improvements.
We no longer escape `(`. The reason we did this before (#9137)
has been addressed in another way (#9252).
We only escape `=`, `+`, `-` at the beginning of a line.
We now also escape `/` at the beginning of a line.
This should reduce unnecessary escapes.
Closes #9386.
Diffstat (limited to 'src')
| -rw-r--r-- | src/Text/Pandoc/Writers/Typst.hs | 24 |
1 files changed, 16 insertions, 8 deletions
diff --git a/src/Text/Pandoc/Writers/Typst.hs b/src/Text/Pandoc/Writers/Typst.hs index afcff1419..f25f706b4 100644 --- a/src/Text/Pandoc/Writers/Typst.hs +++ b/src/Text/Pandoc/Writers/Typst.hs @@ -223,7 +223,7 @@ inlineToTypst inline = case inline of Str txt -> do context <- gets stEscapeContext - return $ literal $ escapeTypst context txt + return $ escapeTypst context txt Space -> return space SoftBreak -> do wrapText <- gets $ writerWrapText . stOptions @@ -322,12 +322,17 @@ textstyle :: PandocMonad m => Doc Text -> [Inline] -> TW m (Doc Text) textstyle s inlines = (<> endCode) . (s <>) . brackets <$> inlinesToTypst inlines -escapeTypst :: EscapeContext -> Text -> Text +escapeTypst :: EscapeContext -> Text -> Doc Text escapeTypst context t = - T.replace "//" "\\/\\/" $ - if T.any needsEscape t - then T.concatMap escapeChar t - else t + (case T.uncons t of + Just (c, _) + | needsEscapeAtLineStart c + -> afterBreak "\\" + _ -> mempty) <> + (literal (T.replace "//" "\\/\\/" + (if T.any needsEscape t + then T.concatMap escapeChar t + else t))) where escapeChar c | c == '\160' = "~" @@ -336,7 +341,6 @@ escapeTypst context t = needsEscape '\160' = True needsEscape '[' = True needsEscape ']' = True - needsEscape '(' = True -- see #9137 needsEscape '#' = True needsEscape '<' = True needsEscape '>' = True @@ -346,12 +350,16 @@ escapeTypst context t = needsEscape '\'' = True needsEscape '"' = True needsEscape '`' = True - needsEscape '=' = True needsEscape '_' = True needsEscape '*' = True needsEscape '~' = True needsEscape ':' = context == TermContext needsEscape _ = False + needsEscapeAtLineStart '/' = True + needsEscapeAtLineStart '+' = True + needsEscapeAtLineStart '-' = True + needsEscapeAtLineStart '=' = True + needsEscapeAtLineStart _ = False toLabel :: Text -> Doc Text toLabel ident = |
