|
| 1 | +use std::collections::HashSet; |
| 2 | +use std::io; |
| 3 | + |
| 4 | +use mdbook_preprocessor::{errors::Error, parse_input}; |
| 5 | +use mdbook_rustc::{AllTargets, TargetInfo}; |
| 6 | + |
| 7 | +const TIER_1_HOST_MARKER: &str = "{{TIER_1_HOST_TABLE}}"; |
| 8 | +const TIER_1_NOHOST_MARKER: &str = "{{TIER_1_NOHOST_TABLE}}"; |
| 9 | +const TIER_2_HOST_MARKER: &str = "{{TIER_2_HOST_TABLE}}"; |
| 10 | +const TIER_2_NOHOST_MARKER: &str = "{{TIER_2_NOHOST_TABLE}}"; |
| 11 | +const TIER_3_MARKER: &str = "{{TIER_3_TABLE}}"; |
| 12 | + |
| 13 | +const EMPTY_TIER_1_NOHOST_MSG: &str = "At this time, all Tier 1 targets are [Tier 1 with Host Tools](#tier-1-with-host-tools).\n"; |
| 14 | +const EMPTY_TIER_2_NOHOST_MSG: &str = "At this time, all Tier 2 targets are [Tier 2 with Host Tools](#tier-2-with-host-tools).\n"; |
| 15 | + |
| 16 | +fn main() -> Result<(), Error> { |
| 17 | + let mut args = std::env::args().skip(1); |
| 18 | + match args.next().as_deref() { |
| 19 | + Some("supports") => { |
| 20 | + // Supports all renderers. |
| 21 | + return; |
| 22 | + } |
| 23 | + Some(arg) => { |
| 24 | + return Err(Error::msg("unknown argument: {arg}")); |
| 25 | + } |
| 26 | + None => {} |
| 27 | + } |
| 28 | + |
| 29 | + let (ctx, book) = mdbook_preprocessor::parse_input(io::stdin().lock())?; |
| 30 | + let targets = AllTargets::load(); |
| 31 | + |
| 32 | + book.for_each_chapter_mut(|chapter| { |
| 33 | + if chapter.source_path.as_deref() != Some("platform-support.md") { |
| 34 | + return; |
| 35 | + } |
| 36 | + |
| 37 | + let mut target_chapters = HashSet::new(); |
| 38 | + |
| 39 | + for target_chapter in chapter.subitems.iter().filter_map(||) { |
| 40 | + if let Some(path) = target_chapter.path.map(|p| p.to_str()) { |
| 41 | + target_chapters.insert(path.trim_start_matches("platform-support/").trim_end_matches(".md")); |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + let mut new_content = String::new(); |
| 46 | + |
| 47 | + for line in chapter.content.lines() { |
| 48 | + match line.trim() { |
| 49 | + TIER_1_HOST_MARKER => write_host_table(&mut new_content, &targets.tier1_host, &target_chapters), |
| 50 | + TIER_1_NOHOST_MARKER => write_nohost_table(&mut new_content, &targets.tier1_nohost, &target_chapters, EMPTY_TIER_1_NOHOST_MSG), |
| 51 | + TIER_2_HOST_MARKER => write_host_table(&mut new_content, &targets.tier2_host, &target_chapters), |
| 52 | + TIER_2_NOHOST_MARKER => write_nohost_table(&mut new_content, &targets.tier2_nohost, &target_chapters, EMPTY_TIER_2_NOHOST_MSG), |
| 53 | + TIER_3_MARKER => write_tier3_table(&mut new_content, &targets.tier3, &target_chapters), |
| 54 | + _ => { |
| 55 | + new_content.push(line); |
| 56 | + new_content.push("\n"); |
| 57 | + }, |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + chapter.content = new_content; |
| 62 | + }) |
| 63 | + |
| 64 | + serde_json::to_writer(io::stdout().lock(), &book)?; |
| 65 | + Ok(()) |
| 66 | +} |
| 67 | + |
| 68 | +fn write_host_table(out: &mut String, targets: &[TargetInfo], target_chapters: &HashSet<&str>) { |
| 69 | + out.push("target | notes\n-------|-------\n"); |
| 70 | + for target in targets { |
| 71 | + write_target_tuple(out, target, target_chapters); |
| 72 | + _ = writeln!(out, " | {}", |
| 73 | + target.tuple, |
| 74 | + target.meta.description.unwrap_or("") |
| 75 | + ); |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +fn write_nohost_table(out: &mut String, targets: &[TargetInfo], target_chapters: &HashSet<&str>, empty_msg: &str) { |
| 80 | + if targets.is_empty() { |
| 81 | + out.push(empty_msg); |
| 82 | + return; |
| 83 | + } |
| 84 | + |
| 85 | + out.push("target | std | notes\n-------|:---:|-------\n"); |
| 86 | + for target in targets { |
| 87 | + write_target_tuple(out, target, target_chapters); |
| 88 | + _ = writeln!(out, " | {} | {}", |
| 89 | + target.tuple, |
| 90 | + support_symbol(target.meta.std), |
| 91 | + target.meta.description.unwrap_or("") |
| 92 | + ); |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +fn write_tier3_table(out: &mut String, targets: &[TargetInfo], target_chapters: &HashSet<&str>) { |
| 97 | + out.push("target | std | host | notes\n-------|:---:|:----:|-------\n"); |
| 98 | + for target in targets { |
| 99 | + write_target_tuple(out, target, target_chapters); |
| 100 | + _ = writeln!(out, " | {} | {} | {}", |
| 101 | + target.tuple, |
| 102 | + support_symbol(target.meta.std), |
| 103 | + support_symbol(target.meta.host_tools), |
| 104 | + target.meta.description.unwrap_or("") |
| 105 | + ); |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +fn write_target_tuple(out: &mut String, target: &TargetInfo, target_chapters: &HashSet<&str>) { |
| 110 | + // let doc_page = target.meta.doc_page.as_deref().unwrap_or(target.tuple); |
| 111 | + let doc_page = target.tuple; |
| 112 | + |
| 113 | + if target_chapters.contains(doc_page) { |
| 114 | + _ = write!(out, "[`{}`](platform-support/{}.md)", target.tuple, doc_page); |
| 115 | + } else { |
| 116 | + _ = write!(out, "`{}`", target.tuple); |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +fn support_symbol(support: Option<bool>) -> &'static str { |
| 121 | + match support { |
| 122 | + Some(true) => "✓", |
| 123 | + Some(false) => "*", |
| 124 | + None => "?", |
| 125 | + } |
| 126 | +} |
0 commit comments