Quickly Generate a Slide Deck for Hugo with Blogdown

Like many people in the data science world, I am using the Hugo Academic theme with blogdown to create my website. Last week, I put a slide deck on my site using a tutorial from the Miller Lab. It was helpful in getting the deck to show up, but called for a lot of tedious manuel labor, so I wrote a script to make putting slides up easier.

Here’s a quick tutorial for using my script so that you too can easily make a slide deck using Hugo Academic with blogdown.

Initial Steps

  • Download your slide deck as a pdf.
  • In your site’s directory, go to content –> slides and create a new folder for your slide deck.
  • Place the pdf of the slide deck in the new folder.

Making the Code Go

  • Copy and save the code below as a .R file
  • Set your working directory to the directory housing your site.
  • Source the file.
  • Provide the name of the folder with the slides.
  • Provide the name of the pdf with the slides. (Do Not Include .pdf)
  • fill out the front matter elements of the newly created index.md in the folder with your slides and you are good to go.
#import libraries
require(pdftools) #converts pdf slide show to jpegs
require(glue) #allow for naming

#Saves you a headache
print("Is your working directory set to the folder where you site is housed?")
#Asks for the name of the folder where the slides will be housed
directory <- readline(prompt = "What is the name of the folder with the slides?")
#Asks for the name of the pdf of slide deck. Do not include .pdf in your response.
pdf_name <- readline(promp = "What is the name of the pdf?")

#gets current directroy
cur_wd <- getwd()
#creates the file path to the directory 
directory_path <- glue("{cur_wd}/content/slides/{directory}")
#creates the file path to the pdf
file_path <- glue("{directory_path}/{pdf_name}.pdf")


#sets working directory so the pdf to jpeg function exports to the right place
setwd(directory_path)
#converts pdf to jpeg, let's you know that it's working via verbose
pdf_convert(file_path, format = "jpeg", pages = NULL,
            filenames = NULL, dpi = 300, opw = "", upw = "", verbose = TRUE)

#how long is the slide show 
deck_length <- pdf_length(file_path)
#yaml header for index.md
header <- "---
authors: 
categories: []
date: 
slides: 
  highlight_style: dracula
  theme: black
summary: 
title:
---"

#initialize deck
deck <- ""
#creates and appends the code to display the slides
for (i in 1:deck_length){
  slide_name <- glue("{pdf_name}_{i}")
  slide <- glue('\n\n{{{{< slide background-image="{slide_name}.jpeg" >}}}}\n___\n')
  deck <- paste0(c(deck, slide))
}

#saves thß slide deck as index.md
cat(header, deck, file = "index.md", sep = c(""))
#resets working directory
setwd(cur_wd)