From 74a8db8c52e5a0efaac7cca8d78d85b12accf0db Mon Sep 17 00:00:00 2001 From: Julian Eager Date: Mon, 22 Aug 2022 23:45:01 +0800 Subject: [PATCH] fix appendix anchors --- src/extract.rs | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/src/extract.rs b/src/extract.rs index c86ed451..bc99cc5a 100644 --- a/src/extract.rs +++ b/src/extract.rs @@ -40,6 +40,9 @@ lazy_static! { }; static ref KEY_WORDS_SET: RegexSet = RegexSet::new(KEY_WORDS.iter().map(|(r, _)| r.as_str())).unwrap(); + + static ref SECTION_ID: Regex = Regex::new(r"^[0-9](\.[0-9])*").unwrap(); + static ref APPENDIX_ID: Regex = Regex::new(r"^[A-Z](\.[0-9])*").unwrap(); } #[derive(Debug, StructOpt)] @@ -301,7 +304,7 @@ fn write_rust( section: &Section, features: &[Feature], ) -> Result<(), std::io::Error> { - writeln!(w, "//! {}#section-{}", target, section.id)?; + writeln!(w, "//! {}#{}{}", target, anchor_prefix(section.id.value), section.id)?; writeln!(w, "//!")?; writeln!(w, "//! {}", section.full_title)?; writeln!(w, "//!")?; @@ -311,7 +314,7 @@ fn write_rust( writeln!(w)?; for feature in features { - writeln!(w, "//= {}#section-{}", target, section.id)?; + writeln!(w, "//= {}#{}{}", target, anchor_prefix(section.id.value), section.id)?; writeln!(w, "//= type=spec")?; writeln!(w, "//= level={}", feature.level)?; for line in feature.quote.iter() { @@ -329,7 +332,7 @@ fn write_toml( section: &Section, features: &[Feature], ) -> Result<(), std::io::Error> { - writeln!(w, "target = \"{}#section-{}\"", target, section.id)?; + writeln!(w, "target = \"{}#{}{}\"", target, anchor_prefix(section.id.value), section.id)?; writeln!(w)?; writeln!(w, "# {}", section.full_title)?; writeln!(w, "#")?; @@ -351,3 +354,13 @@ fn write_toml( Ok(()) } + +fn anchor_prefix(id: &str) -> &str { + if let Some(_) = SECTION_ID.captures(id) { + &"section-" + } else if let Some(_) = APPENDIX_ID.captures(id) { + &"appendix-" + } else { + &"" + } +}