toc4faq <- function(htmlfile="admb-project.github.io/docs/faq/index.html", newfile='admb-project.github.io/docs/faq/index.html', replace=FALSE) { ################################################################################ # # toc4faq # # Purpose: To add a table of contents to the HTML file ADMB Frequently Asked Questions # Written: Ian Taylor, NOAA NWFSC. Ian.Taylor-at-noaa.gov # Returns: Writes file to newfile # ################################################################################ fullfaq <- readLines(htmlfile) faq <- fullfaq[grep('Start of FAQ',fullfaq):grep('End of FAQ',fullfaq)] # get heading lines from body of FAQ headings <- faq[sort(c(grep('

',faq),grep('

',faq)))] n <- length(headings) # make a data frame of key elements df <- data.frame(type=rep(NA,n),anchors=NA,titles=NA) df$type[grep("

",headings)] <- "h1" df$type[grep("

",headings)] <- "h2" for(i in 1:n){ string <- headings[i] string <- strsplit(strsplit(string,'name=\"')[[1]][2],"")[[1]][1] string <- strsplit(string,'\">')[[1]] df$anchors[i] <- string[1] df$titles[i] <- string[2] } ### write a table of contents toc <- c("") # loop over key elements gathered above for(i in 1:n){ newline <- paste('',df$titles[i],'',sep='') if(df$type[i]=="h1"){ newline <- paste("

",newline,'

',sep="") } if(df$type[i]=="h2"){ newline <- paste("
  • ",newline,'
  • ',sep="") } toc <- c(toc,newline) if(i") if(df$type[i]=="h2" & df$type[i+1]=="h1") toc <- c(toc,"") } if(i==n & df$type[i]=="h2") toc <- c(toc,"", "
    ") } toc <- c(toc,"") newfaq <- c('---', 'layout: default', 'title: ADMB Frequently Asked Questions', '---', '

    ADMB Frequently Asked Questions (FAQs)

    ', '

    If you have ideas for additions or modifications, please send them to', ' Ian.Taylor@noaa.gov.
    ', ' ADMB developers with editing privileges are welcome', ' to make changes, but should read the ', ' ', ' Note on Editing the FAQ before doing so.

    ', '', ' ', '', toc, # the new table of contents '', faq, # the old guts ' ') if(file.exists(newfile)){ if(!replace){ stop(paste("File exists:",newfile,"Set 'replace=TRUE' to overwrite")) }else{ print(paste("Replacing file:",newfile),quote=FALSE) file.remove(newfile) } }else{ print(paste("Writing new html file:",newfile),quote=FALSE) } writeLines(newfaq, newfile) return(invisible(newfaq)) }