Skip to content

Commit

Permalink
support table captions via 'Table: ' or ': ' after the table
Browse files Browse the repository at this point in the history
  • Loading branch information
yihui committed May 7, 2024
1 parent 52fcc3b commit 2f1a880
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 4 deletions.
5 changes: 5 additions & 0 deletions R/mark.R
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,11 @@ mark = function(input, output = NULL, text = NULL, options = NULL, meta = list()
}, 'html', function(z2) gsub(id4, ' ', restore_html(z2)))
# some code blocks with "attributes" are verbatim ones
ret = match_replace(ret, '```+\\{.+}', function(x) gsub(id4, ' ', x, fixed = TRUE))
# table caption: a paragraph that starts with 'Table: ' or ': ' after </table>
ret = gsub(
'(<table>)(?s)(.+?</table>)\n<p>(Table)?: (?s)(.+?)</p>',
'\\1\n<caption>\\4</caption>\\2', ret, perl = TRUE
)
# auto identifiers
if (isTRUE(options[['auto_identifiers']])) ret = auto_identifier(ret)
# number sections
Expand Down
13 changes: 11 additions & 2 deletions inst/examples/render-options.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,24 @@ a^{2}+b^{2} & = c^{2}\\\\
mark(mkd)
mark(mkd, options = "-latex_math")

# tables example (need 4 spaces at beginning of line here)
# table example
mark("
First Header | Second Header
------------- | -------------
Content Cell | Content Cell
Content Cell | Content Cell
")

# but not here
# caption
mark("
| a | b |
|---|--:|
| A | 9 |
Table: A table _caption_.
")

# no table
mark("
First Header | Second Header
------------- | -------------
Expand Down
13 changes: 11 additions & 2 deletions man/markdown_options.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions tests/tests.Rout.save
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,23 @@ a^{2}+b^{2} &amp; = c^{2}\
</tbody>
</table>

> mark("\n| a | b |\n|---|--:|\n| A | 9 |\n\nTable: A table _caption_.\n")
<table>
<caption>A table <em>caption</em>.</caption>
<thead>
<tr>
<th>a</th>
<th align="right">b</th>
</tr>
</thead>
<tbody>
<tr>
<td>A</td>
<td align="right">9</td>
</tr>
</tbody>
</table>

> mark("\nFirst Header | Second Header\n------------- | -------------\nContent Cell | Content Cell\nContent Cell | Content Cell\n",
+ options .... [TRUNCATED]
<p>First Header | Second Header
Expand Down

0 comments on commit 2f1a880

Please sign in to comment.