Skip to content

Commit 9273fa9

Browse files
authored
Merge pull request #109 from pharmaverse/82_set_up_styling_and_spelling_github_actions
Closes #82 add spelling action
2 parents 6eb73b6 + aa4748c commit 9273fa9

37 files changed

+996
-75
lines changed

.Rbuildignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
^\.github$

.github/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.html

.github/workflows/link_check.yml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
name: Links (Fail Fast)
2+
3+
on:
4+
pull_request:
5+
branches:
6+
- main
7+
8+
jobs:
9+
linkChecker:
10+
runs-on: ubuntu-latest
11+
steps:
12+
- uses: actions/checkout@v4
13+
14+
- name: Set up R # Install R from CRAN
15+
uses: r-lib/actions/setup-r@v2
16+
with:
17+
r-version: '4.4.3' # You can specify a different R version if needed
18+
19+
- name: Install R packages
20+
run: |
21+
Rscript -e 'install.packages("fs")'
22+
shell: bash
23+
24+
- name: Switch .qmd to .md
25+
run: Rscript R/switch.R
26+
shell: bash
27+
28+
- name: Link Checker
29+
uses: lycheeverse/[email protected]
30+
with:
31+
fail: true
32+
env:
33+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

.github/workflows/spellcheck.yml

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
name: Spellcheck
2+
on:
3+
pull_request:
4+
branches: ['main']
5+
jobs:
6+
Spelling:
7+
runs-on: ubuntu-latest
8+
container:
9+
image: "rocker/tidyverse:4.3.1"
10+
steps:
11+
- name: Checkout repo
12+
uses: actions/checkout@v4
13+
14+
- name: Install packages
15+
run: |
16+
if (!require("roxygen2")) install.packages("roxygen2")
17+
if (!require("spelling")) install.packages("spelling")
18+
if (!require("testthat")) install.packages("testthat")
19+
shell: Rscript {0}
20+
21+
- name: Remove links from .qmd files (Bash)
22+
run: |
23+
find . -type f -name "*.qmd" -print0 | while IFS= read -r -d '' file; do
24+
sed -i -E '
25+
s/\[([^\]]+)\]\((https?:\/\/[^)]+)\)/\1/g; # Remove Markdown links, keep text
26+
s#https?://[a-zA-Z0-9./?=_-]+(\#[a-zA-Z0-9_-]*)?##g # Remove standalone URLs, including fragments
27+
' "$file"
28+
echo "Processed: $file"
29+
done
30+
shell: bash
31+
32+
- name: Check spelling
33+
run: |
34+
library(spelling)
35+
library(readr)
36+
37+
qmd_files <- list.files(pattern = ".*\\.qmd$", recursive = TRUE)
38+
39+
words <- spelling::spell_check_files(
40+
qmd_files,
41+
ignore = read_lines("inst/WORDLIST")
42+
)
43+
44+
print(words)
45+
46+
testthat::test_that(desc = "Check spelling", code = {
47+
testthat::expect_equal(
48+
object = words,
49+
expected = spelling::spell_check_files(
50+
path = "inst/WORDLIST",
51+
ignore = read_lines("inst/WORDLIST")
52+
)
53+
)
54+
})
55+
shell: Rscript {0}

.github/workflows/style_check.yml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
name: Style
2+
on:
3+
pull_request: {branches: ['main']}
4+
5+
jobs:
6+
Style:
7+
runs-on: ubuntu-latest
8+
container: {image: "rocker/tidyverse:4.2.1"}
9+
steps:
10+
- name: Checkout repo
11+
uses: actions/checkout@v4
12+
- name: Install styler
13+
run: install.packages("styler")
14+
shell: Rscript {0}
15+
- name: styler version
16+
run: packageVersion("styler")
17+
shell: Rscript {0}
18+
- name: Run styler
19+
run: styler::style_dir(dry = "fail")
20+
shell: Rscript {0}

DESCRIPTION

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,5 @@ Suggests:
5050
knitr,
5151
rmarkdown
5252
Encoding: UTF-8
53+
Language: en-US
54+

NAMESPACE

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Generated by roxygen2: do not edit by hand
2+

