Skip to content

Commit 1d13cbc

Browse files
committed
feat: add write_html function and tests for converting RTF to HTML using LibreOffice.
1 parent cfbca58 commit 1d13cbc

4 files changed

Lines changed: 149 additions & 0 deletions

File tree

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export(rtf_subline)
1818
export(rtf_title)
1919
export(utf8Tortf)
2020
export(write_docx)
21+
export(write_html)
2122
export(write_rtf)
2223
importFrom(grDevices,colors)
2324
importFrom(utils,tail)

R/write_rtf.R

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,3 +130,61 @@ write_docx <- function(rtf, file) {
130130

131131
invisible(file)
132132
}
133+
134+
#' Write an RTF Table or Figure to an HTML File
135+
#'
136+
#' @description
137+
#' The write_html function writes an RTF encoding string to an .html file
138+
#' by first writing to a temporary RTF file and then converting it to HTML
139+
#' using LibreOffice.
140+
#'
141+
#' @section Specification:
142+
#' \if{latex}{
143+
#' \itemize{
144+
#' \item Write RTF encoding to a temporary RTF file.
145+
#' \item Convert RTF to HTML using LibreOffice command-line tool via \code{rtf_convert_format()}.
146+
#' }
147+
#' }
148+
#' \if{html}{The contents of this section are shown in PDF user manual only.}
149+
#'
150+
#' @param rtf A character rtf encoding string rendered by `rtf_encode()`.
151+
#' @param file A character string naming a file to save html file.
152+
#'
153+
#' @details
154+
#' This function requires LibreOffice to be installed on the system.
155+
#' The function uses the internal \code{rtf_convert_format()} function
156+
#' to perform the conversion from RTF to HTML format.
157+
#'
158+
#' Currently only Unix/Linux/macOS systems are supported.
159+
#'
160+
#' @export
161+
write_html <- function(rtf, file) {
162+
# Normalize the output file path
163+
file <- normalizePath(file, mustWork = FALSE)
164+
165+
# Create output directory if it doesn't exist
166+
output_dir <- dirname(file)
167+
if (!dir.exists(output_dir)) {
168+
dir.create(output_dir, recursive = TRUE)
169+
}
170+
171+
# Create a temporary RTF file
172+
tmpdir <- tempfile(pattern = "r2rtf_")
173+
dir.create(tmpdir)
174+
on.exit(unlink(tmpdir, recursive = TRUE), add = TRUE)
175+
176+
file_basename <- tools::file_path_sans_ext(basename(file))
177+
rtf_path <- file.path(tmpdir, paste0(file_basename, ".rtf"))
178+
write_rtf(rtf, rtf_path)
179+
180+
# Convert RTF to HTML using existing rtf_convert_format function
181+
rtf_convert_format(
182+
input = rtf_path,
183+
output_file = basename(file),
184+
output_dir = output_dir,
185+
format = "html",
186+
overwrite = TRUE
187+
)
188+
189+
invisible(file)
190+
}

man/write_html.Rd

Lines changed: 36 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,3 +113,57 @@ test_that("write_docx fails gracefully when LibreOffice is not available", {
113113
"Can't find libreoffice"
114114
)
115115
})
116+
117+
test_that("write_html creates HTML file from RTF encoding", {
118+
skip_on_os("windows") # rtf_convert_format only supports Unix/Linux/macOS
119+
skip_if_not(
120+
nzchar(Sys.which("libreoffice")) ||
121+
nzchar(Sys.which("libreoffice24.8")) ||
122+
nzchar(Sys.which("libreoffice7.6")),
123+
"LibreOffice is not installed"
124+
)
125+
126+
# Create RTF encoding
127+
rtf <- head(iris) |>
128+
rtf_body() |>
129+
rtf_encode()
130+
131+
# Write to HTML
132+
html_file <- file.path(tempdir(), "test_table.html")
133+
result <- write_html(rtf, html_file)
134+
135+
# Verify file was created
136+
expect_true(file.exists(html_file))
137+
expect_equal(result, html_file)
138+
139+
# Clean up
140+
unlink(html_file)
141+
})
142+
143+
test_that("write_html creates output directory if needed", {
144+
skip_on_os("windows") # rtf_convert_format only supports Unix/Linux/macOS
145+
skip_if_not(
146+
nzchar(Sys.which("libreoffice")) ||
147+
nzchar(Sys.which("libreoffice24.8")) ||
148+
nzchar(Sys.which("libreoffice7.6")),
149+
"LibreOffice is not installed"
150+
)
151+
152+
# Create RTF encoding
153+
rtf <- head(cars) |>
154+
rtf_body() |>
155+
rtf_encode()
156+
157+
# Create nested path
158+
nested_path <- file.path(tempdir(), "test_html_output", "nested", "test.html")
159+
160+
# Write to HTML
161+
result <- write_html(rtf, nested_path)
162+
163+
# Verify directory and file were created
164+
expect_true(dir.exists(dirname(nested_path)))
165+
expect_true(file.exists(nested_path))
166+
167+
# Clean up
168+
unlink(file.path(tempdir(), "test_html_output"), recursive = TRUE)
169+
})

0 commit comments

Comments
 (0)