- Return HTML directly, drop rmarkdown - Improve classes and specificity of rules for result styling - Improve font size of full page regex resultspull/21/head
| miniUI, | miniUI, | ||||
| purrr, | purrr, | ||||
| rlang, | rlang, | ||||
| rmarkdown, | |||||
| rstudioapi, | rstudioapi, | ||||
| shiny (>= 0.13), | shiny (>= 0.13), | ||||
| stringi, | stringi, |
| #' @inheritParams view_regex | #' @inheritParams view_regex | ||||
| #' @keywords internal | #' @keywords internal | ||||
| wrap_result <- function(x, escape = FALSE, exact = FALSE) { | wrap_result <- function(x, escape = FALSE, exact = FALSE) { | ||||
| if (is.null(x$idx[[1]])) return(if (escape) escape_html(x$text) else x$text) | |||||
| if (is.null(x$idx[[1]])) { | |||||
| return(if (escape) escape_html(x$text) else x$text) | |||||
| } | |||||
| text <- x$text | text <- x$text | ||||
| inserts <- x$idx | inserts <- x$idx | ||||
| #' | #' | ||||
| #' @param text Text to search | #' @param text Text to search | ||||
| #' @param pattern Regex pattern to look for | #' @param pattern Regex pattern to look for | ||||
| #' @param render Render results to an HTML doc and open in RStudio viewer? | |||||
| #' @param render Render results as HTML? | |||||
| #' @param escape Escape HTML-related characters in `text`? | #' @param escape Escape HTML-related characters in `text`? | ||||
| #' @param exact Should the regex pattern be displayed as entered by the user | #' @param exact Should the regex pattern be displayed as entered by the user | ||||
| #' into R console or source (default)? When `TRUE`, regex is displayed with | #' into R console or source (default)? When `TRUE`, regex is displayed with | ||||
| result_pad <- sprintf("pad%02d", max_pad_level - 3) | result_pad <- sprintf("pad%02d", max_pad_level - 3) | ||||
| } | } | ||||
| } | } | ||||
| paste('<p class="results', result_pad, '">', resi, "</p>") | |||||
| paste('<p class="regexplain', result_pad, '">', resi, "</p>") | |||||
| }) | }) | ||||
| res <- paste(res, collapse = "") | res <- paste(res, collapse = "") | ||||
| if (!nchar(pattern)) res <- paste("<p class='results'>", text, "</p>") | |||||
| if (!nchar(pattern)) res <- paste("<p class='regexplain'>", text, "</p>") | |||||
| if (knitr) { | if (knitr) { | ||||
| # embed css | |||||
| group_css <- htmltools::htmlDependency( | |||||
| name = "regexplain-groups", version = packageVersion("regexplain"), | |||||
| src = system.file("styles", package = "regexplain"), | |||||
| stylesheet = "groups.css") | |||||
| res <- htmltools::attachDependencies(htmltools::HTML(res), group_css) | |||||
| return(res) | |||||
| return( | |||||
| htmltools::tagList( | |||||
| htmltools::HTML(res), | |||||
| regexplain_dependencies(full = FALSE) | |||||
| ) | |||||
| ) | |||||
| } | } | ||||
| if (!render) return(res) | if (!render) return(res) | ||||
| head <- if (!result_only) c( | |||||
| "---", "pagetitle: View Regex", "---", | |||||
| "<h5>Pattern</h5>", | |||||
| "<p><pre>", wrap_regex(pattern, escape, exact), "</pre></p>", | |||||
| "<h5>Matches</h5>" | |||||
| ) | |||||
| res <- c(head, res) | |||||
| tmp <- tempfile(fileext = ".Rmd") | |||||
| cat(res, file = tmp, sep = "\n") | |||||
| tmp_html <- suppressWarnings( | |||||
| rmarkdown::render( | |||||
| tmp, | |||||
| output_format = rmarkdown::html_document( | |||||
| css = c(system.file("styles", 'skeleton.css', package='regexplain'), | |||||
| system.file("styles", 'view_regex.css', package='regexplain'), | |||||
| system.file("styles", 'groups.css', package='regexplain')), | |||||
| theme = NULL, | |||||
| md_extensions = "-autolink_bare_uris"), | |||||
| quiet = TRUE | |||||
| )) | |||||
| rstudioapi::viewer(tmp_html) | |||||
| page <- result_page(wrap_regex(pattern, escape, exact), res, "View Regex") | |||||
| htmltools::browsable(page) | |||||
| } | } | ||||
| deprecate_knitr_option <- function(...) { | deprecate_knitr_option <- function(...) { | ||||
| } | } | ||||
| regex_opts[setdiff(names(regex_opts), "knitr")] | regex_opts[setdiff(names(regex_opts), "knitr")] | ||||
| } | } | ||||
| result_page <- function(pattern, result, title = NULL) { | |||||
| pattern <- htmltools::HTML(pattern) | |||||
| result <- htmltools::HTML(result) | |||||
| page_title <- title | |||||
| htmltools::tagList( | |||||
| htmltools::div( | |||||
| class = "regexplain__result", | |||||
| if (!is.null(title)) htmltools::tags$head(htmltools::tags$title(page_title)), | |||||
| htmltools::h2("Pattern"), | |||||
| htmltools::pre(class = "regexplain__pattern", pattern), | |||||
| htmltools::h2("Matches"), | |||||
| result, | |||||
| regexplain_dependencies(full = TRUE) | |||||
| ) | |||||
| ) | |||||
| } | |||||
| regexplain_dependencies <- function(full = TRUE) { | |||||
| htmltools::htmlDependency( | |||||
| name = "regexplain", | |||||
| version = packageVersion("regexplain"), | |||||
| package = "regexplain", | |||||
| src = "styles", | |||||
| stylesheet = c( | |||||
| "groups.css", | |||||
| if (full) c("skeleton.css", "view_regex.css") | |||||
| ), | |||||
| all_files = FALSE | |||||
| ) | |||||
| } |
| .results { | |||||
| .regexplain { | |||||
| font-family: monospace; | font-family: monospace; | ||||
| color: #888888; | color: #888888; | ||||
| } | } | ||||
| .group { | |||||
| .regexplain .group { | |||||
| border-bottom: 2px solid; | border-bottom: 2px solid; | ||||
| color: black; | color: black; | ||||
| padding: 0px; | padding: 0px; | ||||
| } | } | ||||
| .g00 { | |||||
| .regexplain .group.g00, .regexplain__pattern .g00 { | |||||
| padding: 1px; | padding: 1px; | ||||
| background-color: #f0f0f0; | background-color: #f0f0f0; | ||||
| border: 1px solid #b0b0b0; | border: 1px solid #b0b0b0; | ||||
| border-right: 1px solid #b0b0b0; | border-right: 1px solid #b0b0b0; | ||||
| border-top: 1px solid #b0b0b0; | border-top: 1px solid #b0b0b0; | ||||
| } | } | ||||
| .g01 { | |||||
| .regexplain .group.g01, .regexplain__pattern .g01 { | |||||
| border-color: green; | border-color: green; | ||||
| color: green; | color: green; | ||||
| } | } | ||||
| .g02 { | |||||
| .regexplain .group.g02, .regexplain__pattern .g02 { | |||||
| border-color: blue; | border-color: blue; | ||||
| color: blue; | color: blue; | ||||
| } | } | ||||
| .g03 { | |||||
| .regexplain .group.g03, .regexplain__pattern .g03 { | |||||
| border-color: red; | border-color: red; | ||||
| color: red; | color: red; | ||||
| } | } | ||||
| .g04 { | |||||
| .regexplain .group.g04, .regexplain__pattern .g04 { | |||||
| border-color: orange; | border-color: orange; | ||||
| color: orange; | color: orange; | ||||
| } | } | ||||
| .g05 { | |||||
| .regexplain .group.g05, .regexplain__pattern .g05 { | |||||
| border-color: purple; | border-color: purple; | ||||
| color: purple; | color: purple; | ||||
| } | } | ||||
| .g06 { | |||||
| .regexplain .group.g06, .regexplain__pattern .g06 { | |||||
| border-color: DeepPink; | border-color: DeepPink; | ||||
| color: DeepPink; | color: DeepPink; | ||||
| } | } | ||||
| .g07 { | |||||
| .regexplain .group.g07, .regexplain__pattern .g07 { | |||||
| border-color: Tomato; | border-color: Tomato; | ||||
| color: Tomato; | color: Tomato; | ||||
| } | } | ||||
| .g08 { | |||||
| .regexplain .group.g08, .regexplain__pattern .g08 { | |||||
| border-color: DarkSeaGreen; | border-color: DarkSeaGreen; | ||||
| color: DarkSeaGreen; | color: DarkSeaGreen; | ||||
| } | } | ||||
| .g09 { | |||||
| .regexplain .group.g09, .regexplain__pattern .g09 { | |||||
| border-color: DeepSkyBlue; | border-color: DeepSkyBlue; | ||||
| color: DeepSkyBlue; | color: DeepSkyBlue; | ||||
| } | } | ||||
| .g10 { | |||||
| .regexplain .group.g10, .regexplain__pattern .g10 { | |||||
| border-color: Sienna; | border-color: Sienna; | ||||
| color: Sienna; | color: Sienna; | ||||
| } | } | ||||
| .pad01 { | |||||
| .regexplain .group.pad01 { | |||||
| padding-bottom: 3px; | padding-bottom: 3px; | ||||
| } | } | ||||
| .pad02 { | |||||
| .regexplain .group.pad02 { | |||||
| padding-bottom: 6px; | padding-bottom: 6px; | ||||
| } | } | ||||
| .pad03 { | |||||
| .regexplain .group.pad03 { | |||||
| padding-bottom: 9px; | padding-bottom: 9px; | ||||
| } | } | ||||
| .pad04 { | |||||
| .regexplain .group.pad04 { | |||||
| padding-bottom: 12px; | padding-bottom: 12px; | ||||
| } | } | ||||
| .pad05 { | |||||
| .regexplain .group.pad05 { | |||||
| padding-bottom: 15px; | padding-bottom: 15px; | ||||
| } | } | ||||
| .pad06 { | |||||
| .regexplain .group.pad06 { | |||||
| padding-bottom: 18px; | padding-bottom: 18px; | ||||
| margin-bottom: 18px; | margin-bottom: 18px; | ||||
| } | } | ||||
| .pad07 { | |||||
| .regexplain .group.pad07 { | |||||
| padding-bottom: 21px; | padding-bottom: 21px; | ||||
| margin-bottom: 21px; | margin-bottom: 21px; | ||||
| } | } | ||||
| .pad08 { | |||||
| .regexplain .group.pad08 { | |||||
| padding-bottom: 23px; | padding-bottom: 23px; | ||||
| margin-bottom: 23px; | margin-bottom: 23px; | ||||
| } | } | ||||
| .pad09 { | |||||
| .regexplain .group.pad09 { | |||||
| padding-bottom: 25px; | padding-bottom: 25px; | ||||
| margin-bottom: 25px; | margin-bottom: 25px; | ||||
| } | } |
| ul, | ul, | ||||
| ol, | ol, | ||||
| form { | form { | ||||
| margin-bottom: 1rem; } | |||||
| margin-bottom: 1rem; | |||||
| font-size: 20px; | |||||
| } | |||||
| body { | body { | ||||
| line-height: 18px;} | |||||
| line-height: 18px; | |||||
| font-size: 20px; | |||||
| } |
| \sQuote{Details}.} | \sQuote{Details}.} | ||||
| }} | }} | ||||
| \item{render}{Render results to an HTML doc and open in RStudio viewer?} | |||||
| \item{render}{Render results as HTML?} | |||||
| \item{escape}{Escape HTML-related characters in \code{text}?} | \item{escape}{Escape HTML-related characters in \code{text}?} | ||||
| expect_equal(unique(m[[1]]$idx$pass), c(1L, 2L)) | expect_equal(unique(m[[1]]$idx$pass), c(1L, 2L)) | ||||
| }) | }) | ||||
| test_that("view_regex generall works", { | |||||
| result <- "<p class=\"results \"> <span class=\"group g00\"><span class=\"group g01\">t</span>e</span><span class=\"group g00\"><span class=\"group g01\">s</span>t</span> </p>" | |||||
| test_that("view_regex generally works", { | |||||
| result <- "<p class=\"regexplain \"> <span class=\"group g00\"><span class=\"group g01\">t</span>e</span><span class=\"group g00\"><span class=\"group g01\">s</span>t</span> </p>" | |||||
| expect_equal(view_regex("test", "(\\w)\\w", render = FALSE), result) | expect_equal(view_regex("test", "(\\w)\\w", render = FALSE), result) | ||||
| }) | }) |