aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn MacFarlane <[email protected]>2024-01-29 09:29:52 -0800
committerJohn MacFarlane <[email protected]>2024-01-29 10:53:51 -0800
commitb6ac32b1161eae21bbb9525415ed64d11adf7a54 (patch)
tree876274f85b6e911dcf227b74dc5a04b9e99bc476
parentd90444d4c869598434b3d4bee8386b16ff57b6f6 (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.
-rw-r--r--src/Text/Pandoc/Writers/Typst.hs24
-rw-r--r--test/command/9386.md17
-rw-r--r--test/writer.typst12
3 files changed, 39 insertions, 14 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 =
diff --git a/test/command/9386.md b/test/command/9386.md
new file mode 100644
index 000000000..043fb3090
--- /dev/null
+++ b/test/command/9386.md
@@ -0,0 +1,17 @@
+```
+% pandoc -t typst --wrap=preserve
+A string of text that is long enough that after 73 chars that includes a
+/ slash creates a newline
+^D
+A string of text that is long enough that after 73 chars that includes a
+\/ slash creates a newline
+```
+
+```
+% pandoc -t typst --wrap=preserve
+A string of text that is long enough that after 73 chars that includes
+a / slash creates a newline
+^D
+A string of text that is long enough that after 73 chars that includes
+a / slash creates a newline
+```
diff --git a/test/writer.typst b/test/writer.typst
index bd3f62b33..f7883a0e8 100644
--- a/test/writer.typst
+++ b/test/writer.typst
@@ -634,9 +634,9 @@ Ellipses…and…and….
These shouldn’t be math:
- To get the famous equation, write `$e = mc^2$`.
-- \$22,000 is a #emph[lot] of money. So is \$34,000. \(It worked if "lot" is
+- \$22,000 is a #emph[lot] of money. So is \$34,000. (It worked if "lot" is
emphasized.)
-- Shoes \(\$20) and socks \(\$5).
+- Shoes (\$20) and socks (\$5).
- Escaped `$`: \$73 #emph[this should be emphasized] 23\$.
Here’s a LaTeX table:
@@ -679,7 +679,7 @@ Left bracket: \[
Right bracket: \]
-Left paren: \(
+Left paren: (
Right paren: )
@@ -778,7 +778,7 @@ or here: <http://example.com/>
= Images
<images>
-From "Voyage dans la Lune" by Georges Melies \(1902):
+From "Voyage dans la Lune" by Georges Melies (1902):
#figure([#box(width: 150.0pt, image("lalune.jpg"));],
caption: [
@@ -796,8 +796,8 @@ Here is a footnote reference,#footnote[Here is the footnote. It can go anywhere
after the footnote reference. It need not be placed at the end of the document.]
and another.#footnote[Here’s the long note. This one contains multiple blocks.
-Subsequent blocks are indented to show that they belong to the footnote \(as
-with list items).
+Subsequent blocks are indented to show that they belong to the footnote (as with
+list items).
```
{ <code> }