Просмотр исходного кода

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 лет назад
Родитель
Сommit
8e9f185f75
5 измененных файлов: 51 добавлений и 43 удалений
  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 Просмотреть файл



# ---- UI ---- # ---- UI ----
ui <- miniPage( 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")), shiny::includeCSS(system.file("styles", "gadget.css", package = "regexplain")),
gadgetTitleBar( gadgetTitleBar(
"RegExplain", "RegExplain",

+ 30
- 8
R/run_regex.R Просмотреть файл

#' @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 to an HTML doc and open in RStudio viewer?
#' @param escape Escape HTML-related characters in `text`? #' @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 #' @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
#' the double `\\\\` required for escaping backslashes in R. When `FALSE`, #' the double `\\\\` required for escaping backslashes in R. When `FALSE`,
#' regex is displayed as interpreted by the regex engine (i.e. double `\\\\` #' regex is displayed as interpreted by the regex engine (i.e. double `\\\\`
#' as a single `\\`). #' 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 #' @inheritDotParams base::regexec ignore.case perl fixed useBytes
#' @export #' @export
view_regex <- function( view_regex <- function(
..., ...,
render = TRUE, render = TRUE,
escape = render, escape = render,
knitr = FALSE,
exact = escape
exact = escape,
result_only = FALSE
) { ) {
knitr <- isTRUE(getOption('knitr.in.progress'))
if (knitr) { if (knitr) {
render <- FALSE render <- FALSE
escape <- TRUE 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, wrap_result, escape = escape, exact = exact)
res <- purrr::map_chr(res, function(resi) { res <- purrr::map_chr(res, function(resi) {
result_pad <- "" result_pad <- ""
}) })
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='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) if (!render) return(res)
head <- c(
head <- if (!result_only) c(
"---", "pagetitle: View Regex", "---", "---", "pagetitle: View Regex", "---",
"<h5>Pattern</h5>", "<h5>Pattern</h5>",
"<p><pre>", wrap_regex(pattern, escape, exact), "</pre></p>", "<p><pre>", wrap_regex(pattern, escape, exact), "</pre></p>",
rmarkdown::render( rmarkdown::render(
tmp, tmp,
output_format = rmarkdown::html_document(css = c(system.file("styles", 'skeleton.css', package='regexplain'), 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, theme = NULL,
md_extensions = "-autolink_bare_uris"), md_extensions = "-autolink_bare_uris"),
quiet = TRUE quiet = TRUE
)) ))
rstudioapi::viewer(tmp_html) 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 Просмотреть файл

#help_text_selected > h3 { #help_text_selected > h3 {
margin-top: 0px; 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 Просмотреть файл

font-family: monospace; font-family: monospace;
color: #888888; color: #888888;
} }

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

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

.g00 { .g00 {
padding: 1px; padding: 1px;
background-color: #f0f0f0; background-color: #f0f0f0;
border-right: 1px solid #b0b0b0; border-right: 1px solid #b0b0b0;
border-top: 1px solid #b0b0b0; border-top: 1px solid #b0b0b0;
} }

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

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

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

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

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

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

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

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

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

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

.pad01 { .pad01 {
padding-bottom: 3px; padding-bottom: 3px;
} }
padding-bottom: 25px; padding-bottom: 25px;
margin-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 Просмотреть файл

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

body {
line-height: 18px;}

Загрузка…
Отмена
Сохранить