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

Rename to regexplain

- Colorize regex in view_regex
- Use exact=TRUE param to show all backslashes in wrap_regex
- Rename wrap_span -> wrap_result
tags/v0.1.0
Garrick Aden-Buie 8 лет назад
Родитель
Сommit
48c42da456
7 измененных файлов: 130 добавлений и 9 удалений
  1. +4
    -4
      DESCRIPTION
  2. +33
    -4
      R/run_regex.R
  3. +44
    -0
      Readme.Rmd
  4. +43
    -0
      Readme.md
  5. Двоичные данные
      docs/view-regex.png
  6. +6
    -1
      man/view_regex.Rd
  7. +0
    -0
      regexplain.Rproj

+ 4
- 4
DESCRIPTION Просмотреть файл

Package: regexhelp
Package: regexplain
Title: Rstudio addin to help you with your regexes (in progress) Title: Rstudio addin to help you with your regexes (in progress)
Version: 0.0.0.9000
Version: 0.0.1.9000
Date: 2018-03-07 Date: 2018-03-07
Authors@R: person("Garrick", "Aden-Buie", email = "g.adenbuie@gmail.com", role = c("aut", "cre")) Authors@R: person("Garrick", "Aden-Buie", email = "g.adenbuie@gmail.com", role = c("aut", "cre"))
Description: Test and view regexes. Description: Test and view regexes.
tidyr, tidyr,
rstudioapi rstudioapi
RoxygenNote: 6.0.1 RoxygenNote: 6.0.1
URL: https://github.com/gadenbuie/regexhelp
BugReports: https://github.com/gadenbuie/regexhelp/issues
URL: https://github.com/gadenbuie/regexplain
BugReports: https://github.com/gadenbuie/regexplain/issues

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

utils::modifyList(z, x, TRUE) utils::modifyList(z, x, TRUE)
} }


wrap_span <- function(x, escape = FALSE) {
wrap_result <- function(x, escape = FALSE) {
if (is.null(x$idx[[1]])) return(x$text) if (is.null(x$idx[[1]])) return(x$text)
text <- x$text text <- x$text
idx <- x$idx idx <- x$idx
paste(out, collapse = '') paste(out, collapse = '')
} }


wrap_regex <- function(pattern, escape = TRUE, exact = TRUE) {
stopifnot(length(pattern) == 1)
if(escape) pattern <- escape_html(pattern)
r_open_parens <- "(?<![\\\\])\\("
x <- strsplit(pattern, r_open_parens, perl = TRUE)[[1]]
first <- x[1]
x <- x[-1]
x <- paste0(
'<span class="g', sprintf("%02d", seq_along(x)), '">(',
x,
collapse = ""
)
x <- gsub("(?<![\\\\])\\)", ")</span>", x, perl = TRUE)
if (exact) x <- gsub("\\\\", "\\\\\\\\", x)
paste0(first, x)
}

