From 1345c05b18e8d16c76a8e39276e1cdf61fe1ae54 Mon Sep 17 00:00:00 2001 From: Mathieu David Date: Fri, 24 Feb 2017 11:51:00 +0100 Subject: [PATCH 1/2] Fix anchors, Fixes #211 --- src/renderer/html_handlebars/hbs_renderer.rs | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/renderer/html_handlebars/hbs_renderer.rs b/src/renderer/html_handlebars/hbs_renderer.rs index 3e12d39a..e47173d6 100644 --- a/src/renderer/html_handlebars/hbs_renderer.rs +++ b/src/renderer/html_handlebars/hbs_renderer.rs @@ -95,14 +95,15 @@ impl Renderer for HtmlHandlebars { debug!("[*]: Render template"); let rendered = try!(handlebars.render("index", &data)); + let filename = Path::new(&ch.path).with_extension("html"); + // create links for headers - let rendered = build_header_links(rendered); + let rendered = build_header_links(rendered, filename.to_str().unwrap_or("")); // fix code blocks let rendered = fix_code_blocks(rendered); // Write to file - let filename = Path::new(&ch.path).with_extension("html"); info!("[*] Creating {:?} ✓", filename.display()); try!(book.write_file(filename, &rendered.into_bytes())); @@ -144,7 +145,7 @@ impl Renderer for HtmlHandlebars { debug!("[*]: Render template"); let rendered = try!(handlebars.render("index", &data)); - let rendered = build_header_links(rendered); + let rendered = build_header_links(rendered, "print.html"); // fix code blocks let rendered = fix_code_blocks(rendered); @@ -224,7 +225,7 @@ fn make_data(book: &MDBook) -> Result Ok(data) } -fn build_header_links(html: String) -> String { +fn build_header_links(html: String, filename: &str) -> String { let regex = Regex::new(r"(.*?)").unwrap(); regex.replace_all(&html, |caps: &Captures| { @@ -251,7 +252,8 @@ fn build_header_links(html: String) -> String { } }).collect::(); - format!("{text}", level=level, id=id, text=text) + format!("{text}", + level=level, id=id, text=text, filename=filename) }).into_owned() } From e218257e42b95343884b75fe529b35caac273757 Mon Sep 17 00:00:00 2001 From: Mathieu David Date: Tue, 28 Feb 2017 01:17:42 +0100 Subject: [PATCH 2/2] fix anchor links --- src/renderer/html_handlebars/hbs_renderer.rs | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/src/renderer/html_handlebars/hbs_renderer.rs b/src/renderer/html_handlebars/hbs_renderer.rs index e47173d6..7c06fd62 100644 --- a/src/renderer/html_handlebars/hbs_renderer.rs +++ b/src/renderer/html_handlebars/hbs_renderer.rs @@ -97,8 +97,9 @@ impl Renderer for HtmlHandlebars { 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 = fix_anchor_links(rendered, filename.to_str().unwrap_or("")); // fix code blocks let rendered = fix_code_blocks(rendered); @@ -146,6 +147,7 @@ impl Renderer for HtmlHandlebars { let rendered = try!(handlebars.render("index", &data)); let rendered = build_header_links(rendered, "print.html"); + let rendered = fix_anchor_links(rendered, "print.html"); // fix code blocks let rendered = fix_code_blocks(rendered); @@ -257,6 +259,22 @@ fn build_header_links(html: String, filename: &str) -> String { }).into_owned() } +// anchors to the same page (href="#anchor") do not work because of +// 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##"]+)href="#([^"]+)"([^>]*)>"##).unwrap(); + regex.replace_all(&html, |caps: &Captures| { + let before = &caps[1]; + let anchor = &caps[2]; + let after = &caps[3]; + + format!("", + before=before, filename=filename, anchor=anchor, after=after) + }).into_owned() +} + + // The rust book uses annotations for rustdoc to test code snippets, like the following: // ```rust,should_panic // fn main() {