Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix anchor in spec links #68

Merged
merged 4 commits into from
Aug 31, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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, "//! {}#{}", 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, "//= {}#{}", 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 = \"{}#{}\"", 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 {
&""
}
eagr marked this conversation as resolved.
Show resolved Hide resolved
}
6 changes: 5 additions & 1 deletion www/src/result.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,15 @@ input.annotations.forEach((anno, id) => {
anno.isOk = anno.isComplete || anno.exception === anno.spec;
}

function anchorPrefix(id) {
return /^[1-9]/.test(id) ? 'section' : 'appendix';
eagr marked this conversation as resolved.
Show resolved Hide resolved
}

anno.id = id;
anno.source = blobLinker(anno);
anno.specification = specifications[anno.target_path];
anno.section = anno.specification.sections[`section-${anno.target_section}`];
anno.target = `${anno.specification.id}#${anno.section.id}`;
anno.target = `${anno.specification.id}#${anchorPrefix(anno.section.id)}-${anno.section.id}`;
anno.features = [];
anno.tracking_issues = [];
anno.tags = anno.tags || [];
Expand Down