Skip to content

Commit

Permalink
close #36: make before_chapter_script and after_chapter_script work f…
Browse files Browse the repository at this point in the history
…or new_session = TRUE
  • Loading branch information
yihui committed Mar 4, 2016
1 parent fae1a1f commit cfde0e8
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
20 changes: 19 additions & 1 deletion R/render.R
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,26 @@ render_new_session = function(files, main, force_, output_format, clean, envir,
rerun = if (force_) TRUE else {
!utils::file_test('-ot', files, files_md) # Rmd not older than md
}
add1 = insert_chapter_script(config, 'before')
add2 = insert_chapter_script(config, 'after')
# compile chapters in separate R sessions
for (f in files[rerun]) Rscript_render(f, render_args, render_meta)
for (f in files[rerun]) {
if (length(add1) && length(add2) == 0) {
Rscript_render(f, render_args, render_meta)
next
}
# first backup the original Rmd to a tempfile
f2 = tempfile('bookdown', '.')
file.copy(f, f2, overwrite = TRUE)
# write add1/add2 to the original Rmd, compile it, and restore it
tryCatch({
txt = c(add1, readUTF8(f), add2)
writeUTF8(txt, f)
Rscript_render(f, render_args, render_meta)
}, finally = {
if (file.copy(f2, f, overwrite = TRUE)) unlink(f2)
})
}
if (!all(dirname(files_md) == '.'))
file.copy(files_md[!rerun], basename(files_md[!rerun]), overwrite = TRUE)

Expand Down
2 changes: 1 addition & 1 deletion inst/examples/index.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ Merging all chapters into one Rmd file and knitting it is one way to render the

The default approach in **bookdown** is MK. To switch to KM, you either use the argument `new_session = TRUE` when calling `render_book()`, or set `new_session: yes` in the configuration file `_bookdown.yml`.

Note the configurations `before_chapter_script` and `after_chapter_script` are ignored by KM. You can still configure `book_filename`, but it should be a Markdown filename, e.g., `_main.md`. All other configurations work for both MK and KM.
You can still configure `book_filename`, but it should be a Markdown filename, e.g., `_main.md`, although the filename extension does not really matter, and you can even leave out the extension, e.g., just set `book_filename: _main`. All other configurations work for both MK and KM.

## Some tips

Expand Down

0 comments on commit cfde0e8

Please sign in to comment.