diff options
-rw-r--r-- | src/Text/Pandoc/Writers/MediaWiki.hs | 73 | ||||
-rw-r--r-- | tests/tables.mediawiki | 141 |
2 files changed, 152 insertions, 62 deletions
diff --git a/src/Text/Pandoc/Writers/MediaWiki.hs b/src/Text/Pandoc/Writers/MediaWiki.hs index cdaa8bef0..f22172505 100644 --- a/src/Text/Pandoc/Writers/MediaWiki.hs +++ b/src/Text/Pandoc/Writers/MediaWiki.hs @@ -118,17 +118,27 @@ blockToMediaWiki opts (BlockQuote blocks) = do contents <- blockListToMediaWiki opts blocks return $ "<blockquote>" ++ contents ++ "</blockquote>" -blockToMediaWiki opts (Table caption aligns widths headers rows) = do +blockToMediaWiki opts (Table capt aligns widths headers rows') = do let alignStrings = map alignmentToString aligns - captionDoc <- if null caption + captionDoc <- if null capt then return "" else do - c <- inlineListToMediaWiki opts caption - return $ "<caption>" ++ c ++ "</caption>" - colHeads <- colHeadsToMediaWiki opts alignStrings widths headers - rows' <- mapM (tableRowToMediaWiki opts alignStrings) rows - return $ "<table>\n" ++ captionDoc ++ colHeads ++ vcat rows' ++ "\n</table>" - + c <- inlineListToMediaWiki opts capt + return $ "<caption>" ++ c ++ "</caption>\n" + let percent w = show (truncate (100*w) :: Integer) ++ "%" + let coltags = if all (== 0.0) widths + then "" + else unlines $ map + (\w -> "<col width=\"" ++ percent w ++ "\" />") widths + head' <- if all null headers + then return "" + else do + hs <- tableRowToMediaWiki opts alignStrings 0 headers + return $ "<thead>\n" ++ hs ++ "\n</thead>\n" + body' <- zipWithM (tableRowToMediaWiki opts alignStrings) [1..] rows' + return $ "<table>\n" ++ captionDoc ++ coltags ++ head' ++ + "<tbody>\n" ++ unlines body' ++ "</tbody>\n</table>\n" + blockToMediaWiki opts x@(BulletList items) = do oldUseTags <- get >>= return . stUseTags let useTags = oldUseTags || not (isSimpleList x) @@ -249,25 +259,27 @@ isPlainOrPara (Plain _) = True isPlainOrPara (Para _) = True isPlainOrPara _ = False -tr :: String -> String -tr x = "<tr>\n" ++ x ++ "\n</tr>" - -- | Concatenates strings with line breaks between them. vcat :: [String] -> String vcat = intercalate "\n" -- Auxiliary functions for tables: -colHeadsToMediaWiki :: WriterOptions - -> [[Char]] - -> [Double] +tableRowToMediaWiki :: WriterOptions + -> [String] + -> Int -> [[Block]] -> State WriterState String -colHeadsToMediaWiki opts alignStrings widths headers = do - heads <- sequence $ zipWith3 - (\alignment columnwidth item -> tableItemToMediaWiki opts "th" alignment columnwidth item) - alignStrings widths headers - return $ tr $ vcat heads +tableRowToMediaWiki opts alignStrings rownum cols' = do + let celltype = if rownum == 0 then "th" else "td" + let rowclass = case rownum of + 0 -> "header" + x | x `rem` 2 == 1 -> "odd" + _ -> "even" + cols'' <- sequence $ zipWith + (\alignment item -> tableItemToMediaWiki opts celltype alignment item) + alignStrings cols' + return $ "<tr class=\"" ++ rowclass ++ "\">\n" ++ unlines cols'' ++ "</tr>" alignmentToString :: Alignment -> [Char] alignmentToString alignment = case alignment of @@ -276,27 +288,16 @@ alignmentToString alignment = case alignment of AlignCenter -> "center" AlignDefault -> "left" -tableRowToMediaWiki :: WriterOptions - -> [[Char]] - -> [[Block]] - -> State WriterState String -tableRowToMediaWiki opts aligns columns = - (sequence $ zipWith3 (tableItemToMediaWiki opts "td") aligns (repeat 0) columns) >>= - return . tr . vcat - tableItemToMediaWiki :: WriterOptions - -> [Char] - -> [Char] - -> Double + -> String + -> String -> [Block] -> State WriterState String -tableItemToMediaWiki opts tag' align' width' item = do +tableItemToMediaWiki opts celltype align' item = do + let mkcell x = "<" ++ celltype ++ " align=\"" ++ align' ++ "\">" ++ + x ++ "</" ++ celltype ++ ">" contents <- blockListToMediaWiki opts item - let attrib = " align=\"" ++ align' ++ "\"" ++ - if width' /= 0 - then " style=\"width: " ++ (show (truncate (100 * width') :: Integer)) ++ "%;\"" - else "" - return $ "<" ++ tag' ++ attrib ++ ">" ++ contents ++ "</" ++ tag' ++ ">" + return $ mkcell contents -- | Convert list of Pandoc block elements to MediaWiki. blockListToMediaWiki :: WriterOptions -- ^ Options diff --git a/tests/tables.mediawiki b/tests/tables.mediawiki index 2f505e6aa..4836ecd79 100644 --- a/tests/tables.mediawiki +++ b/tests/tables.mediawiki @@ -1,123 +1,212 @@ Simple table with caption: <table> -<caption>Demonstration of simple table syntax.</caption><tr> +<caption>Demonstration of simple table syntax.</caption> +<thead> +<tr class="header"> <th align="right">Right</th> <th align="left">Left</th> <th align="center">Center</th> <th align="left">Default</th> -</tr><tr> +</tr> +</thead> +<tbody> +<tr class="odd"> <td align="right">12</td> <td align="left">12</td> <td align="center">12</td> <td align="left">12</td> </tr> -<tr> +<tr class="even"> <td align="right">123</td> <td align="left">123</td> <td align="center">123</td> <td align="left">123</td> </tr> -<tr> +<tr class="odd"> <td align="right">1</td> <td align="left">1</td> <td align="center">1</td> <td align="left">1</td> </tr> +</tbody> </table> + Simple table without caption: <table> -<tr> +<thead> +<tr class="header"> <th align="right">Right</th> <th align="left">Left</th> <th align="center">Center</th> <th align="left">Default</th> -</tr><tr> +</tr> +</thead> +<tbody> +<tr class="odd"> <td align="right">12</td> <td align="left">12</td> <td align="center">12</td> <td align="left">12</td> </tr> -<tr> +<tr class="even"> <td align="right">123</td> <td align="left">123</td> <td align="center">123</td> <td align="left">123</td> </tr> -<tr> +<tr class="odd"> <td align="right">1</td> <td align="left">1</td> <td align="center">1</td> <td align="left">1</td> </tr> +</tbody> </table> + Simple table indented two spaces: <table> -<caption>Demonstration of simple table syntax.</caption><tr> +<caption>Demonstration of simple table syntax.</caption> +<thead> +<tr class="header"> <th align="right">Right</th> <th align="left">Left</th> <th align="center">Center</th> <th align="left">Default</th> -</tr><tr> +</tr> +</thead> +<tbody> +<tr class="odd"> <td align="right">12</td> <td align="left">12</td> <td align="center">12</td> <td align="left">12</td> </tr> -<tr> +<tr class="even"> <td align="right">123</td> <td align="left">123</td> <td align="center">123</td> <td align="left">123</td> </tr> -<tr> +<tr class="odd"> <td align="right">1</td> <td align="left">1</td> <td align="center">1</td> <td align="left">1</td> </tr> +</tbody> </table> + Multiline table with caption: <table> -<caption>Here's the caption. It may span multiple lines.</caption><tr> -<th align="center" style="width: 15%;">Centered Header</th> -<th align="left" style="width: 13%;">Left Aligned</th> -<th align="right" style="width: 16%;">Right Aligned</th> -<th align="left" style="width: 33%;">Default aligned</th> -</tr><tr> +<caption>Here's the caption. It may span multiple lines.</caption> +<col width="15%" /> +<col width="13%" /> +<col width="16%" /> +<col width="33%" /> +<thead> +<tr class="header"> +<th align="center">Centered Header</th> +<th align="left">Left Aligned</th> +<th align="right">Right Aligned</th> +<th align="left">Default aligned</th> +</tr> +</thead> +<tbody> +<tr class="odd"> <td align="center">First</td> <td align="left">row</td> <td align="right">12.0</td> <td align="left">Example of a row that spans multiple lines.</td> </tr> -<tr> +<tr class="even"> <td align="center">Second</td> <td align="left">row</td> <td align="right">5.0</td> <td align="left">Here's another one. Note the blank line between rows.</td> </tr> +</tbody> </table> + Multiline table without caption: <table> -<tr> -<th align="center" style="width: 15%;">Centered Header</th> -<th align="left" style="width: 13%;">Left Aligned</th> -<th align="right" style="width: 16%;">Right Aligned</th> -<th align="left" style="width: 33%;">Default aligned</th> -</tr><tr> +<col width="15%" /> +<col width="13%" /> +<col width="16%" /> +<col width="33%" /> +<thead> +<tr class="header"> +<th align="center">Centered Header</th> +<th align="left">Left Aligned</th> +<th align="right">Right Aligned</th> +<th align="left">Default aligned</th> +</tr> +</thead> +<tbody> +<tr class="odd"> <td align="center">First</td> <td align="left">row</td> <td align="right">12.0</td> <td align="left">Example of a row that spans multiple lines.</td> </tr> -<tr> +<tr class="even"> <td align="center">Second</td> <td align="left">row</td> <td align="right">5.0</td> <td align="left">Here's another one. Note the blank line between rows.</td> </tr> +</tbody> </table> + +Table without column headers: + +<table> +<tbody> +<tr class="odd"> +<td align="right">12</td> +<td align="left">12</td> +<td align="center">12</td> +<td align="right">12</td> +</tr> +<tr class="even"> +<td align="right">123</td> +<td align="left">123</td> +<td align="center">123</td> +<td align="right">123</td> +</tr> +<tr class="odd"> +<td align="right">1</td> +<td align="left">1</td> +<td align="center">1</td> +<td align="right">1</td> +</tr> +</tbody> +</table> + +Multiline table without column headers: + +<table> +<col width="15%" /> +<col width="13%" /> +<col width="16%" /> +<col width="33%" /> +<tbody> +<tr class="odd"> +<td align="center">First</td> +<td align="left">row</td> +<td align="right">12.0</td> +<td align="left">Example of a row that spans multiple lines.</td> +</tr> +<tr class="even"> +<td align="center">Second</td> +<td align="left">row</td> +<td align="right">5.0</td> +<td align="left">Here's another one. Note the blank line between rows.</td> +</tr> +</tbody> +</table> + |