|
| 1 | +#' Use Hex Sticker in README |
| 2 | +#' |
| 3 | +#' @description |
| 4 | +#' Adds a hex sticker image (optionally wrapped in a hyperlink) at the end of |
| 5 | +#' the top-level heading (the line starting with \verb{# }) in \code{README.md}. |
| 6 | +#' This is a common pattern for R package README files. |
| 7 | +#' |
| 8 | +#' @param img_path Character. Path to the image file (e.g., |
| 9 | +#' \code{"man/figures/logo.png"}). |
| 10 | +#' @param url Character. URL the image should link to. If \code{NULL} (default), |
| 11 | +#' no hyperlink is added. |
| 12 | +#' @param alt_text Character. Alt text for the image. Defaults to |
| 13 | +#' \code{"package logo"}. |
| 14 | +#' @param height Numeric or character. Image height in pixels. Defaults to |
| 15 | +#' \code{139}. |
| 16 | +#' @param align Character. Image alignment attribute. Defaults to \code{"right"}. |
| 17 | +#' @param path Character. Path to the package root directory. If \code{NULL}, |
| 18 | +#' uses the current working directory (with RStudio document detection). |
| 19 | +#' @param ... Additional HTML attributes to include in the \verb{<img>} tag as |
| 20 | +#' named arguments. |
| 21 | +#' |
| 22 | +#' @return Invisibly returns \code{TRUE} on success. |
| 23 | +#' @export |
| 24 | +#' |
| 25 | +#' @examples |
| 26 | +#' \dontrun{ |
| 27 | +#' use_hexsticker("man/figures/logo.png", url = "https://my-pkg-website.com") |
| 28 | +#' use_hexsticker("man/figures/logo_white.png", alt_text = "Package logo", height = 139) |
| 29 | +#' } |
| 30 | +use_hexsticker <- function( |
| 31 | + img_path, |
| 32 | + url = NULL, |
| 33 | + alt_text = "package logo", |
| 34 | + height = 139, |
| 35 | + align = "right", |
| 36 | + path = NULL, |
| 37 | + ... |
| 38 | +) { |
| 39 | + # Build <img> tag attributes |
| 40 | + img_attrs <- c( |
| 41 | + src = img_path, |
| 42 | + alt = alt_text, |
| 43 | + align = align, |
| 44 | + height = as.character(height) |
| 45 | + ) |
| 46 | + |
| 47 | + extra_attrs <- list(...) |
| 48 | + if (length(extra_attrs) > 0) { |
| 49 | + extra_named <- stats::setNames( |
| 50 | + vapply(extra_attrs, as.character, character(1)), |
| 51 | + names(extra_attrs) |
| 52 | + ) |
| 53 | + img_attrs <- c(img_attrs, extra_named) |
| 54 | + } |
| 55 | + |
| 56 | + img_attr_str <- paste( |
| 57 | + sprintf('%s="%s"', names(img_attrs), img_attrs), |
| 58 | + collapse = " " |
| 59 | + ) |
| 60 | + img_tag <- sprintf("<img %s/>", img_attr_str) |
| 61 | + |
| 62 | + # Wrap in anchor if url is provided |
| 63 | + html_tag <- if (is.null(url)) { |
| 64 | + img_tag |
| 65 | + } else { |
| 66 | + sprintf('<a href="%s">%s</a>', url, img_tag) |
| 67 | + } |
| 68 | + |
| 69 | + # Locate README.md |
| 70 | + path <- path %||% get_wd() |
| 71 | + readme_path <- file.path(path, "README.md") |
| 72 | + |
| 73 | + if (!file.exists(readme_path)) { |
| 74 | + cli::cli_abort(c("x" = "README.md not found at {.path {readme_path}}.")) |
| 75 | + } |
| 76 | + |
| 77 | + lines <- readLines(readme_path, warn = FALSE) |
| 78 | + |
| 79 | + if (length(lines) == 0L) { |
| 80 | + cli::cli_abort(c("x" = "README.md is empty.")) |
| 81 | + } |
| 82 | + |
| 83 | + # Find the top-level heading (first line starting with "# ") |
| 84 | + heading_idx <- grep("^#\\s", lines) |
| 85 | + if (length(heading_idx) == 0L) { |
| 86 | + cli::cli_abort(c("x" = "No top-level heading found in README.md.")) |
| 87 | + } |
| 88 | + |
| 89 | + # Append HTML tag to the heading line |
| 90 | + lines[heading_idx[1L]] <- paste(lines[heading_idx[1L]], html_tag) |
| 91 | + |
| 92 | + writeLines(lines, readme_path) |
| 93 | + |
| 94 | + cli::cli_inform(c( |
| 95 | + "v" = "Added hex sticker reference to {.path {readme_path}}", |
| 96 | + ">" = "Image: {.file {img_path}}" |
| 97 | + )) |
| 98 | + |
| 99 | + invisible(TRUE) |
| 100 | +} |
0 commit comments