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