diff --git a/DESCRIPTION b/DESCRIPTION index 961bfe0f..175ef1bb 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -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 diff --git a/R/reprex-undo.R b/R/reprex-undo.R index cf4a961b..2e0cb191 100644 --- a/R/reprex-undo.R +++ b/R/reprex-undo.R @@ -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", + "", + "", + "", + " 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, diff --git a/tests/testthat/test-undo.R b/tests/testthat/test-undo.R index d5f5566d..8bef49fb 100644 --- a/tests/testthat/test-undo.R +++ b/tests/testthat/test-undo.R @@ -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) +})