Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement venue = "discord" #474

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -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", , "[email protected]", role = c("aut", "cre"),
comment = c(ORCID = "0000-0002-6983-2759")),
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
export(reprex)
export(reprex_addin)
export(reprex_clean)
export(reprex_dircord)
export(reprex_document)
export(reprex_html)
export(reprex_invert)
Expand Down
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -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).
Expand Down
5 changes: 4 additions & 1 deletion R/reprex.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -262,7 +265,7 @@
#' @export
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,

Expand Down
2 changes: 1 addition & 1 deletion R/reprex_document.R
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
#' @export
#' @examples
#' reprex_document()
reprex_document <- function(venue = c("gh", "r", "rtf", "html", "slack", "so", "ds"),
reprex_document <- function(venue = c("gh", "r", "rtf", "html", "slack", "so", "ds", "discord"),

advertise = NULL,
session_info = opt(FALSE),
Expand Down
5 changes: 3 additions & 2 deletions R/reprex_impl.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,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,
Expand Down Expand Up @@ -115,7 +115,8 @@ advertise_default <- function(venue) {
so = TRUE,
r = FALSE,
rtf = FALSE,
slack = FALSE
slack = FALSE,
discord = FALSE
)
default[[venue]]
}
Expand Down
26 changes: 25 additions & 1 deletion R/reprex_render.R
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ reprex_render_impl <- function(input,
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
)
Expand Down Expand Up @@ -326,16 +327,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("<!--[[:space:]]*-->", "", x, perl = TRUE)
}

# used when venue is "rtf"
Expand Down
4 changes: 4 additions & 0 deletions R/utils-io.R
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down
4 changes: 4 additions & 0 deletions R/venues.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ reprex_rtf <- function(...) reprex(..., venue = "rtf")
#' @rdname reprex_venue
reprex_slack <- function(...) reprex(..., venue = "slack")

#' @export
#' @rdname reprex_venue
reprex_dircord <- 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")
Expand Down
5 changes: 4 additions & 1 deletion man/reprex.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions man/reprex_addin.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion man/reprex_document.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions man/reprex_venue.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions man/un-reprex.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions tests/testthat/test-venues.R
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,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)
})
Loading