r
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

39 lines
1.2KB

  1. #' Prints the body of Office Open XML generated when converting markdown
  2. #' to docx via pandoc
  3. #'
  4. #' Intended as a developer function for creating docx-generating code.
  5. #' Will usually include some components unneccessary for generating open xml
  6. #' elements, such as bookmarks and `<w:sectPr>` elements
  7. #' @importFrom xml2 read_xml xml_find_first xml_children xml_remove xml_find_all
  8. #' @importFrom rmarkdown pandoc_convert
  9. #' @noRd
  10. md_to_openxml <- function(text, simplify = TRUE,
  11. remove_bookmarks = simplify,
  12. remove_secs = simplify) {
  13. tmpf <- tempfile(fileext = ".md")
  14. cat(text, file = tmpf)
  15. tmpw <- tempfile(fileext = ".docx")
  16. pandoc_convert(tmpf, to = "docx", from = "markdown", output = tmpw)
  17. oml <- (
  18. xml_find_first(
  19. read_xml(unz(tmpw, filename = "word/document.xml")),
  20. "//w:body"
  21. )
  22. )
  23. if (remove_bookmarks) {
  24. xml_remove(xml_find_all(oml, "//w:bookmarkStart | //w:bookmarkEnd"))
  25. }
  26. if (remove_secs) {
  27. xml_remove(xml_find_all(oml, "//w:sectPr"))
  28. }
  29. out <- paste(as.character(xml_children(oml)), collapse = "\n")
  30. unlink(c(tmpf, tmpw))
  31. class(out) <- "xmltext"
  32. out
  33. }
  34. print.xmltext <- function(x) {
  35. cat(x)
  36. }