use std::collections::BTreeMap;
use std::fs;
use std::path::{Path, PathBuf};
use std::process::Command;
use anyhow::{Context, Result};
use serde::Serialize;
use tracing::{info, warn};
pub fn native_language_label(code: &str) -> String {
match code {
"en" => "English",
"zh-Hans" => "简体中文",
"zh-Hant" => "繁體中文",
"ja" => "日本語",
"ko" => "한국어",
"fr" => "Français",
"es" => "Español",
"ru" => "Русский",
"ar" => "العربية",
"de" => "Deutsch",
"pt" => "Português",
_ => return code.to_string(),
}
.to_string()
}
#[derive(Serialize)]
pub struct ManifestNavNode {
pub title: String,
pub href: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub icon: Option<String>,
#[serde(skip_serializing_if = "Vec::is_empty")]
pub children: Vec<ManifestNavNode>,
}
#[derive(Serialize)]
pub struct ManifestPageLang {
pub title: String,
pub html: String,
pub math: bool,
}
#[derive(Serialize)]
pub struct ManifestPage {
pub path: String,
pub langs: BTreeMap<String, ManifestPageLang>,
}
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub struct ManifestSite {
pub title: String,
pub description: String,
pub logo: Option<String>,
pub base: String,
pub default_lang: String,
pub langs: Vec<ManifestLang>,
pub build_stamp: String,
pub favicon: Option<String>,
}
#[derive(Serialize)]
pub struct ManifestLang {
pub code: String,
pub label: String,
}
pub struct ManifestInputs {
pub site: ManifestSite,
pub pages: Vec<ManifestPage>,
pub navs: BTreeMap<String, Vec<ManifestNavNode>>,
}
#[derive(Serialize)]
struct ManifestJson<'a> {
site: &'a ManifestSite,
pages: &'a [ManifestPage],
navs: &'a BTreeMap<String, Vec<ManifestNavNode>>,
}
pub fn emit_manifest(out: &Path, inputs: &ManifestInputs) -> Result<()> {
let json = serde_json::to_string(&ManifestJson {
site: &inputs.site,
pages: &inputs.pages,
navs: &inputs.navs,
})?;
fs::write(out.join("docs-manifest.json"), json)
.with_context(|| format!("write {}", out.join("docs-manifest.json").display()))?;
info!(" webui lane: manifest with {} pages", inputs.pages.len());
Ok(())
}
fn find_webui_dir() -> Option<PathBuf> {
if let Ok(dir) = std::env::var("LAGRANGE_WEBUI_DIR") {
let p = PathBuf::from(&dir);
if p.join("dist-docs/ssr/ssr-docs.js").is_file() {
return Some(p);
}
warn!("LAGRANGE_WEBUI_DIR={dir} has no dist-docs/ssr/ssr-docs.js — ignoring");
}
let p = PathBuf::from("packages/webui");
if p.join("dist-docs/ssr/ssr-docs.js").is_file() {
return Some(p);
}
None
}
pub fn run(out: &Path) -> Result<bool> {
let Some(webui_dir) = find_webui_dir() else {
warn!(
"webui lane SKIPPED: no packages/webui/dist-docs build found — \
the site keeps the legacy chrome (build it with \
`pnpm --dir packages/webui build:docs:all`)"
);
return Ok(false);
};
let node = std::env::var("LAGRANGE_NODE_BIN").unwrap_or_else(|_| "node".to_string());
let script = webui_dir.join("dist-docs/ssr/ssr-docs.js");
let client = webui_dir.join("dist-docs/client");
let manifest = out.join("docs-manifest.json");
let status = Command::new(&node)
.arg(&script)
.arg("--manifest")
.arg(&manifest)
.arg("--out")
.arg(out)
.arg("--client-dir")
.arg(&client)
.status()
.with_context(|| format!("spawn {node} {}", script.display()))?;
if !status.success() {
warn!(
"webui lane FAILED ({}): the per-language pages may be incomplete \
and flat pages keep the legacy chrome",
status
);
return Ok(false);
}
info!("webui lane: hikari shell pages rendered");
Ok(true)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn native_labels_cover_the_workspace_doc_languages() {
assert_eq!(native_language_label("zh-Hans"), "简体中文");
assert_eq!(native_language_label("en"), "English");
assert_eq!(native_language_label("xx-Pseudo"), "xx-Pseudo");
}
#[test]
fn manifest_serializes_the_page_contract_the_renderer_parses() {
let inputs = ManifestInputs {
site: ManifestSite {
title: "Lagrange".into(),
description: "d".into(),
logo: Some("/logo.webp".into()),
base: "".into(),
default_lang: "en".into(),
langs: vec![ManifestLang {
code: "en".into(),
label: "English".into(),
}],
build_stamp: "0.2.12".into(),
favicon: None,
},
pages: vec![ManifestPage {
path: "guides/architecture.html".into(),
langs: BTreeMap::from([(
"en".to_string(),
ManifestPageLang {
title: "Architecture".into(),
html: "<p>x</p>".into(),
math: false,
},
)]),
}],
navs: BTreeMap::from([(
"en".to_string(),
vec![ManifestNavNode {
title: "Guides".into(),
href: String::new(),
icon: None,
children: vec![ManifestNavNode {
title: "Architecture".into(),
href: "guides/architecture.html".into(),
icon: None,
children: Vec::new(),
}],
}],
)]),
};
let json = serde_json::to_string(&ManifestJson {
site: &inputs.site,
pages: &inputs.pages,
navs: &inputs.navs,
})
.unwrap();
for needle in [
"\"logo\":\"/logo.webp\"",
"\"path\":\"guides/architecture.html\"",
"\"html\":\"<p>x</p>\"",
"\"href\":\"guides/architecture.html\"",
"\"label\":\"English\"",
"\"defaultLang\":\"en\"",
"\"buildStamp\":\"0.2.12\"",
] {
assert!(json.contains(needle), "manifest missing {needle}: {json}");
}
assert!(!json.contains("default_lang"), "snake_case leaked: {json}");
assert!(!json.contains("build_stamp"), "snake_case leaked: {json}");
assert!(json.contains("\"math\":false"), "{json}");
}
}