git.iko.soy/typeable/pandoc
on master help sign in
Muse writer: add support for grid tables
07fc850Alexander Krotov
src/Text/Pandoc/Writers/Muse.hs+35−23
@@ -163,6 +163,32 @@ flatBlockListToMuse bs@(DefinitionList _ : DefinitionList _ : _) = catWithBlankL
163flatBlockListToMuse bs@(_ : _) = catWithBlankLines bs 0
163flatBlockListToMuse bs@(_ : _) = catWithBlankLines bs 0
164flatBlockListToMuse [] = return mempty
164flatBlockListToMuse [] = return mempty
165
165
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+
166-- | Convert list of Pandoc block elements to Muse.
192-- | Convert list of Pandoc block elements to Muse.
167blockListToMuse :: PandocMonad m
193blockListToMuse :: PandocMonad m
168 => [Block] -- ^ List of block elements
194 => [Block] -- ^ List of block elements
@@ -252,29 +278,15 @@ blockToMuse (Header level (ident,_,_) inlines) = do
252 return $ blankline <> attr' $$ nowrap (header' <> contents) <> blankline
278 return $ blankline <> attr' $$ nowrap (header' <> contents) <> blankline
253-- https://www.gnu.org/software/emacs-muse/manual/muse.html#Horizontal-Rules-and-Anchors
279-- https://www.gnu.org/software/emacs-muse/manual/muse.html#Horizontal-Rules-and-Anchors
254blockToMuse HorizontalRule = return $ blankline $$ "----" $$ blankline
280blockToMuse HorizontalRule = return $ blankline $$ "----" $$ blankline
255−blockToMuse (Table caption _ _ headers rows) = do
281+blockToMuse (Table caption aligns widths headers rows) =
256− caption' <- inlineListToMuse caption
282+ if all (== 0.0) widths
257− headers' <- mapM blockListToMuse headers
283+ then simpleTable caption headers rows
258− rows' <- mapM (mapM blockListToMuse) rows
284+ else do
259− let noHeaders = all null headers
285+ opts <- asks envOptions
260−
286+ gridTable opts blocksToDoc True (map (const AlignDefault) aligns) widths headers rows
261− let numChars = maximum . map offset
287+ where
262− -- FIXME: width is not being used.
288+ blocksToDoc opts blocks =
263− let widthsInChars =
289+ local (\env -> env { envOptions = opts }) $ blockListToMuse blocks
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
278blockToMuse (Div _ bs) = flatBlockListToMuse bs
290blockToMuse (Div _ bs) = flatBlockListToMuse bs
279blockToMuse Null = return empty
291blockToMuse Null = return empty
280
292
test/tables.muse+24−11
@@ -23,17 +23,24 @@ Simple table indented two spaces:
23
23
24Multiline table with caption:
24Multiline table with caption:
25
25
26− Centered Header || Left Aligned || Right Aligned || Default aligned
26++----------+---------+-----------+--------------------------+
27− First | row | 12.0 | Example of a row that spans multiple lines.
27+| First | row | 12.0 | Example of a row that |
28+| | | | spans multiple lines. |
29++----------+---------+-----------+--------------------------+
28− Second | row | 5.0 | Here’s another one. Note the blank line between rows.
30+| Second | row | 5.0 | Here’s another one. Note |
29− |+ Here’s the caption. It may span multiple lines. +|
31+| | | | the blank line between |
30−
32+| | | | rows. |
33++----------+---------+-----------+--------------------------+
31Multiline table without caption:
34Multiline table without caption:
32
35
33− Centered Header || Left Aligned || Right Aligned || Default aligned
36++----------+---------+-----------+--------------------------+
34− First | row | 12.0 | Example of a row that spans multiple lines.
37+| First | row | 12.0 | Example of a row that |
38+| | | | spans multiple lines. |
39++----------+---------+-----------+--------------------------+
35− Second | row | 5.0 | Here’s another one. Note the blank line between rows.
40+| Second | row | 5.0 | Here’s another one. Note |
36−
41+| | | | the blank line between |
42+| | | | rows. |
43++----------+---------+-----------+--------------------------+
37Table without column headers:
44Table without column headers:
38
45
39 12 | 12 | 12 | 12
46 12 | 12 | 12 | 12
@@ -42,5 +49,11 @@ Table without column headers:
42
49
43Multiline table without column headers:
50Multiline table without column headers:
44
51
52++----------+---------+-----------+--------------------------+
45− First | row | 12.0 | Example of a row that spans multiple lines.
53+| First | row | 12.0 | Example of a row that |
54+| | | | spans multiple lines. |
55++----------+---------+-----------+--------------------------+
46− Second | row | 5.0 | Here’s another one. Note the blank line between rows.
56+| Second | row | 5.0 | Here’s another one. Note |
57+| | | | the blank line between |
58+| | | | rows. |
59++----------+---------+-----------+--------------------------+