Fixes issues with adding <span> and </span> around zero-length groups and more generally groups that start and end at the same index. Ensures that g00 wraps all others, then at index with close/opens, closes open groups first before opening new groups.tags/v0.2.2
| @@ -1,6 +1,6 @@ | |||
| Package: regexplain | |||
| Title: Rstudio Addin to Explain, Test and Build Regular Expressions | |||
| Version: 0.2.1 | |||
| Version: 0.2.1.9000 | |||
| Date: 2018-04-04 | |||
| Authors@R: c( | |||
| person("Garrick", "Aden-Buie", email = "g.adenbuie@gmail.com", role = c("aut", "cre")), | |||
| @@ -60,12 +60,23 @@ wrap_result <- function(x, escape = FALSE, exact = FALSE) { | |||
| inserts <- inserts %>% | |||
| tidyr::gather(type, loc, start:end) %>% | |||
| filter(!is.na(.data$loc)) %>% | |||
| dplyr::arrange(loc, class, desc(type)) %>% | |||
| mutate( | |||
| class = ifelse(.data$pad > 0, sprintf("%s pad%02d", .data$class, .data$pad), .data$class), | |||
| insert = ifelse(.data$type == 'start', sprintf('<span class="%s">', .data$class), "</span>") | |||
| ) %>% | |||
| ) | |||
| inserts_g0 <- filter(inserts, class == "group g00") | |||
| inserts_other <- filter(inserts, class != "group g00") | |||
| inserts <- dplyr::bind_rows( | |||
| filter(inserts_g0, type == "start"), | |||
| inserts_other, | |||
| filter(inserts_g0, type == "end") | |||
| ) %>% | |||
| mutate(type = sprintf("%05d%s", 1:nrow(.), type)) %>% | |||
| group_by(.data$loc, .data$type) %>% | |||
| summarize(insert = paste(.data$insert, collapse = '')) | |||
| summarize(insert = paste(.data$insert, collapse = '')) %>% | |||
| dplyr::ungroup() %>% | |||
| mutate(type = sub("^\\d{5}", "", type)) | |||
| # inserts now gives html (span open and close) to insert and loc | |||
| # first split text at inserts$loc locations, | |||
| @@ -0,0 +1,35 @@ | |||
| context("test-wrap_result.R") | |||
| test_that("wrap_result handles zero length groups", { | |||
| # Issue #9 | |||
| # Type 'q()' to quit R. | |||
| # (?<=\()([^)]*)(?=\)) | |||
| text <- "Type 'q()' to quit R." | |||
| pattern <- "(?<=\\()([^)]*)(?=\\))" | |||
| res <- wrap_result(run_regex(text, pattern, perl = TRUE)[[1]]) | |||
| expect_equal(res, "Type 'q(<span class=\"group g00\"><span class=\"group g01\"></span></span>)' to quit R.") | |||
| }) | |||
| test_that("wrap_results generally works", { | |||
| text <- "apples" | |||
| pattern <- "apples" | |||
| res <- wrap_result(run_regex(text, pattern, perl = TRUE)[[1]]) | |||
| expect_equal(res, "<span class=\"group g00\">apples</span>") | |||
| text <- "He wheeled the bike past the winding road." | |||
| pattern <- "(a|the) ([^ ]+)" | |||
| res <- wrap_result(run_regex(text, pattern, perl = TRUE)[[1]]) | |||
| expect_equal(res, "He wheeled <span class=\"group g00\"><span class=\"group g01\">the</span> <span class=\"group g02\">bike</span></span> past the winding road.") | |||
| text <- ".15in" | |||
| pattern <- "^(auto|inherit|((\\.\\d+)|(\\d+(\\.\\d+)?))(%|in|cm|mm|em|ex|pt|pc|px|vh|vw|vmin|vmax))$" | |||
| res <- wrap_result(run_regex(text, pattern, perl = TRUE)[[1]]) | |||
| expect_equal(res, "<span class=\"group g00\"><span class=\"group g01\"><span class=\"group g02 pad01\"><span class=\"group g03 pad02\">.15</span></span><span class=\"group g06 pad01\">in</span></span></span>") | |||
| }) | |||
| test_that("wrap_results works when groups start and end at same index", { | |||
| text <- "7282298386" | |||
| pattern <- "\\(?(\\d{3})[-). ]?(\\d{3})[- .]?(\\d{4})" | |||
| res <- wrap_result(run_regex(text, pattern, perl = TRUE)[[1]]) | |||
| expect_equal(res, "<span class=\"group g00\"><span class=\"group g01\">728</span><span class=\"group g02\">229</span><span class=\"group g03\">8386</span></span>") | |||
| }) | |||