Skip to content

Commit

Permalink
Patch package index to include a select box for different versions of…
Browse files Browse the repository at this point in the history
… documentation
  • Loading branch information
jessebraham committed Feb 10, 2025
1 parent 0344b14 commit 156e913
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 0 deletions.
1 change: 1 addition & 0 deletions xtask/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ console = "0.15.10"
csv = "1.3.1"
env_logger = "0.11.5"
esp-metadata = { path = "../esp-metadata", features = ["clap"] }
kuchikiki = "0.8.2"
log = "0.4.22"
minijinja = "2.5.0"
semver = { version = "1.0.23", features = ["serde"] }
Expand Down
97 changes: 97 additions & 0 deletions xtask/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::{
collections::HashSet,
fs,
path::{Path, PathBuf},
process::Command,
Expand All @@ -7,6 +8,7 @@ use std::{
use anyhow::{bail, ensure, Context as _, Result};
use clap::{Args, Parser, ValueEnum};
use esp_metadata::{Arch, Chip, Config};
use kuchikiki::{traits::*, NodeRef};
use minijinja::Value;
use strum::IntoEnumIterator;
use xtask::{
Expand Down Expand Up @@ -511,6 +513,8 @@ fn build_documentation(workspace: &Path, mut args: BuildDocumentationArgs) -> Re
build_documentation_for_package(workspace, package, Some(chip))?;
}
}

patch_documentation_index_for_package(workspace, package)?;
}

Ok(())
Expand Down Expand Up @@ -746,6 +750,99 @@ fn generate_documentation_meta_for_package(
Ok(metadata)
}

fn patch_documentation_index_for_package(workspace: &Path, package: Package) -> Result<()> {
let package_path = workspace.join("docs").join(package.to_string());
let package_name = package.to_string().replace('-', "_");

let mut versions = HashSet::new();
let mut index_paths = Vec::new();

for version_path in fs::read_dir(package_path)? {
let version_path = version_path?.path();
if !version_path.is_dir() {
continue;
}

let version = version_path
.components()
.last()
.unwrap()
.as_os_str()
.to_string_lossy();
let version = semver::Version::parse(&version)?;

versions.insert(version.clone());

if package.chip_features_matter() {
for chip_path in fs::read_dir(version_path)? {
let chip_path = chip_path?.path();
if chip_path.is_dir() {
let path = chip_path.join(&package_name).join("index.html");
index_paths.push((version.clone(), path));
}
}
} else {
let path = version_path.join(&package_name).join("index.html");
index_paths.push((version, path));
}
}

for (version, index_path) in index_paths {
let html = fs::read_to_string(&index_path)?;
let document = kuchikiki::parse_html().one(html);

let elem = document
.select_first(".sidebar-crate")
.expect("Unable to select '.sidebar-crate' element in HTML");

if let Ok(select) = document.select_first(".version-select") {
select.as_node().detach();
}

let node = elem.as_node();
node.append(build_select_element(&version, &versions));

fs::write(&index_path, document.to_string())?;
}

Ok(())
}

fn build_select_element(version: &semver::Version, versions: &HashSet<semver::Version>) -> NodeRef {
let mut html = String::from(
r#"
<div class="version-select" style="margin-top: 0.5rem; width: 100%">
<label for="version-select">Version:</label>
"#,
);

html.push_str(
r#"
<select
name="version-select"
style="text-align: center; width: 100%"
onchange="window.location.href = window.location.href.replace(/[\d]+\.[\d]+\.[\d]+[^\/]*/g, this.value)"
>"#
);

for v in versions {
html.push_str(&format!(
r#"<option value="{}" {}>{}</option>"#,
v.to_string(),
if v == version {
"selected=\"selected\""
} else {
""
},
v.to_string()
));
}

html.push_str("</select></div>");

kuchikiki::parse_html().one(html)
}

fn build_package(workspace: &Path, args: BuildPackageArgs) -> Result<()> {
// Absolute path of the package's root:
let package_path = xtask::windows_safe_path(&workspace.join(args.package.to_string()));
Expand Down

0 comments on commit 156e913

Please sign in to comment.