From 0cf4652ad6cb59f5c10058f1dfc03cb2b2ab620d Mon Sep 17 00:00:00 2001 From: fiddlosopher Date: Tue, 9 Mar 2010 04:34:07 +0000 Subject: Docbook writer: handle headerless tables. In addition, use cols, thead, and tbody. git-svn-id: https://pandoc.googlecode.com/svn/trunk@1874 788f1e2b-df1e-0410-8736-df70ead52e1b --- src/Text/Pandoc/Writers/Docbook.hs | 53 ++- tests/tables.docbook | 657 ++++++++++++++++++++----------------- 2 files changed, 388 insertions(+), 322 deletions(-) diff --git a/src/Text/Pandoc/Writers/Docbook.hs b/src/Text/Pandoc/Writers/Docbook.hs index 996e50e6b..153f93391 100644 --- a/src/Text/Pandoc/Writers/Docbook.hs +++ b/src/Text/Pandoc/Writers/Docbook.hs @@ -168,25 +168,22 @@ blockToDocbook _ HorizontalRule = empty -- not semantic blockToDocbook opts (Table caption aligns widths headers rows) = let alignStrings = map alignmentToString aligns captionDoc = if null caption - then empty - else inTagsIndented "caption" - (inlinesToDocbook opts caption) + then empty + else inTagsIndented "caption" + (inlinesToDocbook opts caption) tableType = if isEmpty captionDoc then "informaltable" else "table" - in inTagsIndented tableType $ captionDoc $$ - (colHeadsToDocbook opts alignStrings widths headers) $$ - (vcat $ map (tableRowToDocbook opts alignStrings) rows) - -colHeadsToDocbook :: WriterOptions - -> [[Char]] - -> [Double] - -> [[Block]] - -> Doc -colHeadsToDocbook _ _ _ hs | all null hs = empty -colHeadsToDocbook opts alignStrings widths headers = - let heads = zipWith3 (\align width item -> - tableItemToDocbook opts "th" align width item) - alignStrings widths headers - in inTagsIndented "tr" $ vcat heads + percent w = show (truncate (100*w) :: Integer) ++ "%" + coltags = if all (== 0.0) widths + then empty + else vcat $ map (\w -> + selfClosingTag "col" [("width", percent w)]) widths + head' = if all null headers + then empty + else inTagsIndented "thead" $ + tableRowToDocbook opts alignStrings "th" headers + body' = inTagsIndented "tbody" $ + vcat $ map (tableRowToDocbook opts alignStrings "td") rows + in inTagsIndented tableType $ captionDoc $$ coltags $$ head' $$ body' alignmentToString :: Alignment -> [Char] alignmentToString alignment = case alignment of @@ -195,22 +192,22 @@ alignmentToString alignment = case alignment of AlignCenter -> "center" AlignDefault -> "left" -tableRowToDocbook :: WriterOptions -> [[Char]] -> [[Block]] -> Doc -tableRowToDocbook opts aligns cols = inTagsIndented "tr" $ - vcat $ zipWith3 (tableItemToDocbook opts "td") aligns (repeat 0) cols +tableRowToDocbook :: WriterOptions + -> [String] + -> String + -> [[Block]] + -> Doc +tableRowToDocbook opts aligns celltype cols = + inTagsIndented "tr" $ vcat $ + zipWith (tableItemToDocbook opts celltype) aligns cols tableItemToDocbook :: WriterOptions -> [Char] -> [Char] - -> Double -> [Block] -> Doc -tableItemToDocbook opts tag align width item = - let attrib = [("align", align)] ++ - if width /= 0 - then [("style", "{width: " ++ - show (truncate (100*width) :: Integer) ++ "%;}")] - else [] +tableItemToDocbook opts tag align item = + let attrib = [("align", align)] in inTags True tag attrib $ vcat $ map (blockToDocbook opts) item -- | Take list of inline elements and return wrapped doc. diff --git a/tests/tables.docbook b/tests/tables.docbook index 6622fb2b2..6483a97c0 100644 --- a/tests/tables.docbook +++ b/tests/tables.docbook @@ -5,123 +5,131 @@ Demonstration of simple table syntax. - - - Right - - - Left - - - Center - - - Default - - - - - 12 - - - 12 - - - 12 - - - 12 - - - - - 123 - - - 123 - - - 123 - - - 123 - - - - - 1 - - - 1 - - - 1 - - - 1 - - + + + + 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 - - + + + + Right + + + Left + + + Center + + + Default + + + + + + + 12 + + + 12 + + + 12 + + + 12 + + + + + 123 + + + 123 + + + 123 + + + 123 + + + + + 1 + + + 1 + + + 1 + + + 1 + + + Simple table indented two spaces: @@ -130,62 +138,66 @@ Demonstration of simple table syntax. - - - Right - - - Left - - - Center - - - Default - - - - - 12 - - - 12 - - - 12 - - - 12 - - - - - 123 - - - 123 - - - 123 - - - 123 - - - - - 1 - - - 1 - - - 1 - - - 1 - - + + + + Right + + + Left + + + Center + + + Default + + + + + + + 12 + + + 12 + + + 12 + + + 12 + + + + + 123 + + + 123 + + + 123 + + + 123 + + + + + 1 + + + 1 + + + 1 + + + 1 + + + Multiline table with caption: @@ -194,140 +206,197 @@ Here's the caption. It may span multiple lines. - - - Centered Header - - - Left Aligned - - - Right Aligned - - - Default 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. - - + + + + + + + + Centered Header + + + Left Aligned + + + Right Aligned + + + Default 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 Header - - - Left Aligned - - - Right Aligned - - - Default 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. - - + + + + + + + + Centered Header + + + Left Aligned + + + Right Aligned + + + Default 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: - - - 12 - - - 12 - - - 12 - - - 12 - - - - - 123 - - - 123 - - - 123 - - - 123 - - - - - 1 - - - 1 - - - 1 - - - 1 - - + + + + 12 + + + 12 + + + 12 + + + 12 + + + + + 123 + + + 123 + + + 123 + + + 123 + + + + + 1 + + + 1 + + + 1 + + + 1 + + + + + + Multiline table without column headers: + + + + + + + + + + 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. + + + -- cgit v1.2.3