@@ -34,7 +34,8 @@ module Text.Pandoc.Writers.Markdown (
35import Text.Pandoc.Definition
35import Text.Pandoc.Definition
36import Text.Pandoc.Shared
36import Text.Pandoc.Shared
37+import Text.Pandoc.Blocks
37−import Data.List ( group, isPrefixOf, drop, find )
38+import Data.List ( group, isPrefixOf, drop, find, intersperse )
38import Text.PrettyPrint.HughesPJ hiding ( Str )
39import Text.PrettyPrint.HughesPJ hiding ( Str )
39import Control.Monad.State
40import Control.Monad.State
@@ -154,8 +155,38 @@ blockToMarkdown opts (BlockQuote blocks) = do
154 contents <- blockListToMarkdown opts blocks
155 contents <- blockListToMarkdown opts blocks
155 let quotedContents = unlines $ map ("> " ++) $ lines $ render contents
156 let quotedContents = unlines $ map ("> " ++) $ lines $ render contents
156 return $ text quotedContents
157 return $ text quotedContents
157−blockToMarkdown opts (Table caption _ _ headers rows) = blockToMarkdown opts
158+blockToMarkdown opts (Table caption aligns widths headers rows) = do
158− (Para [Str "pandoc: TABLE unsupported in Markdown writer"])
159+ caption' <- inlineListToMarkdown opts caption
160+ let caption'' = if null caption
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 '-'
182+ let underline = hsep $
183+ map (\width -> text $ replicate width '-') widthsInChars
184+ let spacer = if isMultilineTable
187+ let body = vcat $ intersperse spacer $ map blockToDoc rows'
188+ return $ nest 2 $ border $$ (blockToDoc head) $$ underline $$ body $$
189+ border $$ caption'' $$ text ""
159blockToMarkdown opts (BulletList items) = do
190blockToMarkdown opts (BulletList items) = do
160 contents <- mapM (bulletListItemToMarkdown opts) items
191 contents <- mapM (bulletListItemToMarkdown opts) items
161 return $ (vcat contents) <> text "\n"
192 return $ (vcat contents) <> text "\n"