git.iko.soy/typeable/pandoc
on master help sign in
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.

commit27599602042a43bfe1506c008dab862352bba0b1 authorJohn MacFarlane <jgm@berkeley.edu> dateSat 11 Feb 2017, 22:36 (10 years ago) parent510a6ee
4 files changed+43−45
src/Text/Pandoc/Readers/RST.hs+1−1 src/Text/Pandoc/Writers/RST.hs+5−4 test/tables-rstsubset.native+3−6 test/tables.rst+34−34
src/Text/Pandoc/Readers/RST.hs+1−1
@@ -709,7 +709,7 @@ tableDirective top _fields body = do
709709 -- align is not applicable since we can't represent whole table align
710710 return $ B.singleton $ Table (B.toList title)
711711 aligns' widths' header' rows'
712− bs -> return mempty
712+ _ -> return mempty
713713
714714-- TODO:
715715-- - Only supports :format: fields with a single format for :raw: roles,
src/Text/Pandoc/Writers/RST.hs+5−4
@@ -266,9 +266,6 @@ blockToRST (BlockQuote blocks) = do
266266 return $ (nest tabstop contents) <> blankline
267267blockToRST (Table caption _ widths headers rows) = do
268268 caption' <- inlineListToRST caption
269− let caption'' = if null caption
270− then empty
271− else blankline <> text "Table: " <> caption'
272269 headers' <- mapM blockListToRST headers
273270 rawRows <- mapM (mapM blockListToRST) rows
274271 -- let isSimpleCell [Plain _] = True
@@ -299,7 +296,11 @@ blockToRST (Table caption _ widths headers rows) = do
299296 let head'' = if all null headers
300297 then empty
301298 else head' $$ border '='
302− return $ border '-' $$ head'' $$ body $$ border '-' $$ caption'' $$ blankline
299+ let tbl = border '-' $$ head'' $$ body $$ border '-'
300+ return $ if null caption
301+ then tbl $$ blankline
302+ else (".. table:: " <> caption') $$ blankline $$ nest 3 tbl $$
303+ blankline
303304blockToRST (BulletList items) = do
304305 contents <- mapM bulletListItemToRST items
305306 -- ensure that sublists have preceding blank line
test/tables-rstsubset.native+3−6
@@ -1,5 +1,5 @@
11[Para [Str "Simple",Space,Str "table",Space,Str "with",Space,Str "caption:"]
2−,Table [] [AlignDefault,AlignDefault,AlignDefault,AlignDefault] [0.125,0.1125,0.1375,0.15]
2+,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]
33 [[Plain [Str "Right"]]
44 ,[Plain [Str "Left"]]
55 ,[Plain [Str "Center"]]
@@ -16,7 +16,6 @@
1616 ,[Plain [Str "1"]]
1717 ,[Plain [Str "1"]]
1818 ,[Plain [Str "1"]]]]
19−,Para [Str "Table:",Space,Str "Demonstration",Space,Str "of",Space,Str "simple",Space,Str "table",Space,Str "syntax."]
2019,Para [Str "Simple",Space,Str "table",Space,Str "without",Space,Str "caption:"]
2120,Table [] [AlignDefault,AlignDefault,AlignDefault,AlignDefault] [0.125,0.1125,0.1375,0.15]
2221 [[Plain [Str "Right"]]
@@ -36,7 +35,7 @@
3635 ,[Plain [Str "1"]]
3736 ,[Plain [Str "1"]]]]
3837,Para [Str "Simple",Space,Str "table",Space,Str "indented",Space,Str "two",Space,Str "spaces:"]
39−,Table [] [AlignDefault,AlignDefault,AlignDefault,AlignDefault] [0.125,0.1125,0.1375,0.15]
38+,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]
4039 [[Plain [Str "Right"]]
4140 ,[Plain [Str "Left"]]
4241 ,[Plain [Str "Center"]]
@@ -53,9 +52,8 @@
5352 ,[Plain [Str "1"]]
5453 ,[Plain [Str "1"]]
5554 ,[Plain [Str "1"]]]]
56−,Para [Str "Table:",Space,Str "Demonstration",Space,Str "of",Space,Str "simple",Space,Str "table",Space,Str "syntax."]
5755,Para [Str "Multiline",Space,Str "table",Space,Str "with",Space,Str "caption:"]
58−,Table [] [AlignDefault,AlignDefault,AlignDefault,AlignDefault] [0.175,0.1625,0.1875,0.3625]
56+,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]
5957 [[Plain [Str "Centered",Space,Str "Header"]]
6058 ,[Plain [Str "Left",Space,Str "Aligned"]]
6159 ,[Plain [Str "Right",Space,Str "Aligned"]]
@@ -68,7 +66,6 @@
6866 ,[Plain [Str "row"]]
6967 ,[Plain [Str "5.0"]]
7068 ,[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."]]]]
71−,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."]
7269,Para [Str "Multiline",Space,Str "table",Space,Str "without",Space,Str "caption:"]
7370,Table [] [AlignDefault,AlignDefault,AlignDefault,AlignDefault] [0.175,0.1625,0.1875,0.3625]
7471 [[Plain [Str "Centered",Space,Str "Header"]]
test/tables.rst+34−34
@@ -1,16 +1,16 @@
11Simple table with caption:
22
3−+---------+--------+----------+-----------+
4−| Right | Left | Center | Default |
5−+=========+========+==========+===========+
6−| 12 | 12 | 12 | 12 |
7−+---------+--------+----------+-----------+
8−| 123 | 123 | 123 | 123 |
9−+---------+--------+----------+-----------+
10−| 1 | 1 | 1 | 1 |
11−+---------+--------+----------+-----------+
12−
13−Table: Demonstration of simple table syntax.
3+.. table:: Demonstration of simple table syntax.
4+
5+ +---------+--------+----------+-----------+
6+ | Right | Left | Center | Default |
7+ +=========+========+==========+===========+
8+ | 12 | 12 | 12 | 12 |
9+ +---------+--------+----------+-----------+
10+ | 123 | 123 | 123 | 123 |
11+ +---------+--------+----------+-----------+
12+ | 1 | 1 | 1 | 1 |
13+ +---------+--------+----------+-----------+
1414
1515Simple table without caption:
1616
@@ -26,33 +26,33 @@ Simple table without caption:
2626
2727Simple table indented two spaces:
2828
29−+---------+--------+----------+-----------+
30−| Right | Left | Center | Default |
31−+=========+========+==========+===========+
32−| 12 | 12 | 12 | 12 |
33−+---------+--------+----------+-----------+
34−| 123 | 123 | 123 | 123 |
35−+---------+--------+----------+-----------+
36−| 1 | 1 | 1 | 1 |
37−+---------+--------+----------+-----------+
29+.. table:: Demonstration of simple table syntax.
3830
39−Table: Demonstration of simple table syntax.
31+ +---------+--------+----------+-----------+
32+ | Right | Left | Center | Default |
33+ +=========+========+==========+===========+
34+ | 12 | 12 | 12 | 12 |
35+ +---------+--------+----------+-----------+
36+ | 123 | 123 | 123 | 123 |
37+ +---------+--------+----------+-----------+
38+ | 1 | 1 | 1 | 1 |
39+ +---------+--------+----------+-----------+
4040
4141Multiline table with caption:
4242
43−+-------------+------------+--------------+----------------------------+
44−| Centered | Left | Right | Default aligned |
45−| Header | Aligned | Aligned | |
46−+=============+============+==============+============================+
47−| First | row | 12.0 | Example of a row that |
48−| | | | spans multiple lines. |
49−+-------------+------------+--------------+----------------------------+
50−| Second | row | 5.0 | Here’s another one. Note |
51−| | | | the blank line between |
52−| | | | rows. |
53−+-------------+------------+--------------+----------------------------+
54−
55−Table: Here’s the caption. It may span multiple lines.
43+.. table:: Here’s the caption. It may span multiple lines.
44+
45+ +-------------+------------+--------------+----------------------------+
46+ | Centered | Left | Right | Default aligned |
47+ | Header | Aligned | Aligned | |
48+ +=============+============+==============+============================+
49+ | First | row | 12.0 | Example of a row that |
50+ | | | | spans multiple lines. |
51+ +-------------+------------+--------------+----------------------------+
52+ | Second | row | 5.0 | Here’s another one. Note |
53+ | | | | the blank line between |
54+ | | | | rows. |
55+ +-------------+------------+--------------+----------------------------+
5656
5757Multiline table without caption:
5858