git.iko.soy/typeable/pandoc
on master help sign in
Add support for writing nested tables to asciidoc (#6972)

Added field to WriterState that denotes the current nesting level for traversing tables. Depending on the value of that field nested tables are recognized and written. Asciidoc supports one level of nesting. If deeper tables are to be written, they are omitted and a warning is issued.

commit668596cc89f8f6bddafd35fd031638bc0c416183 authortimo-a <timo-a@gmx.ch> dateMon 28 Dec 2020, 03:42 (6 years ago) parentdcd8941
2 files changed+114−7
src/Text/Pandoc/Writers/AsciiDoc.hs+32−7 test/command/nested-table-to-asciidoc-6942.md+82
src/Text/Pandoc/Writers/AsciiDoc.hs+32−7
@@ -37,6 +37,7 @@ import Text.Pandoc.Shared
3737import Text.Pandoc.Templates (renderTemplate)
3838import Text.Pandoc.Writers.Shared
3939
40+
4041data WriterState = WriterState { defListMarker :: Text
4142 , orderedListLevel :: Int
4243 , bulletListLevel :: Int
@@ -45,6 +46,10 @@ data WriterState = WriterState { defListMarker :: Text
4546 , asciidoctorVariant :: Bool
4647 , inList :: Bool
4748 , hasMath :: Bool
49+ -- |0 is no table
50+ -- 1 is top level table
51+ -- 2 is a table in a table
52+ , tableNestingLevel :: Int
4853 }
4954
5055defaultWriterState :: WriterState
@@ -56,6 +61,7 @@ defaultWriterState = WriterState { defListMarker = "::"
5661 , asciidoctorVariant = False
5762 , inList = False
5863 , hasMath = False
64+ , tableNestingLevel = 0
5965 }
6066
6167-- | Convert Pandoc to AsciiDoc.
@@ -194,7 +200,7 @@ blockToAsciiDoc opts (BlockQuote blocks) = do
194200 else contents
195201 let bar = text "____"
196202 return $ bar $$ chomp contents' $$ bar <> blankline
197−blockToAsciiDoc opts (Table _ blkCapt specs thead tbody tfoot) = do
203+blockToAsciiDoc opts block@(Table _ blkCapt specs thead tbody tfoot) = do
198204 let (caption, aligns, widths, headers, rows) =
199205 toLegacyTable blkCapt specs thead tbody tfoot
200206 caption' <- inlineListToAsciiDoc opts caption
@@ -236,23 +242,42 @@ blockToAsciiDoc opts (Table _ blkCapt specs thead tbody tfoot) = do
236242 $ zipWith colspec aligns widths')
237243 <> text ","
238244 <> headerspec <> text "]"
245+
246+ -- construct cells and recurse in case of nested tables
247+ parentTableLevel <- gets tableNestingLevel
248+ let currentNestingLevel = parentTableLevel + 1
249+
250+ modify $ \st -> st{ tableNestingLevel = currentNestingLevel }
251+
252+ let separator = text (if parentTableLevel == 0
253+ then "|" -- top level separator
254+ else "!") -- nested separator
255+
239256 let makeCell [Plain x] = do d <- blockListToAsciiDoc opts [Plain x]
240− return $ text "|" <> chomp d
257+ return $ separator <> chomp d
241258 makeCell [Para x] = makeCell [Plain x]
242− makeCell [] = return $ text "|"
243− makeCell bs = do d <- blockListToAsciiDoc opts bs
244− return $ text "a|" $$ d
259+ makeCell [] = return separator
260+ makeCell bs = if currentNestingLevel == 2
261+ then do
262+ --asciidoc only supports nesting once
263+ report $ BlockNotRendered block
264+ return separator
265+ else do
266+ d <- blockListToAsciiDoc opts bs
267+ return $ (text "a" <> separator) $$ d
268+
245269 let makeRow cells = hsep `fmap` mapM makeCell cells
246270 rows' <- mapM makeRow rows
247271 head' <- makeRow headers
272+ modify $ \st -> st{ tableNestingLevel = parentTableLevel }
248273 let head'' = if all null headers then empty else head'
249274 let colwidth = if writerWrapText opts == WrapAuto
250275 then writerColumns opts
251276 else 100000
252277 let maxwidth = maximum $ map offset (head':rows')
253278 let body = if maxwidth > colwidth then vsep rows' else vcat rows'
254− let border = text "|==="
255− return $
279+ let border = separator <> text "==="
280+ return $
256281 caption'' $$ tablespec $$ border $$ head'' $$ body $$ border $$ blankline
257282blockToAsciiDoc opts (BulletList items) = do
258283 inlist <- gets inList
test/command/nested-table-to-asciidoc-6942.mdnew file+82
@@ -0,0 +1,82 @@
1+A table within a table should be convertet into a table within table
2+
3+```
4+% pandoc -f html -t asciidoc
5+<!doctype html>
6+<html>
7+<head>
8+<meta charset="utf-8">
9+<title> NestedTables </title>
10+</head>
11+<body>
12+<table>
13+ <tr>
14+ <td >
15+ <table> <tr> <td> a1 </td> <td> a2 </td> </tr> </table>
16+ </td>
17+ <td>b</td>
18+ </tr>
19+ <tr>
20+ <td>c</td> <td>d </td>
21+ </tr>
22+</table>
23+</body>
24+</html>
25+^D
26+[width="100%",cols="50%,50%",]
27+|===
28+a|
29+[cols=",",]
30+!===
31+!a1 !a2
32+!===
33+
34+|b
35+|c |d
36+|===
37+```
38+
39+A table within a table within a table cannot be converted because asciidoc only
40+supports two levels of tables.
41+The table on level 3 is thus converted to level 2 and a warning is produced
42+```
43+% pandoc -f html -t asciidoc --verbose
44+<!doctype html>
45+<html>
46+<head>
47+<meta charset="utf-8">
48+<title> NestedTables </title>
49+</head>
50+<body>
51+<table>
52+ <tr>
53+ <td>
54+ <table> <tr>
55+ <td> a1 </td>
56+ <td>
57+ <table> <tr> <td> 1 </td> <td> 2 </td> </tr> </table>
58+ </td>
59+ </tr> </table>
60+ </td>
61+ <td>b</td>
62+ </tr>
63+ <tr>
64+ <td>c</td> <td>d </td>
65+ </tr>
66+</table>
67+</body>
68+</html>
69+^D
70+[INFO] Not rendering Table ("",[],[]) (Caption Nothing []) [(AlignDefault,ColWidth 0.5),(AlignDefault,ColWidth 0.5)] (TableHead ("",[],[]) []) [TableBody ("",[],[]) (RowHeadColumns 0) [] [Row ("",[],[]) [Cell ("",[],[]) AlignDefault (RowSpan 1) (ColSpan 1) [Plain [Str "a1"]],Cell ("",[],[]) AlignDefault (RowSpan 1) (ColSpan 1) [Table ("",[],[]) (Caption Nothing []) [(AlignDefault,ColWidthDefault),(AlignDefault,ColWidthDefault)] (TableHead ("",[],[]) []) [TableBody ("",[],[]) (RowHeadColumns 0) [] [Row ("",[],[]) [Cell ("",[],[]) AlignDefault (RowSpan 1) (ColSpan 1) [Plain [Str "1"]],Cell ("",[],[]) AlignDefault (RowSpan 1) (ColSpan 1) [Plain [Str "2"]]]]] (TableFoot ("",[],[]) [])]]]] (TableFoot ("",[],[]) [])
71+[width="100%",cols="50%,50%",]
72+|===
73+a|
74+[width="100%",cols="50%,50%",]
75+!===
76+!a1 !
77+!===
78+
79+|b
80+|c |d
81+|===
82+```