R/html2Rmd.R

#' transfer html with R trunks into Rmd using pandoc
#' @param url the url
#' @param encode the page's encoding
#' @param content_node the main article node
#' @return greeting
#' @export
#' @examples
#' url <- "https://www.brodrigues.co/blog/2018-12-27-fun_gganimate/#"
#' html2Rmd(url,content_node = "div.col-lg-9")

html2Rmd  <- function(url, content_node,encodes = "utf-8")
{
	library(rvest)
	pg <- xml2::read_html(url, encoding = encodes)
	xmlfile <- rvest::html_node(pg, content_node)
	tfile <- "~/temp.xml"
	date <- lubridate::today() %>% format("%Y%m%d")
	outmd <- glue::glue("~/out-{date}.md")
	outrmd <- glue::glue("~/out-{date}.Rmd")

	if (file.exists(outrmd)) file.rename(outrmd,glue::glue("~/out-{date}-old.Rmd") )

	xml2::write_xml(xmlfile, file = tfile, encoding = encodes)
	system(glue::glue("pandoc -f html -t markdown -o {outmd} {tfile}"))
	file.remove(tfile)
	file.rename(outmd, outrmd)
	system(glue::glue("perl -pi -w -e 's/\\.r/r/g;' {outrmd}"))
	return("check the file in cwd and modify it.")
}
Gabegit/gmtools documentation built on May 6, 2019, 5:32 p.m.