diff --git a/DESCRIPTION b/DESCRIPTION index e6b7f28..877d59d 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: reprex Title: Prepare Reproducible Example Code via the Clipboard -Version: 2.1.1.9000 +Version: 2.1.1.9001 Authors@R: c( person("Jennifer", "Bryan", , "jenny@posit.co", role = c("aut", "cre"), comment = c(ORCID = "0000-0002-6983-2759")), diff --git a/NAMESPACE b/NAMESPACE index 68165e1..65a90e5 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -3,6 +3,7 @@ export(reprex) export(reprex_addin) export(reprex_clean) +export(reprex_discord) export(reprex_document) export(reprex_html) export(reprex_invert) diff --git a/NEWS.md b/NEWS.md index dc0f76c..262d30d 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,8 @@ # reprex (development version) +* New reprex `venue = "discord"` returns output that allows syntax highlighting in discord. (#474) +* Reprex venues `"slack"`, and `"discord"` now correctly simplify image hyperlinks. (#474) + # reprex 2.1.1 * `reprex(style = FALSE)` will never nag about installing styler (#461). diff --git a/R/reprex.R b/R/reprex.R index 5a5d2de..cfeec17 100644 --- a/R/reprex.R +++ b/R/reprex.R @@ -113,6 +113,9 @@ #' * "ds" for Discourse, e.g., #' [forum.posit.co]( https://forum.posit.co/). Note: this is #' currently just an alias for "gh". +#' * "discord" for pasting into a Discord message. This is almost exactly the same +#' as "slack" but it removes a blank space between the opening backticks and the +#' language name, i.e. it returns " ` ```r ` " instead of " ` ``` r ` ". #' @param advertise Logical. Whether to include a footer that describes when and #' how the reprex was created. If unspecified, the option `reprex.advertise` #' is consulted and, if that is not defined, default is `TRUE` for venues @@ -264,7 +267,7 @@ reprex <- function( x = NULL, input = NULL, wd = NULL, - venue = c("gh", "r", "rtf", "html", "slack", "so", "ds"), + venue = c("gh", "r", "rtf", "html", "slack", "so", "ds", "discord"), render = TRUE, diff --git a/R/reprex_document.R b/R/reprex_document.R index 152b57f..5eeb3aa 100644 --- a/R/reprex_document.R +++ b/R/reprex_document.R @@ -33,7 +33,7 @@ #' @examples #' reprex_document() reprex_document <- function( - venue = c("gh", "r", "rtf", "html", "slack", "so", "ds"), + venue = c("gh", "r", "rtf", "html", "slack", "so", "ds", "discord"), advertise = NULL, session_info = opt(FALSE), diff --git a/R/reprex_impl.R b/R/reprex_impl.R index ee4a041..ec177f2 100644 --- a/R/reprex_impl.R +++ b/R/reprex_impl.R @@ -2,7 +2,7 @@ reprex_impl <- function( x_expr = NULL, input = NULL, wd = NULL, - venue = c("gh", "r", "rtf", "html", "slack", "so", "ds"), + venue = c("gh", "r", "rtf", "html", "slack", "so", "ds", "discord"), render = TRUE, new_session = TRUE, @@ -125,7 +125,8 @@ advertise_default <- function(venue) { so = TRUE, r = FALSE, rtf = FALSE, - slack = FALSE + slack = FALSE, + discord = FALSE ) default[[venue]] } diff --git a/R/reprex_render.R b/R/reprex_render.R index 7447c23..725fbd7 100644 --- a/R/reprex_render.R +++ b/R/reprex_render.R @@ -173,6 +173,7 @@ reprex_render_impl <- function(input, new_session = TRUE, html_preview = NULL) { r = pp_md_to_r(md_file, comment = comment), rtf = pp_highlight(pp_md_to_r(md_file, comment = comment)), slack = pp_slackify(md_file), + discord = pp_discordify(md_file), html = pp_html_render(md_file), md_file ) @@ -334,16 +335,39 @@ pp_slackify <- function(input) { slack_file } +# used when venue is "discord" +# https://www.markdownguide.org/tools/discord/ +pp_discordify <- function(input) { + output_lines <- read_lines(md_file(input)) + output_lines <- discord_info_string(output_lines) + output_lines <- simplify_image_links(output_lines) + discord_file <- md_file_discord(input) + write_lines(output_lines, discord_file) + discord_file +} + # remove "info strings" from opening code fences, e.g. ```r # https://spec.commonmark.org/0.29/#info-string remove_info_strings <- function(x) { sub("^```[^`]*$", "```", x, perl = TRUE) } +# remove blank space from opening code fences, e.g. ``` r +discord_info_string <- function(x) { + sub("^```[[:blank:]]?", "```", x, perl = TRUE) +} + # input: ![](https://i.imgur.com/woc4vHs.png) # output: https://i.imgur.com/woc4vHs.png simplify_image_links <- function(x) { - sub("(^!\\[\\]\\()(.+)(\\)$)", "\\2", x, perl = TRUE) + x <- remove_empty_html_comments(x) + sub("(^!\\[\\]\\()(.+)(\\))$", "\\2", x, perl = TRUE) +} + +# input: ![](https://i.imgur.com/woc4vHs.png) +# output: ![](https://i.imgur.com/woc4vHs.png) +remove_empty_html_comments <- function(x) { + sub("", "", x, perl = TRUE) } # used when venue is "rtf" diff --git a/R/utils-io.R b/R/utils-io.R index ff47421..01c1b64 100644 --- a/R/utils-io.R +++ b/R/utils-io.R @@ -216,6 +216,10 @@ md_file_slack <- function(path) { path_mutate(path, suffix = "slack", ext = "md") } +md_file_discord <- function(path) { + path_mutate(path, suffix = "discord", ext = "md") +} + std_file <- function(path) { path_mutate(path, suffix = "std_out_err", ext = "txt") } diff --git a/R/venues.R b/R/venues.R index ace3021..f9f60a9 100644 --- a/R/venues.R +++ b/R/venues.R @@ -26,6 +26,10 @@ reprex_rtf <- function(...) reprex(..., venue = "rtf") #' @rdname reprex_venue reprex_slack <- function(...) reprex(..., venue = "slack") +#' @export +#' @rdname reprex_venue +reprex_discord <- function(...) reprex(..., venue = "discord") + # these should exist for completeness, but I predict they'd never get used and # they just clutter the auto-complete landscape # reprex_gh <- function(...) reprex(..., venue = "gh") diff --git a/man/reprex.Rd b/man/reprex.Rd index d06798b..46b8666 100644 --- a/man/reprex.Rd +++ b/man/reprex.Rd @@ -8,7 +8,7 @@ reprex( x = NULL, input = NULL, wd = NULL, - venue = c("gh", "r", "rtf", "html", "slack", "so", "ds"), + venue = c("gh", "r", "rtf", "html", "slack", "so", "ds", "discord"), render = TRUE, advertise = NULL, session_info = opt(FALSE), @@ -69,6 +69,9 @@ support CommonMark-style fenced code blocks in January 2019. \item "ds" for Discourse, e.g., \href{https://forum.posit.co/}{forum.posit.co}. Note: this is currently just an alias for "gh". +\item "discord" for pasting into a Discord message. This is almost exactly the same +as "slack" but it removes a blank space between the opening backticks and the +language name, i.e. it returns " \verb{```r} " instead of " \verb{``` r} ". }} \item{render}{Logical. Whether to call \code{\link[rmarkdown:render]{rmarkdown::render()}} on the templated diff --git a/man/reprex_addin.Rd b/man/reprex_addin.Rd index 41910c0..534a73e 100644 --- a/man/reprex_addin.Rd +++ b/man/reprex_addin.Rd @@ -35,6 +35,9 @@ support CommonMark-style fenced code blocks in January 2019. \item "ds" for Discourse, e.g., \href{https://forum.posit.co/}{forum.posit.co}. Note: this is currently just an alias for "gh". +\item "discord" for pasting into a Discord message. This is almost exactly the same +as "slack" but it removes a blank space between the opening backticks and the +language name, i.e. it returns " \verb{```r} " instead of " \verb{``` r} ". }} } \description{ diff --git a/man/reprex_document.Rd b/man/reprex_document.Rd index 59f0263..6d7001d 100644 --- a/man/reprex_document.Rd +++ b/man/reprex_document.Rd @@ -5,7 +5,7 @@ \title{reprex output format} \usage{ reprex_document( - venue = c("gh", "r", "rtf", "html", "slack", "so", "ds"), + venue = c("gh", "r", "rtf", "html", "slack", "so", "ds", "discord"), advertise = NULL, session_info = opt(FALSE), style = opt(FALSE), @@ -41,6 +41,9 @@ support CommonMark-style fenced code blocks in January 2019. \item "ds" for Discourse, e.g., \href{https://forum.posit.co/}{forum.posit.co}. Note: this is currently just an alias for "gh". +\item "discord" for pasting into a Discord message. This is almost exactly the same +as "slack" but it removes a blank space between the opening backticks and the +language name, i.e. it returns " \verb{```r} " instead of " \verb{``` r} ". }} \item{advertise}{Logical. Whether to include a footer that describes when and diff --git a/man/reprex_venue.Rd b/man/reprex_venue.Rd index c1b9766..3bcbc28 100644 --- a/man/reprex_venue.Rd +++ b/man/reprex_venue.Rd @@ -6,6 +6,7 @@ \alias{reprex_r} \alias{reprex_rtf} \alias{reprex_slack} +\alias{reprex_discord} \title{Venue-specific shortcuts} \usage{ reprex_html(...) @@ -15,6 +16,8 @@ reprex_r(...) reprex_rtf(...) reprex_slack(...) + +reprex_discord(...) } \arguments{ \item{...}{Passed along to \code{\link[=reprex]{reprex()}}.} diff --git a/man/un-reprex.Rd b/man/un-reprex.Rd index 16636f2..8babcec 100644 --- a/man/un-reprex.Rd +++ b/man/un-reprex.Rd @@ -70,6 +70,9 @@ support CommonMark-style fenced code blocks in January 2019. \item "ds" for Discourse, e.g., \href{https://forum.posit.co/}{forum.posit.co}. Note: this is currently just an alias for "gh". +\item "discord" for pasting into a Discord message. This is almost exactly the same +as "slack" but it removes a blank space between the opening backticks and the +language name, i.e. it returns " \verb{```r} " instead of " \verb{``` r} ". }} \item{comment}{regular expression that matches commented output lines} diff --git a/tests/testthat/test-venues.R b/tests/testthat/test-venues.R index 19ae597..e959e0a 100644 --- a/tests/testthat/test-venues.R +++ b/tests/testthat/test-venues.R @@ -127,3 +127,23 @@ test_that("venue = 'slack' works", { ret <- ret[nzchar(ret)] expect_identical(ret, output) }) + +test_that("venue = 'discord' works", { + skip_on_cran() + input <- c( + "#' Hello world", + "## comment", + "1:5" + ) + output <- c( + "Hello world", + "```r", + "## comment", + "1:5", + "#> [1] 1 2 3 4 5", + "```" + ) + ret <- reprex(input = input, venue = "discord") + ret <- ret[nzchar(ret)] + expect_identical(ret, output) +})