Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for number_offset metadata with number-sections.lua #1999

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ rmarkdown 2.7

- Automatically exclude renv directory for `render_site()` (thanks, @jmbuhr, #1996)

- Number offset for section headers can be set via the `number_offset` top-level option of the YAML front matter. This feature supports variety of formats including those without native `--number-sections` option (e.g., `md_document`) (thanks, @atusy, #1999).

rmarkdown 2.6
================================================================================

Expand Down
7 changes: 7 additions & 0 deletions R/render.R
Original file line number Diff line number Diff line change
Expand Up @@ -875,6 +875,13 @@ render <- function(input,

pandoc_args <- output_format$pandoc$args

# let number_offset in metadata be an alternative of --number-offset option
if (("--number-sections" %in% pandoc_args) && !is.null(env$metadata$number_offset)) {
pandoc_args <- c(
pandoc_args, paste0("--number-offset=", env$metadata$number_offset)
)
}

# if Lua filters are provided, add the command line switch
if (!is.null(lua_filters <- output_format$pandoc$lua_filters)) {
lua_filters <- pandoc_lua_filter_args(lua_filters)
Expand Down
17 changes: 17 additions & 0 deletions inst/rmarkdown/lua/number-sections.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,18 @@ if FORMAT == "docx" then -- to be consistent with Pandoc >= 2.10.1
separator = pandoc.Str("\t")
end

function Meta(meta)
local offset = meta.number_offset
if offset then
offset = (type(offset) == "table") and (pandoc.utils.stringify(offset)) or offset
local i = 0
for int in string.gmatch(offset, "[0-9]+") do
i = i + 1
section_number_table[i] = tonumber(int)
end
end
end

function Header(elem)
-- If unnumbered
if (elem.classes:find("unnumbered")) then
Expand Down Expand Up @@ -51,3 +63,8 @@ function Header(elem)

return elem
end

return {
{Meta = Meta},
{Header = Header}
}
30 changes: 22 additions & 8 deletions tests/testthat/test-lua-filters.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,28 @@ test_that("pagebreak Lua filters works", {
})

test_that("number_sections Lua filter works", {
numbers <- c("1", "1.1", "2", "2.1")
headers <- c("#", "##", "#", "##")
rmd <- paste0(headers, " ", numbers, "\n\n")
result <- .generate_md_and_convert(rmd, md_document(number_sections = TRUE))
expected <- paste(numbers, numbers)
# pandoc 2.11.2 default to atx headers
if (pandoc_available("2.11.2")) expected <- paste(headers, expected)
expect_identical(result[result %in% expected], expected)
test_number_sections <- function(number_offset = 0L) {
numbers <- paste0(
c(1L, 1L, 2L, 2L) + number_offset,
c("", ".1", "", ".1")
)
headers <- c("#", "##", "#", "##")
result <- .generate_md_and_convert(
paste0(headers, " ", numbers, "\n\n"),
md_document(
number_sections = TRUE,
pandoc_args = if (number_offset > 0) {
sprintf("--metadata=number_offset:%s", number_offset)
}
)
)
expected <- paste(numbers, numbers)
# pandoc 2.11.2 default to atx headers
if (pandoc_available("2.11.2")) expected <- paste(headers, expected)
expect_identical(result[result %in% expected], expected)
}
test_number_sections(1L)
test_number_sections(2L)
})

test_that("formats have the expected Lua filter", {
Expand Down