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

Proof of concept: readme_document format #43

Closed
wants to merge 7 commits into from
Closed
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
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ Suggests:
rmarkdown,
jsonlite,
MASS,
rmarkdown,
testthat,
covr,
pkgload,
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ export(evaluate_and_highlight)
export(highlight)
export(href_article)
export(href_topic)
export(readme_document)
import(rlang)
2 changes: 1 addition & 1 deletion R/highlight.R
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ highlight <- function(text, classes = classes_chroma(), pre_class = NULL) {

paste0(
"<pre class='", paste0(pre_class, collapse = " "), "'>\n",
out, "\n",
out,
"</pre>"
)
}
Expand Down
8 changes: 7 additions & 1 deletion R/metadata.R
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ remote_metadata_slow <- function(package) {
if (has_name(yaml, "articles")) {
yaml$articles <- unlist(yaml$articles)
}
if (!has_name(yaml, "urls")) {
base_url <- dirname(url)
yaml$urls <- list(
reference = paste0(base_url, "/reference"),
article = paste0(base_url, "/articles")
)
}
return(yaml)
}
}
Expand Down Expand Up @@ -78,7 +85,6 @@ package_urls <- function(package) {
parse_urls <- function(x) {
urls <- trimws(strsplit(trimws(x), "[,\\s]+", perl = TRUE)[[1]])
urls <- urls[grepl("^http", urls)]
urls <- sub("/$", "", urls)

sub_special_cases(urls)
}
Expand Down
61 changes: 61 additions & 0 deletions R/readme-document.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#' Auto-linking for README files
#'
#' A drop-in replacement for [rmarkdown::github_document] that applies
#' highlighting and auto-linking to the resulting Markdown file.
#' GitHub strips the highlighting when displaying the README file,
#' it is still available when the site is displayed in pkgdown.
#'
#' @param ... Passed on to [rmarkdown::github_document]
#' @examples
#'
#' # Include this into the front matter of your README.Rmd:
#' #
#' # output: downlit::readme_document
#' @export
readme_document <- function(...) {
check_packages()

format <- rmarkdown::github_document(...)

format_pre_knit <- format$pre_knit
format_post_knit <- format$post_knit

format_post_processor <- format$post_processor

old_options <- NULL

format$pre_knit <- function(...) {
old_options <<- options(crayon.enabled = TRUE)

# Run preknit at end
if (!is.null(format_pre_knit)) {
format_pre_knit(...)
}
}

format$post_knit <- function(...) {
if (!is.null(format_post_knit)) {
out <- format_post_knit(...)
} else {
out <- NULL
}
options(old_options)
out
}

format$post_processor <- function(front_matter, utf8_input, output_file, clean, quiet) {
if (!quiet) {
message("Auto-linking code")
}
downlit_md_path(output_file, output_file)

# Run postprocessor at end, on downlit file
if (!is.null(format_post_processor)) {
output_file <- format_post_processor(front_matter, utf8_input, output_file, clean, quiet)
}

output_file
}

format
}
23 changes: 23 additions & 0 deletions man/readme_document.Rd

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

2 changes: 1 addition & 1 deletion tests/testthat/test-highlight.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

> # implicit package
> cat(highlight("library(MASS)"))
<span class='nf'><a href='https://rdrr.io/r/base/library.html'>library</a></span>(<span class='k'><a href='http://www.stats.ox.ac.uk/pub/MASS4'>MASS</a></span>)
<span class='nf'><a href='https://rdrr.io/r/base/library.html'>library</a></span>(<span class='k'><a href='http://www.stats.ox.ac.uk/pub/MASS4/'>MASS</a></span>)

> cat(highlight("addterm()"))
<span class='nf'><a href='https://rdrr.io/pkg/MASS/man/addterm.html'>addterm</a></span>()
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-link.R
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ test_that("library() linked to package reference", {

expect_equal(href_expr_(library()), NA_character_)
expect_equal(href_expr_(library(rlang)), "http://rlang.r-lib.org")
expect_equal(href_expr_(library(MASS)), "http://www.stats.ox.ac.uk/pub/MASS4")
expect_equal(href_expr_(library(MASS)), "http://www.stats.ox.ac.uk/pub/MASS4/")
})

# vignette ----------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-metadata.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ test_that("can extract urls for package", {

expect_equal(package_urls("base"), character())
expect_equal(package_urls("packagethatdoesn'texist"), character())
expect_equal(package_urls("MASS"), "http://www.stats.ox.ac.uk/pub/MASS4")
expect_equal(package_urls("MASS"), "http://www.stats.ox.ac.uk/pub/MASS4/")
})

test_that("handle common url formats", {
Expand Down