# Copyright (C) 2017 Metrum Research Group, LLC
#
# This file is part of mdcontent
#
# mdcontent is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# mdcontent is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with mdcontent If not, see <http://www.gnu.org/licenses/>.
default <- list(title="",file="",
tags="",maintainer="",ext="")
parse_index <- function(file,where,link) {
stopifnot(file.exists(file))
index <- yaml.load_file(file)
out <- vector(mode="list",length=length(index))
for(i in seq_along(index)) {
x <- index[[i]]
file <- paste0(names(index)[i])
tags <- paste(x$tags,collapse=',')
dd <- data_frame(title=x$title,
file=file,
tags=x$tags,
description=x$description,
maintainer=x$maintainer)
out[[i]] <- dd
}
out <- bind_rows(out) %>% mutate(path = where)
out <- mutate(out,File = file.path(link,file))
out
}
taglink <- function(tag) {
hash <- paste0("#tag--",tolower(gsub("[[:punct:]]", "-",tag)))
paste0("[",tag,"](",hash,")")
}
initfile <- function(file,what) {
if(file.exists(file)) {
file.remove(file)
}
cat(file=file, what)
return(file)
}
catapp <- function(file,...) {
cat(file=file,...,append=TRUE)
}
link <- function(a,b) paste0("[",a,"](",b,")")
link2 <- function(a,b,sep=":",basename_only=TRUE) {
bb <- b
if(basename_only) bb <- basename(bb)
paste0(a,sep," ",paste0("[",bb,"](",b,")"))
}
lvl1 <- function(...) {
x <- paste(unlist(list(...)),collapse=" ")
paste0("- ", x)
}
subheader <- function(...) {
x <- paste(unlist(list(...)),collapse=" ")
paste0("## ", x)
}
subsubheader <- function(...) paste0("#", subheader(...))
lvl2 <- function(...) {
x <- paste(unlist(list(...)),collapse=" ")
paste0(" - ", x)
}
mlvl2 <- function(...) {
x <- unlist(list(...))
x <- paste0(" - ",x)
x <- paste(x,collapse="\n")
x
}
details <- function(x) {
paste0("<details>\n",x[1],"</details>")
}
write_file <- function(x,con) {
tags <- unique(x$tags)
File <- x$File[1]
lnk <- link(x$title[1],x$File[1])
tagsl <- taglink(tags)
descr <- x$description
catapp(file=con, lvl1(lnk),"\n")
#catapp(file=con, lvl2("__title__: ", x$title[1]),"\n")
catapp(file=con, lvl2("__file__: ", x$file[1]), "\n")
#catapp(file=con,lvl2("__maintainer__: ", x$maintainer[1]),"\n")
catapp(file=con,lvl2("__tags__: ", tagsl),"\n")
catapp(file=con,lvl2(x$description[1]), "\n")
}
write_tag <- function(x,con) {
.tag <- x$tags[1]
catapp(con, subsubheader("tag: ", .tag), "\n")
catapp(con, mlvl2(link2(x$title,x$File)),"\n")
}
##' Write an index.md file
##'
##' @param target target directory
##' @param index a yaml file containing index information
##' @param output output file name
##' @param link path prefix to link index document to indexed files
##'
##' @export
write_index <- function(target,index="index.yaml",output="index.md",link='.') {
out <- parse_index(index,target,link)
con <- initfile(output, "## Index by file\n")
out <- dplyr::arrange(out,file)
file_chunks <- split(out,out$File)
foo <- lapply(file_chunks,write_file,con=con)
catapp(con,"\n\n## Index by tag\n")
out <- dplyr::arrange(out,tags)
tag_chunks <- split(out,out$tags)
foo <- lapply(tag_chunks,write_tag,con=con)
return(out)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.