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.

62 lines
2.0KB

  1. # This should all move to officer/worded but using this as a playground
  2. #' @importFrom xml2 xml_find_first xml_add_child xml_set_attrs xml_child
  3. set_section_properties <- function(x, section = NULL, properties = NULL) {
  4. doc_obj <- x$doc_obj$get()
  5. if (is.null(section)) {
  6. sec <- xml_find_first(doc_obj, "/w:document/w:body/w:sectPr")
  7. if (inherits(sec, "xml_missing")) {
  8. sec <- xml_add_child(
  9. xml_find_first(doc_obj, "/w:document/w:body"),
  10. "w:sectPr"
  11. )
  12. }
  13. } else {
  14. sec <- section
  15. }
  16. for (i in seq_along(properties)) {
  17. node_name <- prepend_ns(names(properties)[i])
  18. property <- xml_child(sec, node_name)
  19. if (inherits(property, "xml_missing")) {
  20. property <- xml_add_child(sec, node_name)
  21. }
  22. names(properties[[i]]) <- prepend_ns(names(properties[[i]]))
  23. xml_set_attrs(property, properties[[i]])
  24. }
  25. x
  26. }
  27. set_body_margins <- function(x, mar = c(
  28. top = 1, bottom = 1, left = 1,
  29. right = 1, gutter = 0, header = 0,
  30. footer = 0
  31. )) {
  32. if (length(mar) == 1 && (is.null(names(mar)) || names(mar) == "all")) {
  33. mar <- c(
  34. top = mar, bottom = mar, left = mar, right = mar,
  35. gutter = 0, header = 0, footer = 0
  36. )
  37. }
  38. mar <- structure(as.character(mar * 1440), .Names = names(mar))
  39. set_section_properties(x, properties = list(pgMar = mar))
  40. x
  41. }
  42. set_body_linenumbers <- function(x, start = 1L, by = 1L, distance = NULL,
  43. restart = c("page", "section", "never")) {
  44. restart <- match.arg(restart)
  45. lnNumType <- c(
  46. start = as.character(as.integer(start)),
  47. countBy = as.character(as.integer(by)),
  48. restart = switch(restart,
  49. page = "newPage",
  50. section = "newSection",
  51. never = "continuous"
  52. )
  53. )
  54. if (!is.null(distance)) {
  55. lnNumType <- c(lnNumType, distance = as.character(distance * 1440))
  56. }
  57. set_section_properties(x, properties = list(lnNumType = lnNumType))
  58. x
  59. }