git.iko.soy/typeable/pandoc
on master help sign in
Markdown writer: Prettier pipe tables.
ab5dda7John MacFarlane
src/Text/Pandoc/Writers/Markdown.hs+16−8
@@ -478,16 +478,24 @@ addMarkdownAttribute s =
478
478
479pipeTable :: Bool -> [Alignment] -> [Doc] -> [[Doc]] -> State WriterState Doc
479pipeTable :: Bool -> [Alignment] -> [Doc] -> [[Doc]] -> State WriterState Doc
480pipeTable headless aligns rawHeaders rawRows = do
480pipeTable headless aligns rawHeaders rawRows = do
481+ let sp = text " "
482+ let blockFor AlignLeft x y = lblock (x + 2) (sp <> y) <> lblock 0 empty
483+ blockFor AlignCenter x y = cblock (x + 2) (sp <> y) <> lblock 0 empty
484+ blockFor AlignRight x y = rblock (x + 2) (sp <> y) <> lblock 0 empty
485+ blockFor _ x y = lblock (x + 2) (sp <> y) <> lblock 0 empty
486+ let widths = map (max 3 . maximum . map offset) $ transpose (rawHeaders : rawRows)
481 let torow cs = nowrap $ text "|" <>
487 let torow cs = nowrap $ text "|" <>
482− hcat (intersperse (text "|") $ map chomp cs) <> text "|"
488+ hcat (intersperse (text "|") $
483− let toborder (a, h) = let wid = max (offset h) 3
489+ zipWith3 blockFor aligns widths (map chomp cs))
490+ <> text "|"
484− in text $ case a of
491+ let toborder (a, w) = text $ case a of
485− AlignLeft -> ':':replicate (wid - 1) '-'
492+ AlignLeft -> ':':replicate (w + 1) '-'
486− AlignCenter -> ':':replicate (wid - 2) '-' ++ ":"
493+ AlignCenter -> ':':replicate w '-' ++ ":"
487− AlignRight -> replicate (wid - 1) '-' ++ ":"
494+ AlignRight -> replicate (w + 1) '-' ++ ":"
488− AlignDefault -> replicate wid '-'
495+ AlignDefault -> replicate (w + 2) '-'
489 let header = if headless then empty else torow rawHeaders
496 let header = if headless then empty else torow rawHeaders
497+ let border = nowrap $ text "|" <> hcat (intersperse (text "|") $
490− let border = torow $ map toborder $ zip aligns rawHeaders
498+ map toborder $ zip aligns widths) <> text "|"
491 let body = vcat $ map torow rawRows
499 let body = vcat $ map torow rawRows
492 return $ header $$ border $$ body
500 return $ header $$ border $$ body
493
501