Skip to content

Instantly share code, notes, and snippets.

@markheckmann
Last active August 29, 2015 14:11
Show Gist options
  • Save markheckmann/d1c3d2f31f32f9f35e2f to your computer and use it in GitHub Desktop.
Save markheckmann/d1c3d2f31f32f9f35e2f to your computer and use it in GitHub Desktop.
Post-process markdown code to paste into Wordpress
change_tex_delimiters <- function(x)
{
# replace latex equation delimiters ($$) if present
eq <- which(x == "$$") # find positions of equations
if (length(eq) > 0) {
eq.begin <- eq[c(T,F)]
eq.end <- eq[c(F,T)]
for (i in eq.begin)
x[[i]] <- "$latex \\displaystyle"
for (i in eq.end)
x[[i]] <- "$"
}
# replace inline latex $ delimiters
ptn <- "\\$(.*?)\\$" # find non-greedy between delimiters
replacement <- "$latex \\1$" # \\1 = back-reference
str_replace_all(x, ptn, replacement)
}
change_backticks_to_italics <- function(x)
{
# change backticks (`) to simple italics (*)
ptn <- "`([^\\`]+?)`" # do not replace ```r or ```
replacement <- "\\*\\1\\*" # \\1 = back-reference
str_replace_all(x, ptn, replacement)
}
md_for_wp <- function(f, f.out=NULL)
{
x <- readLines(f)
xn <- change_tex_delimiters(x)
xn <- change_backticks_to_italics(xn)
if (is.null(f.out))
f.out <- str_replace(f, "\\.md$", "-wp.md")
writeLines(xn, f.out)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment