Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add html_to_r() #1812

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ Authors@R: c(
person("Sebastian", "Meyer", role = "ctb"),
person("Sietse", "Brouwer", role = "ctb"),
person(c("Simon", "de"), "Bernard", role = "ctb"),
person("Steve", "Condylios", role = c("ctb"), comment = c(ORCID = "0000-0003-0599-844X")),
person("Sylvain", "Rousseau", role = "ctb"),
person("Taiyun", "Wei", role = "ctb"),
person("Thibaut", "Assus", role = "ctb"),
Expand Down Expand Up @@ -153,6 +154,7 @@ Collate:
'defaults.R'
'concordance.R'
'engine.R'
'extract.R'
'highlight.R'
'themes.R'
'header.R'
Expand Down
3 changes: 3 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export(hook_pngquant)
export(hook_purl)
export(hook_r2swf)
export(hook_scianimator)
export(html_to_r)
export(image_uri)
export(imgur_upload)
export(include_app)
Expand Down Expand Up @@ -138,6 +139,8 @@ import(graphics)
import(methods)
import(stats)
import(utils)
importFrom(stringr,str_match_all)
importFrom(stringr,str_replace_all)
importFrom(xfun,attr)
importFrom(xfun,file_ext)
importFrom(xfun,file_string)
Expand Down
88 changes: 88 additions & 0 deletions R/extract.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
#' Extract R code from a knitted R Markdown HTML file.
#'
#' @name html_to_r
#' @author Steve Condylios
#' @usage html_to_r(rmarkdown_html, padding, inc_out)
#' @param rmarkdown_html The HTML output of a knitted Rmd file
#' @param padding Specifies what goes between the last character of a code block and the
#' first character of the next code block. Defaults to two newlines (which gives
#' the visual appearance of one newline between code blocks).
#' @param inc_out \code{TRUE}/\code{FALSE} as to whether to include output of code chucks. Defaults
#' to \code{TRUE}.
#'
#' @return A character vector of length 1 containing the R code extracted from the
#' R Markdown HTML file.
#'
#' @export
#'
#' @examples
#' cat((rmarkdown_html <- "<html><body>Intro
#' ```R\n1 * 1\n```\nmore text\n```r\n2 * 2\n```
#' some more text\n```\n3 * 3\n```\nThe End.
#' </body></html>\n"))
#'
#' html_to_r(rmarkdown_html)
#'
#' cat(html_to_r(rmarkdown_html))
#'
#' @importFrom stringr str_match_all str_replace_all



html_to_r <- function(rmarkdown_html, padding, inc_out) {

if(missing(padding)) { padding = "\n\n" }
if(missing(inc_out)) { inc_out = TRUE }

extract_body <- function(rmarkdown_html) {
# light-weight replacement for html_nodes()
as.character(xml2::xml_find_all(xml2::read_html(rmarkdown_html), ".//body"))
}

body <- extract_body(rmarkdown_html)

remove_body_tags <- function(body) {
# light-weight replacement for html_text()
inner <- substr(body, 7, nchar(body))
substr(inner, 1, nchar(inner)-6)
}

inner <- remove_body_tags(body)

# only include |``` if output is to be included
chunks <- if(inc_out == TRUE) {
str_match_all(inner, "(```R|```r|```)((.|\\s)*?)```")
}else{
str_match_all(inner, "(```R|```r)((.|\\s)*?)```")
}


clean_chunks <- function(chunks) {

# remove first char
neat_chunks <- substr(chunks[[1]][,3] , 2, nchar(chunks[[1]][,3]))

# last first char
substr(neat_chunks , 1, nchar(neat_chunks) - 1)
}

neat_chunks <- clean_chunks(chunks)


replace_character_entities <- function(char_entity){
xml2::xml_text(xml2::read_html(paste0("<x>", char_entity, "</x>")))
}

neat_chunks <- unname(sapply(neat_chunks, replace_character_entities))

paste0(neat_chunks, collapse=padding)
}









39 changes: 39 additions & 0 deletions man/html_to_r.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

69 changes: 69 additions & 0 deletions tests/testit/test-extract.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
library(testit)

assert(
'html_to_r() extracts R code within ```R decorator',
identical(
{
rmarkdown_html <- "<html><body>Intro
```R\n1 * 1\n```\nmore text\n</body></html>\n"

html_to_r(rmarkdown_html)

},

"1 * 1")
)



assert(
'html_to_r() extracts R code within ```r decorator',
identical(
{
rmarkdown_html <- "<html><body>more text\n```r\n2 * 2\n```
some more text\n</body></html>\n"

html_to_r(rmarkdown_html)

},

"2 * 2")
)



assert(
'html_to_r() extracts R code within ``` decorator',
identical(
{
rmarkdown_html <- "<html><body>some more text\n```\n3 * 3\n```\nThe End.
</body></html>\n"

html_to_r(rmarkdown_html)

},

"3 * 3")
)



assert(
'html_to_r() extracts R code from R Markdown HTML file containing a varity of decorators',
identical(
{
rmarkdown_html <- "<html><body>Intro
```R\n1 * 1\n```\nmore text\n```r\n2 * 2\n```
some more text\n```\n3 * 3\n```\nThe End.
</body></html>\n"

html_to_r(rmarkdown_html)

},

"1 * 1\n\n2 * 2\n\n3 * 3")
)