Skip to content

Commit

Permalink
updated converter
Browse files Browse the repository at this point in the history
  • Loading branch information
beanumber committed Dec 13, 2018
1 parent c7619a3 commit ba05dbc
Showing 1 changed file with 15 additions and 12 deletions.
27 changes: 15 additions & 12 deletions R/Rnw2Rmd.R
Expand Up @@ -20,22 +20,25 @@ Rnw2Rmd <- function(path, new_path = NULL) {
# chunks
x <- gsub("(<<)(.*)(>>=)", "```{r \\2}", x)
x <- gsub("^@", "```", x)
x <- gsub("(\\\\Sexpr\\{)(.+)(\\})", "`r \\2`", x)
x <- gsub("(\\\\Sexpr\\{)([^\\}]+)(\\})", "`r \\2`", x)
# sections
x <- gsub("(\\\\chapter\\{)(.+)(\\})", "# \\2", x)
x <- gsub("(\\\\section\\{)(.+)(\\})", "## \\2", x)
x <- gsub("(\\\\subsection\\{)(.+)(\\})", "### \\2", x)
x <- gsub("(\\\\subsubsection\\{)(.+)(\\})", "#### \\2", x)
x <- gsub("(\\\\chapter\\{)([^\\}]+)(\\})", "# \\2", x)
x <- gsub("(\\\\section\\{)([^\\}]+)(\\})", "## \\2", x)
x <- gsub("(\\\\subsection\\{)([^\\}]+)(\\})", "### \\2", x)
x <- gsub("(\\\\subsubsection\\{)([^\\}]+)(\\})", "#### \\2", x)
# references
x <- gsub("(\\\\citep\\{)(.+)(\\})", "[@\\2]", x)
x <- gsub("(\\\\cite\\{)(.+)(\\})", "@\\2", x)
x <- gsub("(\\\\ref\\{)(.+)(\\})", "\\\\@ref(\\2)", x)
x <- gsub("(\\\\label\\{)(.+)(\\})", "{#\\2}", x)
x <- gsub("(\\\\citep\\{)([^\\}]+)(\\})", "[@\\2]", x)
x <- gsub("(\\\\cite\\{)([^\\}]+)(\\})", "@\\2", x)
x <- gsub("(\\\\ref\\{)([^\\}]+)(\\})", "\\\\@ref(\\2)", x)
x <- gsub("(\\\\label\\{)([^\\}]+)(\\})", "{#\\2}", x)
x <- gsub("(\\\\index\\{)([^\\}]+)(\\})(\\{)([^\\}]+)(\\})\\%", "\\\\index{\\2}{\\5}", x)
# LaTeX
x <- gsub("\\\\item", "- ", x)
x <- gsub("(\\\\emph\\{)(.+)(\\})", "**\\2**", x)
x <- gsub("(\\\\textbf\\{)(.+)(\\})", "*\\2*", x)
x <- gsub("(\\\\href\\{)(.+)(\\})(\\{)(.+)(\\})", "[\\2](\\5)", x)
x <- gsub("(\\\\emph\\{)([^\\}]+)(\\})", "*\\2*", x)
x <- gsub("(\\\\textit\\{)([^\\}]+)(\\})", "*\\2*", x)
x <- gsub("(\\\\textbf\\{)([^\\}]+)(\\})", "**\\2**", x)
x <- gsub("(\\\\href\\{)([^\\}]+)(\\})(\\{)([^\\}]+)(\\})", "[\\5](\\2)", x)
x <- gsub("(\\\\url\\{)([^\\}]+)(\\})", "(\\2)", x)
# x <- gsub('(``)(.+)(\\")', '"\\2"', x)

x <- gsub("{\\\\tt ([a-zA-Z0-9. _()=]*)} ", "`\\1` ", x, perl = TRUE) # need to clean up {`
Expand Down

0 comments on commit ba05dbc

Please sign in to comment.