Skip to content

Commit

Permalink
'add .extensions and .pandoc_args arguments' (#26)
Browse files Browse the repository at this point in the history
* add .extensions and .pandoc_args arguments

* update vignettes

* update NEWS

* build docs and man

* update NEWS
  • Loading branch information
atusy authored Oct 22, 2020
1 parent 0a21e54 commit 2bf00d3
Show file tree
Hide file tree
Showing 14 changed files with 83 additions and 31 deletions.
3 changes: 2 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
# ftExtra 0.0.3.6
# ftExtra 0.0.3.7

* Support markdown footnote with `colformat_md`. Currently, one footnote per a cell is allowed, and it must be located at the end of the cell content (#22).
* Add `footnote_options()` to configure options for footnotes (#23).
* Support formatting markdown texts on header with `colformat_md(part = "header")` (#23).
* Support single- and double-quotes by a lua filter (dfc82e0).
* Support attributes with Span, Link, and Code. Useful attributes include the `.underline` class and the `color`, `shading.color`, and `font.family` attributes (#24).
* Support inline code (#25).
* Add the `.extensions` and `.pandoc_args` arguments to `colformat_md()` and `as_paragraph_md()` (#26).

# ftExtra 0.0.3

Expand Down
29 changes: 23 additions & 6 deletions R/as-paragraph-md.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,23 @@ image_size <- function(x, y = "width") {
}

parse_md <- function(x,
.from = "markdown",
auto_color_link = "blue",
.from = "markdown",
.pandoc_args = NULL,
.footnote_options = NULL) {
if (!is.character(auto_color_link) || length(auto_color_link) != 1L) {
stop("`auto_color_link` must be a string")
}

md_df <- md2df(x, .from = .from)
md_df <- md2df(
x,
.from = .from,
.pandoc_args = c(
"--lua-filter", system.file("lua/smart.lua", package = "ftExtra"),
"--lua-filter", system.file("lua/inline-code.lua", package = "ftExtra"),
.pandoc_args
)
)

if (is.null(.footnote_options) || (all(names(md_df) != "Note"))) {
y <- md_df
Expand Down Expand Up @@ -96,7 +105,12 @@ construct_chunk <- function(x, auto_color_link = "blue") {
#' @param auto_color_link A color of the link texts.
#' @param .from
#' Pandoc's `--from` argument (default: `'markdown+autolink_bare_uris'`).
#' @param .footnote_options Spec options for footnotes via `colformat_md`.
#' @param .extensions
#' Pandoc's extensions (see <https://www.pandoc.org/MANUAL.html#extensions>)
#' @param .pandoc_args
#' Extra arguments given to Pandoc
#' @param ...
#' Arguments passed to internal functions.
#'
#' @examples
#' if (rmarkdown::pandoc_available()) {
Expand All @@ -114,10 +128,13 @@ construct_chunk <- function(x, auto_color_link = "blue") {
as_paragraph_md <- function(x,
auto_color_link = "blue",
.from = "markdown+autolink_bare_uris",
.footnote_options = NULL) {
.extensions = NULL,
.pandoc_args = NULL,
...) {
structure(lapply(x, parse_md,
.from = .from,
.from = paste0(.from, paste(.extensions, collapse="")),
.pandoc_args = .pandoc_args,
auto_color_link = auto_color_link,
.footnote_options = .footnote_options),
...),
class = "paragraph")
}
5 changes: 4 additions & 1 deletion R/colformat.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ colformat_md <- function(x,
part = c("body", "header", "all"),
auto_color_link = "blue",
.footnote_options = footnote_options(),
.from = "markdown+autolink_bare_uris"
.from = "markdown+autolink_bare_uris",
.extensions = NULL,
.pandoc_args = NULL
) {
.j <- rlang::enexpr(j)
part <- match.arg(part)
Expand Down Expand Up @@ -56,6 +58,7 @@ colformat_md <- function(x,
unlist(dataset[col], use.names = FALSE),
auto_color_link = auto_color_link,
.from = .from,
.extensions = .extensions,
.footnote_options = .footnote_options
))

Expand Down
6 changes: 2 additions & 4 deletions R/md2ast.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#' @param x A character vector
#' @param .from Markdown format
#' @noRd
md2ast <- function(x, .from = "markdown") {
md2ast <- function(x, .from = "markdown", .pandoc_args = NULL) {
tf <- tempfile()
writeLines(x, tf)
jsonlite::fromJSON(
Expand All @@ -12,9 +12,7 @@ md2ast <- function(x, .from = "markdown") {
tf,
"--from", .from,
"--to json",
"--lua-filter", system.file("lua/smart.lua", package = "ftExtra"),
"--lua-filter", system.file("lua/inline-code.lua", package = "ftExtra"),
""
paste(.pandoc_args, collapse = " ")
),
intern = TRUE
),
Expand Down
4 changes: 2 additions & 2 deletions R/md2df.R
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,8 @@ ast2df <- function(x) {

#' Convert Pandoc's Markdown to data frame
#' @noRd
md2df <- function(x, .from) {
ast <- md2ast(x, .from = .from)
md2df <- function(x, .from = "markdown", .pandoc_args = NULL) {
ast <- md2ast(x, .from = .from, .pandoc_args = .pandoc_args)

if ((ast$blocks[[1]]$t != "Para") || (length(ast$blocks) > 1)) {
stop("Markdown text must be a single paragraph")
Expand Down
4 changes: 2 additions & 2 deletions docs/articles/format_columns.html

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

7 changes: 4 additions & 3 deletions docs/news/index.html

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

2 changes: 1 addition & 1 deletion docs/pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ articles:
format_columns: format_columns.html
group-rows: group-rows.html
transform-headers: transform-headers.html
last_built: 2020-10-22T12:58Z
last_built: 2020-10-22T13:37Z

16 changes: 13 additions & 3 deletions docs/reference/as_paragraph_md.html

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

12 changes: 11 additions & 1 deletion docs/reference/colformat_md.html

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

2 changes: 1 addition & 1 deletion docs/reference/footnote_options.html

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

10 changes: 8 additions & 2 deletions man/as_paragraph_md.Rd

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

8 changes: 7 additions & 1 deletion man/colformat_md.Rd

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

6 changes: 3 additions & 3 deletions vignettes/format_columns.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -146,11 +146,11 @@ data.frame(linebreak = c("a\nb"), stringsAsFactors = FALSE) %>%

## Emoji

Pandoc's markdown provides an extension, `'emoji'`.
To use it with `colformat_md()`, specify `markdown+emoji`.
Pandoc's markdown provides an extension, `emoji`.
To use it with `colformat_md()`, specify `.extensions="+emoji"`.

```{r}
data.frame(emoji = c(":+1:"), stringsAsFactors = FALSE) %>%
as_flextable() %>%
colformat_md(.from = "markdown+emoji")
colformat_md(.extensions = "+emoji")
```

0 comments on commit 2bf00d3

Please sign in to comment.