Skip to content

Commit b6ac32b

Browse files
committed
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.
1 parent d90444d commit b6ac32b

File tree

3 files changed

+39
-14
lines changed

3 files changed

+39
-14
lines changed

src/Text/Pandoc/Writers/Typst.hs

+16-8
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ inlineToTypst inline =
223223
case inline of
224224
Str txt -> do
225225
context <- gets stEscapeContext
226-
return $ literal $ escapeTypst context txt
226+
return $ escapeTypst context txt
227227
Space -> return space
228228
SoftBreak -> do
229229
wrapText <- gets $ writerWrapText . stOptions
@@ -322,12 +322,17 @@ textstyle :: PandocMonad m => Doc Text -> [Inline] -> TW m (Doc Text)
322322
textstyle s inlines =
323323
(<> endCode) . (s <>) . brackets <$> inlinesToTypst inlines
324324

325-
escapeTypst :: EscapeContext -> Text -> Text
325+
escapeTypst :: EscapeContext -> Text -> Doc Text
326326
escapeTypst context t =
327-
T.replace "//" "\\/\\/" $
328-
if T.any needsEscape t
329-
then T.concatMap escapeChar t
330-
else t
327+
(case T.uncons t of
328+
Just (c, _)
329+
| needsEscapeAtLineStart c
330+
-> afterBreak "\\"
331+
_ -> mempty) <>
332+
(literal (T.replace "//" "\\/\\/"
333+
(if T.any needsEscape t
334+
then T.concatMap escapeChar t
335+
else t)))
331336
where
332337
escapeChar c
333338
| c == '\160' = "~"
@@ -336,7 +341,6 @@ escapeTypst context t =
336341
needsEscape '\160' = True
337342
needsEscape '[' = True
338343
needsEscape ']' = True
339-
needsEscape '(' = True -- see #9137
340344
needsEscape '#' = True
341345
needsEscape '<' = True
342346
needsEscape '>' = True
@@ -346,12 +350,16 @@ escapeTypst context t =
346350
needsEscape '\'' = True
347351
needsEscape '"' = True
348352
needsEscape '`' = True
349-
needsEscape '=' = True
350353
needsEscape '_' = True
351354
needsEscape '*' = True
352355
needsEscape '~' = True
353356
needsEscape ':' = context == TermContext
354357
needsEscape _ = False
358+
needsEscapeAtLineStart '/' = True
359+
needsEscapeAtLineStart '+' = True
360+
needsEscapeAtLineStart '-' = True
361+
needsEscapeAtLineStart '=' = True
362+
needsEscapeAtLineStart _ = False
355363

356364
toLabel :: Text -> Doc Text
357365
toLabel ident =

test/command/9386.md

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
```
2+
% pandoc -t typst --wrap=preserve
3+
A string of text that is long enough that after 73 chars that includes a
4+
/ slash creates a newline
5+
^D
6+
A string of text that is long enough that after 73 chars that includes a
7+
\/ slash creates a newline
8+
```
9+
10+
```
11+
% pandoc -t typst --wrap=preserve
12+
A string of text that is long enough that after 73 chars that includes
13+
a / slash creates a newline
14+
^D
15+
A string of text that is long enough that after 73 chars that includes
16+
a / slash creates a newline
17+
```

test/writer.typst

+6-6
Original file line numberDiff line numberDiff line change
@@ -634,9 +634,9 @@ Ellipses…and…and….
634634
These shouldn’t be math:
635635

636636
- To get the famous equation, write `$e = mc^2$`.
637-
- \$22,000 is a #emph[lot] of money. So is \$34,000. \(It worked if "lot" is
637+
- \$22,000 is a #emph[lot] of money. So is \$34,000. (It worked if "lot" is
638638
emphasized.)
639-
- Shoes \(\$20) and socks \(\$5).
639+
- Shoes (\$20) and socks (\$5).
640640
- Escaped `$`: \$73 #emph[this should be emphasized] 23\$.
641641

642642
Here’s a LaTeX table:
@@ -679,7 +679,7 @@ Left bracket: \[
679679

680680
Right bracket: \]
681681

682-
Left paren: \(
682+
Left paren: (
683683

684684
Right paren: )
685685

@@ -778,7 +778,7 @@ or here: <http://example.com/>
778778

779779
= Images
780780
<images>
781-
From "Voyage dans la Lune" by Georges Melies \(1902):
781+
From "Voyage dans la Lune" by Georges Melies (1902):
782782

783783
#figure([#box(width: 150.0pt, image("lalune.jpg"));],
784784
caption: [
@@ -796,8 +796,8 @@ Here is a footnote reference,#footnote[Here is the footnote. It can go anywhere
796796
after the footnote reference. It need not be placed at the end of the document.]
797797
and another.#footnote[Here’s the long note. This one contains multiple blocks.
798798

799-
Subsequent blocks are indented to show that they belong to the footnote \(as
800-
with list items).
799+
Subsequent blocks are indented to show that they belong to the footnote (as with
800+
list items).
801801

802802
```
803803
{ <code> }

0 commit comments

Comments
 (0)