|
| 1 | +# CLAUDE.md |
| 2 | + |
| 3 | +This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository. |
| 4 | + |
| 5 | +## What this project is |
| 6 | + |
| 7 | +`{css2r}` is an R package that extracts CSS styles from a website and generates a `bslib`-compatible Shiny theme. It ships both as a reusable R6 class (`css2r`) and as a Shiny app (named "Shiny Copy") that wraps it. |
| 8 | + |
| 9 | +The package is built with the [{golem}](https://thinkr-open.github.io/golem/) framework, which structures Shiny apps as R packages. |
| 10 | + |
| 11 | +## Common commands |
| 12 | + |
| 13 | +All commands are run from an R console inside the project. |
| 14 | + |
| 15 | +**Develop interactively (runs the Shiny app in dev mode):** |
| 16 | +```r |
| 17 | +source("dev/run_dev.R") |
| 18 | +``` |
| 19 | + |
| 20 | +**Run all tests:** |
| 21 | +```r |
| 22 | +devtools::test() |
| 23 | +``` |
| 24 | + |
| 25 | +**Run a single test file:** |
| 26 | +```r |
| 27 | +devtools::test(filter = "css2r") |
| 28 | +``` |
| 29 | + |
| 30 | +**Document and reload the package:** |
| 31 | +```r |
| 32 | +golem::document_and_reload() |
| 33 | +``` |
| 34 | + |
| 35 | +**Check the package (equivalent of R CMD check):** |
| 36 | +```r |
| 37 | +devtools::check() |
| 38 | +``` |
| 39 | + |
| 40 | +**Update DESCRIPTION dependencies from parsed imports:** |
| 41 | +```r |
| 42 | +attachment::att_amend_desc() |
| 43 | +``` |
| 44 | + |
| 45 | +**Rebuild README.md from README.Rmd:** |
| 46 | +```r |
| 47 | +devtools::build_readme() |
| 48 | +``` |
| 49 | + |
| 50 | +## Architecture |
| 51 | + |
| 52 | +### Core R6 class: `R/css2r.R` |
| 53 | + |
| 54 | +The `css2r` R6 class is the main logic unit. It performs a sequential pipeline on initialization: |
| 55 | + |
| 56 | +1. `check_internet()` — verifies connectivity via `{curl}` |
| 57 | +2. `download_html()` — fetches the page with `{rvest}` |
| 58 | +3. `extract_css_links()` — parses `<link rel="stylesheet">` tags |
| 59 | +4. `filter_css_links()` — keeps only links from the same domain |
| 60 | +5. `download_css_files()` — fetches CSS content via `{httr}` |
| 61 | +6. `extract_colors()` — regex-extracts 6-digit hex colors, counts frequency |
| 62 | +7. `analyze_colors()` — splits white/black from top-4 brand colors |
| 63 | +8. `detect_google_fonts()` — identifies Google Fonts from CSS link params |
| 64 | +9. `generate_shiny_code()` — assembles `bslib::bs_theme()` call and stores it in `$shiny_code` and `$shiny_theme` |
| 65 | + |
| 66 | +Each step can also be called manually by passing `on_initialize = FALSE`. |
| 67 | + |
| 68 | +### Shiny app (golem structure) |
| 69 | + |
| 70 | +- `R/app_ui.R` — UI definition using `bslib::page()` with Bootstrap 5; a URL input + task button, and `uiOutput` placeholders for results |
| 71 | +- `R/app_server.R` — server logic: calls `css2r$new()`, applies the extracted theme live via `session$setCurrentTheme()`, and sends top colors to JS via `session$sendCustomMessage(type = "apply_gradient", ...)` |
| 72 | +- `R/run_app.R` — exported `run_app()` function (golem entry point) |
| 73 | +- `R/app_config.R` — golem configuration helpers (`app_sys()`, `get_golem_options()`) |
| 74 | +- `inst/app/www/` — static assets: `custom.css`, `handler.js` (handles the `apply_gradient` JS message), `favicon.ico` |
| 75 | +- `app.R` — thin launcher for deployment (`pkgload::load_all()` + `run_app()`) |
| 76 | + |
| 77 | +### Testing |
| 78 | + |
| 79 | +Tests live in `tests/testthat/`. The main test (`test-css2r.R`) hits the live `thinkr.fr` website and is wrapped in `skip_if_offline()`. CI runs R CMD check across macOS, Windows, and Ubuntu via `.github/workflows/R-CMD-check.yaml`. |
0 commit comments