-
Notifications
You must be signed in to change notification settings - Fork 5
feature/xml2 port #26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
LiNk-NY
wants to merge
6
commits into
devel
Choose a base branch
from
feature/xml2_port
base: devel
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
86654e6
migrate XML to xml2
LiNk-NY d7a9acf
add unit tests for getHtmlTitle, getDocumentTitles, and getVignetteIn…
LiNk-NY c38a3db
support accumulating text when using xml2::xml_set_text
LiNk-NY 5e9d56c
additional text accumulation changes
LiNk-NY 1093983
add opening bracket
LiNk-NY 7dad419
add importFrom in NAMESPACE and remove qualified calls
LiNk-NY File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,193 @@ | ||
| ## xml2_helpers.R | ||
| ## | ||
| ## Compatibility wrappers that replicate the XML package's stateful DOM builder | ||
| ## (xmlOutputDOM / xmlTree) and xmlNode / saveXML on top of xml2. | ||
| ## | ||
| ## The original code used three XML idioms: | ||
| ## | ||
| ## 1. xmlOutputDOM(tag, attrs) / xmlTree(tag) -- stateful builder | ||
| ## Builder methods: $addTag(), $addNode(), $closeTag(), $value() | ||
| ## | ||
| ## 2. xmlNode(tag, ..., attrs) -- create a standalone xml2 node | ||
| ## | ||
| ## 3. saveXML(node, file, prefix="") -- serialize to file or string | ||
| ## | ||
| ## All three are re-implemented below so that the rest of the source code | ||
| ## needs only minimal mechanical edits. | ||
|
|
||
| ## --------------------------------------------------------------------------- | ||
| ## 1. xmlNode() replacement | ||
| ## --------------------------------------------------------------------------- | ||
| ## XML::xmlNode(name, ..., attrs = NULL) | ||
| ## children / text content are passed as un-named or named '...' arguments. | ||
| ## 'attrs' is a named character vector. | ||
| ## | ||
| ## We return an xml2 node (xml_node). Because xml2 builds nodes as part of a | ||
| ## document, we keep a lightweight parent document. | ||
|
|
||
| xmlNode <- function(name, ..., attrs = NULL) { | ||
| doc <- xml_new_document() | ||
| root <- xml_add_child(doc, name) | ||
|
|
||
| if (!is.null(attrs) && length(attrs) > 0) { | ||
| for (nm in names(attrs)) | ||
| xml_attr(root, nm) <- attrs[[nm]] | ||
| } | ||
|
|
||
| args <- list(...) | ||
| ## If there is only a single character child and no xml_node children, | ||
| ## use xml_set_text (produces a plain text node). When the args are mixed | ||
| ## (character + xml_node), wrap bare character strings in <span> so that | ||
| ## inline separators (e.g. ", ") are preserved in the HTML output. | ||
| node_children <- vapply(args, inherits, logical(1), "xml_node") | ||
| mixed <- any(node_children) && any(!node_children) | ||
|
|
||
| txt_acc <- character() | ||
| for (child in args) { | ||
| if (inherits(child, "xml_node")) { | ||
| xml_add_child(root, child) | ||
| } else if (is.character(child)) { | ||
| txt <- paste0(child, collapse = "") | ||
| if (nzchar(txt)) { | ||
| if (mixed) { | ||
| sp <- xml_add_child(root, "span") | ||
| xml_set_text(sp, txt) | ||
| } else { | ||
| txt_acc <- c(txt_acc, txt) | ||
| } | ||
| } | ||
| } | ||
| } | ||
| if (!mixed && length(txt_acc)) { | ||
| xml_set_text(root, paste0(txt_acc, collapse = "")) | ||
| } | ||
| root | ||
| } | ||
|
|
||
| ## --------------------------------------------------------------------------- | ||
| ## 2. xmlOutputDOM() / xmlTree() replacement | ||
| ## --------------------------------------------------------------------------- | ||
| ## Both functions return an environment that exposes: | ||
| ## $addTag(name, ..., attrs, close) -- open (and optionally close) a tag | ||
| ## $addNode(node) -- append an xml_node child | ||
| ## $closeTag() -- close the most-recently opened tag | ||
| ## $value() -- return the root xml_node | ||
| ## | ||
| ## The stateful part is a stack of open nodes held in the environment. | ||
|
|
||
| .makeXmlDomBuilder <- function(rootTag, attrs = NULL) { | ||
| doc <- xml_new_document() | ||
| root <- xml_add_child(doc, rootTag) | ||
|
|
||
| if (!is.null(attrs) && length(attrs)) { | ||
| for (nm in names(attrs)) | ||
| xml_attr(root, nm) <- attrs[[nm]] | ||
| } | ||
|
|
||
| ## stack: top of stack is the currently open node | ||
| stack <- list(root) | ||
|
|
||
| current <- function() stack[[length(stack)]] | ||
| push <- function(n) stack[[length(stack) + 1L]] <<- n | ||
| pop <- function() stack[[length(stack)]] <<- NULL | ||
|
|
||
| ## addTag(name, text?, attrs = NULL, close = TRUE) | ||
| addTag <- function(name, ..., attrs = NULL, close = TRUE) { | ||
| node <- xml_add_child(current(), name) | ||
|
|
||
| if (!is.null(attrs) && length(attrs) > 0) { | ||
| for (nm in names(attrs)) | ||
| xml_attr(node, nm) <- attrs[[nm]] | ||
| } | ||
|
|
||
| args <- list(...) | ||
| node_children <- vapply(args, inherits, logical(1), "xml_node") | ||
| mixed <- any(node_children) && any(!node_children) | ||
|
|
||
| txt_acc <- character() | ||
| for (child in args) { | ||
| if (inherits(child, "xml_node")) { | ||
| xml_add_child(node, child) | ||
| } else if (is.character(child)) { | ||
| txt <- paste0(child, collapse = "") | ||
| if (nzchar(txt)) { | ||
| if (mixed) { | ||
| sp <- xml_add_child(node, "span") | ||
| xml_set_text(sp, txt) | ||
| } else { | ||
| txt_acc <- c(txt_acc, txt) | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
Copilot marked this conversation as resolved.
|
||
| if (!mixed && length(txt_acc)) | ||
| xml_set_text(node, paste0(txt_acc, collapse = "")) | ||
|
|
||
| if (!close) { | ||
| push(node) | ||
| } | ||
| invisible(NULL) | ||
| } | ||
|
|
||
| ## addNode(node) -- append a pre-built xml_node | ||
| addNode <- function(node) { | ||
| if (inherits(node, "xml_node")) { | ||
| xml_add_child(current(), node) | ||
| } | ||
| invisible(NULL) | ||
| } | ||
|
|
||
| ## closeTag() -- pop the stack (close the most-recently opened tag) | ||
| closeTag <- function() { | ||
| if (length(stack) > 1L) | ||
| pop() | ||
| invisible(NULL) | ||
| } | ||
|
|
||
| ## value() -- return the root node | ||
| value <- function() root | ||
|
|
||
| list( | ||
| addTag = addTag, | ||
| addNode = addNode, | ||
| closeTag = closeTag, | ||
| value = value | ||
| ) | ||
| } | ||
|
|
||
| xmlOutputDOM <- function(tag = "doc", attrs = NULL, ...) { | ||
| .makeXmlDomBuilder(tag, attrs) | ||
| } | ||
|
|
||
| ## xmlTree is used identically to xmlOutputDOM in biocViews | ||
| xmlTree <- function(tag = "doc", attrs = NULL, ...) { | ||
| .makeXmlDomBuilder(tag, attrs) | ||
| } | ||
|
|
||
| ## --------------------------------------------------------------------------- | ||
| ## 3. saveXML() replacement | ||
| ## --------------------------------------------------------------------------- | ||
| ## XML::saveXML(doc, file = NULL, prefix = "<?xml...>", ...) | ||
| ## * When 'file' is NULL/missing, returns the serialised string. | ||
| ## * When 'file' is a connection or path, writes to it. | ||
| ## * 'prefix' is prepended to the output (used for DOCTYPE in biocViews). | ||
| ## | ||
| ## xml2::as_xml_document() / xml2::write_html() / xml2::write_xml() are used. | ||
|
|
||
| saveXML <- function(doc, file = NULL, prefix = "", ...) { | ||
| if (!inherits(doc, "xml_node")) | ||
| stop("saveXML: 'doc' must be an xml_node") | ||
|
|
||
| txt <- as.character(doc) | ||
|
|
||
| ## Prepend any requested prefix (e.g. DOCTYPE declaration) | ||
| if (nzchar(prefix)) | ||
| txt <- paste0(prefix, "\n", txt) | ||
|
|
||
| if (is.null(file)) { | ||
| return(txt) | ||
| } else { | ||
| writeLines(txt, con = file, sep = "") | ||
| invisible(txt) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| --- | ||
| title: "The Vignette Package" | ||
| author: "Vignette Author" | ||
| date: "`r format(Sys.Date(), '%A, %B %d, %Y')`" | ||
| always_allow_html: yes | ||
| output: | ||
| BiocStyle::html_document: | ||
| df_print: paged | ||
| toc_float: true | ||
| vignette: > | ||
| %\VignetteIndexEntry{Vignette for beginners} | ||
| %\VignetteEngine{knitr::rmarkdown} | ||
| %\VignetteEncoding{UTF-8} | ||
| --- | ||
|
|
||
| ```{r init, results='hide', echo=FALSE, warning=FALSE, message=FALSE} | ||
| library(knitr) | ||
| opts_chunk$set(warning=FALSE, message=FALSE) | ||
| ``` | ||
|
|
||
|
|
||
| # What is a vignette? | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| <!DOCTYPE html> | ||
|
|
||
| <html> | ||
| <head> | ||
| <title>The Vignette Package</title> | ||
| </head> | ||
| </html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| test_getHtmlTitle <- function() { | ||
| doc <- system.file( | ||
| "htmlfrags", | ||
| "topfrag.html", | ||
| package = "biocViews", | ||
| mustWork = TRUE | ||
| ) | ||
| src <- system.file( | ||
| "extdata", | ||
| "vignette.Rmd", | ||
| package = "biocViews", | ||
| mustWork = TRUE | ||
| ) | ||
| checkIdentical( | ||
| getVignetteIndexEntry(src), | ||
| "Vignette for beginners" | ||
| ) | ||
| checkIdentical( | ||
| getHtmlTitle(doc, src), | ||
| "Vignette for beginners" | ||
| ) | ||
| checkIdentical( | ||
| suppressWarnings({ | ||
| getHtmlTitle(doc, "") | ||
| }), | ||
| "Bioconductor Task View: top level views" | ||
| ) | ||
| } | ||
|
|
||
| test_getDocumentTitles <- function() { | ||
| htmlDocs <- system.file("extdata", package = "biocViews") |> | ||
| list.files(pattern = "\\.[Hh][Tt][Mm][Ll]$", full.names = TRUE) | ||
|
LiNk-NY marked this conversation as resolved.
|
||
|
|
||
| checkIdentical( | ||
| getDocumentTitles( | ||
| basename(htmlDocs), | ||
| ext = "html", | ||
| src = c("Rmd", "Rhtml"), | ||
| dirname(htmlDocs), | ||
| getHtmlTitle | ||
| ), | ||
| "Vignette for beginners" | ||
| ) | ||
| } | ||
|
|
||
| test_getVignetteIndexEntry <- function() { | ||
| rmdDocs <- system.file("extdata", package = "biocViews") |> | ||
| list.files(pattern = "\\.[Rr][Mm][Dd]$", full.names = TRUE) | ||
|
LiNk-NY marked this conversation as resolved.
|
||
|
|
||
| checkIdentical( | ||
| getVignetteIndexEntry(rmdDocs), | ||
| "Vignette for beginners" | ||
| ) | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.