rm(list = ls())
library( distill )
library( stringr )
<- list.files(
files path = "old_posts",
pattern = "index.Rmd",
recursive = TRUE,
full.names = TRUE
)
for (file in files) {
print(file)
<- readLines(file)
lines
# find the date
<- grep( "date: ", lines, fixed = TRUE)
idx <- ""
date
if (length(idx) == 1) {
<- str_sub( lines[idx], 7, 16)
date
}
# Find the second --- and put in the output type
<- grep("---", lines, fixed = TRUE)
idx <- ""
content <- c(lines, " ", " ", " ")
lines if (length(idx) == 2) {
<- paste( lines[ (idx[2]+1):length(lines) ] ,
content collapse = "\n")
}
# find the title
<- grep("title: ", lines, fixed=TRUE )
idx <- "No Title"
title if( length(idx)>0 ) {
<- str_trim( gsub('[[:punct:] ]+',
title " ",
str_remove(lines[idx], "title: ")))
}
<- str_replace_all( tolower(title),
ofile pattern=" ",
replacement = "-" )
<- paste(date, ofile, sep="-")
nfile <- paste("_posts/",
path
nfile,"/",
ofile, ".Rmd", sep="")
if( !file.exists(path) ) {
::create_post( title[1],
distillcollection="posts",
date = date,
date_prefix=date,
edit = FALSE)
cat( content,
file = path,
append=TRUE,
sep="\n")
} }
OK, now that I have moved over most of the manuscripts from the previous site, it comes time to move over some of the older posts. Many of these posts are important as there is code and data associated with them that I’d like to have access to going forward.
Like before, however, there is some futzing around that needs to be done. However, this time, I took a different appraoch. I was less concerned about keeping the category information and all of them were authored by myself, so what I did was:
- Pick out a set of 100 or so entries to keep.
- Move them into a top-level folder called
old_posts
.
- Read them in, one at a time, and extracted the title and date.
- Created a new post using
distill::create_post()
using that date and title.
- Pasted the contents of the markdown (skipping all the YAML crazyness) on the end of the new post (n.b., did you know that you could use
cat()
to concatenate onto the end of a file on the filesystem? I didn’t!).
So here is the basic code I used for it.
But the problem is that when we use distill::createpost()
, it puts in the following default content into the markdown. And then I just appended the original content onto the end of it.
So I did a quick global search and replace using my favorite TextMate and then ran the following code to render it all out and looking good.
for( file in list.files(path="_posts/",
pattern = "*.Rmd",
recursive = TRUE,
full.names = TRUE) ) {
::render( file )
rmarkdown }