Skip to content

Commit

Permalink
support the chunk option collapse = TRUE (per request #40)
Browse files Browse the repository at this point in the history
  • Loading branch information
yihui committed Nov 15, 2024
1 parent b7a2f0d commit 1ce8eaf
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: litedown
Type: Package
Title: A Lightweight Version of R Markdown
Version: 0.4.2
Version: 0.4.3
Authors@R: c(
person("Yihui", "Xie", role = c("aut", "cre"), email = "[email protected]", comment = c(ORCID = "0000-0003-0645-5666")),
person()
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

- Empty table headers are removed in HTML output (they may be generated from data frames or matrices without column names).

- Added support for the chunk option `collapse = TRUE` (thanks, @J-Moravec, #40).

- `pkg_manual()` will point out the name of the problematic Rd file when the Rd file fails to convert to HTML (thanks, @BSchamberger).

- Dropped **knitr** and **rmarkdown** from the `Suggests` field in `DESCRIPTION`. Previously, **litedown** allowed `rmarkdown::render()` to use the output formats `litedown::html_format` and `litedown::latex_format`. Now `rmarkdown::render()` is no longer supported, and `litedown::fuse()` must be used instead.
Expand Down
29 changes: 28 additions & 1 deletion R/fuse.R
Original file line number Diff line number Diff line change
Expand Up @@ -764,6 +764,24 @@ fuse_code = function(x, blocks) {
} else fenced_block(x, a, fence)
}
})

# collapse a code block without attributes into previous adjacent code block
if (isTRUE(opts$collapse) && (n <- length(res)) > 1) {
i1 = 1; k = NULL # indices of elements to be removed from `out`
for (i in 2:n) {
if (i - i1 > 1) i1 = i - 1 # make sure blocks are adjacent
o1 = out[[i1]]; n1 = length(o1); e1 = o1[n1]
# previous block should have a closing fence ```+
if (n1 < 3 || !grepl('^```+$', e1)) next
o2 = out[[i]]
if (continue_block(o1[2], e1, head(o2, 2))) {
out[[i]] = c(o1[-n1], o2[-(1:2)])
k = c(k, i1) # merge previous block into current and remove previous
}
}
if (length(k)) out = out[-k]
}

a = opts$attr.chunk
if (length(x$fences) == 2) {
# add a class name to the chunk output so we can style it differently
Expand Down Expand Up @@ -798,6 +816,15 @@ lineno_attr = function(lang = NA, start = 1, auto = TRUE) c(
'.line-numbers', if (auto) '.auto-numbers', sprintf('data-start="%d"', start)
)

# two blocks are continuous if first 2 elements of next block are '' and
# previous block's closing or opening fence (after removing data-start attribute)
continue_block = function(e1_open, e1_end, e2) {
if (length(e2) != 2 || e2[1] != '') return(FALSE)
if ((e2_open <- e2[2]) == e1_end) return(TRUE)
e3 = sub(' data-start="[0-9]+"', '', c(e1_open, e2_open))
e3[1] == e3[2]
}

new_source = function(x) xfun::new_record(x, 'source')
new_warning = function(x) xfun::new_record(x, 'warning')
new_plot = function(x) xfun::new_record(x, 'plot')
Expand Down Expand Up @@ -917,7 +944,7 @@ reactor = new_opts()
reactor(
eval = TRUE, echo = TRUE, results = 'markup', comment = '#> ',
warning = TRUE, message = TRUE, error = NA, include = TRUE,
strip.white = TRUE, order = 0,
strip.white = TRUE, collapse = FALSE, order = 0,
attr.source = NULL, attr.output = NULL, attr.plot = NULL, attr.chunk = NULL,
attr.message = '.plain .message', attr.warning = '.plain .warning', attr.error = '.plain .error',
cache = FALSE, cache.path = NULL,
Expand Down

0 comments on commit 1ce8eaf

Please sign in to comment.