Skip to content

Commit

Permalink
Merge branch 'main' into collapse-args
Browse files Browse the repository at this point in the history
  • Loading branch information
etiennebacher committed Feb 7, 2025
2 parents 8cc0a3a + 99c392e commit 61cabf8
Show file tree
Hide file tree
Showing 43 changed files with 2,177 additions and 1,803 deletions.
23 changes: 23 additions & 0 deletions .github/workflows/format.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: format

on:
push:
branches: [main, master]
pull_request:
branches: [main, master]

concurrency:
group: ${{ github.workflow }}-${{ github.head_ref }}
cancel-in-progress: true

jobs:
ci:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4
- name: Install `air`
run: curl -LsSf https://github.com/posit-dev/air/releases/latest/download/air-installer.sh | sh
- name: Check for changes
run: air format . --check

35 changes: 28 additions & 7 deletions R/altdoc_variables.R
Original file line number Diff line number Diff line change
@@ -1,11 +1,27 @@
.substitute_altdoc_variables <- function(x, filename, path = ".", tool = "docsify") {
.substitute_altdoc_variables <- function(
x,
filename,
path = ".",
tool = "docsify"
) {
x <- gsub("\\$ALTDOC_VERSION", utils::packageVersion("altdoc"), x)

files <- c("NEWS.md", "CHANGELOG.md", "CODE_OF_CONDUCT.md", "CONTRIBUTING.md", "LICENSE.md", "LICENCE.md", "CITATION.md")
files <- c(
"NEWS.md",
"CHANGELOG.md",
"CODE_OF_CONDUCT.md",
"CONTRIBUTING.md",
"LICENSE.md",
"LICENCE.md",
"CITATION.md"
)
for (vn in files) {
fn <- fs::path_join(c(.doc_path(path), vn))
regex <- sprintf("\\$ALTDOC_%s", fs::path_ext_remove(basename(vn)))
if (fs::file_exists(fn) || fs::file_exists(fs::path_join(c(path, "_quarto", vn)))) {
if (
fs::file_exists(fn) ||
fs::file_exists(fs::path_join(c(path, "_quarto", vn)))
) {
if (tool == "docute") {
new <- paste0("/", vn)
} else {
Expand All @@ -27,7 +43,10 @@
x <- x[!grepl("\\$ALTDOC_PACKAGE_URL_GITHUB", x)]
}

all_urls <- tryCatch(desc::desc_get_urls(path), error = function(e) NULL)
all_urls <- tryCatch(
desc::desc_get_urls(path),
error = function(e) NULL
)
website_url <- Filter(function(x) !grepl("github.com", x), all_urls)

if (length(website_url) > 0) {
Expand All @@ -37,9 +56,11 @@
}

x <- gsub("\\$ALTDOC_PACKAGE_NAME", desc::desc_get("Package", path), x)
x <- gsub("\\$ALTDOC_PACKAGE_VERSION", desc::desc_get("Version", path), x)


x <- gsub(
"\\$ALTDOC_PACKAGE_VERSION",
desc::desc_get("Version", path),
x
)
} else {
x <- gsub("\\$ALTDOC_PACKAGE_NAME", "", x)
x <- gsub("\\$ALTDOC_PACKAGE_VERSION", "", x)
Expand Down
87 changes: 47 additions & 40 deletions R/check.R
Original file line number Diff line number Diff line change
@@ -1,55 +1,62 @@
# Check that project is a package -------------------------

.check_is_package <- function(path) {
if (!.dir_is_package(path)) {
cli::cli_abort("{.code altdoc} only works in packages.")
}
if (!.dir_is_package(path)) {
cli::cli_abort("{.code altdoc} only works in packages.")
}
}

.check_dependency <- function(library_name) {
requireNamespace(library_name, quietly = TRUE)
requireNamespace(library_name, quietly = TRUE)
}

.assert_dependency <- function(library_name, install = FALSE) {
flag <- .check_dependency(library_name)
msg <- sprintf("This functionality requires the `%s` package.", library_name)
if (!isTRUE(flag)) {
if (isTRUE(install)) {
msg <- sprintf("This functionality requires the `%s` package. Do you want to install it?", library_name)
if (isTRUE(utils::askYesNo(msg, default = TRUE))) {
utils::install.packages(library_name)
return(invisible())
}
flag <- .check_dependency(library_name)
msg <- sprintf(
"This functionality requires the `%s` package.",
library_name
)
if (!isTRUE(flag)) {
if (isTRUE(install)) {
msg <- sprintf(
"This functionality requires the `%s` package. Do you want to install it?",
library_name
)
if (isTRUE(utils::askYesNo(msg, default = TRUE))) {
utils::install.packages(library_name)
return(invisible())
}
}
stop(msg, call. = FALSE)
}
stop(msg, call. = FALSE)
}
}


# check if there is only 1 level-one structure
.check_md_structure <- function(file_path) {
if (!fs::file_exists(file_path)) {
return(invisible())
}

content <- .readlines(file_path)
idx <- grep("^```", content)

# backticks are incorrectly paired. We don't know what to do.
if (length(idx) %% 2 != 0) {
return(invisible())
}

# drop code blocks
backtick_pairs <- rev(split(idx, ceiling(seq_along(idx) / 2)))
for (p in backtick_pairs) {
content[p[1]:p[2]] <- NA
}
content <- stats::na.omit(content)

# too many level 1 header
if (isTRUE(sum(grepl("^# ", content)) > 1)) {
bn <- basename(file_path)
cli::cli_alert_danger("Too many level 1 headings in `{bn}`. {.code altdoc} assumes that the only level 1 heading is the title of the document. All other subheadings should be at least level 2, starting with: {.code ##}")
}
if (!fs::file_exists(file_path)) {
return(invisible())
}

content <- .readlines(file_path)
idx <- grep("^```", content)

# backticks are incorrectly paired. We don't know what to do.
if (length(idx) %% 2 != 0) {
return(invisible())
}

# drop code blocks
backtick_pairs <- rev(split(idx, ceiling(seq_along(idx) / 2)))
for (p in backtick_pairs) {
content[p[1]:p[2]] <- NA
}
content <- stats::na.omit(content)

# too many level 1 header
if (isTRUE(sum(grepl("^# ", content)) > 1)) {
bn <- basename(file_path)
cli::cli_alert_danger(
"Too many level 1 headings in `{bn}`. {.code altdoc} assumes that the only level 1 heading is the title of the document. All other subheadings should be at least level 2, starting with: {.code ##}"
)
}
}
6 changes: 4 additions & 2 deletions R/freeze.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
# output = rendered man page or vignette (md file)
# hashes = content of altdoc/freeze.rds
.is_frozen <- function(input, output, hashes) {
if (!fs::file_exists(input) || !fs::file_exists(output) || is.null(hashes)) {
if (
!fs::file_exists(input) || !fs::file_exists(output) || is.null(hashes)
) {
return(FALSE)
}
if (grepl("/vignettes/", input)) {
input <- paste0("vignettes/", basename(input))
input <- paste0("vignettes/", basename(input))
}
out <- FALSE
if (input %in% names(hashes)) {
Expand Down
Loading

0 comments on commit 61cabf8

Please sign in to comment.