I use bookdown with the gitbook ouput to build a book. The .Rmd
files used to build this book are in turn auto-generated from other source files in the following workflow: An R-Script takes folder1/file.txt
and generates the file folder2/file.Rmd
, which is in turn used to build the book.
So far, so good. However, when I use the edit
option in _bookdown.yaml
the link (naturally) refers to folder2/file.Rmd
. It would be a simple thing to replace folder2
with folder1
and .Rmd
with .txt
, but I don't know where I should start.
In R/html.R (in the bookdown package), the link is generated within the function build_chapter
using rmd_cur
as input (which is "The Rmd filename of the current chapter") and the function source_link
.
source_link = function(target, type) {
if (length(target) == 0) return()
setting = source_link_setting(type = type)
if (is.null(setting)) return()
button_link(sprintf(setting$link, target), setting$text)
}
How can I use this information to generate my custom link that points to folder1/file.txt
instead of folder2/file.Rmd
?
If you want the history and view to also link to the text file
source_link = function(target, type) {
if (length(target) == 0) return()
setting = source_link_setting(type = type)
if (is.null(setting)) return()
button_link(sprintf(setting$link, sub("folder2/(.+?)\\.[Rr]md$", "folder1/\\1.txt", target)), setting$text)
}
otherwise overload the build_chapter
function:
build_chapter = function(
head, toc, chapter, link_prev, link_next, rmd_cur, html_cur, foot
) {
# add a has-sub class to the <li> items that has sub lists
toc = gsub('^(<li>)(.+<ul>)$', '<li class="has-sub">\\2', toc)
paste(c(
head,
'<div class="row">',
'<div class="col-sm-12">',
toc,
'</div>',
'</div>',
'<div class="row">',
'<div class="col-sm-12">',
chapter,
'<p style="text-align: center;">',
button_link(link_prev, 'Previous'),
source_link(sub("folder2/(.+?)\\.[Rr]md$", "folder1/\\1.txt", rmd_cur), type = 'edit'),
source_link(rmd_cur, type = 'history'),
source_link(rmd_cur, type = 'view'),
button_link(link_next, 'Next'),
'</p>',
'</div>',
'</div>',
foot
), collapse = '\n')
}