fix anchor links

This commit is contained in:
Mathieu David 2017-02-28 01:17:42 +01:00
parent 1345c05b18
commit e218257e42
1 changed files with 19 additions and 1 deletions

View File

@ -97,8 +97,9 @@ impl Renderer for HtmlHandlebars {
let filename = Path::new(&ch.path).with_extension("html"); let filename = Path::new(&ch.path).with_extension("html");
// create links for headers // create links for headers and fix anchors
let rendered = build_header_links(rendered, filename.to_str().unwrap_or("")); let rendered = build_header_links(rendered, filename.to_str().unwrap_or(""));
let rendered = fix_anchor_links(rendered, filename.to_str().unwrap_or(""));
// fix code blocks // fix code blocks
let rendered = fix_code_blocks(rendered); let rendered = fix_code_blocks(rendered);
@ -146,6 +147,7 @@ impl Renderer for HtmlHandlebars {
let rendered = try!(handlebars.render("index", &data)); let rendered = try!(handlebars.render("index", &data));
let rendered = build_header_links(rendered, "print.html"); let rendered = build_header_links(rendered, "print.html");
let rendered = fix_anchor_links(rendered, "print.html");
// fix code blocks // fix code blocks
let rendered = fix_code_blocks(rendered); let rendered = fix_code_blocks(rendered);
@ -257,6 +259,22 @@ fn build_header_links(html: String, filename: &str) -> String {
}).into_owned() }).into_owned()
} }
// anchors to the same page (href="#anchor") do not work because of
// <base href="../"> pointing to the root folder. This function *fixes*
// that in a very inelegant way
fn fix_anchor_links(html: String, filename: &str) -> String {
let regex = Regex::new(r##"<a([^>]+)href="#([^"]+)"([^>]*)>"##).unwrap();
regex.replace_all(&html, |caps: &Captures| {
let before = &caps[1];
let anchor = &caps[2];
let after = &caps[3];
format!("<a{before}href=\"{filename}#{anchor}\"{after}>",
before=before, filename=filename, anchor=anchor, after=after)
}).into_owned()
}
// The rust book uses annotations for rustdoc to test code snippets, like the following: // The rust book uses annotations for rustdoc to test code snippets, like the following:
// ```rust,should_panic // ```rust,should_panic
// fn main() { // fn main() {