R/insert_paragraphs.R

Defines functions insert_paragraphs

Documented in insert_paragraphs

#' Insert each element of a vector into a document as a string of paragraphs.
#'
#' @param denv The environment of the input docx object.
#' @param vec The character vector to be inserted. Each element should
#' correspond to a separate paragraph.
#'
#' @examples
#' \dontrun{
#' insert_paragraphs(obj, vec)
#' }
insert_paragraphs <- function(
  denv,
  vec
){
  # Append each element.
  vec <- unlist(vec, recursive = TRUE)
  valid_elements <- gsub("[[:space:]]|\n|\n\r|\r", "", vec) != ""
  vec <- vec[valid_elements]
  vec <- gsub("\n$|\n\r$", "", vec)
  vec <- vec[vec != ""]


  for(i in rev(vec)){
    officer::cursor_end(denv$docx)
    cursor_pos <- officer::docx_current_block_xml(denv$docx)
    # Retrieve text.
    string <- i

    # Create xml.
    # new_string <- paste0(
    #   '<w:p xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/',
    #   '2006/main\" xmlns:wp=\"http://schemas.openxmlformats.org/drawingml/',
    #   '2006/wordprocessingDrawing\" xmlns:r=\"http://schemas.openxmlformats.',
    #   'org/officeDocument/2006/relationships\" xmlns:w14=\"http://schemas.',
    #   'microsoft.com/office/word/2010/wordml\">',
    #   htmltools::htmlEscape(string),
    #   '</w:p>'
    # )
    # new_string <- paste0(
    #   '<w:p xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/',
    #   '2006/main\" >',
    #   htmltools::htmlEscape(string),
    #   '</w:p>'
    # )

    # Update obj.
    # tryCatch(
    #   {
    #     new_string <- iconv(new_string, to = "latin1")
    #     new_string <- xml2::as_xml_document(new_string)
    #     xml2::xml_add_sibling(
    #       cursor_pos,
    #       new_string,
    #       .where = "after",
    #       .copy = TRUE
    #     )
    #   },
    #   error = function(e){}
    # )

    officer::body_add_par(
      denv$docx,
      htmltools::htmlEscape(htmltools::htmlEscape(string)),
      style = "Normal"
    )
  }


  # Return nothing.
}
MAMC-DCI/documenter documentation built on Jan. 5, 2023, 4:04 p.m.