diff options
| author | John MacFarlane <[email protected]> | 2024-10-08 21:58:25 -0700 |
|---|---|---|
| committer | John MacFarlane <[email protected]> | 2024-10-08 21:58:25 -0700 |
| commit | bdb1172385422d46243e6b5ede31fb6054fade5f (patch) | |
| tree | 66c2612fc4bac967b52905b892cc1e66dd13828f /src | |
| parent | 0b51580e2d0f0c1b5032198efb31f126d3c62e43 (diff) | |
Typst writer: make `smart` extension work.
If `smart` is not enabled, a command in the default template will
disable smartquote substitutions.
When `smart` is enabled, render curly apostrophes as straight
and escape straight apostrophes.
When `smart` is disabled, render curly apostrophes as curly
and don't escape straight apostrophes.
And similarly for quotes, em and en dashes.
This should give more idiomatic typst output, with fewer unnecessary
escapes.
Closes #10271.
Diffstat (limited to 'src')
| -rw-r--r-- | src/Text/Pandoc/Extensions.hs | 5 | ||||
| -rw-r--r-- | src/Text/Pandoc/Writers/Typst.hs | 32 |
2 files changed, 26 insertions, 11 deletions
diff --git a/src/Text/Pandoc/Extensions.hs b/src/Text/Pandoc/Extensions.hs index 868ab9609..7f1b35c17 100644 --- a/src/Text/Pandoc/Extensions.hs +++ b/src/Text/Pandoc/Extensions.hs @@ -471,7 +471,8 @@ getDefaultExtensions "jats_articleauthoring" = getDefaultExtensions "jats" getDefaultExtensions "opml" = pandocExtensions -- affects notes getDefaultExtensions "markua" = extensionsFromList [] -getDefaultExtensions "typst" = extensionsFromList [Ext_citations] +getDefaultExtensions "typst" = extensionsFromList [Ext_citations, + Ext_smart] getDefaultExtensions "dokuwiki" = extensionsFromList [Ext_smart] getDefaultExtensions _ = extensionsFromList [Ext_auto_identifiers] @@ -656,6 +657,6 @@ getAllExtensions f = universalExtensions <> getAll f getAll "mediawiki" = autoIdExtensions <> extensionsFromList [ Ext_smart ] - getAll "typst" = extensionsFromList [Ext_citations] + getAll "typst" = extensionsFromList [Ext_citations, Ext_smart] getAll "djot" = extensionsFromList [Ext_sourcepos] getAll _ = mempty diff --git a/src/Text/Pandoc/Writers/Typst.hs b/src/Text/Pandoc/Writers/Typst.hs index 80ae6dab3..42a17dccc 100644 --- a/src/Text/Pandoc/Writers/Typst.hs +++ b/src/Text/Pandoc/Writers/Typst.hs @@ -84,6 +84,7 @@ pandocToTypst options (Pandoc meta blocks) = do Right l -> resetField "lang" (langLanguage l) . maybe id (resetField "region") (langRegion l)) + $ defField "smart" (isEnabled Ext_smart options) $ defField "toc-depth" (tshow $ writerTOCDepth options) $ defField "figure-caption-position" (toPosition $ writerFigureCaptionPosition options) @@ -351,8 +352,9 @@ inlineToTypst :: PandocMonad m => Inline -> TW m (Doc Text) inlineToTypst inline = case inline of Str txt -> do + opts <- gets stOptions context <- gets stEscapeContext - return $ escapeTypst context txt + return $ escapeTypst (isEnabled Ext_smart opts) context txt Space -> return space SoftBreak -> do wrapText <- gets $ writerWrapText . stOptions @@ -396,11 +398,17 @@ inlineToTypst inline = contents <- inlinesToTypst inlines return $ toTypstTextElement typstTextAttrs contents <> lab Quoted quoteType inlines -> do - let q = case quoteType of - DoubleQuote -> literal "\"" - SingleQuote -> literal "'" + opts <- gets stOptions + let smart = isEnabled Ext_smart opts contents <- inlinesToTypst inlines - return $ q <> contents <> q + return $ + case quoteType of + DoubleQuote + | smart -> "\"" <> contents <> "\"" + | otherwise -> "“" <> contents <> "”" + SingleQuote + | smart -> "'" <> contents <> "'" + | otherwise -> "‘" <> contents <> "’" Cite citations inlines -> do opts <- gets stOptions if isEnabled Ext_citations opts @@ -455,8 +463,8 @@ textstyle s inlines = , needsEscapeAtLineStart c -> ("\\" <>) _ -> id -escapeTypst :: EscapeContext -> Text -> Doc Text -escapeTypst context t = +escapeTypst :: Bool -> EscapeContext -> Text -> Doc Text +escapeTypst smart context t = (case T.uncons t of Just (c, _) | needsEscapeAtLineStart c @@ -469,9 +477,17 @@ escapeTypst context t = where escapeChar c | c == '\160' = "~" + | c == '\8217', smart = "'" -- apostrophe + | c == '\8212', smart = "---" -- em dash + | c == '\8211', smart = "--" -- en dash | needsEscape c = "\\" <> T.singleton c | otherwise = T.singleton c needsEscape '\160' = True + needsEscape '\8217' = smart + needsEscape '\8212' = smart + needsEscape '\8211' = smart + needsEscape '\'' = smart + needsEscape '"' = smart needsEscape '[' = True needsEscape ']' = True needsEscape '#' = True @@ -480,8 +496,6 @@ escapeTypst context t = needsEscape '@' = True needsEscape '$' = True needsEscape '\\' = True - needsEscape '\'' = True - needsEscape '"' = True needsEscape '`' = True needsEscape '_' = True needsEscape '*' = True |
