git.iko.soy/typeable/pandoc
on master help sign in
Muse writer: add support for grid tables
commit07fc8501726563d32b57fa5740e90dec17f8f4a8 authorAlexander Krotov <ilabdsf@gmail.com> dateThu 25 Oct 2018, 18:35 (8 years ago) parent02e515c
2 files changed+59−34
src/Text/Pandoc/Writers/Muse.hs+35−23 test/tables.muse+24−11
src/Text/Pandoc/Writers/Muse.hs+35−23
@@ -163,6 +163,32 @@ flatBlockListToMuse bs@(DefinitionList _ : DefinitionList _ : _) = catWithBlankL
163163flatBlockListToMuse bs@(_ : _) = catWithBlankLines bs 0
164164flatBlockListToMuse [] = return mempty
165165
166+simpleTable :: PandocMonad m
167+ => [Inline]
168+ -> [TableCell]
169+ -> [[TableCell]]
170+ -> Muse m Doc
171+simpleTable caption headers rows = do
172+ caption' <- inlineListToMuse caption
173+ headers' <- mapM blockListToMuse headers
174+ rows' <- mapM (mapM blockListToMuse) rows
175+ let noHeaders = all null headers
176+ let numChars = maximum . map offset
177+ let widthsInChars =
178+ map numChars $ transpose (headers' : rows')
179+ let hpipeBlocks sep blocks = hcat $ intersperse sep' blocks
180+ where h = maximum (1 : map height blocks)
181+ sep' = lblock (length sep) $ vcat (replicate h (text sep))
182+ let makeRow sep = (" " <>) . hpipeBlocks sep . zipWith lblock widthsInChars
183+ let head' = makeRow " || " headers'
184+ let rowSeparator = if noHeaders then " | " else " | "
185+ rows'' <- mapM (\row -> makeRow rowSeparator <$> mapM blockListToMuse row) rows
186+ let body = vcat rows''
187+ return $ (if noHeaders then empty else head')
188+ $$ body
189+ $$ (if null caption then empty else " |+ " <> caption' <> " +|")
190+ $$ blankline
191+
166192-- | Convert list of Pandoc block elements to Muse.
167193blockListToMuse :: PandocMonad m
168194 => [Block] -- ^ List of block elements
@@ -252,29 +278,15 @@ blockToMuse (Header level (ident,_,_) inlines) = do
252278 return $ blankline <> attr' $$ nowrap (header' <> contents) <> blankline
253279-- https://www.gnu.org/software/emacs-muse/manual/muse.html#Horizontal-Rules-and-Anchors
254280blockToMuse HorizontalRule = return $ blankline $$ "----" $$ blankline
255−blockToMuse (Table caption _ _ headers rows) = do
256− caption' <- inlineListToMuse caption
257− headers' <- mapM blockListToMuse headers
258− rows' <- mapM (mapM blockListToMuse) rows
259− let noHeaders = all null headers
260−
261− let numChars = maximum . map offset
262− -- FIXME: width is not being used.
263− let widthsInChars =
264− map numChars $ transpose (headers' : rows')
265− -- FIXME: Muse doesn't allow blocks with height more than 1.
266− let hpipeBlocks sep blocks = hcat $ intersperse sep' blocks
267− where h = maximum (1 : map height blocks)
268− sep' = lblock (length sep) $ vcat (replicate h (text sep))
269− let makeRow sep = (" " <>) . hpipeBlocks sep . zipWith lblock widthsInChars
270− let head' = makeRow " || " headers'
271− let rowSeparator = if noHeaders then " | " else " | "
272− rows'' <- mapM (\row -> makeRow rowSeparator <$> mapM blockListToMuse row) rows
273− let body = vcat rows''
274− return $ (if noHeaders then empty else head')
275− $$ body
276− $$ (if null caption then empty else " |+ " <> caption' <> " +|")
277− $$ blankline
281+blockToMuse (Table caption aligns widths headers rows) =
282+ if all (== 0.0) widths
283+ then simpleTable caption headers rows
284+ else do
285+ opts <- asks envOptions
286+ gridTable opts blocksToDoc True (map (const AlignDefault) aligns) widths headers rows
287+ where
288+ blocksToDoc opts blocks =
289+ local (\env -> env { envOptions = opts }) $ blockListToMuse blocks
278290blockToMuse (Div _ bs) = flatBlockListToMuse bs
279291blockToMuse Null = return empty
280292
test/tables.muse+24−11
@@ -23,17 +23,24 @@ Simple table indented two spaces:
2323
2424Multiline table with caption:
2525
26− Centered Header || Left Aligned || Right Aligned || Default aligned
27− First | row | 12.0 | Example of a row that spans multiple lines.
28− Second | row | 5.0 | Here’s another one. Note the blank line between rows.
29− |+ Here’s the caption. It may span multiple lines. +|
30−
26++----------+---------+-----------+--------------------------+
27+| First | row | 12.0 | Example of a row that |
28+| | | | spans multiple lines. |
29++----------+---------+-----------+--------------------------+
30+| Second | row | 5.0 | Here’s another one. Note |
31+| | | | the blank line between |
32+| | | | rows. |
33++----------+---------+-----------+--------------------------+
3134Multiline table without caption:
3235
33− Centered Header || Left Aligned || Right Aligned || Default aligned
34− First | row | 12.0 | Example of a row that spans multiple lines.
35− Second | row | 5.0 | Here’s another one. Note the blank line between rows.
36−
36++----------+---------+-----------+--------------------------+
37+| First | row | 12.0 | Example of a row that |
38+| | | | spans multiple lines. |
39++----------+---------+-----------+--------------------------+
40+| Second | row | 5.0 | Here’s another one. Note |
41+| | | | the blank line between |
42+| | | | rows. |
43++----------+---------+-----------+--------------------------+
3744Table without column headers:
3845
3946 12 | 12 | 12 | 12
@@ -42,5 +49,11 @@ Table without column headers:
4249
4350Multiline table without column headers:
4451
45− First | row | 12.0 | Example of a row that spans multiple lines.
46− Second | row | 5.0 | Here’s another one. Note the blank line between rows.
52++----------+---------+-----------+--------------------------+
53+| First | row | 12.0 | Example of a row that |
54+| | | | spans multiple lines. |
55++----------+---------+-----------+--------------------------+
56+| Second | row | 5.0 | Here’s another one. Note |
57+| | | | the blank line between |
58+| | | | rows. |
59++----------+---------+-----------+--------------------------+