@@ -48,7 +48,7 @@ import Text.Pandoc.Shared ( escapeURI, removeFormatting, camelCaseToHyphenated
4848 , trimr, normalize, substitute )
4949import Text.Pandoc.Writers.Shared ( defField, metaToJSON )
5050import Text.Pandoc.Templates ( renderTemplate' )
51−import Data.List ( intersect, intercalate, isPrefixOf )
51+import Data.List ( intersect, intercalate, isPrefixOf, transpose )
5252import Data.Default (Default(..))
5353import Network.URI ( isURI )
5454import Control.Monad ( zipWithM )
@@ -181,20 +181,32 @@ blockToDokuWiki opts (BlockQuote blocks) = do
181181 then return $ "> " ++ contents
182182 else return $ "<HTML><blockquote>\n" ++ contents ++ "</blockquote></HTML>"
183183
184−blockToDokuWiki opts (Table capt aligns _ headers rows') = do
184+blockToDokuWiki opts (Table capt aligns _ headers rows) = do
185185 captionDoc <- if null capt
186186 then return ""
187187 else do
188188 c <- inlineListToDokuWiki opts capt
189189 return $ "" ++ c ++ "\n"
190− head' <- if all null headers
191− then return ""
192− else do
193− hs <- tableHeaderToDokuWiki opts aligns headers
194− return $ hs ++ "\n"
195− body' <- mapM (tableRowToDokuWiki opts aligns) rows'
196− return $ captionDoc ++ head' ++
197− unlines body'
190+ headers' <- if all null headers
191+ then return []
192+ else zipWithM (tableItemToDokuWiki opts) aligns headers
193+ rows' <- mapM (zipWithM (tableItemToDokuWiki opts) aligns) rows
194+ let widths = map (maximum . map length) $ transpose (headers':rows')
195+ let padTo (width, al) s =
196+ case (width - length s) of
197+ x | x > 0 ->
198+ if al == AlignLeft || al == AlignDefault
199+ then s ++ replicate x ' '
200+ else if al == AlignRight
201+ then replicate x ' ' ++ s
202+ else replicate (x `div` 2) ' ' ++
203+ s ++ replicate (x - x `div` 2) ' '
204+ | otherwise -> s
205+ let renderRow sep cells = sep ++
206+ intercalate sep (zipWith padTo (zip widths aligns) cells) ++ sep
207+ return $ captionDoc ++
208+ (if null headers' then "" else renderRow "^" headers' ++ "\n") ++
209+ unlines (map (renderRow "|") rows')
198210
199211blockToDokuWiki opts x@(BulletList items) = do
200212 oldUseTags <- stUseTags <$> ask
@@ -357,32 +369,10 @@ backSlashLineBreaks cs = reverse $ g $ reverse $ concatMap f cs
357369
358370-- Auxiliary functions for tables:
359371
360−-- TODO Eliminate copy-and-pasted code in tableHeaderToDokuWiki and tableRowToDokuWiki
361−tableHeaderToDokuWiki :: WriterOptions
362− -> [Alignment]
363− -> [[Block]]
364− -> DokuWiki String
365−tableHeaderToDokuWiki opts aligns cols' = do
366− cols'' <- zipWithM
367− (tableItemToDokuWiki opts)
368− aligns cols'
369− return $ "^ " ++ joinHeaders cols'' ++ " ^"
370−
371−tableRowToDokuWiki :: WriterOptions
372− -> [Alignment]
373− -> [[Block]]
374− -> DokuWiki String
375−tableRowToDokuWiki opts alignStrings cols' = do
376− cols'' <- zipWithM
377− (tableItemToDokuWiki opts)
378− alignStrings cols'
379− return $ "| " ++ "" ++ joinColumns cols'' ++ " |"
380−
381372tableItemToDokuWiki :: WriterOptions
382373 -> Alignment
383374 -> [Block]
384375 -> DokuWiki String
385−-- TODO Fix celltype and align' defined but not used
386376tableItemToDokuWiki opts align' item = do
387377 let mkcell x = (if align' == AlignRight || align' == AlignCenter
388378 then " "
@@ -394,14 +384,6 @@ tableItemToDokuWiki opts align' item = do
394384 blockListToDokuWiki opts item
395385 return $ mkcell contents
396386
397−-- | Concatenates columns together.
398−joinColumns :: [String] -> String
399−joinColumns = intercalate " | "
400−
401−-- | Concatenates headers together.
402−joinHeaders :: [String] -> String
403−joinHeaders = intercalate " ^ "
404−
405387-- | Convert list of Pandoc block elements to DokuWiki.
406388blockListToDokuWiki :: WriterOptions -- ^ Options
407389 -> [Block] -- ^ List of block elements