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.


December 21, 2021

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($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, "/", 
                       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:\n    affiliation: Center for Environmental Studies\n    affiliation_url:\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")

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.