From ea6721944903122b53c657b4901dbf687a1e416a Mon Sep 17 00:00:00 2001 From: fiddlosopher Date: Thu, 11 Mar 2010 03:01:18 +0000 Subject: MediaWiki writer: fixed headerless tables. git-svn-id: https://pandoc.googlecode.com/svn/trunk@1878 788f1e2b-df1e-0410-8736-df70ead52e1b --- src/Text/Pandoc/Writers/MediaWiki.hs | 73 +++++++++--------- 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 $ "
" ++ contents ++ "
" -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 $ "" ++ c ++ "" - colHeads <- colHeadsToMediaWiki opts alignStrings widths headers - rows' <- mapM (tableRowToMediaWiki opts alignStrings) rows - return $ "\n" ++ captionDoc ++ colHeads ++ vcat rows' ++ "\n
" - + c <- inlineListToMediaWiki opts capt + return $ "" ++ c ++ "\n" + let percent w = show (truncate (100*w) :: Integer) ++ "%" + let coltags = if all (== 0.0) widths + then "" + else unlines $ map + (\w -> "") widths + head' <- if all null headers + then return "" + else do + hs <- tableRowToMediaWiki opts alignStrings 0 headers + return $ "\n" ++ hs ++ "\n\n" + body' <- zipWithM (tableRowToMediaWiki opts alignStrings) [1..] rows' + return $ "\n" ++ captionDoc ++ coltags ++ head' ++ + "\n" ++ unlines body' ++ "\n
\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 = "\n" ++ x ++ "\n" - -- | 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 $ "\n" ++ unlines cols'' ++ "" 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 ++ "" contents <- blockListToMediaWiki opts item - let attrib = " align=\"" ++ align' ++ "\"" ++ - if width' /= 0 - then " style=\"width: " ++ (show (truncate (100 * width') :: Integer)) ++ "%;\"" - else "" - return $ "<" ++ tag' ++ attrib ++ ">" ++ contents ++ "" + 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: - + + + - + + + + - + - + +
Demonstration of simple table syntax.
Demonstration of simple table syntax.
Right Left Center Default
12 12 12 12
123 123 123 123
1 1 1 1
+ Simple table without caption: - + + - + + + + - + - + +
Right Left Center Default
12 12 12 12
123 123 123 123
1 1 1 1
+ Simple table indented two spaces: - + + + - + + + + - + - + +
Demonstration of simple table syntax.
Demonstration of simple table syntax.
Right Left Center Default
12 12 12 12
123 123 123 123
1 1 1 1
+ Multiline table with caption: - - - - - - + +++++ + + + + + + + + + - + +
Here's the caption. It may span multiple lines.
Centered HeaderLeft AlignedRight AlignedDefault aligned
Here's the caption. It may span multiple lines.
Centered HeaderLeft AlignedRight AlignedDefault aligned
First row 12.0 Example of a row that spans multiple lines.
Second row 5.0 Here's another one. Note the blank line between rows.
+ Multiline table without caption: - - - - - - +++++ + + + + + + + + + - + +
Centered HeaderLeft AlignedRight AlignedDefault aligned
Centered HeaderLeft AlignedRight AlignedDefault aligned
First row 12.0 Example of a row that spans multiple lines.
Second row 5.0 Here's another one. Note the blank line between rows.
+ +Table without column headers: + + + + + + + + + + + + + + + + + + + + + + +
12121212
123123123123
1111
+ +Multiline table without column headers: + + +++++ + + + + + + + + + + + + + +
Firstrow12.0Example of a row that spans multiple lines.
Secondrow5.0Here's another one. Note the blank line between rows.
+ -- cgit v1.2.3