Skip to content
Snippets Groups Projects
Commit 877ccfaf authored by Fernando Mayer's avatar Fernando Mayer
Browse files

Import, document and add checks

Import knitr, markdown and RWordPress. Some small documentation
improvements. Add checks for the imported packages at beggining of the
function.
parent 14bb121c
Branches
No related tags found
No related merge requests found
...@@ -2,8 +2,9 @@ ...@@ -2,8 +2,9 @@
#' #'
#' @name knit2wpCrayon #' @name knit2wpCrayon
#' #'
#' @description This function improves the \code{RWordPress::knit2wp} to #' @description This function improves \code{RWordPress::knit2wp} to
#' allow properly render code when using Crayon Sintax Highlighter. #' allow properly render code when using Crayon Sintax Highlighter
#' in Wordpress.
#' #'
#' @param input a markdown or Rmarkdown file. #' @param input a markdown or Rmarkdown file.
#' @param title title for the post. #' @param title title for the post.
...@@ -16,15 +17,15 @@ ...@@ -16,15 +17,15 @@
#' @param upload logical, if the file is to be updated to the blog. #' @param upload logical, if the file is to be updated to the blog.
#' @param publish logical, if the post is to be published or stay in #' @param publish logical, if the post is to be published or stay in
#' draft mode. #' draft mode.
#' @param write logical, is the result of knit be written to a html #' @param write logical, if the result of knit should be written to a
#' file. This is useful to copy from this file and paste inside the #' html file. This is useful to copy from this file and paste inside
#' wordpress post editor (on text mode, not visual mode). #' the Wordpress editor (on text mode, not visual mode).
#' #'
#' @return None is returned by the function. #' @return Nothing is returned by the function.
#' #'
#' @author Walmes Zeviani, \email{walmes@@ufpr.br} #' @author Walmes Zeviani, \email{walmes@@ufpr.br}
#' #'
#' @import knitr #' @import knitr markdown RWordPress
#' @export #' @export
#' @examples #' @examples
#' \donttest{ #' \donttest{
...@@ -53,19 +54,35 @@ knit2wpCrayon <- function(input, title="A post from knitr", ..., ...@@ -53,19 +54,35 @@ knit2wpCrayon <- function(input, title="A post from knitr", ...,
action=c("newPost", "editPost", "newPage"), action=c("newPost", "editPost", "newPage"),
postid, encoding=getOption("encoding"), postid, encoding=getOption("encoding"),
upload=FALSE, publish=FALSE, write=TRUE){ upload=FALSE, publish=FALSE, write=TRUE){
##
##-------------------------------------------
## Loading required packages.
##
if (!requireNamespace("knitr", quietly=TRUE)){
stop("`knitr` needed for this function to work. Please install it.",
call.=FALSE)
}
if (!requireNamespace("markdown", quietly=TRUE)){
stop("`markdown` needed for this function to work. Please install it.",
call.=FALSE)
}
if (!requireNamespace("RWordPress", quietly=TRUE)){
stop("`RWordPress` needed for this function to work. Please install it.",
call.=FALSE)
}
out <- knit(input, encoding=encoding) out <- knit(input, encoding=encoding)
on.exit(unlink(out)) on.exit(unlink(out))
con <- file(out, encoding=encoding) con <- file(out, encoding=encoding)
on.exit(close(con), add=TRUE) on.exit(close(con), add=TRUE)
content <- knitr:::native_encode(readLines(con, warn=FALSE)) content <- native_encode(readLines(con, warn=FALSE))
content <- paste(content, collapse="\n") content <- paste(content, collapse="\n")
content <- markdown::markdownToHTML(text=content, fragment.only=TRUE) content <- markdownToHTML(text=content, fragment.only=TRUE)
content <- gsub( content <- gsub(
pattern="<pre><code class=\"([[:alpha:]]+)\">(.+?)</code></pre>", pattern="<pre><code class=\"([[:alpha:]]+)\">(.+?)</code></pre>",
replacement="<pre class=\"lang:\\1 decode:true\">\\2</pre>", replacement="<pre class=\"lang:\\1 decode:true\">\\2</pre>",
x=content) x=content)
content=knitr:::native_encode(content, "UTF-8") content=native_encode(content, "UTF-8")
title=knitr:::native_encode(title, "UTF-8") title=native_encode(title, "UTF-8")
if (write){ if (write){
writeLines(text=content, writeLines(text=content,
con=gsub(x=out, pattern="\\.md$", replacement=".html")) con=gsub(x=out, pattern="\\.md$", replacement=".html"))
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment