From 75484f20d7acbf89166dfd9215a4092318f35afd Mon Sep 17 00:00:00 2001 From: atusy <30277794+atusy@users.noreply.github.com> Date: Fri, 25 Dec 2020 21:19:34 +0900 Subject: [PATCH 1/7] number-sections.lua support number_offset metadata --- inst/rmarkdown/lua/number-sections.lua | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/inst/rmarkdown/lua/number-sections.lua b/inst/rmarkdown/lua/number-sections.lua index 50596b5a9e..12541c7672 100644 --- a/inst/rmarkdown/lua/number-sections.lua +++ b/inst/rmarkdown/lua/number-sections.lua @@ -21,6 +21,13 @@ if FORMAT == "docx" then -- to be consistent with Pandoc >= 2.10.1 separator = pandoc.Str("\t") end +function Meta(meta) + if meta.number_offset then + section_number_table[1] = tonumber( + pandoc.utils.stringify(meta.number_offset)) - 1 + end +end + function Header(elem) -- If unnumbered if (elem.classes:find("unnumbered")) then From 36974f3b58c5159ec26b3f1da22a8f26db8be8c7 Mon Sep 17 00:00:00 2001 From: atusy <30277794+atusy@users.noreply.github.com> Date: Fri, 25 Dec 2020 21:52:12 +0900 Subject: [PATCH 2/7] support --metadata option and update tests --- inst/rmarkdown/lua/number-sections.lua | 13 ++++++++--- tests/testthat/test-lua-filters.R | 30 +++++++++++++++++++------- 2 files changed, 32 insertions(+), 11 deletions(-) diff --git a/inst/rmarkdown/lua/number-sections.lua b/inst/rmarkdown/lua/number-sections.lua index 12541c7672..17690dbe76 100644 --- a/inst/rmarkdown/lua/number-sections.lua +++ b/inst/rmarkdown/lua/number-sections.lua @@ -22,9 +22,11 @@ if FORMAT == "docx" then -- to be consistent with Pandoc >= 2.10.1 end function Meta(meta) - if meta.number_offset then - section_number_table[1] = tonumber( - pandoc.utils.stringify(meta.number_offset)) - 1 + local offset = meta.number_offset + if offset then + section_number_table[1] = tonumber( + (type(offset) == "table") and (pandoc.utils.stringify(offset)) or offset + ) - 1 end end @@ -58,3 +60,8 @@ function Header(elem) return elem end + +return { + {Meta = Meta}, + {Header = Header} +} diff --git a/tests/testthat/test-lua-filters.R b/tests/testthat/test-lua-filters.R index ad45c652fb..af73507641 100644 --- a/tests/testthat/test-lua-filters.R +++ b/tests/testthat/test-lua-filters.R @@ -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 = 1L) { + numbers <- paste0( + c(0L, 0L, 1L, 1L) + 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 > 1) { + 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", { From 3513e02e771f5628111ed54b4848387be334b4bb Mon Sep 17 00:00:00 2001 From: atusy <30277794+atusy@users.noreply.github.com> Date: Fri, 25 Dec 2020 22:02:23 +0900 Subject: [PATCH 3/7] add NEWS.md --- NEWS.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/NEWS.md b/NEWS.md index 827ef6beda..083b4a0132 100644 --- a/NEWS.md +++ b/NEWS.md @@ -9,6 +9,8 @@ rmarkdown 2.7 - Automatically exclude renv directory for `render_site()` (thanks, @jmbuhr, #1996) +- Support `number_offset` feature to formats that supports the `number_sections` feature with a Lua filter. Note that `number_offset` needs be specified as a top-level option of the YAML front matter (thanks, @atusy, #1999). + rmarkdown 2.6 ================================================================================ From b009ac43aaaea63f5d949fd5264772326af3c059 Mon Sep 17 00:00:00 2001 From: atusy <30277794+atusy@users.noreply.github.com> Date: Fri, 25 Dec 2020 22:19:34 +0900 Subject: [PATCH 4/7] fix inconsistency with native --number-offset --- inst/rmarkdown/lua/number-sections.lua | 2 +- tests/testthat/test-lua-filters.R | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/inst/rmarkdown/lua/number-sections.lua b/inst/rmarkdown/lua/number-sections.lua index 17690dbe76..cffbc8d665 100644 --- a/inst/rmarkdown/lua/number-sections.lua +++ b/inst/rmarkdown/lua/number-sections.lua @@ -26,7 +26,7 @@ function Meta(meta) if offset then section_number_table[1] = tonumber( (type(offset) == "table") and (pandoc.utils.stringify(offset)) or offset - ) - 1 + ) end end diff --git a/tests/testthat/test-lua-filters.R b/tests/testthat/test-lua-filters.R index af73507641..8392518d61 100644 --- a/tests/testthat/test-lua-filters.R +++ b/tests/testthat/test-lua-filters.R @@ -27,9 +27,9 @@ test_that("pagebreak Lua filters works", { }) test_that("number_sections Lua filter works", { - test_number_sections <- function(number_offset = 1L) { + test_number_sections <- function(number_offset = 0L) { numbers <- paste0( - c(0L, 0L, 1L, 1L) + number_offset, + c(1L, 1L, 2L, 2L) + number_offset, c("", ".1", "", ".1") ) headers <- c("#", "##", "#", "##") @@ -37,7 +37,7 @@ test_that("number_sections Lua filter works", { paste0(headers, " ", numbers, "\n\n"), md_document( number_sections = TRUE, - pandoc_args = if (number_offset > 1) { + pandoc_args = if (number_offset > 0) { sprintf("--metadata=number_offset:%s", number_offset) } ) From cf82825d7971171a821d2c5bd526e4b19f5d1542 Mon Sep 17 00:00:00 2001 From: atusy <30277794+atusy@users.noreply.github.com> Date: Fri, 25 Dec 2020 22:35:07 +0900 Subject: [PATCH 5/7] formats with native --number-sections option also support number_offset in the YAML --- R/render.R | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/R/render.R b/R/render.R index e743d9bb0c..b854ee8307 100644 --- a/R/render.R +++ b/R/render.R @@ -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) From 533a6e36224a3a8f0e8679099257af6fa959219c Mon Sep 17 00:00:00 2001 From: atusy <30277794+atusy@users.noreply.github.com> Date: Fri, 25 Dec 2020 22:39:32 +0900 Subject: [PATCH 6/7] update NEWS --- NEWS.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/NEWS.md b/NEWS.md index 083b4a0132..2b4badc11c 100644 --- a/NEWS.md +++ b/NEWS.md @@ -9,7 +9,7 @@ rmarkdown 2.7 - Automatically exclude renv directory for `render_site()` (thanks, @jmbuhr, #1996) -- Support `number_offset` feature to formats that supports the `number_sections` feature with a Lua filter. Note that `number_offset` needs be specified as a top-level option of the YAML front matter (thanks, @atusy, #1999). +- 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 ================================================================================ From e3fd3f09a267efe73911141d921281347695e515 Mon Sep 17 00:00:00 2001 From: atusy <30277794+atusy@users.noreply.github.com> Date: Sat, 26 Dec 2020 00:11:19 +0900 Subject: [PATCH 7/7] support something like number_sections: "1,2" --- inst/rmarkdown/lua/number-sections.lua | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/inst/rmarkdown/lua/number-sections.lua b/inst/rmarkdown/lua/number-sections.lua index cffbc8d665..eb65382cf1 100644 --- a/inst/rmarkdown/lua/number-sections.lua +++ b/inst/rmarkdown/lua/number-sections.lua @@ -24,9 +24,12 @@ end function Meta(meta) local offset = meta.number_offset if offset then - section_number_table[1] = tonumber( - (type(offset) == "table") and (pandoc.utils.stringify(offset)) or offset - ) + 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