@@ -34,7 +34,8 @@ module Text.Pandoc.Writers.RST (
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
@@ -176,8 +177,38 @@ blockToRST opts (CodeBlock str) = return $ (text "::\n") $$ text "" $$
176blockToRST opts (BlockQuote blocks) = do
177blockToRST opts (BlockQuote blocks) = do
177 contents <- blockListToRST opts blocks
178 contents <- blockListToRST opts blocks
178 return $ (nest (writerTabStop opts) contents) <> text "\n"
179 return $ (nest (writerTabStop opts) contents) <> text "\n"
179−blockToRST opts (Table caption _ _ headers rows) = blockToRST opts
180+blockToRST opts (Table caption aligns widths headers rows) = do
180− (Para [Str "pandoc: TABLE unsupported in RST writer"])
181+ caption' <- inlineListToRST opts caption
182+ let caption'' = if null caption
184+ else text "" $$ (text "Table: " <> caption')
185+ headers' <- mapM (blockListToRST opts) headers
186+ let widthsInChars = map (floor . (78 *)) widths
187+ let alignHeader alignment = case alignment of
188+ AlignLeft -> leftAlignBlock
189+ AlignCenter -> centerAlignBlock
190+ AlignRight -> rightAlignBlock
191+ AlignDefault -> leftAlignBlock
192+ let hpipeBlocks blocks = hcatBlocks [beg, middle, end]
193+ where height = maximum (map heightOfBlock blocks)
194+ sep = TextBlock 3 height (replicate height " | ")
195+ beg = TextBlock 2 height (replicate height "| ")
196+ end = TextBlock 2 height (replicate height " |")
197+ middle = hcatBlocks $ intersperse sep blocks
198+ let makeRow = hpipeBlocks . (zipWith docToBlock widthsInChars)
199+ let head = makeRow headers'
200+ rows' <- mapM (\row -> do
201+ cols <- mapM (blockListToRST opts) row
202+ return $ makeRow cols) rows
203+ let tableWidth = sum widthsInChars
204+ let maxRowHeight = maximum $ map heightOfBlock (head:rows')
205+ let border ch = char '+' <> char ch <>
206+ (hcat $ intersperse (char ch <> char '+' <> char ch) $
207+ map (\l -> text $ replicate l ch) widthsInChars) <>
209+ let body = vcat $ intersperse (border '-') $ map blockToDoc rows'
210+ return $ border '-' $$ blockToDoc head $$ border '=' $$ body $$
211+ border '-' $$ caption'' $$ text ""
181blockToRST opts (BulletList items) = do
212blockToRST opts (BulletList items) = do
182 contents <- mapM (bulletListItemToRST opts) items
213 contents <- mapM (bulletListItemToRST opts) items
183 return $ (vcat contents) <> text "\n"
214 return $ (vcat contents) <> text "\n"