Skip to content

Commit 01d0543

Browse files
committed
feat: add use_hexsticker function
Also adds codecov.yml, ignores it in .Rbuildignore, and refactors get_wd() logic.
1 parent d680d99 commit 01d0543

8 files changed

Lines changed: 178 additions & 4 deletions

File tree

.Rbuildignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@
1313
^_pkgdown\.yml$
1414
^docs$
1515
^pkgdown$
16+
^codecov\.yml$

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ export(news_md_check)
1212
export(news_md_show)
1313
export(render_rmd)
1414
export(update_time_in_standalone)
15+
export(use_hexsticker)

R/01_file_path_utils.R

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@ is_pkg <- function(path) {
33
}
44

55
get_wd <- function() {
6+
current_wd <- NULL
67
if (rlang::is_installed("rstudioapi") && interactive()) {
78
current_wd <- dirname(rstudioapi::getActiveDocumentContext()$path)
8-
} else {
9+
}
10+
if (is.null(current_wd)) {
911
current_wd <- getwd()
1012
}
1113
if (is_pkg(dirname(current_wd))) {
12-
dirname(current_wd)
13-
} else {
14-
current_wd
14+
return(dirname(current_wd))
1515
}
16+
current_wd
1617
}

R/41_use_hexsticker.R

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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+
}

README.Rmd

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,8 @@ Use `usethis::use_standalone("Exceret/rpkgkit", "<name>")` to import:
4949
### Other Utilities
5050

5151
- `make_func_call_explicit()` - Make function calls explicit by adding package prefixes
52+
5253
- `render_rmd()` - Render R Markdown documents
54+
55+
- `use_hexsticker()` - Paste hex sticker to README.md
5356

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,7 @@ Use `usethis::use_standalone("Exceret/rpkgkit", "<name>")` to import:
6161

6262
- `make_func_call_explicit()` - Make function calls explicit by adding
6363
package prefixes
64+
6465
- `render_rmd()` - Render R Markdown documents
66+
67+
- `use_hexsticker()` - Paste hex sticker to README.md

codecov.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
comment: false
2+
3+
coverage:
4+
status:
5+
project:
6+
default:
7+
target: auto
8+
threshold: 1%
9+
informational: true
10+
patch:
11+
default:
12+
target: auto
13+
threshold: 1%
14+
informational: true

man/use_hexsticker.Rd

Lines changed: 51 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)