@@ -34,7 +34,8 @@ module Text.Pandoc.Writers.Markdown (
3434 ) where
3535import Text.Pandoc.Definition
3636import Text.Pandoc.Shared
37−import Data.List ( group, isPrefixOf, drop, find )
37+import Text.Pandoc.Blocks
38+import Data.List ( group, isPrefixOf, drop, find, intersperse )
3839import Text.PrettyPrint.HughesPJ hiding ( Str )
3940import Control.Monad.State
4041
@@ -154,8 +155,38 @@ blockToMarkdown opts (BlockQuote blocks) = do
154155 contents <- blockListToMarkdown opts blocks
155156 let quotedContents = unlines $ map ("> " ++) $ lines $ render contents
156157 return $ text quotedContents
157−blockToMarkdown opts (Table caption _ _ headers rows) = blockToMarkdown opts
158− (Para [Str "pandoc: TABLE unsupported in Markdown writer"])
158+blockToMarkdown opts (Table caption aligns widths headers rows) = do
159+ caption' <- inlineListToMarkdown opts caption
160+ let caption'' = if null caption
161+ then empty
162+ else text "" $$ (text "Table: " <> caption')
163+ headers' <- mapM (blockListToMarkdown opts) headers
164+ let widthsInChars = map (floor . (78 *)) widths
165+ let alignHeader alignment = case alignment of
166+ AlignLeft -> leftAlignBlock
167+ AlignCenter -> centerAlignBlock
168+ AlignRight -> rightAlignBlock
169+ AlignDefault -> leftAlignBlock
170+ let makeRow = hsepBlocks . (zipWith alignHeader aligns) .
171+ (zipWith docToBlock widthsInChars)
172+ let head = makeRow headers'
173+ rows' <- mapM (\row -> do
174+ cols <- mapM (blockListToMarkdown opts) row
175+ return $ makeRow cols) rows
176+ let tableWidth = sum widthsInChars
177+ let maxRowHeight = maximum $ map heightOfBlock (head:rows')
178+ let isMultilineTable = maxRowHeight > 1
179+ let border = if isMultilineTable
180+ then text $ replicate tableWidth '-'
181+ else empty
182+ let underline = hsep $
183+ map (\width -> text $ replicate width '-') widthsInChars
184+ let spacer = if isMultilineTable
185+ then text ""
186+ else empty
187+ let body = vcat $ intersperse spacer $ map blockToDoc rows'
188+ return $ nest 2 $ border $$ (blockToDoc head) $$ underline $$ body $$
189+ border $$ caption'' $$ text ""
159190blockToMarkdown opts (BulletList items) = do
160191 contents <- mapM (bulletListItemToMarkdown opts) items
161192 return $ (vcat contents) <> text "\n"