R/CICD.R

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# Load required packages
2+
library(spelling)
3+
library(readr)
4+
library(stringr)
5+
library(dplyr)
6+
7+
#-------------------------- Spell-check Without Modifying Files --------------------------
8+
9+
# Function to extract non-code content while keeping comments from code blocks
10+
clean_text_for_spellcheck <- function(file) {
11+
text <- read_lines(file)
12+
13+
text_clean <- text %>%
14+
# Remove Markdown links but keep link text
15+
str_replace_all("\\[([^\\]]+)\\]\\([^)]*\\)", "\\1") %>%
16+
# Remove standalone URLs
17+
str_replace_all("https?://[^\\s)\"']+", "")
18+
19+
return(text_clean)
20+
}
21+
22+
#-------------------------- Spell-check Without Modifying Files --------------------------
23+
24+
# Get all .qmd files
25+
qmd_files <- list.files(pattern = ".*\\.qmd$", recursive = TRUE)
26+
file <- qmd_files[63]
27+
# Run spell check per file while ignoring code chunks
28+
29+
spell_check_results <- lapply(qmd_files, function(file) {
30+
cleaned_text <- clean_text_for_spellcheck(file)
31+
words <- spelling::spell_check_text(cleaned_text, ignore = read_lines("inst/WORDLIST"))
32+
33+
if (nrow(words) > 0) {
34+
words$file <- file # Add filename column
35+
}
36+
37+
return(words)
38+
})
39+
40+
# Combine results into a single dataframe
41+
all_typos <- bind_rows(spell_check_results)
42+
43+
# Print results
44+
if (nrow(all_typos) > 0) {
45+
print(all_typos %>% select(file, word, found))
46+
} else {
47+
message("No spelling errors found!")
48+
}
49+
50+
#-------------------------- Add Words to Wordlist (If Needed) --------------------------
51+
52+
# Uncomment if you want to manually add words to the wordlist
53+
# write(all_typos$word, file = "inst/WORDLIST", append = TRUE)
54+
# sort_words <- sort(read_lines("inst/WORDLIST")) %>% unique()
55+
# write_lines(sort_words, "inst/WORDLIST")
56+
57+
#-------------------------- Style-check ----------------------------------------
58+
59+
# This is what happens in CI/CD:
60+
61+
styler::style_dir(dry = "fail")
62+
63+
# Fix it locally:
64+
styler::style_dir()
65+
66+
# Now it should pass:
67+
styler::style_dir(dry = "fail")

R/switch.R

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#' fixes link-check problem
2+
#' Finds whether any files use the string: "(.mailto:" for linking email adresses
3+
#' then just replaces with: "(mailto:" as is markdown standard
4+
#'
5+
#' @param file_list vector of filenames
6+
#'
7+
#' @return messages only. side effect is: changes files.
8+
modify_files <- function(file_list) {
9+
# Create an empty vector to store the file names that contain the string
10+
matching_files <- c()
11+
12+
# Iterate over each file in the directory
13+
for (file in file_list) {
14+
# Read the contents of the file
15+
file_contents <- readLines(file)
16+
17+
# Check if the file contains the string "(.mailto:"
18+
if (any(grepl("\\(\\.mailto:", file_contents))) {
19+
# Add the file name to the vector
20+
matching_files <- c(matching_files, file)
21+
}
22+
}
23+
24+
# Iterate over the matching files
25+
for (file in matching_files) {
26+
# Read the contents of the file
27+
file_contents <- readLines(file)
28+
29+
# Remove the "." from each line that contains the string
30+
modified_contents <- gsub("\\(\\.mailto:", "(mailto:", file_contents)
31+
32+
# Write the modified contents back to the file
33+
writeLines(modified_contents, file)
34+
35+
# Print a message indicating the modification has been made
36+
message("Modified file:", file, "\n")
37+
}
38+
39+
# Print the list of matching files
40+
message("Matching files:", matching_files, "\n")
41+
}
42+
43+
# get all qmd files
44+
all_qmd <- list.files(full.names = FALSE, all.files = FALSE, pattern = ".qmd$", recursive = TRUE)
45+
46+
# modify if needed the link to email problem for link checker
47+
modify_files(all_qmd)
48+
# get filenames ending with .md
49+
all_md <- gsub(".qmd$", ".md", all_qmd)
50+
# rename all files
51+
file.rename(all_qmd, all_md)

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
---
2+
editor:
3+
markdown:
4+
wrap: 72
5+
---
6+
17
# Pharmaverse Examples
28

39
The true beauty of pharmaverse (and open source in general) is when
@@ -80,3 +86,7 @@ the packages being used.
8086
We recommend you notify the maintainers of all the packages used in your
8187
example, as then they are more likely to help inform us if ever anything
8288
changes in their packages that breaks any of our examples.
89+
90+
To check spelling and style in a post, open the `CICD.R` file in the R/
91+
folder. Run the script line by line to first check the spelling in your
92+
post and then to make sure your code is compatible with our code-style.

0 commit comments

Comments
 (0)