diff --git a/DESCRIPTION b/DESCRIPTION index 325621513..580185551 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Package: bookdown Type: Package Title: Authoring Books and Technical Documents with R Markdown -Version: 0.42.1 +Version: 0.42.2 Authors@R: c( person("Yihui", "Xie", role = c("aut", "cre"), email = "xie@yihui.name", comment = c(ORCID = "0000-0003-0645-5666")), person("Christophe", "Dervieux", , "cderv@posit.co", role = c("ctb"), diff --git a/NEWS.md b/NEWS.md index 96c8abdde..cc999d593 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,7 @@ # CHANGES IN bookdown VERSION 0.43 +- Support `split_by` for section level higher than `2` (i.e., `##` sections) in `gitbook` (thanks, @katrinabrock #1490, @lcougnaud #1346 #1347). + # CHANGES IN bookdown VERSION 0.42 - New option in `gitbook`'s font settings menu to control line spacing (thanks, @hayden-MB, #1479). diff --git a/R/gitbook.R b/R/gitbook.R index aeb2f5250..5570ae071 100644 --- a/R/gitbook.R +++ b/R/gitbook.R @@ -25,7 +25,9 @@ gitbook = function( fig_caption = TRUE, number_sections = TRUE, self_contained = FALSE, anchor_sections = TRUE, lib_dir = 'libs', global_numbering = !number_sections, pandoc_args = NULL, extra_dependencies = list(), ..., template = 'default', - split_by = c('chapter', 'chapter+number', 'section', 'section+number', 'rmd', 'none'), + split_by = c('chapter', 'section', '0', '1', '2', '3', '4', '5', '6', 'chapter+number', + 'section+number', '0+number', '1+number', '2+number', '3+number', + '4+number', '5+number', '6+number', 'rmd', 'none') , split_bib = TRUE, config = list(), table_css = TRUE, code_folding = c("none", "show", "hide") ) { gb_config = config @@ -53,6 +55,7 @@ gitbook = function( template = template, pandoc_args = pandoc_args2(pandoc_args), ... ) config$pandoc$lua_filters = append(config$pandoc$lua_filters, lua_filters) + split_by = as.character(split_by) split_by = match.arg(split_by) post = config$post_processor # in case a post processor have been defined config$post_processor = function(metadata, input, output, clean, verbose) { diff --git a/R/html.R b/R/html.R index dad883f82..31b5ea173 100644 --- a/R/html.R +++ b/R/html.R @@ -16,15 +16,17 @@ #' @param split_by How to name the HTML output files from the book: \code{rmd} #' uses the base filenames of the input Rmd files to create the HTML #' filenames, e.g. generate \file{chapter1.html} for \file{chapter1.Rmd}; -#' \code{none} means do not split the HTML file (the book will be a single -#' HTML file); \code{chapter} means split the file by the first-level headers; -#' \code{section} means the second-level headers. For \code{chapter} and -#' \code{section}, the HTML filenames will be determined by the header ID's, -#' e.g. the filename for the first chapter with a chapter title \code{# -#' Introduction} will be \file{introduction.html}; for \code{chapter+number} -#' and \code{section+number}, the chapter/section numbers will be prepended to -#' the HTML filenames, e.g. \file{1-introduction.html} and -#' \file{2-1-literature.html}. +#' \code{none} or \code{"0"} means do not split the HTML file (the book will be +#' a single HTML file); \code{chapter} or \code{"1"} means split the file by +#' the first-level headers; \code{section} or \code{"2"} means the second-level +#' headers, \code{"3"}-\code{"6"} means split the file by the [3-6]-level +#' headers. For \code{chapter}, \code{section} and \code{"1"}-\code{"6"}, the +#' HTML filenames will be determined by the header ID's, e.g. the filename +#' for the first chapter with a chapter title \code{# Introduction} will be +#' \file{introduction.html}; for \code{"chapter+number"}, \code{"section+number"} +#' and \code{"[1-6]+number"} the chapter/section (and higher level section) +#' numbers will be prepended to the HTML filenames, e.g. +#' \file{1-introduction.html} and \file{2-1-literature.html}. #' @param split_bib Whether to split the bibliography onto separate pages where #' the citations are actually used. #' @param page_builder A function to combine different parts of a chapter into a @@ -54,13 +56,16 @@ html_chapters = function( template = bookdown_file('templates/default.html'), global_numbering = !number_sections, pandoc_args = NULL, ..., base_format = rmarkdown::html_document, split_bib = TRUE, page_builder = build_chapter, - split_by = c('section+number', 'section', 'chapter+number', 'chapter', 'rmd', 'none') + split_by = c('chapter', 'section', '0', '1', '2', '3', '4', '5', '6', 'chapter+number', + 'section+number', '0+number', '1+number', '2+number', '3+number', + '4+number', '5+number', '6+number', 'rmd', 'none') ) { config = get_base_format(base_format, list( toc = toc, number_sections = number_sections, fig_caption = fig_caption, self_contained = FALSE, lib_dir = lib_dir, template = template, pandoc_args = pandoc_args2(pandoc_args), ... )) + split_by = as.character(split_by) split_by = match.arg(split_by) post = config$post_processor # in case a post processor have been defined config$post_processor = function(metadata, input, output, clean, verbose) { @@ -257,12 +262,15 @@ split_chapters = function( ) { use_rmd_names = split_by == 'rmd' - split_level = switch( - split_by, none = 0, chapter = 1, `chapter+number` = 1, - section = 2, `section+number` = 2, rmd = 1 - ) - if (!(split_level %in% 0:2)) stop('split_level must be 0, 1, or 2') + split_level = sub('[+]number$', '', split_by) + split_level = switch(split_level, + none = 0, + chapter = 1, + section = 2, + rmd = 1, + as.numeric(split_level) + ) x = read_utf8(output) x = clean_html_tags(x) @@ -280,41 +288,75 @@ split_chapters = function( # restore_appendix_html erase the section ids of the hidden PART or APPENDIX # sections. if (split_level > 1) { + body = x[(i5 + 1):(i6 - 1)] - h1 = grep('^