Skip to content

Commit

Permalink
typst,table - support '.typst:no-figure' and 'typst:figure:kind=kind' (
Browse files Browse the repository at this point in the history
…#9778)

This extends support for fine-grained properties in Typst.

If the `typst:no-figure` class is present on a Table, the table will not be placed in a figure.

If the `typst:figure:kind` attribute is present, its value will be used for the figure's `kind`.

These features are documented in `doc/typst-property-output.md`.

Closes #9777.
  • Loading branch information
cscheid committed Jun 3, 2024
1 parent b9491cc commit 347985f
Show file tree
Hide file tree
Showing 4 changed files with 153 additions and 21 deletions.
10 changes: 9 additions & 1 deletion doc/typst-property-output.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down Expand Up @@ -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.
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.
44 changes: 24 additions & 20 deletions src/Text/Pandoc/Writers/Typst.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 ", "
Expand Down Expand Up @@ -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
Expand Down
62 changes: 62 additions & 0 deletions test/command/9777-b.md
Original file line number Diff line number Diff line change
@@ -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
)
```

58 changes: 58 additions & 0 deletions test/command/9777.md
Original file line number Diff line number Diff line change
@@ -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],
)
```

0 comments on commit 347985f

Please sign in to comment.