#' Clean the posts from the wordpress-to-hugo-exporter plugin
#'
#' @param path_wp Path to the wordpress posts
#' @param path_hugo Path to the Hugo posts
#' @param domain_old The old domain mentioned in the posts
#' @param domain_new The new domain
#' @param mark_private prefix of the private posts
#'
#' @return adapted posts for a blogdown site
#' @export
#'
#' @examples mig_wp(path_wp = NA)
mig_wp <- function(path_wp, path_hugo, domain_old, domain_new, mark_private = 'private_'){
if(is.na(path_wp)) return(message('Please give a valid path.'))
for (i in dir(path_wp)){
# read the old posts
txt <- readLines(paste0(path_wp, i), encoding = 'UTF-8')
# change the old domains into new in the posts
txt <- gsub(domain_old, domain_new, txt)
# add a summary field
summ <- txt[grep('---', txt)[2]+1]
summ <- gsub('"', '', summ)
newline <- paste0('summary: "', summ, '"')
txtnew <- c(txt[1:3], newline, txt[4:length(txt)])
# add a line of the original link
newline <- paste0('\n[Original Link](', domain_old, substring(txt[grep('url', txt)[1]], 6), ')\n')
txtnew <- c(txt, newline)
# higlight the private posts
filename <- ifelse(mark_private & 'private: true' %in% txt,
paste0(path_hugo, mark_private, i),
paste0(path_hugo, i))
# save new posts
writeLines(txtnew, filename, useBytes = TRUE)
}
}
#' create content/<section>/.md files automatically if a new figure or video in static/<section>/ is found. Only for the chipsenkbeil/grid-side theme
#'
#' @param sec the section name
#' @param overwrite logical. whether overwrite the existing posts.
#'
#' @return new posts
#' @export
#'
#' @examples
#' sections <- gsub('static/', '', dir('static/'))
#' sections <- sections[!sections %in% c('cover')]
#' lapply(sections, gs_newpost)
gs_newpost <- function(sec, overwrite = FALSE){
# get the full path of the media files and markdown files
media <- dir(paste0('static/', sec), full.names = TRUE)
sec_path <- paste0('content/', sec)
if(!dir.exists(sec_path)) dir.create(sec_path)
md <- dir(sec_path, full.names = TRUE)
# get the file names (without suffix)
suffix <- gsub('.*\\.([[:alnum:]]+)$', '\\1', media)
media_short <- gsub('.*/([^/]+)\\.[[:alnum:]]+$', '\\1', media)
md_short <- gsub('.*/([^/]+)\\.[[:alnum:]]+$', '\\1', md)
# detect new media files
if(overwrite) {
new <- 1: length(media_short)
} else {
new <- which(!media_short %in% md_short)
}
for(i in new){
# create .md posts based on images
if(suffix[i] %in% c('jpg', 'jpeg', 'png', 'gif')){
name_split <- strsplit(gsub('_',' ', media_short[i]), '\\+')[[1]]
media_date <- as.Date(file.info(media[i])$mtime)
newmd <- c(
'+++',
paste0('title = "', name_split[3], '. ', name_split[2], ', ', media_date, '. Camera: ', name_split[1], '."'),
paste0('date = "', media_date, '"'),
paste0('image = "', gsub('static', '', media[i]), '"'),
'+++'
)
}
# create .md posts based on videos
if(suffix[i] %in% c('mp4', 'webm', 'ogv', '3gp')){
newmd <- c(
'+++',
paste0('title = "', gsub('_', ' ', media_short[i]), '"'),
paste0('video_', suffix[i], ' = "/video/', media_short[i], '.', suffix[i], '"'),
'+++',
'',
'<video controls>',
paste0('<source src="/', sec, '/', media_short[i], '.', suffix[i], '" type="video/', suffix[i], '">'
),
'Your browser does not support the video tag.',
'</video>'
)
}
# create .md posts based on audios
if(suffix[i] %in% c('m4a', 'ogg', 'mp3')){
newmd <- c(
'+++',
paste0('title = "', gsub('_', ' ', media_short[i]), '"'),
paste0('date = "', Sys.Date(), '"'),
'+++',
'',
'<audio controls>',
paste0(
'<source src="/', sec, '/', media_short[i], '.', suffix[i], '" type="audio/',
switch (suffix[i],
m4a = 'mp4',
ogg = 'ogg',
mp3 = 'mpeg'
), '">'
),
'</audio>'
)
}
# write the posts
writeLines(newmd, paste0('content/', sec, '/', media_short[i], '.md'), useBytes = TRUE)
}
}
#' create a new section automatically. Only for the chipsenkbeil/grid-side theme
#'
#' @param sec The name of the new section
#' @return new sections
#' @export
#'
#' @examples
#' lapply(sections, gs_newsec)
gs_newsec <- function(sec){
layout_path <- paste0('layouts/', sec)
if(!dir.exists(layout_path)){
dir.create(layout_path)
writeLines('{{ partial "gallery/single.html" . }}',
paste0(layout_path, '/single.html'),
useBytes = TRUE)
cfg <- readLines('config.toml', encoding = 'UTF-8')
cfg_new <- c(cfg,
' [[Params.Cells.List]]',
paste0('name = "', toupper(sec), '"'),
paste0('link = "', sec, '/"'),
paste0('image = "/cover/', sec, '.jpg"')
)
writeLines(cfg_new, 'config.toml', useBytes = TRUE)
}
list_path <- paste0('layouts/section/', sec, '.html')
if(!file.exists(list_path)){
writeLines('{{ partial "gallery/list.html" . }}', list_path, useBytes = TRUE)
}
}
#' Build a hugo blog site for a steem author
#'
#' @param author author name without @
#' @param initial if initialize a site
#' @param template the hugo template
#' @param post_df A dataframe with the posts contents retrieved from SteemSQL
#' @param my_github A character string of a github repo
#' @param dest_path A character string
#' @param orginal_link A character string
#' @param post_df_source A character string of the data frame source.
#'
#' @return a blogdown-hugo web site
#' @export
#' @examples new_steem()
new_steem <- function(author = NA,
post_df,
dest_path = 'blog',
initial = FALSE,
template = 'xmin',
post_df_source = c('appbase_api', 'steemsql.com'),
orginal_link = c('steemit.com',
'cnsteem.com',
'busy.org',
'steemdb.com',
'steemd.com'),
my_github = 'your_name/your_repo'){
if(is.na(author)) {
return(print('Please give a valid author.'))
}
post_df_source <- match.arg(post_df_source)
# Set up for the first time
if(initial) {
template_path <- system.file(paste0('template/blogdown_' , template, '.zip'),
package = 'steemr')
unzip(template_path, exdir = dest_path)
# create _index.markdown
index_demo <- readLines(paste0(dest_path, '/content/_index.markdown'),
encoding = 'UTF-8')
index <- gsub(pattern = 'steemauthor',
replacement = author,
index_demo)
writeLines(text = index,
con = paste0(dest_path, '/content/_index.markdown'),
useBytes = TRUE)
### change config.toml
config_demo <- readLines(paste0(dest_path, '/config.toml'),
encoding = 'UTF-8')
config <- gsub(pattern = 'steemauthor',
replacement = author,
config_demo)
writeLines(text = config,
con = paste0(dest_path, '/config.toml'))
}
steemr::bmd(post_df = post_df,
dest_path = paste0(dest_path, '/content/post'),
post_df_source = post_df_source,
if_yaml = TRUE)
### create about.md
knitr:: knit(paste0(dest_path, '/R/about.Rmd'),
output = paste0(dest_path, '/content/about.md'),
encoding = 'UTF-8')
oldwd <- getwd()
setwd(dest_path)
blogdown::build_site(local = FALSE)
setwd(oldwd)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.