Skip to content
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

Improve show method; add nodelist show functions #108

Merged
merged 30 commits into from
Jun 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
909b5e9
first pass not working
zkamvar May 7, 2024
81e028d
add show methods
zkamvar May 8, 2024
da21d6a
add show_censor()
zkamvar May 8, 2024
e1723dd
update documentation
zkamvar May 8, 2024
311de29
fix tests for windows
zkamvar May 8, 2024
ed3e7e3
add to_md_vec
zkamvar May 9, 2024
7d1e0f4
update show method function names
zkamvar May 9, 2024
ecfc4b2
bump news
zkamvar May 9, 2024
269c5f7
fix news mistake
zkamvar May 9, 2024
bb0cccf
update snaps
zkamvar May 9, 2024
1fccb08
Merge branch 'main' into znk-add-show-arg
zkamvar May 9, 2024
fcbfbd6
remove dead code; test missing line
zkamvar May 9, 2024
96f59e3
Merge branch 'main' into znk-add-show-arg
zkamvar May 23, 2024
615b6a9
update test
zkamvar May 23, 2024
385c5b0
add an example for the show family
zkamvar May 23, 2024
b8293b8
include "rel" attribute for censorship
zkamvar May 23, 2024
6bde934
add warning for yarn class new pattern
zkamvar May 23, 2024
0cc72f4
update show tests to use example doc
zkamvar May 23, 2024
9af9619
update show documentation
zkamvar May 23, 2024
674430c
add stylesheet path to to_md_vec()
zkamvar May 23, 2024
89de85c
add md_vec method to yarn
zkamvar May 23, 2024
2061d00
test md_vec method
zkamvar May 23, 2024
ff81f40
bump news; redocument
zkamvar May 23, 2024
571d2f0
avoid utf-8 things
zkamvar May 23, 2024
1bfbee8
Merge branch 'main' into znk-add-show-arg
zkamvar May 27, 2024
66d2aa1
update and further document show funs
zkamvar Jun 14, 2024
b78bd4f
Merge branch 'main' into znk-add-show-arg
zkamvar Jun 14, 2024
60e79ee
fix class check xml_nodelist -> xml_nodeset
zkamvar Jun 14, 2024
19d0c62
Merge branch 'main' into znk-add-show-arg
zkamvar Jun 14, 2024
cda6afc
update documentation
zkamvar Jun 21, 2024
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
4 changes: 4 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@ export(get_protected)
export(md_ns)
export(protect_curly)
export(protect_math)
export(show_block)
export(show_censor)
export(show_list)
export(stylesheet)
export(to_md)
export(to_md_vec)
export(to_xml)
export(yarn)
importFrom(R6,R6Class)
Expand Down
11 changes: 11 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,17 @@

## NEW FEATURES

