Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion R/conversion.R
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ convert <- function(text,
"\n" = "\\line ",
"\\pagenumber" = "\\chpgn ",
"\\totalpage" = "\\totalpage",
"\\pagefield" = "{\\field{\\*\\fldinst NUMPAGES }} "
"\\pagefield" = "{\\field{\\*\\fldinst NUMPAGES }} ",
"\\pagenumber_hardcoding" = "\\pgnhardcoding"
)

# Define Pattern for latex code
Expand Down
9 changes: 6 additions & 3 deletions R/rtf_encode_table.R
Original file line number Diff line number Diff line change
Expand Up @@ -151,10 +151,13 @@ rtf_encode_table <- function(tbl, verbose = FALSE) {
sep = "\n"
)

rtf_feature <- paste(unlist(rtf_feature), collapse = "\n")
## Post Processing for page numbers (per page basis)
for (i in seq_len(n_page)) {
rtf_feature[i] <- gsub("\\pgnhardcoding", i, rtf_feature[i], fixed = TRUE)
rtf_feature[i] <- gsub("\\totalpage", n_page, rtf_feature[i], fixed = TRUE)
}

## Post Processing for total page number
rtf_feature <- gsub("\\totalpage", n_page, rtf_feature, fixed = TRUE) # total page number
rtf_feature <- paste(unlist(rtf_feature), collapse = "\n") # total page number

end <- as_rtf_end()
if (verbose) {
Expand Down
12 changes: 12 additions & 0 deletions tests/testthat/_snaps/independent-testing-rtf_encode_table.md

Large diffs are not rendered by default.

73 changes: 73 additions & 0 deletions tests/testthat/test-independent-testing-rtf_encode_table.R
Original file line number Diff line number Diff line change
Expand Up @@ -345,3 +345,76 @@ test_that("Test case when using subline_by, page_by, group_by simultaneously wit
)
expect_true(tmp[[length(tmp)]])
})

test_that("Test pagenumber_hardcoding with multi-page table", {
tbl <- iris[1:100, ] |>
rtf_title("Table: Iris Data (\\pagenumber_hardcoding/\\totalpage)") |>
rtf_colheader("Sepal.Length | Sepal.Width | Petal.Length | Petal.Width | Species") |>
rtf_body(col_rel_width = rep(1, 5))

encoded <- rtf_encode_table(tbl)

expect_snapshot_output(encoded)
})

test_that("Test pagenumber_hardcoding replaces correctly per page", {
tbl <- iris[1:100, ] |>
rtf_title("Table (\\pagenumber_hardcoding/\\totalpage)") |>
rtf_body(col_rel_width = rep(1, 5))

encoded <- rtf_encode_table(tbl)

pages <- strsplit(encoded$body, "\\\\page")[[1]]
n_pages <- length(pages)

for (i in seq_len(n_pages)) {
expect_true(grepl(as.character(i), pages[i], fixed = TRUE))
expect_true(grepl(as.character(n_pages), pages[i], fixed = TRUE))
}
})

test_that("Test pagenumber_hardcoding in page header", {
tbl <- iris[1:100, ] |>
rtf_page() |>
rtf_body() |>
rtf_page_header(text = "Page \\pagenumber_hardcoding of \\totalpage")

encoded <- rtf_encode_table(tbl)

pages <- strsplit(encoded$body, "\\\\page")[[1]]
n_pages <- length(pages)

for (i in seq_len(min(3, n_pages))) {
expect_true(grepl(paste0("Page ", i), pages[i]))
expect_true(grepl(paste0("of ", n_pages), pages[i]))
}
})

test_that("Test pagenumber_hardcoding with totalpage in footnote", {
tbl <- iris[1:100, ] |>
rtf_body(col_rel_width = rep(1, 5)) |>
rtf_footnote("Page \\pagenumber_hardcoding of \\totalpage")

attr(tbl, "page")$page_footnote <- "all"

encoded <- rtf_encode_table(tbl)

pages <- strsplit(encoded$body, "\\\\page")[[1]]
n_pages <- length(pages)

for (i in seq_len(min(3, n_pages))) {
expect_true(grepl(as.character(i), pages[i]))
expect_true(grepl(as.character(n_pages), pages[i]))
}
})

test_that("Test pagenumber_hardcoding survives without extra spaces", {
tbl <- iris[1:50, ] |>
rtf_title("Test (\\pagenumber_hardcoding/\\totalpage)") |>
rtf_body()

encoded <- rtf_encode_table(tbl)

expect_false(grepl("\\\\totalpage ", encoded$body, fixed = TRUE))
expect_false(grepl("\\\\pgnhardcoding", encoded$body, fixed = TRUE))
})