<- list.files(path="../../_manuscripts",
files recursive = TRUE,
full.names = TRUE,
pattern = "index.md")
As part of moving over from Hugo to Distill, I need to move over all my manuscripts. While putting everything into Markdown is a good idea for portability, there does not seem to be a very quick way to translate YAML. IN this case, the old YAML looked like this (n.b., they are all .md files, not .Rmd files like distill likes so the syntax hightlighting will not look right):
Which will need to be translated into the new YAML to resemble:
This may not be that big of an deal but at the end of the day, I’ve got a ton of folders that each represent each manuscript I’ve published. I was able to get a lot of it done using some quick perl like this:
perl -pi -e s/name =/name:/g file.md
However, there is going to be a lot of pain associated with some of it (authors & categories sections). For that, I’ll have to run some R
code. Here is how I did it.
So, for each of these files, I need to:
- Load markdown file
- Save as Rmd
- Use some terminal magic to convert over yaml formatting.
So here it goes:
for( file in files ) {
<- gsub(".md", ".Rmd", file,perl = TRUE)
newfile <- paste("mv",file, newfile )
cmd system( cmd )
}
OK, so that was sufficient for me to get things good enough to compile. And it looks… meh.
I put all the manuscripts in its own category and subfolder but it has all the abstract shoved into the description. However, that causes some issues because some of the abstracts are long and it makes for an unreasonable view of the manuscripts.
and none of the images are showing. Now we’ll have to go through it all and futz around to make it look good. Here is the whole salchicha.
library( yaml )
<- list.files(path="../../_manuscripts",
files recursive = TRUE,
full.names = TRUE,
pattern = "index.Rmd")
for( file in files ) {
print(file)
# load in the YAML
<- read_yaml( file )
old
# Make the new file contents
<- c("---",
new paste("title:", as.yaml(old$title)),
paste("date:", as.yaml(old$date ) ) )
# make authors for post
<- unlist( lapply( old$authors,
pub_authors FUN = function(x) { return(paste("- name:", x))}))
<- c(new,
new "authors:",
pub_authors)
# put authors, year and publication here.
<- strsplit(old$date, "-",fixed = TRUE)[[1]][1]
year <- old$publication
pub
<- paste("description: |")
description <- paste( " ", pub, year, " ", sep=". ")
description
<- c(new,
new "description: |",
description)
# clean up the categories
if( "categories" %in% names(old) ) {
<- tolower( gsub( "\"", "", old$categories ) )
categories <- c(new,
new "categories: ",
unlist( lapply( categories,
FUN = function(x) { return(paste("-", x))})))
}
# put in the Journal
if( length( old$publication) > 0 ) {
<- c(new,
new paste("journal: ", old$publication))
}
if( "doi" %in% names( old ) ) {
<- c(new,
new paste("doi: ", old$doi ))
}
# if there is a bib
<- list.files( dirname(file),
bibs pattern = "*.bib",
full.names = TRUE)
if( length(bibs) == 1 ) {
<- paste( "mv",
cmd
bibs, paste( dirname(bibs),
"bibliography.bib",
sep="/"))
system(cmd)
<- c(new,
new "bibliography: bibliography.bib" )
}
#add end stuff
if( "respository_url" %in% names( old ) ) {
<- c(new,
new paste("repository_url:",as.yaml(old$respository_url ) ) )
}<- c( new,
new paste("output:\n",as.yaml(old$output)),
"---",
"")
<- gsub("\n\n", "\n", paste( new, collapse="\n") )
new
# put in links to PDF and doi if presnt
<- ""
links if( "url_pdf" %in% names( old ) ) {
<- old$url_pdf
url <- paste( "[![PDF Download](https://img.shields.io/badge/PDF-21B02C.svg)](", url, ")", sep="")
val <- paste( links, val)
links
}
if( "doi" %in% names( old ) ) {
<- paste("https://doi.org",old$doi, sep="/")
url <- paste( "[![ DOI ",
val $doi,
old"](https://img.shields.io/badge/DOI-474747.svg)](",
url,")", sep="")
<- paste( links, val )
links
}
<- c( new, links )
new
# Add image if present
if( "featured" %in% names( old ) ) {
<- paste("![](",old$featured,")")
img <- c( new,
new "",
img )
}
if( "description" %in% names( old ) ) {
<- c(new,
new "",
"## Abstract",
"",
$description )
old
}
# Previously, I saved to a different file so as to not overwrite the important stuff.
# Once it worked, then write over the old one.
# newfile <- paste( dirname(file), "/manuscript.Rmd", sep="")
write(new, file=file)
::render(file)
rmarkdown
}
Now, I’ve just got to go clean up the old temporary files using something like:
find _manuscripts -iname manuscrip* -delete
for( file in list.files(path="../../_manuscripts",
recursive = TRUE,
full.names = TRUE,
pattern = "index.Rmd") ) {
::render( file )
rmarkdown }