| 1 |
# ------------------------------------------------------------------- |
| 2 |
# - NAME: prepareStaticContent.R |
| 3 |
# - AUTHOR: Reto Stauffer |
| 4 |
# - DATE: 2018-09-15 |
| 5 |
# ------------------------------------------------------------------- |
| 6 |
# - DESCRIPTION: |
| 7 |
# ------------------------------------------------------------------- |
| 8 |
# - EDITORIAL: 2016-10-23, RS: Created file on pc24-c707. |
| 9 |
# ------------------------------------------------------------------- |
| 10 |
# - L@ST MODIFIED: 2018-09-22 14:19 on marvin |
| 11 |
# ------------------------------------------------------------------- |
| 12 |
|
| 13 |
library("colorspace") |
| 14 |
|
| 15 |
# ------------------------------------------------------------------- |
| 16 |
# Snippet from: |
| 17 |
# http://yihui.name/en/2012/10/build-static-html-help/ |
| 18 |
# Create static html from Rd files. |
| 19 |
# ------------------------------------------------------------------- |
| 20 |
static_help = function(pkg, methods, links = tools::findHTMLlinks()) { |
| 21 |
pkgRdDB = tools:::fetchRdDB(file.path(find.package(pkg), 'help', pkg)) |
| 22 |
force(links); topics = names(pkgRdDB) |
| 23 |
for (p in topics) { |
| 24 |
if ( ! is.null(methods) ) { |
| 25 |
if ( ! p %in% methods ) next |
| 26 |
} |
| 27 |
message(sprintf("Create static html help page for %s::%s",pkg,p)) |
| 28 |
content = pkgRdDB[[p]] |
| 29 |
tools::Rd2HTML(pkgRdDB[[p]], sprintf("tmp_%s.html",p), |
| 30 |
package = pkg, Links = links, no_links = is.null(links)) |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
|
| 35 |
cat(" * Create static help pages using Rd2HTML\n") |
| 36 |
static_help("colorspace","cvd_emulator") |
| 37 |
static_help("colorspace","cvd_image") |
| 38 |
|
| 39 |
|
| 40 |
# ------------------------------------------------------------------- |
| 41 |
# Parsing html content and create new static help page for |
| 42 |
# the hclwizard. |
| 43 |
# ------------------------------------------------------------------- |
| 44 |
# Reading help page content |
| 45 |
getHTMLcontent <- function(file) { |
| 46 |
# Helper function to extract the different sections |
| 47 |
extract <- function(html,sec) { |
| 48 |
r <- regmatches(html,regexpr(sprintf("<h3>%s</h3>.*?(?=(<h3>|<hr\\s?/>|$))",sec),html,perl=TRUE)) |
| 49 |
r <- gsub("#LB#","\n",r) |
| 50 |
r |
| 51 |
} |
| 52 |
title <- function(html) { |
| 53 |
r <- regmatches(html,regexpr("<h2>.*</h2>",html)) |
| 54 |
r <- gsub("#LB#","\n",r) |
| 55 |
r |
| 56 |
} |
| 57 |
if ( ! file.exists(file) ) stop(sprintf("Cannot find %s",file)) |
| 58 |
# Read content ... |
| 59 |
html <- gsub("\\n","",paste(readLines(file),collapse="#LB#")) |
| 60 |
# Remove hyperrefs |
| 61 |
html <- gsub("</a>","",html,perl=TRUE) |
| 62 |
html <- gsub("<a href=.*?(?=>)>","",html,perl=TRUE) |
| 63 |
html <- gsub("Author\\Ws\\W","Authors",html) |
| 64 |
# ... and parse content |
| 65 |
res <- list() |
| 66 |
res[['Title']] <- title(html) |
| 67 |
for ( sec in c("Description","Usage","Arguments","Details", |
| 68 |
"Value","Authors","References","See Also") ) { |
| 69 |
res[[sec]] <- extract(html,sec) |
| 70 |
} |
| 71 |
res |
| 72 |
} |
| 73 |
|
| 74 |
# Reading content |
| 75 |
cat(" * Reading html content\n") |
| 76 |
content1 <- getHTMLcontent("tmp_cvd_emulator.html") |
| 77 |
content2 <- getHTMLcontent("tmp_cvd_image.html") |
| 78 |
|
| 79 |
# Create output file |
| 80 |
outfile <- "html/info.html" |
| 81 |
if ( file.exists("html/info.html") ) file.remove(outfile) |
| 82 |
cat(" Add authors on top\n") |
| 83 |
write(content1[["Authors"]], file=outfile, append=TRUE) |
| 84 |
|
| 85 |
# Create new info page content |
| 86 |
for ( sec in c("Title","Description","Details","Value") ) { |
| 87 |
cat(sprintf(" Appending %s\n",sec)) |
| 88 |
write(content1[[sec]], file=outfile, append=TRUE) |
| 89 |
} |
| 90 |
for ( sec in c("Title","Description","Details","Value") ) { |
| 91 |
cat(sprintf(" Appending %s\n",sec)) |
| 92 |
write(content2[[sec]], file=outfile, append=TRUE) |
| 93 |
} |
| 94 |
write(content1[["References"]], file=outfile, append=TRUE) |
| 95 |
|
| 96 |
# Remove temporary rendered html pages |
| 97 |
cat(" * Remove temporary files\n") |
| 98 |
file.remove("tmp_cvd_emulator.html") |
| 99 |
file.remove("tmp_cvd_image.html") |
| 100 |
|
| 101 |
|
| 102 |
|