This R Package has just one special job to be done. If you are not involved with the booksprint project the package will not make sense to you.
The package converts the downloaded fidus-html-files from the book section and converts them into appropriate markdown files.
First you need to install devtools
install.packages("devtools")
then you need to load devtools
library(devtools)
- Go to Fidus writer on Handbuch.io and log in Handbuch.io
- Go to the book section Book section
- Check that the book is properly set up with the right chapters
- Check the name of each chapter. It should begin with 3 letters (indication the specific book) and then a number
- Tick the appropriate book
- Click on export to html
- Save the file in an empty directory. Easiest way is to save it as "handbuch.zip"
- Open that directory in R
- Paste this code into the R console
convert_fidus_to_github <- function(fiduszipfile = "handbuch.zip", tmpdir = "tmp/") {
# This script automates the the process of passing html files
# into the jekyll system. Its done for the handbook for infectious diseases
# Unzip the file
unzip(fiduszipfile, exdir = tmpdir)
# This command lists all html files after deleting index.html
htmlfiles <- list.files(tmpdir, pattern = "document.*\\.html$")
# loop through all html files
for (a in htmlfiles) {
# a <- htmlfiles[5]
# Get rid of the annoying error of a missing newline
write("\n", paste0(tmpdir,a), append = T)
# Getting the necessary information out of the html-file
number <- strsplit(strsplit(a, "\\.")[[1]][1], "-")[[1]][2]
newfilename <- paste0("chapter_", number, ".md")
# read in the document
document <- readLines(paste0(tmpdir,a))
# Get document title
title_line <- document[grep("<title", document)]
title <- strsplit(strsplit(title_line, ">")[[1]][2], "<")[[1]][1]
title <- sub("^.{0,3}", "", title)
# Get content out of document
document <- document[grep("<div class=\"article-part article-richtext article-body\">", document) ][1]
document <- strsplit(document, "<div class=\"article-part article-richtext article-body\">")[[1]][2]
write(document, paste0(tmpdir,"tmp.html"))
write("", paste0(tmpdir,"tmp.md"))
rmarkdown::pandoc_convert(input = paste0("tmp.html"), to = "gfm", output = paste0("tmp.md"), wd = "tmp/")
documentToAppend <- readLines(paste0(tmpdir,"tmp.md"))
# remove the old file if it exists
# if (file.exists(newfilename)) file.remove(newfilename)
# the new file is created line by line. This is where the jekyll front matter is included
write("---", newfilename)
write("layout: page", file = newfilename, append = T)
write(paste("title:", title), newfilename, append = T)
write(paste("nav_order:", number), newfilename, append = T)
write("---", newfilename, append = T)
# write(paste("{{page.title}}"), newfilename, append = T)
write(paste(" "), newfilename, append = T)
write(paste("<details markdown=\"block\"> "), newfilename, append = T)
write(paste(" <summary> "), newfilename, append = T)
write(paste(" ► Inhaltsverzeichnis Kapitel (ausklappbar) "), newfilename, append = T)
write(paste(" </summary>"), newfilename, append = T)
write(paste(" "), newfilename, append = T)
write(paste("1. TOC"), newfilename, append = T)
write(paste("{:toc}"), newfilename, append = T)
write(paste(" </details>"), newfilename, append = T)
write(paste(" "), newfilename, append = T)
write(paste(" <p></p>"), newfilename, append = T)
write(paste(" "), newfilename, append = T)
write(paste(" "), newfilename, append = T)
write(documentToAppend, newfilename, append = T)
}
# Copying all pictures to the docs folder
pics <- list.files(tmpdir, pattern = ".png$")
for (i in pics) {
file.copy(paste0(tmpdir, i), i, overwrite = TRUE)
}
unlink(tmpdir, recursive = T) # Delete tmp dir
}
- Start the function
convert_fidus_to_github(fiduszipfile = "handbuch.zip")`
Now you should have markdown and pictures in you folder, that can be uploaded to github
Probably easiest to write me an E-Mail.