Skip to content

Commit

Permalink
fix appendix anchors
Browse files Browse the repository at this point in the history
  • Loading branch information
eagr committed Aug 22, 2022
1 parent 2acd2f6 commit 74a8db8
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions src/extract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -301,7 +304,7 @@ fn write_rust<W: std::io::Write>(
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, "//!")?;
Expand All @@ -311,7 +314,7 @@ fn write_rust<W: std::io::Write>(
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() {
Expand All @@ -329,7 +332,7 @@ fn write_toml<W: std::io::Write>(
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, "#")?;
Expand All @@ -351,3 +354,13 @@ fn write_toml<W: std::io::Write>(

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 {
&""
}
}

0 comments on commit 74a8db8

Please sign in to comment.