diff options
-rw-r--r-- | src/Text/Pandoc/Writers/Docbook.hs | 53 | ||||
-rw-r--r-- | 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 @@ <caption> Demonstration of simple table syntax. </caption> - <tr> - <th align="right"> - Right - </th> - <th align="left"> - Left - </th> - <th align="center"> - Center - </th> - <th align="left"> - Default - </th> - </tr> - <tr> - <td align="right"> - 12 - </td> - <td align="left"> - 12 - </td> - <td align="center"> - 12 - </td> - <td align="left"> - 12 - </td> - </tr> - <tr> - <td align="right"> - 123 - </td> - <td align="left"> - 123 - </td> - <td align="center"> - 123 - </td> - <td align="left"> - 123 - </td> - </tr> - <tr> - <td align="right"> - 1 - </td> - <td align="left"> - 1 - </td> - <td align="center"> - 1 - </td> - <td align="left"> - 1 - </td> - </tr> + <thead> + <tr> + <th align="right"> + Right + </th> + <th align="left"> + Left + </th> + <th align="center"> + Center + </th> + <th align="left"> + Default + </th> + </tr> + </thead> + <tbody> + <tr> + <td align="right"> + 12 + </td> + <td align="left"> + 12 + </td> + <td align="center"> + 12 + </td> + <td align="left"> + 12 + </td> + </tr> + <tr> + <td align="right"> + 123 + </td> + <td align="left"> + 123 + </td> + <td align="center"> + 123 + </td> + <td align="left"> + 123 + </td> + </tr> + <tr> + <td align="right"> + 1 + </td> + <td align="left"> + 1 + </td> + <td align="center"> + 1 + </td> + <td align="left"> + 1 + </td> + </tr> + </tbody> </table> <para> Simple table without caption: </para> <informaltable> - <tr> - <th align="right"> - Right - </th> - <th align="left"> - Left - </th> - <th align="center"> - Center - </th> - <th align="left"> - Default - </th> - </tr> - <tr> - <td align="right"> - 12 - </td> - <td align="left"> - 12 - </td> - <td align="center"> - 12 - </td> - <td align="left"> - 12 - </td> - </tr> - <tr> - <td align="right"> - 123 - </td> - <td align="left"> - 123 - </td> - <td align="center"> - 123 - </td> - <td align="left"> - 123 - </td> - </tr> - <tr> - <td align="right"> - 1 - </td> - <td align="left"> - 1 - </td> - <td align="center"> - 1 - </td> - <td align="left"> - 1 - </td> - </tr> + <thead> + <tr> + <th align="right"> + Right + </th> + <th align="left"> + Left + </th> + <th align="center"> + Center + </th> + <th align="left"> + Default + </th> + </tr> + </thead> + <tbody> + <tr> + <td align="right"> + 12 + </td> + <td align="left"> + 12 + </td> + <td align="center"> + 12 + </td> + <td align="left"> + 12 + </td> + </tr> + <tr> + <td align="right"> + 123 + </td> + <td align="left"> + 123 + </td> + <td align="center"> + 123 + </td> + <td align="left"> + 123 + </td> + </tr> + <tr> + <td align="right"> + 1 + </td> + <td align="left"> + 1 + </td> + <td align="center"> + 1 + </td> + <td align="left"> + 1 + </td> + </tr> + </tbody> </informaltable> <para> Simple table indented two spaces: @@ -130,62 +138,66 @@ <caption> Demonstration of simple table syntax. </caption> - <tr> - <th align="right"> - Right - </th> - <th align="left"> - Left - </th> - <th align="center"> - Center - </th> - <th align="left"> - Default - </th> - </tr> - <tr> - <td align="right"> - 12 - </td> - <td align="left"> - 12 - </td> - <td align="center"> - 12 - </td> - <td align="left"> - 12 - </td> - </tr> - <tr> - <td align="right"> - 123 - </td> - <td align="left"> - 123 - </td> - <td align="center"> - 123 - </td> - <td align="left"> - 123 - </td> - </tr> - <tr> - <td align="right"> - 1 - </td> - <td align="left"> - 1 - </td> - <td align="center"> - 1 - </td> - <td align="left"> - 1 - </td> - </tr> + <thead> + <tr> + <th align="right"> + Right + </th> + <th align="left"> + Left + </th> + <th align="center"> + Center + </th> + <th align="left"> + Default + </th> + </tr> + </thead> + <tbody> + <tr> + <td align="right"> + 12 + </td> + <td align="left"> + 12 + </td> + <td align="center"> + 12 + </td> + <td align="left"> + 12 + </td> + </tr> + <tr> + <td align="right"> + 123 + </td> + <td align="left"> + 123 + </td> + <td align="center"> + 123 + </td> + <td align="left"> + 123 + </td> + </tr> + <tr> + <td align="right"> + 1 + </td> + <td align="left"> + 1 + </td> + <td align="center"> + 1 + </td> + <td align="left"> + 1 + </td> + </tr> + </tbody> </table> <para> Multiline table with caption: @@ -194,140 +206,197 @@ <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> - <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> - <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> + <col width="15%" /> + <col width="13%" /> + <col width="16%" /> + <col width="33%" /> + <thead> + <tr> + <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> + <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> + <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> <para> Multiline table without caption: </para> <informaltable> - <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> - <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> - <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> + <col width="15%" /> + <col width="13%" /> + <col width="16%" /> + <col width="33%" /> + <thead> + <tr> + <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> + <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> + <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> </informaltable> <para> Table without column headers: </para> <informaltable> - <tr> - <td align="right"> - 12 - </td> - <td align="left"> - 12 - </td> - <td align="center"> - 12 - </td> - <td align="right"> - 12 - </td> - </tr> - <tr> - <td align="right"> - 123 - </td> - <td align="left"> - 123 - </td> - <td align="center"> - 123 - </td> - <td align="right"> - 123 - </td> - </tr> - <tr> - <td align="right"> - 1 - </td> - <td align="left"> - 1 - </td> - <td align="center"> - 1 - </td> - <td align="right"> - 1 - </td> - </tr> + <tbody> + <tr> + <td align="right"> + 12 + </td> + <td align="left"> + 12 + </td> + <td align="center"> + 12 + </td> + <td align="right"> + 12 + </td> + </tr> + <tr> + <td align="right"> + 123 + </td> + <td align="left"> + 123 + </td> + <td align="center"> + 123 + </td> + <td align="right"> + 123 + </td> + </tr> + <tr> + <td align="right"> + 1 + </td> + <td align="left"> + 1 + </td> + <td align="center"> + 1 + </td> + <td align="right"> + 1 + </td> + </tr> + </tbody> +</informaltable> +<para> + Multiline table without column headers: +</para> +<informaltable> + <col width="15%" /> + <col width="13%" /> + <col width="16%" /> + <col width="33%" /> + <tbody> + <tr> + <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> + <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> </informaltable> |