diff options
| -rw-r--r-- | data/templates/default.typst | 9 | ||||
| -rw-r--r-- | data/templates/definitions.typst | 5 | ||||
| -rw-r--r-- | src/Text/Pandoc/Writers/Typst.hs | 23 | ||||
| -rw-r--r-- | test/writer.typst | 90 |
4 files changed, 82 insertions, 45 deletions
diff --git a/data/templates/default.typst b/data/templates/default.typst index 43674bc32..c04b5e40f 100644 --- a/data/templates/default.typst +++ b/data/templates/default.typst @@ -1,5 +1,14 @@ $definitions.typst()$ +#show terms: it => { + it.children + .map(child => [ + #strong[#child.term] + #block(inset: (left: 1.5em, top: -0.4em))[#child.description] + ]) + .join() +} + $if(template)$ #import "$template$": conf $else$ diff --git a/data/templates/definitions.typst b/data/templates/definitions.typst index 02fe80f38..acdc9ea89 100644 --- a/data/templates/definitions.typst +++ b/data/templates/definitions.typst @@ -1,9 +1,4 @@ // Some definitions presupposed by pandoc's typst output. -#let definition(term, ..defs) = [ - #strong(term) \ - #(defs.pos().join("\n")) -] - #let blockquote(body) = [ #set text( size: 0.92em ) #block(inset: (left: 1.5em, top: 0.2em, bottom: 0.2em))[#body] diff --git a/src/Text/Pandoc/Writers/Typst.hs b/src/Text/Pandoc/Writers/Typst.hs index a4bbeb5c6..1a146fe26 100644 --- a/src/Text/Pandoc/Writers/Typst.hs +++ b/src/Text/Pandoc/Writers/Typst.hs @@ -42,11 +42,17 @@ import Text.Collate.Lang (Lang(..), parseLang) writeTypst :: PandocMonad m => WriterOptions -> Pandoc -> m Text writeTypst options document = evalStateT (pandocToTypst options document) - WriterState{ stOptions = options, stNotes = [] } + WriterState{ stOptions = options, + stEscapeContext = NormalContext, + stNotes = [] } + +data EscapeContext = NormalContext | TermContext + deriving (Show, Eq) data WriterState = WriterState { stOptions :: WriterOptions, + stEscapeContext :: EscapeContext, stNotes :: [Doc Text] } @@ -197,10 +203,12 @@ blockToTypst block = defListItemToTypst :: PandocMonad m => ([Inline], [[Block]]) -> TW m (Doc Text) defListItemToTypst (term, defns) = do + modify $ \st -> st{ stEscapeContext = TermContext } term' <- inlinesToTypst term + modify $ \st -> st{ stEscapeContext = NormalContext } defns' <- mapM blocksToTypst defns - return $ "#definition" <> brackets term' <> mconcat (map brackets defns') - + return $ nowrap ("/ " <> term' <> ": " <> "#block[") $$ + chomp (vcat defns') $$ "]" listItemToTypst :: PandocMonad m => Int -> Doc Text -> [Block] -> TW m (Doc Text) listItemToTypst ind marker blocks = do contents <- blocksToTypst blocks @@ -212,7 +220,9 @@ inlinesToTypst ils = hcat <$> mapM inlineToTypst ils inlineToTypst :: PandocMonad m => Inline -> TW m (Doc Text) inlineToTypst inline = case inline of - Str txt -> return $ literal $ escapeTypst txt + Str txt -> do + context <- gets stEscapeContext + return $ literal $ escapeTypst context txt Space -> return space SoftBreak -> do wrapText <- gets $ writerWrapText . stOptions @@ -284,8 +294,8 @@ inlineToTypst inline = textstyle :: PandocMonad m => Doc Text -> [Inline] -> TW m (Doc Text) textstyle s inlines = (s <>) . brackets <$> inlinesToTypst inlines -escapeTypst :: Text -> Text -escapeTypst t = +escapeTypst :: EscapeContext -> Text -> Text +escapeTypst context t = if T.any needsEscape t then T.concatMap escapeChar t else t @@ -307,6 +317,7 @@ escapeTypst t = needsEscape '=' = True needsEscape '_' = True needsEscape '*' = True + needsEscape ':' = context == TermContext needsEscape _ = False toLabel :: Text -> Doc Text diff --git a/test/writer.typst b/test/writer.typst index 18a618163..26462a269 100644 --- a/test/writer.typst +++ b/test/writer.typst @@ -1,9 +1,4 @@ // Some definitions presupposed by pandoc's typst output. -#let definition(term, ..defs) = [ - #strong(term) \ - #(defs.pos().join("\n")) -] - #let blockquote(body) = [ #set text( size: 0.92em ) #block(inset: (left: 1.5em, top: 0.2em, bottom: 0.2em))[#body] @@ -16,6 +11,15 @@ #let endnote(num, contents) = [ #stack(dir: ltr, spacing: 3pt, super[#num], contents) ] +#show terms: it => { + it.children + .map(child => [ + #strong[#child.term] + #block(inset: (left: 1.5em, top: -0.4em))[#child.description] + ]) + .join() +} + #let conf( title: none, authors: none, @@ -388,43 +392,56 @@ B. Williams #label("definition-lists") Tight using spaces: -#definition[apple][red fruit] +/ apple: #block[ +red fruit +] -#definition[orange][orange fruit] +/ orange: #block[ +orange fruit +] -#definition[banana][yellow fruit] +/ banana: #block[ +yellow fruit +] Tight using tabs: -#definition[apple][red fruit] +/ apple: #block[ +red fruit +] -#definition[orange][orange fruit] +/ orange: #block[ +orange fruit +] -#definition[banana][yellow fruit] +/ banana: #block[ +yellow fruit +] Loose: -#definition[apple][red fruit - +/ apple: #block[ +red fruit ] -#definition[orange][orange fruit - +/ orange: #block[ +orange fruit ] -#definition[banana][yellow fruit - +/ banana: #block[ +yellow fruit ] Multiple blocks with italics: -#definition[#emph[apple]][red fruit +/ #emph[apple]: #block[ +red fruit contains seeds, crisp, pleasant to taste - ] -#definition[#emph[orange]][orange fruit +/ #emph[orange]: #block[ +orange fruit ``` { orange code block } @@ -433,42 +450,47 @@ contains seeds, crisp, pleasant to taste #blockquote[ orange block quote ] - ] Multiple definitions, tight: -#definition[apple][red fruit][computer] +/ apple: #block[ +red fruit +computer +] -#definition[orange][orange fruit][bank] +/ orange: #block[ +orange fruit +bank +] Multiple definitions, loose: -#definition[apple][red fruit - -][computer +/ apple: #block[ +red fruit +computer ] -#definition[orange][orange fruit - -][bank +/ orange: #block[ +orange fruit +bank ] Blank line after term, indented marker, alternate markers: -#definition[apple][red fruit - -][computer +/ apple: #block[ +red fruit +computer ] -#definition[orange][orange fruit +/ orange: #block[ +orange fruit + sublist + sublist - ] = HTML Blocks |
