From 877ccfafa29b9b92e0ed3d743e1134482ad9a3b7 Mon Sep 17 00:00:00 2001 From: Fernando Mayer <fernandomayer@gmail.com> Date: Tue, 15 Sep 2015 16:00:51 -0300 Subject: [PATCH] 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. --- R/knit2wpCrayon.R | 51 +++++++++++++++++++++++++++++++---------------- 1 file changed, 34 insertions(+), 17 deletions(-) diff --git a/R/knit2wpCrayon.R b/R/knit2wpCrayon.R index 80dc617..bb4c0b3 100644 --- a/R/knit2wpCrayon.R +++ b/R/knit2wpCrayon.R @@ -2,8 +2,9 @@ #' #' @name knit2wpCrayon #' -#' @description This function improves the \code{RWordPress::knit2wp} to -#' allow properly render code when using Crayon Sintax Highlighter. +#' @description This function improves \code{RWordPress::knit2wp} to +#' allow properly render code when using Crayon Sintax Highlighter +#' in Wordpress. #' #' @param input a markdown or Rmarkdown file. #' @param title title for the post. @@ -16,65 +17,81 @@ #' @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 #' draft mode. -#' @param write logical, is the result of knit be written to a html -#' file. This is useful to copy from this file and paste inside the -#' wordpress post editor (on text mode, not visual mode). +#' @param write logical, if the result of knit should be written to a +#' html file. This is useful to copy from this file and paste inside +#' 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} #' -#' @import knitr +#' @import knitr markdown RWordPress #' @export #' @examples #' \donttest{ #' #' library(knitr) #' library(RWordPress) -#' +#' #' post <- "2015-08-24_polyGui.Rmd" #' title <- "Interface para regressão polinomial" #' categ <- c("gui", "rbloggers_pt") #' keywd <- c("gWdigets", "legTools", "lm", "poly") -#' +#' #' pass <- scan(n=1, what=character()) #' options(WordpressLogin=c(walmes=pass), #' WordpressURL="http://blog.leg.ufpr.br/xmlrpc.php") -#' +#' #' knit2wpCrayon(post, title=title, #' action="editPost", postid=179, #' categories=categ, mt_keywords=keywd, #' ## write=TRUE, upload=FALSE, #' write=FALSE, upload=TRUE, #' publish=FALSE) -#' +#' #' } knit2wpCrayon <- function(input, title="A post from knitr", ..., action=c("newPost", "editPost", "newPage"), postid, encoding=getOption("encoding"), 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) on.exit(unlink(out)) con <- file(out, encoding=encoding) 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 <- markdown::markdownToHTML(text=content, fragment.only=TRUE) + content <- markdownToHTML(text=content, fragment.only=TRUE) content <- gsub( pattern="<pre><code class=\"([[:alpha:]]+)\">(.+?)</code></pre>", replacement="<pre class=\"lang:\\1 decode:true\">\\2</pre>", x=content) - content=knitr:::native_encode(content, "UTF-8") - title=knitr:::native_encode(title, "UTF-8") + content=native_encode(content, "UTF-8") + title=native_encode(title, "UTF-8") if (write){ writeLines(text=content, con=gsub(x=out, pattern="\\.md$", replacement=".html")) } if (upload){ action=match.arg(action) - WPargs=list(content=list(description=content, title=title, + WPargs=list(content=list(description=content, title=title, ...), publish=publish) - if (action=="editPost") + if (action=="editPost") WPargs=c(postid=postid, WPargs) do.call("library", list(package="RWordPress", character.only=TRUE)) print(do.call(action, args=WPargs)) -- GitLab