use std::fs;
use std::path::{Path, PathBuf};
pub const REPO: &str = "https://github.com/vonglurt/radbeeper";
pub const SITE: &str = "https://vonglurt.github.io/radbeeper/";
pub const CRATE: &str = "https://crates.io/crates/radbeeper";
pub const COPAL: &str = "https://github.com/vonglurt/copal";
pub const DESCRIPTION: &str = "RadBeeper shows what a GQ GMC-320 Plus Geiger \
counter is counting, on Linux: a reading at a glance in a terminal or a \
window, a log with no gaps, a web page you can publish, and a drum \
spectrogram that shows whether anything is arriving on a schedule. For \
Alpine and Copal Linux.";
pub const KEYWORDS: &str = "geiger counter linux, GMC-320 Plus, GQ GMC, geiger \
muller counter USB, radiation monitor Linux, tty serial geiger counter, counts \
per minute logging, radiation data logging, Alpine Linux geiger, Copal Linux, \
Hyprland Wayland radiation monitor, rust geiger counter, CPM to uSv/h";
pub fn esc_text(s: &str) -> String {
s.replace('&', "&").replace('<', "<").replace('>', ">")
}
pub fn esc_attr(s: &str) -> String {
esc_text(s).replace('"', """).replace('\'', "'")
}
pub fn slug(text: &str) -> String {
let lower = text.to_lowercase();
let kept: String = lower
.chars()
.filter(|c| c.is_alphanumeric() || *c == '_' || c.is_whitespace() || *c == '-')
.collect();
let mut out = String::with_capacity(kept.len());
let mut in_space = false;
for c in kept.chars() {
if c.is_whitespace() {
in_space = true;
continue;
}
if in_space && !out.is_empty() {
out.push('-');
}
in_space = false;
out.push(c);
}
out.trim_matches('-').to_string()
}
fn word_or(c: char, marker: char) -> bool {
c.is_alphanumeric() || c == '_' || c == marker
}
fn wrap(src: &[char], marker: char, run: usize, tag: &str, guard: bool) -> Vec<char> {
let mut out: Vec<char> = Vec::with_capacity(src.len());
let mut i = 0usize;
'scan: while i < src.len() {
let starts = i + run <= src.len() && src[i..i + run].iter().all(|c| *c == marker);
if starts {
if guard {
if i > 0 && word_or(src[i - 1], marker) {
out.push(src[i]);
i += 1;
continue;
}
}
let mut j = i + run;
while j + run <= src.len() {
if src[j] == '\n' {
break;
}
let closes = src[j..j + run].iter().all(|c| *c == marker);
if closes && j > i + run {
if guard {
let after = src.get(j + run).copied();
if after.map(|c| word_or(c, marker)).unwrap_or(false) {
break;
}
}
out.extend(format!("<{}>", tag).chars());
out.extend_from_slice(&src[i + run..j]);
out.extend(format!("</{}>", tag).chars());
i = j + run;
continue 'scan;
}
if guard && src[j] == marker {
break;
}
j += 1;
}
}
out.push(src[i]);
i += 1;
}
out
}
fn bracket(src: &[char], i: usize) -> Option<(String, String, usize)> {
if src.get(i) != Some(&'[') {
return None;
}
let mut j = i + 1;
while j < src.len() && src[j] != ']' {
j += 1;
}
if j >= src.len() || src.get(j + 1) != Some(&'(') {
return None;
}
let text: String = src[i + 1..j].iter().collect();
let mut k = j + 2;
while k < src.len() && src[k] != ')' {
k += 1;
}
if k >= src.len() {
return None;
}
let target: String = src[j + 2..k].iter().collect();
Some((text, target, k + 1))
}
pub fn inline(text: &str, link: &dyn Fn(&str) -> String) -> String {
let mut held: Vec<String> = Vec::new();
let src: Vec<char> = text.chars().collect();
let mut staged: Vec<char> = Vec::with_capacity(src.len());
let mut i = 0usize;
while i < src.len() {
if src[i] == '`' {
if let Some(end) = (i + 1..src.len()).find(|k| src[*k] == '`') {
if end > i + 1 {
held.push(src[i + 1..end].iter().collect());
staged.extend(format!("\u{0}{}\u{0}", held.len() - 1).chars());
i = end + 1;
continue;
}
}
}
staged.push(src[i]);
i += 1;
}
let escaped: Vec<char> = esc_text(&staged.iter().collect::<String>()).chars().collect();
let mut out: Vec<char> = Vec::with_capacity(escaped.len());
let mut i = 0usize;
while i < escaped.len() {
if escaped[i] == '!' {
if let Some((alt, target, next)) = bracket(&escaped, i + 1) {
out.extend(
format!(
"<img src=\"{}\" alt=\"{}\" loading=\"lazy\">",
esc_attr(&target),
esc_attr(&alt)
)
.chars(),
);
i = next;
continue;
}
}
if escaped[i] == '[' {
if let Some((label, target, next)) = bracket(&escaped, i) {
if !label.is_empty() {
out.extend(
format!("<a href=\"{}\">{}</a>", esc_attr(&link(&target)), label)
.chars(),
);
i = next;
continue;
}
}
}
out.push(escaped[i]);
i += 1;
}
let out = wrap(&out, '*', 2, "strong", false);
let out = wrap(&out, '*', 1, "em", true);
let out = wrap(&out, '_', 1, "em", true);
let mut text: String = String::with_capacity(out.len());
let mut i = 0usize;
while i < out.len() {
if out[i] == '\u{0}' {
let mut j = i + 1;
let mut n = String::new();
while j < out.len() && out[j].is_ascii_digit() {
n.push(out[j]);
j += 1;
}
if out.get(j) == Some(&'\u{0}') && !n.is_empty() {
let k: usize = n.parse().unwrap_or(0);
text.push_str(&format!(
"<code>{}</code>",
esc_text(held.get(k).map(|s| s.as_str()).unwrap_or(""))
));
i = j + 1;
continue;
}
}
text.push(out[i]);
i += 1;
}
text
}
fn is_blank(l: &str) -> bool {
l.trim().is_empty()
}
fn heading(l: &str) -> Option<(usize, &str)> {
let hashes = l.len() - l.trim_start_matches('#').len();
if !(1..=6).contains(&hashes) {
return None;
}
let rest = &l[hashes..];
if !rest.starts_with(|c: char| c.is_whitespace()) {
return None;
}
Some((hashes, rest.trim_start()))
}
fn is_rule(l: &str) -> bool {
let t = l.trim_end();
t.len() >= 3 && t.chars().all(|c| c == '-')
}
fn is_table_rule(l: &str) -> bool {
l.starts_with('|')
&& l.len() > 1
&& l.trim_end()
.chars()
.all(|c| c == '|' || c == '-' || c == ':' || c.is_whitespace())
}
fn bullet(l: &str) -> Option<(bool, &str)> {
let t = l.trim_start();
if let Some(rest) = t.strip_prefix("- ").or_else(|| t.strip_prefix("* ")) {
return Some((false, rest));
}
let digits = t.len() - t.trim_start_matches(|c: char| c.is_ascii_digit()).len();
if digits > 0 {
let after = &t[digits..];
if let Some(rest) = after.strip_prefix(". ") {
return Some((true, rest));
}
}
None
}
fn is_raw_html(l: &str) -> bool {
let t = l.strip_prefix('<').map(|r| r.strip_prefix('/').unwrap_or(r));
match t {
Some(r) => ["details", "summary"].iter().any(|tag| {
r.starts_with(tag)
&& r[tag.len()..]
.chars()
.next()
.map(|c| !c.is_alphanumeric() && c != '_')
.unwrap_or(true)
}),
None => false,
}
}
fn breaks_paragraph(l: &str) -> bool {
l.starts_with("```")
|| heading(l).is_some()
|| is_rule(l)
|| l.starts_with('|')
|| l.starts_with("> ")
|| bullet(l).is_some()
|| is_raw_html(l)
}
pub fn render(md: &str, link: &dyn Fn(&str) -> String) -> String {
let lines: Vec<&str> = md.split('\n').collect();
let mut out: Vec<String> = Vec::new();
let mut i = 0usize;
while i < lines.len() {
let line = lines[i];
if line.starts_with("<!--") {
while i < lines.len() && !lines[i].contains("-->") {
i += 1;
}
i += 1;
continue;
}
if is_raw_html(line) {
if let (Some(a), Some(b)) = (line.find("<summary>"), line.find("</summary>")) {
let inner = &line[a + "<summary>".len()..b];
out.push(format!("<summary>{}</summary>", inline(inner, link)));
} else {
out.push(line.to_string());
}
i += 1;
continue;
}
if is_blank(line) {
i += 1;
continue;
}
if let Some(info) = line.strip_prefix("```") {
let lang = info.trim();
i += 1;
let mut body: Vec<&str> = Vec::new();
while i < lines.len() && !lines[i].starts_with("```") {
body.push(lines[i]);
i += 1;
}
i += 1;
let cls = if lang.is_empty() {
String::new()
} else {
format!(" class=\"language-{}\"", esc_attr(lang))
};
out.push(format!(
"<pre><code{}>{}</code></pre>",
cls,
esc_text(&body.join("\n"))
));
continue;
}
if line.starts_with(" ") && !line[4..].starts_with(char::is_whitespace)
&& !line[4..].is_empty()
{
let mut body: Vec<String> = Vec::new();
while i < lines.len() && (lines[i].starts_with(" ") || is_blank(lines[i])) {
body.push(lines[i].chars().skip(4).collect());
i += 1;
}
out.push(format!(
"<pre><code>{}</code></pre>",
esc_text(body.join("\n").trim_matches('\n'))
));
continue;
}
if let Some((n, text)) = heading(line) {
out.push(format!(
"<h{} id=\"{}\">{}</h{}>",
n,
esc_attr(&slug(text)),
inline(text, link),
n
));
i += 1;
continue;
}
if is_rule(line) {
out.push("<hr>".to_string());
i += 1;
continue;
}
if line.starts_with('|')
&& i + 1 < lines.len()
&& is_table_rule(lines[i + 1])
{
let cells = |l: &str| -> Vec<String> {
l.trim_matches('|')
.split('|')
.map(|c| c.trim().to_string())
.collect()
};
let head = cells(line);
i += 2;
let mut body: Vec<Vec<String>> = Vec::new();
while i < lines.len() && lines[i].starts_with('|') {
body.push(cells(lines[i]));
i += 1;
}
let th: String = head
.iter()
.map(|c| format!("<th>{}</th>", inline(c, link)))
.collect();
let thead = if head.iter().all(|c| c.trim().is_empty()) {
String::new()
} else {
format!("<thead><tr>{}</tr></thead>", th)
};
let rows: String = body
.iter()
.map(|r| {
format!(
"<tr>{}</tr>",
r.iter()
.map(|c| format!("<td>{}</td>", inline(c, link)))
.collect::<String>()
)
})
.collect();
out.push(format!("<table>{}<tbody>{}</tbody></table>", thead, rows));
continue;
}
if line.starts_with("> ") {
let mut body: Vec<String> = Vec::new();
while i < lines.len() && lines[i].starts_with('>') {
body.push(lines[i].trim_start_matches('>').trim().to_string());
i += 1;
}
out.push(format!(
"<blockquote><p>{}</p></blockquote>",
inline(&body.join(" "), link)
));
continue;
}
if let Some((ordered, first)) = bullet(line) {
let tag = if ordered { "ol" } else { "ul" };
let mut items: Vec<String> = Vec::new();
items.push(first.to_string());
i += 1;
while i < lines.len() {
if let Some((_, text)) = bullet(lines[i]) {
items.push(text.to_string());
i += 1;
} else if lines[i].starts_with(" ") && !is_blank(lines[i]) && !items.is_empty() {
let last = items.len() - 1;
items[last] = format!("{} {}", items[last], lines[i].trim());
i += 1;
} else {
break;
}
}
out.push(format!(
"<{}>{}</{}>",
tag,
items
.iter()
.map(|t| format!("<li>{}</li>", inline(t, link)))
.collect::<String>(),
tag
));
continue;
}
let mut body: Vec<&str> = Vec::new();
while i < lines.len() && !is_blank(lines[i]) {
if breaks_paragraph(lines[i]) {
break;
}
body.push(lines[i].trim());
i += 1;
}
if body.is_empty() {
i += 1;
} else {
out.push(format!("<p>{}</p>", inline(&body.join(" "), link)));
}
}
out.join("\n")
}
pub const CSS: &str = r##"
:root{--bg:#fbfaf7;--fg:#1e2a3a;--dim:#5b6472;--faint:#8a919c;--line:#e2ded4;
--card:#fff;--accent:#1e6b63;--code:#f4f2ec}
@media (prefers-color-scheme:dark){:root{--bg:#161822;--fg:#d6dbe6;--dim:#9ea3b8;
--faint:#6d7383;--line:#2a2e3d;--card:#1e2130;--accent:#8cf2e6;--code:#1a1d29}}
*{box-sizing:border-box}
body{margin:0;background:var(--bg);color:var(--fg);
font:16px/1.65 ui-sans-serif,system-ui,-apple-system,"Segoe UI",Roboto,sans-serif}
.wrap{max-width:60rem;margin:0 auto;padding:0 1.25rem}
header.hero{border-bottom:1px solid var(--line);padding:3rem 0 2.25rem}
.drum img{display:block;width:100%;height:auto;margin:1.75rem 0 0;
border:1px solid var(--line);border-radius:8px;background:var(--card)}
.caption{font-size:.9rem;color:var(--dim);margin:.6rem 0 0;max-width:44rem}
.eyebrow{font:600 .75rem/1 ui-monospace,SFMono-Regular,Menlo,monospace;
letter-spacing:.14em;text-transform:uppercase;color:var(--accent);margin:0 0 .9rem}
h1{font-size:2.6rem;line-height:1.12;margin:0 0 .75rem;letter-spacing:-.02em}
.lede{font-size:1.15rem;color:var(--dim);margin:0 0 1.5rem;max-width:44rem}
.badges{display:flex;flex-wrap:wrap;gap:.5rem;margin:0 0 1.5rem;padding:0;list-style:none}
.badges li{font:.8rem/1 ui-monospace,SFMono-Regular,Menlo,monospace;
border:1px solid var(--line);border-radius:999px;padding:.45rem .8rem;color:var(--dim)}
.cta{display:flex;flex-wrap:wrap;gap:.6rem}
.cta a{display:inline-block;text-decoration:none;border-radius:.4rem;
padding:.6rem 1.1rem;font-weight:600;font-size:.95rem;border:1px solid var(--line)}
.cta a.primary{background:var(--accent);color:var(--bg);border-color:var(--accent)}
.facts{display:grid;gap:1rem;grid-template-columns:repeat(auto-fit,minmax(15rem,1fr));
padding:2rem 0;border-bottom:1px solid var(--line)}
.fact{background:var(--card);border:1px solid var(--line);border-radius:.5rem;padding:1rem 1.1rem}
.fact h2{font-size:.78rem;letter-spacing:.1em;text-transform:uppercase;
color:var(--accent);margin:0 0 .4rem}
.fact p{margin:0;font-size:.92rem;color:var(--dim)}
main{padding:2.5rem 0 3rem}
main h1,main h2,main h3,main h4{line-height:1.25;margin:2rem 0 .7rem;letter-spacing:-.01em}
main h1{font-size:1.9rem} main h2{font-size:1.45rem;padding-top:.6rem;
border-top:1px solid var(--line)} main h3{font-size:1.12rem} main h4{font-size:1rem}
main>h1:first-child{margin-top:0;border:0}
p{margin:0 0 1rem}
a{color:var(--accent)}
img{max-width:100%;height:auto;border-radius:.4rem;display:block;margin:1.2rem 0}
code{font:.88em/1.5 ui-monospace,SFMono-Regular,Menlo,monospace;
background:var(--code);padding:.13em .38em;border-radius:.25rem}
pre{background:var(--code);border:1px solid var(--line);border-radius:.45rem;
padding:.9rem 1rem;overflow-x:auto;margin:0 0 1.2rem}
pre code{background:none;padding:0;font-size:.85rem;line-height:1.55}
table{border-collapse:collapse;width:100%;margin:0 0 1.3rem;font-size:.93rem;display:block;overflow-x:auto}
th,td{border:1px solid var(--line);padding:.5rem .7rem;text-align:left;vertical-align:top}
th{background:var(--card);font-weight:600}
blockquote{margin:0 0 1.2rem;padding:.7rem 1rem;border-left:3px solid var(--accent);
background:var(--card);color:var(--dim)}
blockquote p{margin:0}
hr{border:0;border-top:1px solid var(--line);margin:2rem 0}
ul,ol{margin:0 0 1.1rem;padding-left:1.4rem} li{margin:.3rem 0}
details{border:1px solid var(--line);border-radius:.45rem;padding:.7rem 1rem;margin:0 0 1.2rem;
background:var(--card)} summary{cursor:pointer;font-weight:600}
footer{border-top:1px solid var(--line);padding:2rem 0 3rem;color:var(--faint);font-size:.88rem}
footer a{color:var(--dim)}
.nav{display:flex;flex-wrap:wrap;gap:1.1rem;margin:0 0 1rem;padding:0;list-style:none;font-size:.9rem}
@media (max-width:40rem){h1{font-size:2rem}.lede{font-size:1.05rem}}
"##;
fn hero() -> String {
format!(r##"<header class="hero"><div class="wrap">
<p class="eyebrow">GQ GMC-320 Plus · USB · Linux</p>
<h1>See what your Geiger counter is counting, and keep a record of it</h1>
<p class="lede">Plug in a <strong>GQ GMC-320 Plus</strong>, switch it on, and
RadBeeper finds it. It shows the reading at a glance, in a terminal or a
window, logs it whether or not you are watching, and builds a web page you
can publish. Its <strong>drum spectrogram</strong> shows whether anything is
reaching the counter on a schedule, which radiation never does.</p>
<ul class="badges">
<li>Free, MIT</li><li>One command to install</li><li>Terminal and window</li>
<li>Alpine & Copal Linux</li><li>One counter or several</li>
</ul>
<div class="cta">
<a class="primary" href="#get-started">Get started</a>
<a href="monitor.html">See a live counter's report</a>
<a href="docs/reading-the-drum-spectrogram.html">The drum spectrogram</a>
<a href="docs/technical-description.html">How it works</a>
<a href="{}">Source</a>
</div>
<picture class="drum">
<source srcset="docs/screenshots/gui-drum-dark.gif" media="(prefers-color-scheme: dark)">
<img src="docs/screenshots/gui-drum-antiquity.gif"
alt="radbeeper-gui filling from empty: the dials, the counts strip and the drum spectrogram, a frame per drum row over twenty-two minutes">
</picture>
<p class="caption">Twenty-two minutes of two counters, a frame per row of the
drum, from the moment the window opened. Long captures are the point: a
schedule shows up as a stripe, and a stripe takes time.</p>
</div></header>
<div class="wrap"><section class="facts">
<div class="fact"><h2>Read it at a glance</h2><p>One big number, dials with
the bands named on them, and <strong>five averages at once</strong>, from
three seconds to a working day. You see what the counter says now and whether
now is unusual.</p></div>
<div class="fact"><h2>Half a day on one line</h2><p>A strip of counts that
shows this second in full and the past twelve hours in outline, with a trend
line over it. Point at any bar to see when it was and what it read.</p></div>
<div class="fact"><h2>The drum spectrogram</h2><p>A chart drawn like a
seismograph's paper. <strong>Green is ordinary background.</strong> A hot
stripe from top to bottom is something arriving on a schedule: a fan, a
fault, electrical noise. <a href="docs/reading-the-drum-spectrogram.html">How
to read it</a>.</p></div>
<div class="fact"><h2>A record with no gaps</h2><p>A row to disk every thirty
seconds. The counter's own memory is read back to fill in the time nothing
was listening, and the whole record becomes a web page with one
command.</p></div>
<div class="fact"><h2>One counter or several</h2><p>Plug in a second and both
are read, with nothing to set. The reading gets more precise, each keeps its
own log, and one that stops answering is left out until it
returns.</p></div>
<div class="fact"><h2>Made for Copal Linux</h2><p>RadBeeper is built for
<a href="{}"><strong>Copal</strong></a>, which is made from
<strong>Alpine Linux</strong>, and runs on either. It installs from
<a href="{}">crates.io</a> or as a ready-made binary.
<a href="docs/technical-description.html">The technical
description</a> has the rest.</p></div>
</section></div>
"##, REPO, COPAL, CRATE)
}
fn head(title: &str, description: &str, canonical: &str, extra: &str) -> String {
format!(
"<!DOCTYPE html>\n<html lang=\"en\">\n<head>\n\
<meta charset=\"utf-8\">\n\
<meta name=\"viewport\" content=\"width=device-width,initial-scale=1\">\n\
<title>{}</title>\n\
<meta name=\"description\" content=\"{}\">\n\
<link rel=\"canonical\" href=\"{}\">\n\
<meta property=\"og:type\" content=\"website\">\n\
<meta property=\"og:title\" content=\"{}\">\n\
<meta property=\"og:description\" content=\"{}\">\n\
<meta property=\"og:url\" content=\"{}\">\n\
<meta property=\"og:image\" content=\"{}docs/screenshots/gui-dark.png\">\n\
<meta name=\"twitter:card\" content=\"summary_large_image\">\n\
<style>{}</style>\n{}</head>\n<body>\n",
esc_attr(title),
esc_attr(description),
esc_attr(canonical),
esc_attr(title),
esc_attr(description),
esc_attr(canonical),
SITE,
CSS,
extra
)
}
fn json_str(s: &str) -> String {
format!("\"{}\"", s.replace('\\', "\\\\").replace('"', "\\\""))
}
fn jsonld(version: &str) -> String {
format!(
"<script type=\"application/ld+json\">\n\
{{\"@context\":\"https://schema.org\",\"@type\":\"SoftwareSourceCode\",\n\
\"name\":\"RadBeeper\",\"description\":{},\n\
\"url\":\"{}\",\"codeRepository\":\"{}\",\"programmingLanguage\":\"Rust\",\"license\":\n\
\"https://opensource.org/licenses/MIT\",\"version\":\"{}\",\n\
\"runtimePlatform\":\"Linux\",\"operatingSystem\":\"Linux, Alpine Linux, Copal Linux\",\n\
\"applicationCategory\":\"Science\",\"keywords\":{},\n\
\"author\":{{\"@type\":\"Person\",\"name\":\"Paul Richeson\"}},\n\
\"about\":{{\"@type\":\"Thing\",\"name\":\"Geiger-Muller counter data logging\"}}}}\n\
</script>\n",
json_str(DESCRIPTION),
SITE,
REPO,
version,
json_str(KEYWORDS)
)
}
pub fn relative(here: &str, there: &str) -> String {
let base: Vec<&str> = here.split('/').collect();
let base = &base[..base.len().saturating_sub(1)];
let target: Vec<&str> = there.split('/').collect();
let mut common = 0usize;
while common < base.len() && common + 1 < target.len() && base[common] == target[common] {
common += 1;
}
let mut parts: Vec<String> = vec!["..".to_string(); base.len() - common];
parts.extend(target[common..].iter().map(|s| s.to_string()));
if parts.is_empty() {
".".to_string()
} else {
parts.join("/")
}
}
fn nav(here: &str) -> String {
let items: [(&str, &str); 7] = [
("index.html", "Overview"),
("monitor.html", "Live report"),
("docs/reading-the-drum-spectrogram.html", "Drum spectrogram"),
("docs/technical-description.html", "How it works"),
("docs/index.html", "Lab reports"),
(REPO, "GitHub"),
(CRATE, "crates.io"),
];
let mut out = String::from("<ul class=\"nav\">");
for (href, label) in items {
if href.starts_with("http") {
out.push_str(&format!("<li><a href=\"{}\">{}</a></li>", href, label));
} else if href == here {
out.push_str(&format!("<li><strong>{}</strong></li>", label));
} else {
out.push_str(&format!(
"<li><a href=\"{}\">{}</a></li>",
relative(here, href),
label
));
}
}
out.push_str("</ul>");
out
}
fn foot(here: &str) -> String {
let up = relative(here, "docs/index.html");
format!(
"<footer><div class=\"wrap\">{}\n\
<p>RadBeeper — MIT. Copyright © 2026 Paul Richeson.\n\
Designed and maintained for <a href=\"{}\">Copal Linux</a>, a distillation of\n\
Alpine Linux, which is also its development environment.\n\
<a href=\"{}\">Lab reports</a> ·\n\
<a href=\"{}\">Source</a> ·\n\
<a href=\"{}\">crates.io</a></p>\n\
</div></footer>\n</body>\n</html>\n",
nav(here),
COPAL,
up,
REPO,
CRATE
)
}
fn page_map(root: &Path) -> Vec<(String, String)> {
let mut map = vec![
("README.md".to_string(), "index.html".to_string()),
("docs/README.md".to_string(), "docs/index.html".to_string()),
];
let mut names: Vec<String> = fs::read_dir(root.join("docs"))
.map(|d| {
d.flatten()
.filter_map(|e| e.file_name().into_string().ok())
.filter(|n| n.ends_with(".md"))
.collect()
})
.unwrap_or_default();
names.sort();
for n in &names {
map.push((
format!("docs/{}", n),
format!("docs/{}.html", &n[..n.len() - 3]),
));
}
if root.join("docs/archive/README.md").exists() {
map.push((
"docs/archive/README.md".to_string(),
"docs/archive/index.html".to_string(),
));
}
map
}
fn normpath(p: &str) -> String {
let mut parts: Vec<&str> = Vec::new();
for bit in p.split('/') {
match bit {
"" | "." => {}
".." => {
if matches!(parts.last(), Some(&last) if last != "..") {
parts.pop();
} else {
parts.push("..");
}
}
other => parts.push(other),
}
}
if parts.is_empty() {
".".to_string()
} else {
parts.join("/")
}
}
fn linker<'a>(here: &'a str, map: &'a [(String, String)]) -> impl Fn(&str) -> String + 'a {
let base: String = {
let v: Vec<&str> = here.split('/').collect();
v[..v.len() - 1].join("/")
};
move |href: &str| -> String {
if href.starts_with("http://")
|| href.starts_with("https://")
|| href.starts_with("mailto:")
|| href.starts_with('#')
|| href.starts_with('/')
{
return href.to_string();
}
let (path, anchor) = match href.split_once('#') {
Some((p, a)) => (p, format!("#{}", a)),
None => (href, String::new()),
};
let joined = if base.is_empty() {
path.to_string()
} else {
format!("{}/{}", base, path)
};
let mut target = normpath(&joined);
if path.ends_with('/') || target == "docs" || target == "docs/archive" {
target = format!("{}/README.md", target.trim_end_matches('/'));
}
if let Some((_, out)) = map.iter().find(|(src, _)| *src == target) {
return format!("{}{}", relative(here, out), anchor);
}
if target.ends_with(".md") {
return format!("{}/blob/main/{}{}", REPO, target, anchor);
}
format!("{}{}", href, anchor)
}
}
pub fn first_heading(md: &str) -> String {
for line in md.split('\n') {
if let Some(rest) = line.strip_prefix("# ") {
return rest.trim().to_string();
}
}
"RadBeeper".to_string()
}
pub fn first_paragraph(md: &str) -> String {
for block in md.split("\n\n") {
let b = block.trim();
if b.is_empty()
|| b.starts_with('#')
|| b.starts_with("<!--")
|| b.starts_with('|')
|| b.starts_with("```")
|| b.starts_with('>')
|| b.starts_with("![")
{
continue;
}
let mut plain = String::new();
let src: Vec<char> = b.chars().collect();
let mut i = 0usize;
while i < src.len() {
if src[i] == '[' {
if let Some((label, _t, next)) = bracket(&src, i) {
plain.push_str(&label);
i = next;
continue;
}
}
plain.push(src[i]);
i += 1;
}
let plain: String = plain.chars().filter(|c| !"*`_".contains(*c)).collect();
let words: Vec<&str> = plain.split_whitespace().collect();
let joined = words.join(" ");
return joined.chars().take(300).collect();
}
DESCRIPTION.to_string()
}
fn docs_index(listing: &[(String, String, String)]) -> String {
let rows: Vec<String> = listing
.iter()
.map(|(href, title, desc)| format!("| [{}]({}) | {} |", title, href, desc))
.collect();
format!(
"# Lab reports\n\nThe *why* behind each part of RadBeeper, written up \
properly \u{2014} the arithmetic, the failures that shaped it, and the prior \
art.\n\n| | |\n|---|---|\n{}\n\n---\n\n[\u{2190} back to the overview](../README.md)\n",
rows.join("\n")
)
}
#[allow(clippy::too_many_arguments)]
fn page(
md: &str,
root: &Path,
here: &str,
title: &str,
description: &str,
version: &str,
map: &[(String, String)],
noindex: bool,
) -> std::io::Result<usize> {
let canonical = if here == "index.html" {
SITE.to_string()
} else {
format!("{}{}", SITE, here)
};
let is_home = here == "index.html";
let mut extra = if is_home { jsonld(version) } else { String::new() };
if noindex {
extra.push_str("<meta name=\"robots\" content=\"noindex\">\n");
}
let mut doc = head(title, description, &canonical, &extra);
if is_home {
doc.push_str(&hero());
doc.push_str("<div class=\"wrap\"><main id=\"radbeeper\">\n");
} else {
doc.push_str(&format!("<div class=\"wrap\"><main>\n{}\n", nav(here)));
}
let link = linker(here, map);
doc.push_str(&render(md, &link));
doc.push_str("\n</main></div>\n");
doc.push_str(&foot(here));
let out = root.join(here.replace('/', std::path::MAIN_SEPARATOR_STR));
if let Some(parent) = out.parent() {
fs::create_dir_all(parent)?;
}
fs::write(&out, &doc)?;
Ok(doc.len())
}
pub fn build(root: &Path, version: &str) -> std::io::Result<Vec<(String, usize)>> {
let map = page_map(root);
let mut wrote: Vec<(String, usize)> = Vec::new();
let readme = fs::read_to_string(root.join("README.md"))?;
wrote.push((
"index.html".to_string(),
page(
&readme,
root,
"index.html",
"RadBeeper \u{2014} Geiger counter data logging for Linux",
DESCRIPTION,
version,
&map,
false,
)?,
));
let mut listing: Vec<(String, String, String)> = Vec::new();
for (src, here) in map.iter() {
if !src.starts_with("docs/") || src.contains("archive") || src == "docs/README.md" {
continue;
}
let md = fs::read_to_string(root.join(src))?;
let title = first_heading(&md);
let desc = first_paragraph(&md);
wrote.push((
here.clone(),
page(
&md,
root,
here,
&format!("{} \u{2014} RadBeeper", title),
&desc,
version,
&map,
false,
)?,
));
let short: String = desc.chars().take(160).collect();
listing.push((
here.rsplit('/').next().unwrap_or(here).to_string(),
title,
short,
));
}
wrote.push((
"docs/index.html".to_string(),
page(
&docs_index(&listing),
root,
"docs/index.html",
"Lab reports \u{2014} RadBeeper",
"The lab reports behind RadBeeper: the log format, the stream, the \
cascade strip, the spectrum and the entropy source.",
version,
&map,
false,
)?,
));
if root.join("docs/archive/README.md").exists() {
let md = fs::read_to_string(root.join("docs/archive/README.md"))?;
wrote.push((
"docs/archive/index.html".to_string(),
page(
&md,
root,
"docs/archive/index.html",
"Archive \u{2014} RadBeeper",
"Past versions of RadBeeper's documentation, kept as they stood.",
version,
&map,
true,
)?,
));
}
let mut urls = String::new();
for (u, _) in wrote.iter().filter(|(u, _)| !u.contains("archive")) {
urls.push_str(&format!(
"<url><loc>{}{}</loc></url>",
SITE,
if u == "index.html" { "" } else { u.as_str() }
));
}
urls.push_str(&format!("<url><loc>{}monitor.html</loc></url>", SITE));
fs::write(
root.join("sitemap.xml"),
format!(
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">{}</urlset>\n",
urls
),
)?;
fs::write(
root.join("robots.txt"),
format!("User-agent: *\nAllow: /\nSitemap: {}sitemap.xml\n", SITE),
)?;
fs::write(root.join(".nojekyll"), "")?;
Ok(wrote)
}
pub fn version_of(root: &Path) -> String {
if let Ok(text) = fs::read_to_string(root.join("Cargo.toml")) {
for line in text.lines() {
if let Some(rest) = line.strip_prefix("version = \"") {
if let Some(v) = rest.split('"').next() {
return v.to_string();
}
}
}
}
env!("CARGO_PKG_VERSION").to_string()
}
pub fn run(root: Option<&Path>) -> i32 {
let here = PathBuf::from(".");
let root = root.unwrap_or(&here);
let version = version_of(root);
match build(root, &version) {
Ok(pages) => {
for (name, size) in &pages {
println!("site: {:<34} {:>6.1} KB", name, *size as f64 / 1024.0);
}
println!("site: sitemap.xml, robots.txt, .nojekyll");
0
}
Err(e) => {
eprintln!("radbeeper: could not build the site -- {}", e);
1
}
}
}
#[cfg(test)]
mod tests {
use super::*;
fn idt(s: &str) -> String {
s.to_string()
}
#[test]
fn attributes_escape_the_apostrophe_and_text_does_not() {
assert_eq!(esc_text("a & b < c > d \" e ' f"), "a & b < c > d \" e ' f");
assert_eq!(
esc_attr("a & b < c > d \" e ' f"),
"a & b < c > d " e ' f"
);
}
#[test]
fn emphasis_nests_but_a_failed_guard_abandons_the_match() {
assert_eq!(
inline("**the floor of *nominal* is 30**", &idt),
"<strong>the floor of <em>nominal</em> is 30</strong>"
);
assert_eq!(inline("count *c*\u{1d62} for each", &idt), "count *c*\u{1d62} for each");
assert_eq!(inline("snake_case_word", &idt), "snake_case_word");
assert_eq!(inline("1/_N_ steadier", &idt), "1/<em>N</em> steadier");
}
#[test]
fn a_code_span_keeps_its_asterisks() {
assert_eq!(
inline("Python's `**` does", &idt),
"Python's <code>**</code> does"
);
assert_eq!(inline("`a & b`", &idt), "<code>a & b</code>");
}
#[test]
fn a_link_target_is_escaped_twice_exactly_as_python_does_it() {
assert_eq!(
inline("[x](a.md?q=1&r=2)", &idt),
"<a href=\"a.md?q=1&amp;r=2\">x</a>"
);
}
#[test]
fn a_heading_gets_the_anchor_github_would_have_given_it() {
assert_eq!(slug("What you need"), "what-you-need");
assert_eq!(slug("The two meters"), "the-two-meters");
assert_eq!(slug("From a beep to a frame"), "from-a-beep-to-a-frame");
assert_eq!(slug("`code` and & punctuation!"), "code-and-punctuation");
}
#[test]
fn a_page_links_its_neighbours_from_where_it_actually_sits() {
assert_eq!(relative("index.html", "docs/the-log.html"), "docs/the-log.html");
assert_eq!(relative("docs/the-log.html", "index.html"), "../index.html");
assert_eq!(relative("docs/the-log.html", "docs/cascade.html"), "cascade.html");
assert_eq!(
relative("docs/archive/index.html", "index.html"),
"../../index.html"
);
}
#[test]
fn a_document_that_is_not_a_page_here_links_to_the_repository() {
let map = vec![
("README.md".to_string(), "index.html".to_string()),
("docs/the-log.md".to_string(), "docs/the-log.html".to_string()),
];
let from_readme = linker("index.html", &map);
assert_eq!(from_readme("docs/the-log.md"), "docs/the-log.html");
assert_eq!(
from_readme("RELEASING.md"),
format!("{}/blob/main/RELEASING.md", REPO)
);
assert_eq!(from_readme("https://example.com/x.md"), "https://example.com/x.md");
assert_eq!(from_readme("#what-you-need"), "#what-you-need");
let from_doc = linker("docs/the-log.md.html", &map);
assert_eq!(from_doc("../README.md"), "../index.html");
assert_eq!(
from_doc("the-log.md#backfill"),
"the-log.html#backfill"
);
}
#[test]
fn a_table_with_an_empty_header_row_does_not_draw_one() {
let md = "| | |\n|---|---|\n| **a** | one |\n| b | two |\n";
let html = render(md, &idt);
assert!(!html.contains("<thead>"), "{}", html);
assert!(html.contains("<td><strong>a</strong></td><td>one</td>"), "{}", html);
let titled = "| Col | Other |\n|---|---|\n| a | b |\n";
assert!(render(titled, &idt).contains("<thead><tr><th>Col</th>"));
}
}