Skip to content

Commit

Permalink
Use val_child for all properties. val_child gains 'attr' parameter.
Browse files Browse the repository at this point in the history
* Make bold and italic booleans. Account for 0/1 and off/on.
  • Loading branch information
trekonom committed May 19, 2024
1 parent 47d683c commit fd37850
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 14 deletions.
36 changes: 24 additions & 12 deletions R/fortify_docx.R
Original file line number Diff line number Diff line change
Expand Up @@ -146,11 +146,23 @@ par_as_tibble <- function(node, styles, detailed = FALSE) {
par_data
}
#' @importFrom xml2 xml_has_attr
val_child <- function(node, child_path, default = NULL) {
val_child <- function(node, child_path, attr = "val", default = NULL) {
child_node <- xml_child(node, child_path)
if (inherits(child_node, "xml_missing")) return(NA_character_)
if (!xml_has_attr(child_node, "val")) default
else xml_attr(child_node, "val")
if (!xml_has_attr(child_node, attr)) default
else xml_attr(child_node, attr)
}

val_child_lgl <- function(node, child_path, attr = "val", default = NULL) {
val <- val_child(node = node, child_path = child_path, attr = attr, default = default)
if (is.na(val)) return(NA)
else (val %in% c("1", "on", "true"))
}

val_child_int <- function(node, child_path, attr = "val", default = NULL) {
as.integer(
val_child(node = node, child_path = child_path, attr = attr, default = default)
)
}

run_as_tibble <- function(node, styles) {
Expand All @@ -163,15 +175,15 @@ run_as_tibble <- function(node, styles) {
}
run_data <- data.frame(
text = xml_text(node),
bold = val_child(node, "w:rPr/w:b", default = TRUE),
italic = val_child(node, "w:rPr/w:i", default = TRUE),
underline = xml_attr(xml_child(node, "w:rPr/w:u"), "val"),
sz = as.integer(xml_attr(xml_child(node, "w:rPr/w:sz"), "val")),
szCs = as.integer(xml_attr(xml_child(node, "w:rPr/w:szCs"), "val")),
color = xml_attr(xml_child(node, "w:rPr/w:color"), "val"),
shading = xml_attr(xml_child(node, "w:rPr/w:shd"), "val"),
shading_color = xml_attr(xml_child(node, "w:rPr/w:shd"), "color"),
shading_fill = xml_attr(xml_child(node, "w:rPr/w:shd"), "fill"),
bold = val_child_lgl(node, "w:rPr/w:b", default = "true"),
italic = val_child_lgl(node, "w:rPr/w:i", default = "true"),
underline = val_child(node, "w:rPr/w:u"),
sz = val_child_int(node, "w:rPr/w:sz"),
szCs = val_child_int(node, "w:rPr/w:szCs"),
color = val_child(node, "w:rPr/w:color"),
shading = val_child(node, "w:rPr/w:shd"),
shading_color = val_child(node, "w:rPr/w:shd", attr = "color"),
shading_fill = val_child(node, "w:rPr/w:shd", attr = "fill"),
stringsAsFactors = FALSE
)

Expand Down
33 changes: 31 additions & 2 deletions tests/testthat/test-doc-summary.R
Original file line number Diff line number Diff line change
Expand Up @@ -96,16 +96,45 @@ test_that("detailed summary", {
)
)

xml_elt <- paste0(
officer:::wp_ns_yes,
"<w:pPr><w:pStyle w:val=\"Normal\"/></w:pPr>",
"<w:r><w:rPr></w:rPr><w:t>NA</w:t></w:r>",
"<w:r><w:rPr><w:b/><w:i/></w:rPr><w:t>toggle</w:t></w:r>",
"<w:r><w:rPr><w:b w:val=\"0\"/><w:i w:val=\"0\"/></w:rPr><w:t>0</w:t></w:r>",
"<w:r><w:rPr><w:b w:val=\"1\"/><w:i w:val=\"1\"/></w:rPr><w:t>1</w:t></w:r>",
"<w:r><w:rPr><w:b w:val=\"false\"/><w:i w:val=\"false\"/></w:rPr><w:t>false</w:t></w:r>",
"<w:r><w:rPr><w:b w:val=\"true\"/><w:i w:val=\"true\"/></w:rPr><w:t>true</w:t></w:r>",
"<w:r><w:rPr><w:b w:val=\"off\"/><w:i w:val=\"off\"/></w:rPr><w:t>off</w:t></w:r>",
"<w:r><w:rPr><w:b w:val=\"on\"/><w:i w:val=\"on\"/></w:rPr><w:t>on</w:t></w:r>",
"</w:p>"
)

doc <- officer:::body_add_xml(
x = doc, str = xml_elt
)

doc_sum <- docx_summary(doc, detailed = TRUE)

expect_true("run" %in% names(doc_sum))
expect_type(doc_sum$run, "list")
expect_equal(lengths(doc_sum$run), rep(11, 4))
expect_equal(sapply(doc_sum$run, nrow), c(3, 3, 1, 1))
expect_equal(lengths(doc_sum$run), rep(11, 5))
expect_equal(sapply(doc_sum$run, nrow), c(3, 3, 1, 1, 8))

expect_true(all(sapply(doc_sum$run$bold, is.logical)))
expect_true(all(sapply(doc_sum$run$italic, is.logical)))
expect_true(all(sapply(doc_sum$run$sz, is.integer)))
expect_true(all(sapply(doc_sum$run$szCs, is.integer)))
expect_true(all(sapply(doc_sum$run$underline, is_character)))
expect_true(all(sapply(doc_sum$run$color, is_character)))
expect_true(all(sapply(doc_sum$run$shading, is_character)))
expect_true(all(sapply(doc_sum$run$shading_color, is_character)))
expect_true(all(sapply(doc_sum$run$shading_fill, is_character)))
})




test_that("pptx summary", {
example_pptx <- system.file(package = "officer", "doc_examples/example.pptx")
doc <- read_pptx(path = example_pptx)
Expand Down

0 comments on commit fd37850

Please sign in to comment.