Преглед изворни кода

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 година
родитељ
комит
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. BIN
      docs/view-regex.png
  6. +6
    -1
      man/view_regex.Rd
  7. +0
    -0
      regexplain.Rproj

+ 4
- 4
DESCRIPTION Прегледај датотеку

@@ -1,6 +1,6 @@
Package: regexhelp
Package: regexplain
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
Authors@R: person("Garrick", "Aden-Buie", email = "g.adenbuie@gmail.com", role = c("aut", "cre"))
Description: Test and view regexes.
@@ -17,5 +17,5 @@ Imports:
tidyr,
rstudioapi
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 Прегледај датотеку

@@ -16,7 +16,7 @@ run_regex <- function(
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)
text <- x$text
idx <- x$idx
@@ -63,6 +63,23 @@ wrap_span <- function(x, escape = FALSE) {
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
#'
#' @param text Text to search
@@ -71,22 +88,34 @@ wrap_span <- function(x, escape = FALSE) {
#' @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 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]
#' @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) {
render <- FALSE
escape <- TRUE
}
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>")
if (knitr) return(knitr::asis_output(res))
if (!render) return(res)
head <- c(
"---", "pagetitle: View Regex", "---",
"<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>"
)
res <- c(head, res)

+ 44
- 0
Readme.Rmd Прегледај датотеку

@@ -0,0 +1,44 @@
---
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 Прегледај датотеку

@@ -0,0 +1,43 @@
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.

BIN
docs/view-regex.png Прегледај датотеку

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

+ 6
- 1
man/view_regex.Rd Прегледај датотеку

@@ -5,7 +5,7 @@
\title{View grouped regex results}
\usage{
view_regex(text, pattern, ..., render = TRUE, escape = render,
knitr = FALSE)
knitr = FALSE, exact = escape)
}
\arguments{
\item{text}{Text to search}
@@ -20,6 +20,11 @@ view_regex(text, pattern, ..., render = TRUE, escape = render,

\item{knitr}{Print into knitr doc? If `TRUE`, marks text as `asis_output` and
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{
View grouped regex results

regexhelp.Rproj → regexplain.Rproj Прегледај датотеку


Loading…
Откажи
Сачувај