Craft + RStudio + Distill = Post

This is the R side of the Craft -> Website through RStudio and Distill. I’ve set up a workflow here to take posts exported in markdown from Craft to be imported into this site’s workflow.

Rodney Dyer https://dyerlab.org
2021-12-21

So the first thing to do was to set up a staging place to save raw markdown files. Once I had that, I moved the rendering of this website from the “push the button on the build pane in RStudio to”run this script."

This script is in the rood directory of the site and does the following steps:

  1. finds all markdown files in the _toImport folder. For each file it:
  1. After finishing up, it then renders the entire site.

Here is the script.

#' This file is how I'll build the site.
#' 
rm( list=ls() )


# load in and convert any of the markdown stuff in _toImport into 
#  distill articles.
for( file in list.files("_toImport", pattern = "*.md", full.names = TRUE) ) { 
  
  # find previous stuff
  raw <- readLines( file )
  date <- stringr::str_split( file.info(file)$mtime , 
                              " ", 
                              simplify = TRUE)[,1] 
  title <- stringr::str_remove( raw[1], "# ") 
  description <- raw[3]
  contents <- paste( raw[ -1 ], collapse="\n")
  
  # make new post  will create 
  filetitle <- gsub("[[:punct:][:blank:]]+", "-", title ) 
  filetitle <- tolower(filetitle)
  orig_title <- paste( "_posts/", 
                       date, "-", 
                       filetitle, "/", 
                       filetitle,".Rmd", 
                       sep="" )
  
  distill::create_post( filetitle, 
                        date = date,
                        edit = FALSE )
  
  raw_post <- readLines( orig_title )
  raw_post[2] <- paste( "title: \"", title, "\"" )
  raw_post[4] <- paste( "  ", description, sep=" ")
  raw_post[7] <- "    url: https://dyerlab.org\n    affiliation: Center for Environmental Studies\n    affiliation_url: https://ces.vcu.edu\n    orcid_id: 0000-0003-4707-3453"
  raw_post[8] <- paste( raw_post[8], "preview: featured.png", sep="\n")
  raw_post <- c( raw_post[1:13], contents ) 
  raw_post_output <- paste( raw_post, collapse="\n")
  
  new_file <- paste( "_posts/", 
                     date, "-", 
                     filetitle, "/", 
                     "index.Rmd", sep="" )
  writeLines( raw_post_output, 
              con = new_file )
  
  # clean up the posts
  unlink( orig_title )
  unlink( file ) 
  
  rmarkdown::render( new_file )
}

# clean up any of the crap on the site and render the whole thing.
system( "find . -type f -iname '.DS_Store' -delete")
rmarkdown::render_site(".")

What I do not do is push through all the git actions to upload it. I think I’ll just do that manually.

Here is an example of the first Craft \(\to\) Distill posting.

Citation

For attribution, please cite this work as

Dyer (2021, Dec. 21). The Dyer Laboratory: Craft + RStudio + Distill = Post . Retrieved from https://dyerlab.github.io/DLabWebsite/posts/2021-12-21-craft-rstudio-distill-post/

BibTeX citation

@misc{dyer2021craft,
  author = {Dyer, Rodney},
  title = {The Dyer Laboratory: Craft + RStudio + Distill = Post },
  url = {https://dyerlab.github.io/DLabWebsite/posts/2021-12-21-craft-rstudio-distill-post/},
  year = {2021}
}