#' View grouped regex results #' View grouped regex results
#' #'
#' @param text Text to search #' @param text Text to search
#' @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 #' @param knitr Print into knitr doc? If `TRUE`, marks text as `asis_output` and
#' sets `render = FALSE` and `escape = TRUE`. #' sets `render = FALSE` and `escape = TRUE`.
#' @param exact Should regex 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 ... Passed to [run_regex] #' @param ... Passed to [run_regex]
#' @export #' @export
view_regex <- function(text, pattern, ..., render = TRUE, escape = render, knitr = FALSE) {
view_regex <- function(
text,
pattern,
...,
render = TRUE,
escape = render,
knitr = FALSE,
exact = escape
) {
if (knitr) { if (knitr) {
render <- FALSE render <- FALSE
escape <- TRUE escape <- TRUE
} }
res <- run_regex(text, pattern, ...) res <- run_regex(text, pattern, ...)
res <- purrr::map_chr(res, wrap_span, escape = escape)
res <- purrr::map_chr(res, wrap_result, escape = escape)
res <- paste("<p class='results'>", res, "</p>") res <- paste("<p class='results'>", res, "</p>")
if (knitr) return(knitr::asis_output(res)) if (knitr) return(knitr::asis_output(res))
if (!render) return(res) if (!render) return(res)
head <- c( head <- c(
"---", "pagetitle: View Regex", "---", "---", "pagetitle: View Regex", "---",
"<h5>Regex</h5>", "<h5>Regex</h5>",
"<p><pre>", escape_html(pattern), "</pre></p>",
"<p><pre style = 'font-size: 1em;'>", wrap_regex(pattern, escape, exact), "</pre></p>",
"<h5>Results</h5>" "<h5>Results</h5>"
) )
res <- c(head, res) res <- c(head, res)

+ 44
- 0
Readme.Rmd Просмотреть файл

---
title: "regexplain"
output: github_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(regexplain)
```

<!-- Links -->
[regexr]: https://regexr.com/

## WORK IN PROGRESS!!

regexplain is going to be an RStudio addin that helps you interactively build up your regex.
Inspired by [RegExr][regexr] and `stringr::str_view`.

## Done (ish)

You can use `view_regex()` for a `stringr::str_view()` replacement that includes groups.

```r
text <- c("breakfast=eggs;lunch=pizza",
"breakfast=bacon;lunch=spaghetti",
"no food here")
pattern <- "((\\w+)=)(\\w+).+(ch=s?p)"

view_regex(text, pattern)
```

![Example `view_regex(text, pattern)`.](docs/view-regex.png)


## Planned (ish)

1. An Rstudio addin gadget that allows you to interactively enter the regex and see the results.
Like the above example, where the regex field is a text input.

2. Import data from your environment, like a character vector, file, or data.frame column when opening the gadget.

3. Help tab in the gadget, pulling from `?regex` but with some navigation.

4. Tab to interactively explore output of varying regex-applying functions. In other words, see what `stringr::str_locate_all` or `stringr::str_match_all` or `grep` or `grepl` return when applying the regex to your text.

+ 43
- 0
Readme.md Просмотреть файл

regexplain
================

<!-- Links -->

## WORK IN PROGRESS\!\!

regexplain is going to be an RStudio addin that helps you interactively
build up your regex. Inspired by [RegExr](https://regexr.com/) and
`stringr::str_view`.

## Done (ish)

You can use `view_regex()` for a `stringr::str_view()` replacement that
includes groups.

``` r
text <- c("breakfast=eggs;lunch=pizza",
"breakfast=bacon;lunch=spaghetti",
"no food here")
pattern <- "((\\w+)=)(\\w+).+(ch=s?p)"

view_regex(text, pattern)
```

![Example `view_regex(text, pattern)`.](docs/view-regex.png)

## Planned (ish)

1. An Rstudio addin gadget that allows you to interactively enter the
regex and see the results. Like the above example, where the regex
field is a text input.

2. Import data from your environment, like a character vector, file, or
data.frame column when opening the gadget.

3. Help tab in the gadget, pulling from `?regex` but with some
navigation.

4. Tab to interactively explore output of varying regex-applying
functions. In other words, see what `stringr::str_locate_all` or
`stringr::str_match_all` or `grep` or `grepl` return when applying
the regex to your text.

Двоичные данные
docs/view-regex.png Просмотреть файл

Before After
Width: 1880  |  Height: 442  |  Size: 29KB

+ 6
- 1
man/view_regex.Rd Просмотреть файл

\title{View grouped regex results} \title{View grouped regex results}
\usage{ \usage{
view_regex(text, pattern, ..., render = TRUE, escape = render, view_regex(text, pattern, ..., render = TRUE, escape = render,
knitr = FALSE)
knitr = FALSE, exact = escape)
} }
\arguments{ \arguments{
\item{text}{Text to search} \item{text}{Text to search}


\item{knitr}{Print into knitr doc? If `TRUE`, marks text as `asis_output` and \item{knitr}{Print into knitr doc? If `TRUE`, marks text as `asis_output` and
sets `render = FALSE` and `escape = TRUE`.} sets `render = FALSE` and `escape = TRUE`.}

\item{exact}{Should regex 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 `\`).}
} }
\description{ \description{
View grouped regex results View grouped regex results

regexhelp.Rproj → regexplain.Rproj Просмотреть файл


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