|
| 1 | + |
| 2 | +read_sponsors <- function(csv_path) { |
| 3 | + read.csv(csv_path, stringsAsFactors = FALSE) |
| 4 | +} |
| 5 | + |
| 6 | +render_sponsor_grid <- function(df, ncol = 4, img_prefix = "", show_name = FALSE) { |
| 7 | + stopifnot(all(c("image", "website") %in% names(df))) |
| 8 | + width <- floor(100 / ncol) |
| 9 | + |
| 10 | + url_join <- function(prefix, path) { |
| 11 | + if (is.null(prefix) || prefix == "") return(path) |
| 12 | + prefix <- sub("/+$", "", prefix) |
| 13 | + path <- sub("^/+", "", path) |
| 14 | + paste0(prefix, "/", path) |
| 15 | + } |
| 16 | + |
| 17 | + out <- c() |
| 18 | + out <- c(out, ":::: {.columns}") |
| 19 | + out <- c(out, "::: {.column width=\"10%\"}") |
| 20 | + out <- c(out, ":::") |
| 21 | + out <- c(out, "::: {.column width=\"90%\"}", "") |
| 22 | + out <- c(out, ":::: {.columns}") |
| 23 | + |
| 24 | + for (i in seq_len(nrow(df))) { |
| 25 | + img <- url_join(img_prefix, df$image[i]) |
| 26 | + |
| 27 | + out <- c(out, sprintf("::: {.column width=\"%s%%\"}", width)) |
| 28 | + |
| 29 | + # Use HTML img + CSS class (NO markdown width=) |
| 30 | + out <- c(out, sprintf( |
| 31 | + "<a href='%s' target='_blank' class='sponsor-link'>%s</a>", |
| 32 | + df$website[i], |
| 33 | + sprintf("<img src='%s' alt='%s' class='sponsor-logo'>", |
| 34 | + img, |
| 35 | + if ("name" %in% names(df)) df$name[i] else "Sponsor logo") |
| 36 | + )) |
| 37 | + |
| 38 | + if (show_name && ("name" %in% names(df)) && nzchar(df$name[i])) { |
| 39 | + out <- c(out, sprintf("<div class='sponsor-name'>%s</div>", df$name[i])) |
| 40 | + } |
| 41 | + |
| 42 | + out <- c(out, ":::") |
| 43 | + } |
| 44 | + |
| 45 | + out <- c(out, "::::", "") |
| 46 | + out <- c(out, ":::") |
| 47 | + out <- c(out, "::: {.column width=\"10%\"}") |
| 48 | + out <- c(out, ":::") |
| 49 | + out <- c(out, "::::") |
| 50 | + |
| 51 | + cat(paste(out, collapse = "\n")) |
| 52 | +} |
| 53 | + |
| 54 | + |
| 55 | +render_sponsors_home <- function(csv_path, title = "", ncol = 4) { |
| 56 | + df <- read_sponsors(csv_path) |
| 57 | + cat(sprintf("# %s\n\n", title)) |
| 58 | + render_sponsor_grid(df, ncol = ncol) |
| 59 | +} |
| 60 | + |
| 61 | +render_sponsors_by_level <- function( |
| 62 | + csv_path, |
| 63 | + level_order = c("Diamond", "Gold", "Silver", "Bronze", "Supporter"), |
| 64 | + ncol_by_level = c(Diamond = 2, Gold = 3, Silver = 5, Bronze = 6, Supporter = 6), |
| 65 | + heading = c("bold", "h2"), |
| 66 | + img_prefix = "" |
| 67 | +) { |
| 68 | + heading <- match.arg(heading) |
| 69 | + |
| 70 | + df <- read_sponsors(csv_path) |
| 71 | + if (!("level" %in% names(df))) stop("CSV must include a 'level' column.") |
| 72 | + |
| 73 | + df$level <- factor(df$level, levels = level_order) |
| 74 | + |
| 75 | + levels_present <- unique(as.character(df$level)) |
| 76 | + levels_present <- level_order[level_order %in% levels_present] |
| 77 | + |
| 78 | + for (lvl in levels_present) { |
| 79 | + df_lvl <- df[as.character(df$level) == lvl, , drop = FALSE] |
| 80 | + if (nrow(df_lvl) == 0) next |
| 81 | + |
| 82 | + if (heading == "h2") { |
| 83 | + cat(sprintf("\n## %s\n\n", lvl)) |
| 84 | + } else { |
| 85 | + cat(sprintf("\n**%s**\n\n", lvl)) |
| 86 | + } |
| 87 | + |
| 88 | + ncol <- unname(ncol_by_level[lvl]) |
| 89 | + if (is.na(ncol)) ncol <- 4 |
| 90 | + |
| 91 | + render_sponsor_grid(df_lvl, ncol = ncol, img_prefix = img_prefix) |
| 92 | + cat("\n") |
| 93 | + } |
| 94 | +} |
0 commit comments