forked from eddelbuettel/ctv-hpc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ctv2md.r
executable file
·46 lines (36 loc) · 1.74 KB
/
ctv2md.r
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#!/usr/bin/r
## if you do not have /usr/bin/r from littler, just use Rscript
##
## Copyright 2014 - 2017 Dirk Eddelbuettel
## Released under GPL-2 or later
ctv <- "HighPerformanceComputing"
ctvfile <- paste0(ctv, ".ctv")
htmlfile <- paste0(ctv, ".html")
#mdfile <- paste0(ctv, ".md")
mdfile <- "README.md"
## load packages
suppressMessages(library(XML)) # called by ctv
suppressMessages(library(ctv))
r <- getOption("repos") # set CRAN mirror
r["CRAN"] <- "https://cloud.r-project.org"
options(repos=r)
check_ctv_packages(ctvfile) # run the check
## create html file from ctv file
ctv2html(read.ctv(ctvfile), htmlfile)
### these look atrocious, but are pretty straight forward. read them one by one
### - start from the htmlfile
cmd <- paste0("cat ", htmlfile,
### - in lines of the form ^<a href="Word">Word.html</a>
### - capture the 'Word' and insert it into a larger URL containing an absolute reference to task view 'Word'
" | sed -e 's|^<a href=\"\\([a-zA-Z]*\\)\\.html|<a href=\"https://cran.r-project.org/web/views/\\1.html\"|' | ",
### - call pandoc, specifying html as input and github-flavoured markdown as output
"pandoc -s -r html -w markdown_github | ",
### - deal with the header by removing extra ||, replacing |** with ** and **| with **:
"sed -e's/||//g' -e's/|\\*\\*/\\*\\*/g' -e's/\\*\\*|/\\*\\* /g' -e's/|$/ /g' ",
### - make the implicit URL to packages explicit
"-e's|../packages/\\(.*\\)/index.html|https://cran.r-project.org/package=\\1|g' ",
### - write out mdfile
"> ", mdfile)
system(cmd) # run the conversion
unlink(htmlfile) # remove temporary html file
cat("Done.\n")