Skip to content

Commit 18dfb3b

Browse files
gordonwoodhullchristopherkenny
authored andcommitted
brace tables with typst:no-figure and typst:text attributes (jgm#10563)
The combination of jgm#9648 Typst property output and jgm#9778 `typst:no-figure` can cause fonts to spill out of tables. This is because setting Typst text properties across a table requires `set text(...)` outside the table, and previously we were relying on the figure to provide a scope. This adds an extra `#{...}` when the table has class `typst:no-figure` and also has `typst:text:*` attributes.
1 parent 463ab61 commit 18dfb3b

File tree

2 files changed

+92
-6
lines changed

2 files changed

+92
-6
lines changed

src/Text/Pandoc/Writers/Typst.hs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,15 @@ toTypstTextElement typstTextAttrs content = "#text" <> toTypstPropsListParens ty
131131

132132
toTypstSetText :: [(Text, Text)] -> Doc Text
133133
toTypstSetText [] = ""
134-
toTypstSetText typstTextAttrs = "#set text" <> parens (toTypstPropsListSep typstTextAttrs) <> "; " -- newline?
134+
toTypstSetText typstTextAttrs = "set text" <> parens (toTypstPropsListSep typstTextAttrs) <> "; "
135+
136+
toTypstPoundSetText :: [(Text, Text)] -> Doc Text
137+
toTypstPoundSetText [] = ""
138+
toTypstPoundSetText typstTextAttrs = "#" <> toTypstSetText typstTextAttrs
139+
140+
toTypstBracesSetText :: [(Text, Text)] -> Doc Text -> Doc Text
141+
toTypstBracesSetText [] x = "#" <> x
142+
toTypstBracesSetText typstTextAttrs x = "#" <> braces (toTypstSetText typstTextAttrs <> x)
135143

136144
blocksToTypst :: PandocMonad m => [Block] -> TW m (Doc Text)
137145
blocksToTypst blocks = vcat <$> mapM blockToTypst blocks
@@ -259,7 +267,7 @@ blockToTypst block =
259267
ColSpan n -> [ "colspan: " <> tshow n ]) ++
260268
map formatTypstProp typstAttrs2
261269
cellContents <- blocksToTypst bs
262-
let contents2 = brackets (toTypstSetText typstTextAttrs <> cellContents)
270+
let contents2 = brackets (toTypstPoundSetText typstTextAttrs <> cellContents)
263271
pure $ if null cellattrs
264272
then contents2
265273
else "table.cell" <>
@@ -288,7 +296,7 @@ blockToTypst block =
288296
header <- fromHead thead
289297
footer <- fromFoot tfoot
290298
body <- vcat <$> mapM fromTableBody tbodies
291-
let table = toTypstSetText typstTextAttrs <> "#table("
299+
let table = "table("
292300
$$ nest 2
293301
( "columns: " <> columns <> ","
294302
$$ "align: " <> alignarray <> ","
@@ -299,11 +307,11 @@ blockToTypst block =
299307
)
300308
$$ ")"
301309
return $ if "typst:no-figure" `elem` tabclasses
302-
then table
310+
then toTypstBracesSetText typstTextAttrs table
303311
else "#figure("
304312
$$
305313
nest 2
306-
("align(center)[" <> table <> "]"
314+
("align(center)[" <> toTypstPoundSetText typstTextAttrs <> "#" <> table <> "]"
307315
$$ capt'
308316
$$ typstFigureKind
309317
$$ ")")
@@ -332,7 +340,7 @@ blockToTypst block =
332340
let (typstAttrs,typstTextAttrs) = pickTypstAttrs kvs
333341
contents <- blocksToTypst blocks
334342
return $ "#block" <> toTypstPropsListParens typstAttrs <> "["
335-
$$ toTypstSetText typstTextAttrs <> contents
343+
$$ toTypstPoundSetText typstTextAttrs <> contents
336344
$$ ("]" <+> lab)
337345

338346
defListItemToTypst :: PandocMonad m => ([Inline], [[Block]]) -> TW m (Doc Text)

test/command/typst-property-output.md

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,3 +152,81 @@ foo
152152
, kind: table
153153
)
154154
```
155+
156+
```
157+
% pandoc -f html -t typst
158+
<p>Paragraph before.</p>
159+
160+
<table class="typst:no-figure" typst:text:size="3em">
161+
<tr>
162+
<td>A</td>
163+
<td>B</td>
164+
<td>C</td>
165+
</tr>
166+
</table>
167+
168+
<p>Paragraph after.</p>
169+
^D
170+
Paragraph before.
171+
172+
#{set text(size: 3em); table(
173+
columns: 3,
174+
align: (auto,auto,auto,),
175+
[A], [B], [C],
176+
)}
177+
Paragraph after.
178+
```
179+
180+
181+
```
182+
% pandoc -f html -t typst
183+
<p>Paragraph before.</p>
184+
185+
<table typst:text:size="3em">
186+
<tr>
187+
<td>A</td>
188+
<td>B</td>
189+
<td>C</td>
190+
</tr>
191+
</table>
192+
193+
<p>Paragraph after.</p>
194+
^D
195+
Paragraph before.
196+
197+
#figure(
198+
align(center)[#set text(size: 3em); #table(
199+
columns: 3,
200+
align: (auto,auto,auto,),
201+
[A], [B], [C],
202+
)]
203+
, kind: table
204+
)
205+
206+
Paragraph after.
207+
```
208+
209+
210+
```
211+
% pandoc -f html -t typst
212+
<p>Paragraph before.</p>
213+
214+
<table class="typst:no-figure">
215+
<tr>
216+
<td>A</td>
217+
<td>B</td>
218+
<td>C</td>
219+
</tr>
220+
</table>
221+
222+
<p>Paragraph after.</p>
223+
^D
224+
Paragraph before.
225+
226+
#table(
227+
columns: 3,
228+
align: (auto,auto,auto,),
229+
[A], [B], [C],
230+
)
231+
Paragraph after.
232+
```

0 commit comments

Comments
 (0)