* `to_md_vec()` takes an xml node or nodelist and returns a character vector of
the markdown produced.
* `show_list()`, `show_block()`, and `show_censor()` will show the markdown
content of a node, nodelist, or list of nodes without needing to print the
entire document.
* `yarn$show()` method now gains the `lines` parameter, which allows you to
subset the output by the lines of text. A warning is produced if a stylesheet
is supplied in place of `lines`.
* `yarn$md_vec()` is a new method that will generate a character vector of
markdown elements from a query. This is a convenience method that uses
`xml2::xml_find_all()` and `to_md_vec()` in the background.
* `get_protected()` function (and yarn method) will return nodes which have
been protected in some way by {tinkr} via one of the `protect_` family of
functions. Adopting this pattern is preferred over using
Expand Down
69 changes: 50 additions & 19 deletions R/class-yarn.R
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ yarn <- R6::R6Class("yarn",

#' @description show the markdown contents on the screen
#'
#' @param lines a subset of elements to show. Defaults to `TRUE`, which
#' shows all lines of the output. This can be either logical or numeric.
#' @param stylesheet_path path to the xsl stylesheet to convert XML to markdown.
#' @return a character vector with one line for each line in the output
#' @examples
Expand All @@ -104,15 +106,30 @@ yarn <- R6::R6Class("yarn",
#' ex2$head(5)
#' ex2$tail(5)
#' ex2$show()
show = function(stylesheet_path = stylesheet() ) {
show_user(private$md_lines(stylesheet = stylesheet_path))
show = function(lines = TRUE, stylesheet_path = stylesheet()) {
if (is.character(lines) && length(lines) == 1 && file.exists(lines)) {
# when using {tinkr} < 0.3.0
stylesheet_path <- lines
lines <- TRUE
the_call <- match.call()
the_call$stylesheet_path <- the_call$lines
the_call$lines <- NULL
new_call <- capture.output(print(the_call))
rlang::warn(
c(
"!" = "In {tinkr} 0.3.0, the $show() method gains the `lines` argument as the first argument.",
"i" = "To remove this warning, use the following code:",
" " = new_call
),
call. = FALSE)
}
show_user(private$md_lines(stylesheet = stylesheet_path)[lines])
},

#' @description show the head of the markdown contents on the screen
#'
#' @param n the number of elements to show from the top. Negative numbers
#' @param stylesheet_path path to the xsl stylesheet to convert XML to markdown.
#' exclude lines from the bottom
#' @return a character vector with `n` elements
head = function(n = 6L, stylesheet_path = stylesheet()) {
show_user(head(private$md_lines(stylesheet = stylesheet_path), n))
Expand All @@ -122,13 +139,41 @@ yarn <- R6::R6Class("yarn",
#'
#' @param n the number of elements to show from the bottom. Negative numbers
#' @param stylesheet_path path to the xsl stylesheet to convert XML to markdown.
#' exclude lines from the top
#'
#' @return a character vector with `n` elements
tail = function(n = 6L, stylesheet_path = stylesheet()) {
show_user(tail(private$md_lines(stylesheet = stylesheet_path), n))
},

#' @description query and extract markdown elements
#'
#' @param xpath a valid XPath expression
#' @param stylesheet_path path to the xsl stylesheet to convert XML to markdown.
#'
#' @return a vector of markdown elements generated from the query
#' @seealso [to_md_vec()] for a way to generate the same vector from a
#' nodelist without a yarn object
#' @examples
#' path <- system.file("extdata", "example1.md", package = "tinkr")
#' ex <- tinkr::yarn$new(path)
#' # all headings
#' ex$md_vec(".//md:heading")
#' # all headings greater than level 3
#' ex$md_vec(".//md:heading[@level>3]")
#' # all links
#' ex$md_vec(".//md:link")
#' # all links that are part of lists
#' ex$md_vec(".//md:list//md:link")
#' # all code
#' ex$md_vec(".//md:code | .//md:code_block")
md_vec = function(xpath = NULL, stylesheet_path = stylesheet()) {
if (is.null(xpath)) {
return(NULL)
}
nodes <- xml2::xml_find_all(self$body, xpath, ns = self$ns)
return(to_md_vec(nodes, stylesheet_path))
},

#' @description add an arbitrary Markdown element to the document
#'
#' @param md a string of markdown formatted text.
Expand Down Expand Up @@ -232,21 +277,7 @@ yarn <- R6::R6Class("yarn",
encoding = "UTF-8",
# converts the document to markdown and separates the output into lines
md_lines = function(path = NULL, stylesheet = NULL) {
if (is.null(stylesheet)) {
md <- to_md(self, path)
} else {
md <- to_md(self, path, stylesheet)
}
if (!is.null(path) && !is.null(stylesheet)) {
return(md)
}
# Make sure that the yaml is not sitting on top of the first markdown line
if (length(md) == 2) {
md[1] <- paste0(md[1], "\n")
}
f <- textConnection(md)
on.exit(close(f))
readLines(f)
print_lines(self, path = path, stylesheet = stylesheet)
}
)
)
Loading
Loading