Debian Codegraph @

inst/hclwizard/prepareStaticContent.R

raw
1 # -------------------------------------------------------------------
2 # - NAME: prepareStaticContent.R
3 # - AUTHOR: Reto Stauffer
4 # - DATE: 2016-10-23
5 # -------------------------------------------------------------------
6 # - DESCRIPTION:
7 # -------------------------------------------------------------------
8 # - EDITORIAL: 2016-10-23, RS: Created file on pc24-c707.
9 # -------------------------------------------------------------------
10 # - L@ST MODIFIED: 2018-09-22 14:16 on marvin
11 # -------------------------------------------------------------------
12
13 library("colorspace")
14
15
16 # -------------------------------------------------------------------
17 # Make bpy accessible
18 # -------------------------------------------------------------------
19
20 # Make hidden function available
21 bpy <- colorspace:::bpy
22
23 # -------------------------------------------------------------------
24 # Create color maps
25 # -------------------------------------------------------------------
26
27
28 # Remove existing palettes
29 imgdir <- "www/images"
30 files <- list.files(imgdir,"pal_.*.png")
31 if ( length(files) > 0 ) {
32 for ( f in files ) file.remove(sprintf("%s/%s",imgdir,f))
33 }
34
35 # Loading defined color palettes from colorspace package
36 palettes <- colorspace:::GetPaletteConfig(gui = TRUE)
37 names(palettes) <- tolower(names(palettes))
38 names(palettes)[names(palettes)=='typ'] <- "type"
39 # Required for do.call
40 GetPalette <- colorspace:::GetPalette
41
42 N <- 7 # Default number of colors for the palettes
43 idx <- which(! names(palettes) %in% c('name'))
44 for ( i in 1:nrow(palettes) ) {
45 args <- as.list(palettes[i, idx])
46 args$reverse <- FALSE
47 name <- gsub(" ","_", tolower(rownames(palettes)[i]))
48 img <- sprintf("%s/pal_%s.png", imgdir, name)
49 cat(sprintf(" * Drawing: %s\n", img))
50
51 if ( args$type == "base" ) {
52 pal <- eval(parse(text = tolower(rownames(palettes)[i])))
53 } else {
54 pal <- do.call("GetPalette", args)
55 }
56
57 # Draw color map
58 png(file = img, width = 300, height = 1)
59 par(bty = "n", mar = rep(0, 4), oma = rep(0, 4))
60 image(matrix(1:7, ncol = 1),col = pal(N))
61 dev.off()
62 }
63
64
65 # -------------------------------------------------------------------
66 # Snippet from:
67 # http://yihui.name/en/2012/10/build-static-html-help/
68 # Create static html from Rd files.
69 # -------------------------------------------------------------------
70 static_help = function(pkg, methods, links = tools::findHTMLlinks()) {
71 pkgRdDB = tools:::fetchRdDB(file.path(find.package(pkg), 'help', pkg))
72 force(links); topics = names(pkgRdDB)
73 for (p in topics) {
74 if ( ! is.null(methods) ) {
75 if ( ! p %in% methods ) next
76 }
77 message(sprintf("Create static html help page for %s::%s",pkg,p))
78 content = pkgRdDB[[p]]
79 tools::Rd2HTML(pkgRdDB[[p]], sprintf("tmp_%s.html",p),
80 package = pkg, Links = links, no_links = is.null(links))
81 }
82 }
83
84
85 cat(" * Create static help pages using Rd2HTML\n")
86 static_help("colorspace","choose_palette")
87 static_help("colorspace","hcl_palettes")
88 static_help("colorspace","specplot")
89 static_help("colorspace","hclplot")
90
91
92 # -------------------------------------------------------------------
93 # Parsing html content and create new static help page for
94 # the hclwizard.
95 # -------------------------------------------------------------------
96 # Reading help page content
97 getHTMLcontent <- function(file) {
98 # Helper function to extract the different sections
99 extract <- function(html,sec) {
100 ##r <- regmatches(html,regexpr(sprintf("<h3>%s</h3>.*?(?=<h3>)",sec),html,perl=TRUE))
101 r <- regmatches(html,regexpr(sprintf("<h3>%s</h3>.*?(?=(<h3>|<hr\\s?/>|$))",sec),html,perl=TRUE))
102 r <- gsub("#LB#","\n",r)
103 r
104 }
105 title <- function(html) {
106 r <- regmatches(html,regexpr("<h2>.*</h2>",html))
107 r <- gsub("#LB#","\n",r)
108 r
109 }
110 if ( ! file.exists(file) ) stop(sprintf("Cannot find %s",file))
111 # Read content ...
112 html <- gsub("\\n","",paste(readLines(file),collapse="#LB#"))
113 # Remove hyperrefs
114 html <- gsub("</a>","",html,perl=TRUE)
115 html <- gsub("<a href=.*?(?=>)>","",html,perl=TRUE)
116 html <- gsub("Author\\Ws\\W","Authors",html)
117 # ... and parse content
118 res <- list()
119 res[['Title']] <- title(html)
120 for ( sec in c("Description","Usage","Arguments","Details",
121 "Value","Authors","References","See Also") ) {
122 res[[sec]] <- extract(html,sec)
123 }
124 res
125 }
126
127 # Reading content
128 cat(" * Reading html content\n")
129 content1 <- getHTMLcontent("tmp_choose_palette.html")
130 content2 <- getHTMLcontent("tmp_hcl_palettes.html")
131 content3 <- getHTMLcontent("tmp_specplot.html")
132 content4 <- getHTMLcontent("tmp_hclplot.html")
133
134 # Create output file
135 outfile <- "html/info.html"
136 if ( file.exists("html/info.html") ) file.remove(outfile)
137 cat(" Add authors on top\n")
138 write(content1[["Authors"]], file=outfile, append=TRUE)
139
140 # Create new help page content
141 for ( sec in c("Title","Description","Details","Value") ) {
142 cat(sprintf(" Appending %s\n",sec))
143 write(content1[[sec]], file=outfile, append=TRUE)
144 }
145 for ( sec in c("Title","Description","Details","Value") ) {
146 cat(sprintf(" Appending %s\n",sec))
147 write(content2[[sec]], file=outfile, append=TRUE)
148 }
149 for ( sec in c("Title","Description","Details","Value") ) {
150 cat(sprintf(" Appending %s\n",sec))
151 write(content3[[sec]], file=outfile, append=TRUE)
152 }
153 for ( sec in c("Title","Description","Details","Value") ) {
154 cat(sprintf(" Appending %s\n",sec))
155 write(content4[[sec]], file=outfile, append=TRUE)
156 }
157 write(content1[["References"]], file=outfile, append=TRUE)
158
159 # Remove temporary rendered html pages
160 cat(" * Remove temporary files\n")
161 file.remove("tmp_choose_palette.html")
162 file.remove("tmp_hcl_palettes.html")
163 file.remove("tmp_specplot.html")
164 file.remove("tmp_hclplot.html")
165
166
167