From 27599602042a43bfe1506c008dab862352bba0b1 Mon Sep 17 00:00:00 2001 From: John MacFarlane Date: Sat, 11 Feb 2017 22:36:47 +0100 Subject: RST reader/writer: properly handle table captions. Currently the support for the `.. table` directive is a bit limited; we don't yet support the `widths` field. But at least you can have a proper captioned table. --- src/Text/Pandoc/Readers/RST.hs | 2 +- src/Text/Pandoc/Writers/RST.hs | 9 +++--- test/tables-rstsubset.native | 9 ++---- test/tables.rst | 68 +++++++++++++++++++++--------------------- 4 files changed, 43 insertions(+), 45 deletions(-) diff --git a/src/Text/Pandoc/Readers/RST.hs b/src/Text/Pandoc/Readers/RST.hs index bc0fc042c..024c56685 100644 --- a/src/Text/Pandoc/Readers/RST.hs +++ b/src/Text/Pandoc/Readers/RST.hs @@ -709,7 +709,7 @@ tableDirective top _fields body = do -- align is not applicable since we can't represent whole table align return $ B.singleton $ Table (B.toList title) aligns' widths' header' rows' - bs -> return mempty + _ -> return mempty -- TODO: -- - Only supports :format: fields with a single format for :raw: roles, diff --git a/src/Text/Pandoc/Writers/RST.hs b/src/Text/Pandoc/Writers/RST.hs index 6093c668b..5cce64d17 100644 --- a/src/Text/Pandoc/Writers/RST.hs +++ b/src/Text/Pandoc/Writers/RST.hs @@ -266,9 +266,6 @@ blockToRST (BlockQuote blocks) = do return $ (nest tabstop contents) <> blankline blockToRST (Table caption _ widths headers rows) = do caption' <- inlineListToRST caption - let caption'' = if null caption - then empty - else blankline <> text "Table: " <> caption' headers' <- mapM blockListToRST headers rawRows <- mapM (mapM blockListToRST) rows -- let isSimpleCell [Plain _] = True @@ -299,7 +296,11 @@ blockToRST (Table caption _ widths headers rows) = do let head'' = if all null headers then empty else head' $$ border '=' - return $ border '-' $$ head'' $$ body $$ border '-' $$ caption'' $$ blankline + let tbl = border '-' $$ head'' $$ body $$ border '-' + return $ if null caption + then tbl $$ blankline + else (".. table:: " <> caption') $$ blankline $$ nest 3 tbl $$ + blankline blockToRST (BulletList items) = do contents <- mapM bulletListItemToRST items -- ensure that sublists have preceding blank line diff --git a/test/tables-rstsubset.native b/test/tables-rstsubset.native index ecf6911dc..65ced24af 100644 --- a/test/tables-rstsubset.native +++ b/test/tables-rstsubset.native @@ -1,5 +1,5 @@ [Para [Str "Simple",Space,Str "table",Space,Str "with",Space,Str "caption:"] -,Table [] [AlignDefault,AlignDefault,AlignDefault,AlignDefault] [0.125,0.1125,0.1375,0.15] +,Table [Str "Demonstration",Space,Str "of",Space,Str "simple",Space,Str "table",Space,Str "syntax."] [AlignDefault,AlignDefault,AlignDefault,AlignDefault] [0.125,0.1125,0.1375,0.15] [[Plain [Str "Right"]] ,[Plain [Str "Left"]] ,[Plain [Str "Center"]] @@ -16,7 +16,6 @@ ,[Plain [Str "1"]] ,[Plain [Str "1"]] ,[Plain [Str "1"]]]] -,Para [Str "Table:",Space,Str "Demonstration",Space,Str "of",Space,Str "simple",Space,Str "table",Space,Str "syntax."] ,Para [Str "Simple",Space,Str "table",Space,Str "without",Space,Str "caption:"] ,Table [] [AlignDefault,AlignDefault,AlignDefault,AlignDefault] [0.125,0.1125,0.1375,0.15] [[Plain [Str "Right"]] @@ -36,7 +35,7 @@ ,[Plain [Str "1"]] ,[Plain [Str "1"]]]] ,Para [Str "Simple",Space,Str "table",Space,Str "indented",Space,Str "two",Space,Str "spaces:"] -,Table [] [AlignDefault,AlignDefault,AlignDefault,AlignDefault] [0.125,0.1125,0.1375,0.15] +,Table [Str "Demonstration",Space,Str "of",Space,Str "simple",Space,Str "table",Space,Str "syntax."] [AlignDefault,AlignDefault,AlignDefault,AlignDefault] [0.125,0.1125,0.1375,0.15] [[Plain [Str "Right"]] ,[Plain [Str "Left"]] ,[Plain [Str "Center"]] @@ -53,9 +52,8 @@ ,[Plain [Str "1"]] ,[Plain [Str "1"]] ,[Plain [Str "1"]]]] -,Para [Str "Table:",Space,Str "Demonstration",Space,Str "of",Space,Str "simple",Space,Str "table",Space,Str "syntax."] ,Para [Str "Multiline",Space,Str "table",Space,Str "with",Space,Str "caption:"] -,Table [] [AlignDefault,AlignDefault,AlignDefault,AlignDefault] [0.175,0.1625,0.1875,0.3625] +,Table [Str "Here\8217s",Space,Str "the",Space,Str "caption.",Space,Str "It",Space,Str "may",Space,Str "span",Space,Str "multiple",Space,Str "lines."] [AlignDefault,AlignDefault,AlignDefault,AlignDefault] [0.175,0.1625,0.1875,0.3625] [[Plain [Str "Centered",Space,Str "Header"]] ,[Plain [Str "Left",Space,Str "Aligned"]] ,[Plain [Str "Right",Space,Str "Aligned"]] @@ -68,7 +66,6 @@ ,[Plain [Str "row"]] ,[Plain [Str "5.0"]] ,[Plain [Str "Here\8217s",Space,Str "another",Space,Str "one.",Space,Str "Note",SoftBreak,Str "the",Space,Str "blank",Space,Str "line",Space,Str "between",SoftBreak,Str "rows."]]]] -,Para [Str "Table:",Space,Str "Here\8217s",Space,Str "the",Space,Str "caption.",Space,Str "It",Space,Str "may",Space,Str "span",Space,Str "multiple",Space,Str "lines."] ,Para [Str "Multiline",Space,Str "table",Space,Str "without",Space,Str "caption:"] ,Table [] [AlignDefault,AlignDefault,AlignDefault,AlignDefault] [0.175,0.1625,0.1875,0.3625] [[Plain [Str "Centered",Space,Str "Header"]] diff --git a/test/tables.rst b/test/tables.rst index fc7f0b475..184d9894a 100644 --- a/test/tables.rst +++ b/test/tables.rst @@ -1,16 +1,16 @@ Simple table with caption: -+---------+--------+----------+-----------+ -| Right | Left | Center | Default | -+=========+========+==========+===========+ -| 12 | 12 | 12 | 12 | -+---------+--------+----------+-----------+ -| 123 | 123 | 123 | 123 | -+---------+--------+----------+-----------+ -| 1 | 1 | 1 | 1 | -+---------+--------+----------+-----------+ - -Table: Demonstration of simple table syntax. +.. table:: 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: @@ -26,33 +26,33 @@ Simple table without caption: Simple table indented two spaces: -+---------+--------+----------+-----------+ -| Right | Left | Center | Default | -+=========+========+==========+===========+ -| 12 | 12 | 12 | 12 | -+---------+--------+----------+-----------+ -| 123 | 123 | 123 | 123 | -+---------+--------+----------+-----------+ -| 1 | 1 | 1 | 1 | -+---------+--------+----------+-----------+ +.. table:: Demonstration of simple table syntax. -Table: 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: -+-------------+------------+--------------+----------------------------+ -| Centered | Left | Right | Default aligned | -| Header | Aligned | 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: Here’s the caption. It may span multiple lines. +.. table:: Here’s the caption. It may span multiple lines. + + +-------------+------------+--------------+----------------------------+ + | Centered | Left | Right | Default aligned | + | Header | Aligned | 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: -- cgit v1.2.3