diff --git a/DESCRIPTION b/DESCRIPTION index c0777d8..c19c10d 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -27,6 +27,7 @@ Suggests: rmarkdown, jsonlite, MASS, + rmarkdown, testthat, covr, pkgload, diff --git a/NAMESPACE b/NAMESPACE index 7d67f7a..82e44d7 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -21,4 +21,5 @@ export(evaluate_and_highlight) export(highlight) export(href_article) export(href_topic) +export(readme_document) import(rlang) diff --git a/R/highlight.R b/R/highlight.R index 5d4e59b..187cd0e 100644 --- a/R/highlight.R +++ b/R/highlight.R @@ -60,7 +60,7 @@ highlight <- function(text, classes = classes_chroma(), pre_class = NULL) { paste0( "
\n", - out, "\n", + out, "" ) } diff --git a/R/metadata.R b/R/metadata.R index 1e7d281..8f7f768 100644 --- a/R/metadata.R +++ b/R/metadata.R @@ -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) } } @@ -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) } diff --git a/R/readme-document.R b/R/readme-document.R new file mode 100644 index 0000000..c6675fc --- /dev/null +++ b/R/readme-document.R @@ -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 +} diff --git a/man/readme_document.Rd b/man/readme_document.Rd new file mode 100644 index 0000000..8a3a091 --- /dev/null +++ b/man/readme_document.Rd @@ -0,0 +1,23 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/readme-document.R +\name{readme_document} +\alias{readme_document} +\title{Auto-linking for README files} +\usage{ +readme_document(...) +} +\arguments{ +\item{...}{Passed on to \link[rmarkdown:github_document]{rmarkdown::github_document}} +} +\description{ +A drop-in replacement for \link[rmarkdown:github_document]{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. +} +\examples{ + +# Include this into the front matter of your README.Rmd: +# +# output: downlit::readme_document +} diff --git a/tests/testthat/test-highlight.txt b/tests/testthat/test-highlight.txt index 354e47e..da5a5fd 100644 --- a/tests/testthat/test-highlight.txt +++ b/tests/testthat/test-highlight.txt @@ -10,7 +10,7 @@ > # implicit package > cat(highlight("library(MASS)")) -library(MASS) +library(MASS) > cat(highlight("addterm()")) addterm() diff --git a/tests/testthat/test-link.R b/tests/testthat/test-link.R index 4f0d232..a19efd2 100644 --- a/tests/testthat/test-link.R +++ b/tests/testthat/test-link.R @@ -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 ---------------------------------------------------------------- diff --git a/tests/testthat/test-metadata.R b/tests/testthat/test-metadata.R index e4b5550..2b49b58 100644 --- a/tests/testthat/test-metadata.R +++ b/tests/testthat/test-metadata.R @@ -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", {