Skip to content

Commit d8ac20f

Browse files
author
Wang
committed
add //pagenumver_hardcoding
1 parent b478e07 commit d8ac20f

4 files changed

Lines changed: 93 additions & 4 deletions

File tree

R/conversion.R

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,8 @@ convert <- function(text,
8585
"\n" = "\\line ",
8686
"\\pagenumber" = "\\chpgn ",
8787
"\\totalpage" = "\\totalpage",
88-
"\\pagefield" = "{\\field{\\*\\fldinst NUMPAGES }} "
88+
"\\pagefield" = "{\\field{\\*\\fldinst NUMPAGES }} ",
89+
"\\pagenumber_hardcoding" = "\\pgnhardcoding"
8990
)
9091

9192
# Define Pattern for latex code

R/rtf_encode_table.R

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,10 +151,13 @@ rtf_encode_table <- function(tbl, verbose = FALSE) {
151151
sep = "\n"
152152
)
153153

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

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

159162
end <- as_rtf_end()
160163
if (verbose) {

tests/testthat/_snaps/independent-testing-rtf_encode_table.md

Lines changed: 12 additions & 0 deletions
Large diffs are not rendered by default.

tests/testthat/test-independent-testing-rtf_encode_table.R

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,3 +345,76 @@ test_that("Test case when using subline_by, page_by, group_by simultaneously wit
345345
)
346346
expect_true(tmp[[length(tmp)]])
347347
})
348+
349+
test_that("Test pagenumber_hardcoding with multi-page table", {
350+
tbl <- iris[1:100, ] |>
351+
rtf_title("Table: Iris Data (\\pagenumber_hardcoding/\\totalpage)") |>
352+
rtf_colheader("Sepal.Length | Sepal.Width | Petal.Length | Petal.Width | Species") |>
353+
rtf_body(col_rel_width = rep(1, 5))
354+
355+
encoded <- rtf_encode_table(tbl)
356+
357+
expect_snapshot_output(encoded)
358+
})
359+
360+
test_that("Test pagenumber_hardcoding replaces correctly per page", {
361+
tbl <- iris[1:100, ] |>
362+
rtf_title("Table (\\pagenumber_hardcoding/\\totalpage)") |>
363+
rtf_body(col_rel_width = rep(1, 5))
364+
365+
encoded <- rtf_encode_table(tbl)
366+
367+
pages <- strsplit(encoded$body, "\\\\page")[[1]]
368+
n_pages <- length(pages)
369+
370+
for (i in seq_len(n_pages)) {
371+
expect_true(grepl(as.character(i), pages[i], fixed = TRUE))
372+
expect_true(grepl(as.character(n_pages), pages[i], fixed = TRUE))
373+
}
374+
})
375+
376+
test_that("Test pagenumber_hardcoding in page header", {
377+
tbl <- iris[1:100, ] |>
378+
rtf_page() |>
379+
rtf_body() |>
380+
rtf_page_header(text = "Page \\pagenumber_hardcoding of \\totalpage")
381+
382+
encoded <- rtf_encode_table(tbl)
383+
384+
pages <- strsplit(encoded$body, "\\\\page")[[1]]
385+
n_pages <- length(pages)
386+
387+
for (i in seq_len(min(3, n_pages))) {
388+
expect_true(grepl(paste0("Page ", i), pages[i]))
389+
expect_true(grepl(paste0("of ", n_pages), pages[i]))
390+
}
391+
})
392+
393+
test_that("Test pagenumber_hardcoding with totalpage in footnote", {
394+
tbl <- iris[1:100, ] |>
395+
rtf_body(col_rel_width = rep(1, 5)) |>
396+
rtf_footnote("Page \\pagenumber_hardcoding of \\totalpage")
397+
398+
attr(tbl, "page")$page_footnote <- "all"
399+
400+
encoded <- rtf_encode_table(tbl)
401+
402+
pages <- strsplit(encoded$body, "\\\\page")[[1]]
403+
n_pages <- length(pages)
404+
405+
for (i in seq_len(min(3, n_pages))) {
406+
expect_true(grepl(as.character(i), pages[i]))
407+
expect_true(grepl(as.character(n_pages), pages[i]))
408+
}
409+
})
410+
411+
test_that("Test pagenumber_hardcoding survives without extra spaces", {
412+
tbl <- iris[1:50, ] |>
413+
rtf_title("Test (\\pagenumber_hardcoding/\\totalpage)") |>
414+
rtf_body()
415+
416+
encoded <- rtf_encode_table(tbl)
417+
418+
expect_false(grepl("\\\\totalpage ", encoded$body, fixed = TRUE))
419+
expect_false(grepl("\\\\pgnhardcoding", encoded$body, fixed = TRUE))
420+
})

0 commit comments

Comments
 (0)