diff options
| -rw-r--r-- | doc/typst-property-output.md | 10 | ||||
| -rw-r--r-- | src/Text/Pandoc/Writers/Typst.hs | 44 | ||||
| -rw-r--r-- | test/command/9777-b.md | 62 | ||||
| -rw-r--r-- | test/command/9777.md | 58 |
4 files changed, 153 insertions, 21 deletions
diff --git a/doc/typst-property-output.md b/doc/typst-property-output.md index ab0d5a916..112c82afc 100644 --- a/doc/typst-property-output.md +++ b/doc/typst-property-output.md @@ -96,6 +96,14 @@ The following Pandoc AST elements are currently supported. More may be supported : The table is wrapped in a Typst [text element](https://typst.app/docs/reference/text/text/) with `prop` as one of its parameters. + `typst:no-figure` + + : By default, Pandoc will wrap the table in a Typst [figure element](https://typst.app/docs/reference/model/figure/). If this attribute is set, only the table element itself will be emitted. This avoids Typst's crossreference counter of kind `table` from being incremented. + + `typst:figure:kind` + + : If this attribute is set, Pandoc will wrap the table in a Typst [figure element](https://typst.app/docs/reference/model/figure/) with the specified `kind` attribute. This is useful for tables that should be cross-referenced as something other than `Table ...` in the document. Typst will use the `kind` attribute to increment the corresponding counter: `raw` and `image`. + - Table [Cell](https://pandoc.org/lua-filters.html#type-cell) `typst:prop` @@ -151,4 +159,4 @@ will produce the Typst output Here is some #text(fill: orange)[orange text]. ``` -Of course, this simple filter will only work for Typst's [predefined colors](https://typst.app/docs/reference/visualize/color/#predefined-colors). A more complete filter would need to translate the value as well.
\ No newline at end of file +Of course, this simple filter will only work for Typst's [predefined colors](https://typst.app/docs/reference/visualize/color/#predefined-colors). A more complete filter would need to translate the value as well. diff --git a/src/Text/Pandoc/Writers/Typst.hs b/src/Text/Pandoc/Writers/Typst.hs index 38029ca56..9344c6f40 100644 --- a/src/Text/Pandoc/Writers/Typst.hs +++ b/src/Text/Pandoc/Writers/Typst.hs @@ -37,6 +37,7 @@ import Text.Pandoc.Extensions (Extension(..)) import Text.Collate.Lang (Lang(..), parseLang) import Text.Printf (printf) import Data.Char (isAlphaNum) +import Data.Maybe (fromMaybe) -- | Convert Pandoc to Typst. writeTypst :: PandocMonad m => WriterOptions -> Pandoc -> m Text @@ -195,13 +196,14 @@ blockToTypst block = else vsep items') $$ blankline DefinitionList items -> ($$ blankline) . vsep <$> mapM defListItemToTypst items - Table (ident,_,tabkvs) (Caption _ caption) colspecs thead tbodies tfoot -> do + Table (ident,tabclasses,tabkvs) (Caption _ caption) colspecs thead tbodies tfoot -> do let lab = toLabel FreestandingLabel ident capt' <- if null caption then return mempty else do captcontents <- blocksToTypst caption return $ ", caption: " <> brackets captcontents + let typstFigureKind = literal (", kind: " <> fromMaybe "table" (lookup "typst:figure:kind" tabkvs)) let numcols = length colspecs let (aligns, widths) = unzip colspecs let commaSep = hcat . intersperse ", " @@ -273,25 +275,27 @@ blockToTypst block = header <- fromHead thead footer <- fromFoot tfoot body <- vcat <$> mapM fromTableBody tbodies - return $ - "#figure(" - $$ - nest 2 - ("align(center)[" <> toTypstSetText typstTextAttrs <> "#table(" - $$ nest 2 - ( "columns: " <> columns <> "," - $$ "align: " <> alignarray <> "," - $$ toTypstPropsListTerm typstAttrs - $$ header - $$ body - $$ footer - ) - $$ ")]" - $$ capt' - $$ ", kind: table" - $$ ")") - $$ lab - $$ blankline + let table = toTypstSetText typstTextAttrs <> "#table(" + $$ nest 2 + ( "columns: " <> columns <> "," + $$ "align: " <> alignarray <> "," + $$ toTypstPropsListTerm typstAttrs + $$ header + $$ body + $$ footer + ) + $$ ")" + return $ if "typst:no-figure" `elem` tabclasses + then table + else "#figure(" + $$ + nest 2 + ("align(center)[" <> table <> "]" + $$ capt' + $$ typstFigureKind + $$ ")") + $$ lab + $$ blankline Figure (ident,_,_) (Caption _mbshort capt) blocks -> do caption <- blocksToTypst capt contents <- case blocks of diff --git a/test/command/9777-b.md b/test/command/9777-b.md new file mode 100644 index 000000000..3c44cf89e --- /dev/null +++ b/test/command/9777-b.md @@ -0,0 +1,62 @@ +``` +% pandoc -f native -t typst +[ Table + ( "" , [] , [ ( "typst:figure:kind" , "figure" ) ] ) + (Caption Nothing []) + [ ( AlignDefault , ColWidthDefault ) + , ( AlignDefault , ColWidthDefault ) + ] + (TableHead + ( "" , [] , [] ) + [ Row + ( "" , [] , [] ) + [ Cell + ( "" , [] , [] ) + AlignDefault + (RowSpan 1) + (ColSpan 1) + [ Plain [ Str "f" ] ] + , Cell + ( "" , [] , [] ) + AlignDefault + (RowSpan 1) + (ColSpan 1) + [ Plain [ Str "b" ] ] + ] + ]) + [ TableBody + ( "" , [] , [] ) + (RowHeadColumns 0) + [] + [ Row + ( "" , [] , [] ) + [ Cell + ( "" , [] , [] ) + AlignDefault + (RowSpan 1) + (ColSpan 1) + [ Plain [ Str "1" ] ] + , Cell + ( "" , [] , [] ) + AlignDefault + (RowSpan 1) + (ColSpan 1) + [ Plain [ Str "2" ] ] + ] + ] + ] + (TableFoot ( "" , [] , [] ) []) +] +^D +#figure( + align(center)[#table( + columns: 2, + align: (auto,auto,), + table.header([f], [b],), + table.hline(), + [1], [2], + )] + , kind: figure + ) +``` + diff --git a/test/command/9777.md b/test/command/9777.md new file mode 100644 index 000000000..346f2b222 --- /dev/null +++ b/test/command/9777.md @@ -0,0 +1,58 @@ +``` +% pandoc -f native -t typst +[ Table + ( "" , [ "typst:no-figure" ] , [] ) + (Caption Nothing []) + [ ( AlignDefault , ColWidthDefault ) + , ( AlignDefault , ColWidthDefault ) + ] + (TableHead + ( "" , [] , [] ) + [ Row + ( "" , [] , [] ) + [ Cell + ( "" , [] , [] ) + AlignDefault + (RowSpan 1) + (ColSpan 1) + [ Plain [ Str "f" ] ] + , Cell + ( "" , [] , [] ) + AlignDefault + (RowSpan 1) + (ColSpan 1) + [ Plain [ Str "b" ] ] + ] + ]) + [ TableBody + ( "" , [] , [] ) + (RowHeadColumns 0) + [] + [ Row + ( "" , [] , [] ) + [ Cell + ( "" , [] , [] ) + AlignDefault + (RowSpan 1) + (ColSpan 1) + [ Plain [ Str "1" ] ] + , Cell + ( "" , [] , [] ) + AlignDefault + (RowSpan 1) + (ColSpan 1) + [ Plain [ Str "2" ] ] + ] + ] + ] + (TableFoot ( "" , [] , [] ) []) +] +^D +#table( + columns: 2, + align: (auto,auto,), + table.header([f], [b],), + table.hline(), + [1], [2], +) +``` |
