Skip to content

Commit

Permalink
Add compatibility with new Pandoc 2.x way of creating self-contained …
Browse files Browse the repository at this point in the history
…document (#784)

allow to move multiline script node in html

As investigated in #739, Pandoc 2 modified the way it handles js scripts in self contained document. They are not always base64 encoded by default, and are rather included in `<script>` tags as-is. This means that for gitbook post processing, multiline blocks must be identified and moved.
  • Loading branch information
cderv authored and yihui committed Oct 14, 2019
1 parent 2227cd6 commit 8d16e2a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# CHANGES IN bookdown VERSION 0.15

## BUG FIXES

- gitbook toolbar is not missing any more when rendering books with Pandoc 2.x and using `self_contained = TRUE` (thanks, @Pindar777, @RLesur, @cderv, #739).

# CHANGES IN bookdown VERSION 0.14

Expand Down
15 changes: 11 additions & 4 deletions R/gitbook.R
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,17 @@ gitbook_page = function(
# gitbook JS scripts only work after the DOM has been loaded, so move them
# from head to foot
i = grep('^\\s*<script src=".+/gitbook([^/]+)?/js/[.a-z-]+[.]js"></script>\\s*$', head)
# it is probably a self-contained page, so look for base64 encoded scripts
if (length(i) == 0) i = grep(
'^\\s*<script src="data:application/x-javascript;base64,[^"]+"></script>\\s*$', head
)
# it is probably a self-contained page, so look for script node.
# from pandoc2, they are not always base64 encoded scripts, so start and end of scripts
# node must be found and moved.
if (length(i) == 0) {
s_start = grep(
'^\\s*<script( src="data:application/(x-)?javascript;base64,[^"]+")?>', head
)
s_end = grep("</script>\\s*$", head)
# find all lines to move
i = unlist(mapply(seq.int, s_start, s_end, SIMPLIFY = FALSE))
}
s = head[i]; head[i] = ''
j = grep('<!--bookdown:config-->', foot)[1]
foot[j] = paste(c(s, foot[j]), collapse = '\n')
Expand Down

0 comments on commit 8d16e2a

Please sign in to comment.