Sfoglia il codice sorgente

Embed view_regex in html documents easily

- Removed knitr parameter
- Add result_only (not in knitr)
- Soft deprecate kntir parameter
- Separate group.css from other css styles
tags/v0.2.2
Garrick Aden-Buie 7 anni fa
parent
commit
8e9f185f75
5 ha cambiato i file con 51 aggiunte e 43 eliminazioni
  1. +1
    -1
      R/regex_gadget.R
  2. +30
    -8
      R/run_regex.R
  3. +7
    -0
      inst/styles/gadget.css
  4. +0
    -34
      inst/styles/groups.css
  5. +13
    -0
      inst/styles/view_regex.css

+ 1
- 1
R/regex_gadget.R Vedi File

@@ -20,7 +20,7 @@ regex_gadget <- function(

# ---- UI ----
ui <- miniPage(
shiny::includeCSS(system.file("styles", "style.css", package = "regexplain")),
shiny::includeCSS(system.file("styles", "groups.css", package = "regexplain")),
shiny::includeCSS(system.file("styles", "gadget.css", package = "regexplain")),
gadgetTitleBar(
"RegExplain",

+ 30
- 8
R/run_regex.R Vedi File

@@ -176,13 +176,13 @@ wrap_regex <- function(pattern, escape = TRUE, exact = TRUE) {
#' @param pattern Regex pattern to look for
#' @param render Render results to an HTML doc and open in RStudio viewer?
#' @param escape Escape HTML-related characters in `text`?
#' @param knitr Print into knitr doc? If `TRUE`, marks text as `asis_output` and
#' sets `render = FALSE` and `escape = TRUE`.
#' @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
#' the double `\\\\` required for escaping backslashes in R. When `FALSE`,
#' regex is displayed as interpreted by the regex engine (i.e. double `\\\\`
#' as a single `\\`).
#' @param result_only Should only the result be displayed? If `FALSE`, then
#' the colorized regular expression is also displayed in the output.
#' @inheritDotParams base::regexec ignore.case perl fixed useBytes
#' @export
view_regex <- function(
@@ -191,14 +191,18 @@ view_regex <- function(
...,
render = TRUE,
escape = render,
knitr = FALSE,
exact = escape
exact = escape,
result_only = FALSE
) {
knitr <- isTRUE(getOption('knitr.in.progress'))
if (knitr) {
render <- FALSE
escape <- TRUE
}
res <- run_regex(text, pattern, ...)
regex_opts <- deprecate_knitr_option(...)
regex_opts$text <- text
regex_opts$pattern <- pattern
res <- do.call(run_regex, regex_opts)
res <- purrr::map_chr(res, wrap_result, escape = escape, exact = exact)
res <- purrr::map_chr(res, function(resi) {
result_pad <- ""
@@ -213,9 +217,18 @@ view_regex <- function(
})
res <- paste(res, collapse = "")
if (!nchar(pattern)) res <- paste("<p class='results'>", text, "</p>")
if (knitr) return(knitr::asis_output(res))
if (knitr) {
# embed css
css <- if (!isTRUE(getOption("regexplain.knitr_css_loaded"))) paste(
"<style>",
paste(readLines(system.file("styles", "groups.css", package = "regexplain")), collapse = "\n"),
"</style>",
sep = "\n")
if (!is.null(css)) options("regexplain.knitr_css_loaded" = TRUE)
return(knitr::asis_output(paste(css, res, sep = "\n")))
}
if (!render) return(res)
head <- c(
head <- if (!result_only) c(
"---", "pagetitle: View Regex", "---",
"<h5>Pattern</h5>",
"<p><pre>", wrap_regex(pattern, escape, exact), "</pre></p>",
@@ -228,10 +241,19 @@ view_regex <- function(
rmarkdown::render(
tmp,
output_format = rmarkdown::html_document(css = c(system.file("styles", 'skeleton.css', package='regexplain'),
system.file("styles", 'style.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)
}

deprecate_knitr_option <- function(...) {
regex_opts <- list(...)
if ("knitr" %in% names(regex_opts)) {
warning("The `knitr` parameter of `view_regex()` has been removed. Running `view_regex()` in R Markdown is automatically detected.")
}
regex_opts[setdiff(names(regex_opts), "knitr")]
}

+ 7
- 0
inst/styles/gadget.css Vedi File

@@ -64,3 +64,10 @@ dd {
#help_text_selected > h3 {
margin-top: 0px;
}

.gadget-result {
border: 1px solid #ccc;
border-radius: 0.5rem;
padding: 8px;
overflow-wrap: break-word;
}

inst/styles/style.css → inst/styles/groups.css Vedi File

@@ -2,20 +2,11 @@
font-family: monospace;
color: #888888;
}

.gadget-result {
border: 1px solid #ccc;
border-radius: 0.5rem;
padding: 8px;
overflow-wrap: break-word;
}

.group {
border-bottom: 2px solid;
color: black;
padding: 0px;
}

.g00 {
padding: 1px;
background-color: #f0f0f0;
@@ -24,57 +15,46 @@
border-right: 1px solid #b0b0b0;
border-top: 1px solid #b0b0b0;
}

.g01 {
border-color: green;
color: green;
}

.g02 {
border-color: blue;
color: blue;
}

.g03 {
border-color: red;
color: red;
}

.g04 {
border-color: orange;
color: orange;
}

.g05 {
border-color: purple;
color: purple;
}

.g06 {
border-color: DeepPink;
color: DeepPink;
}

.g07 {
border-color: Tomato;
color: Tomato;
}

.g08 {
border-color: DarkSeaGreen;
color: DarkSeaGreen;
}

.g09 {
border-color: DeepSkyBlue;
color: DeepSkyBlue;
}

.g10 {
border-color: Sienna;
color: Sienna;
}

.pad01 {
padding-bottom: 3px;
}
@@ -106,17 +86,3 @@
padding-bottom: 25px;
margin-bottom: 25px;
}

pre,
blockquote,
dl,
figure,
table,
p,
ul,
ol,
form {
margin-bottom: 1rem; }

body {
line-height: 18px;}

+ 13
- 0
inst/styles/view_regex.css Vedi File

@@ -0,0 +1,13 @@
pre,
blockquote,
dl,
figure,
table,
p,
ul,
ol,
form {
margin-bottom: 1rem; }

body {
line-height: 18px;}

Loading…
Annulla
Salva