Skip to content

Commit

Permalink
Add WIP to issue tidyverse#67
Browse files Browse the repository at this point in the history
  • Loading branch information
crew102 committed Aug 31, 2018
1 parent 5830aa3 commit c930f5a
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 1 deletion.
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ Suggests:
rstudioapi,
shiny,
styler (>= 1.0.2),
testthat (>= 2.0.0)
testthat (>= 2.0.0),
gh
VignetteBuilder: knitr
Encoding: UTF-8
LazyData: true
Expand Down
76 changes: 76 additions & 0 deletions R/reprex-undo.R
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,82 @@ reprex_rescue <- function(input = NULL,
)
}

reprex_scrape <- function(input,
outfile = NULL,
comment = opt("#>")) {
venue <- id_venue(input)
src <- switch(
venue,
"gh" = pull_gh_issue(input),
"so" = pull_so_question(input)
)
src <- strsplit(src, "\r?\n")[[1]]

reprex_undo(
src,
is_md = TRUE,
venue = venue,
outfile = outfile,
comment = comment
)
}

id_venue <- function(input) {
if (grepl("^https?://stackoverflow\\.com", input)) {
"so"
} else if (grepl("^https?://github\\.com/", input)) {
"gh"
} else {
stop(
"`input` needs to be a URL to a GitHub issue or Stack Overflow question",
call. = FALSE
)
}
}

pull_gh_issue <- function(input) {

if (!requireNamespace("gh", quietly = TRUE)) {
stop(
"Install the `gh` package in order to use `reprex_scrape()` ",
"with GitHub issues", call. = FALSE
)
}

no_host <- sub("^https?://github\\.com/", "", input)
paths <- strsplit(no_host, "/")[[1]]
if (!(identical(paths[3], "issues") && length(paths) == 4)) {
stop(
"GitHub URLs need to point to an issue and be in the form:\n",
"https://github.com/tidyverse/reprex/issues/168",
call. = FALSE
)
}

resp <- gh::gh(
"/repos/:owner/:repo/issues/:number",
owner = paths[1], repo = paths[2], number = paths[4]
)
resp[["body"]]
}

# stub
pull_so_question <- function(input) {
txt <- c(
"I would like to extract `\"/arsenal-vs-man-city/\"` from",
"",
"<!-- language-all: lang-r -->",
"",
" library(stringr)",
" str_extract_all(\"/sports/football/arsenal-vs-man-city/stats/\", \"/.*?-vs-.*?/\")",
" #> [[1]]",
" #> [1] \"/sports/football/arsenal-vs-man-city/\"",
"",
"I'd like to know what the correct way to do this is and also why my way is wrong."
)
paste0(txt, collapse = "\n")
}

reprex_undo <- function(input = NULL,
outfile = NULL,
venue,
Expand Down
14 changes: 14 additions & 0 deletions tests/testthat/test-undo.R
Original file line number Diff line number Diff line change
Expand Up @@ -141,3 +141,17 @@ test_that("reprex_invert() can name outfile based on input filepath", {
out <- reprex_invert(input = "a_reprex.md", outfile = NA)
expect_identical(readLines("a_reprex_clean.R"), out)
})

test_that("reprex_scrap() outputs the expected lines", {
skip_on_cran()
expected <- c(
"#' This is a sample reprex.",
"# adding two vectors",
"x <- 1:4",
"y <- 2:5",
"x + y",
"#' Created on 2018-08-31 by the [reprex package](http://reprex.tidyverse.org) (v0.2.0)."
)
out <- reprex_scrape("https://github.com/r-lib/ghurl/issues/7")
expect_identical(expected, out)
})

0 comments on commit c930f5a

Please sign in to comment.