From e40b2933366a08226435c1d2b17de9236349475d Mon Sep 17 00:00:00 2001 From: Mathieu David Date: Wed, 30 Dec 2015 00:46:55 +0100 Subject: [PATCH] Fix #70 render inline code blocks in the sidebar --- src/renderer/html_handlebars/hbs_renderer.rs | 12 ++--------- src/renderer/html_handlebars/helpers/toc.rs | 21 +++++++++++++++++++- src/utils/mod.rs | 18 ++++++++++++++++- 3 files changed, 39 insertions(+), 12 deletions(-) diff --git a/src/renderer/html_handlebars/hbs_renderer.rs b/src/renderer/html_handlebars/hbs_renderer.rs index f45079cb..8d687603 100644 --- a/src/renderer/html_handlebars/hbs_renderer.rs +++ b/src/renderer/html_handlebars/hbs_renderer.rs @@ -1,6 +1,5 @@ extern crate handlebars; extern crate rustc_serialize; -extern crate pulldown_cmark; use renderer::html_handlebars::helpers; use renderer::Renderer; @@ -16,7 +15,7 @@ use std::collections::BTreeMap; use self::handlebars::{Handlebars, JsonRender}; use self::rustc_serialize::json::{Json, ToJson}; -use self::pulldown_cmark::{Parser, html}; + pub struct HtmlHandlebars; @@ -73,7 +72,7 @@ impl Renderer for HtmlHandlebars { try!(f.read_to_string(&mut content)); // Render markdown using the pulldown-cmark crate - content = render_html(&content); + content = utils::render_markdown(&content); print_content.push_str(&content); // Remove content from previous file and render content for this one @@ -241,10 +240,3 @@ fn make_data(book: &MDBook) -> Result, Box> { debug!("[*]: JSON constructed"); Ok(data) } - -fn render_html(text: &str) -> String { - let mut s = String::with_capacity(text.len() * 3 / 2); - let p = Parser::new(&text); - html::push_html(&mut s, p); - s -} diff --git a/src/renderer/html_handlebars/helpers/toc.rs b/src/renderer/html_handlebars/helpers/toc.rs index dda08724..76ec33ba 100644 --- a/src/renderer/html_handlebars/helpers/toc.rs +++ b/src/renderer/html_handlebars/helpers/toc.rs @@ -1,11 +1,13 @@ extern crate handlebars; extern crate rustc_serialize; +extern crate pulldown_cmark; use std::path::Path; use std::collections::BTreeMap; use self::rustc_serialize::json; use self::handlebars::{Handlebars, HelperDef, RenderError, RenderContext, Helper, Context}; +use self::pulldown_cmark::{Parser, html, Event, Tag}; // Handlebars helper to construct TOC #[derive(Clone, Copy)] @@ -93,7 +95,24 @@ impl HelperDef for RenderToc { } if let Some(name) = item.get("name") { - try!(rc.writer.write(name.as_bytes())); + // Render only inline code blocks + + // filter all events that are not inline code blocks + let parser = Parser::new(&name).filter(|event|{ + match event { + &Event::Start(Tag::Code) | &Event::End(Tag::Code) => true, + &Event::InlineHtml(_) => true, + &Event::Text(_) => true, + _ => false, + } + }); + + // render markdown to html + let mut markdown_parsed_name = String::with_capacity(name.len() * 3 / 2); + html::push_html(&mut markdown_parsed_name, parser); + + // write to the handlebars template + try!(rc.writer.write(markdown_parsed_name.as_bytes())); } if path_exists { diff --git a/src/utils/mod.rs b/src/utils/mod.rs index 1a549d0d..a808d32e 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -1,7 +1,11 @@ +extern crate pulldown_cmark; + use std::path::{Path, PathBuf, Component}; use std::error::Error; use std::fs::{self, metadata, File}; +use self::pulldown_cmark::{Parser, html}; + /// This is copied from the rust source code until Path_ Ext stabilizes. /// You can use it, but be aware that it will be removed when those features go to rust stable pub trait PathExt { @@ -131,7 +135,7 @@ pub fn remove_dir_content(dir: &Path) -> Result<(), Box> { Ok(()) } -/// **Untested!** +/// /// /// Copies all files of a directory to another one except the files with the extensions given in the /// `ext_blacklist` array @@ -178,6 +182,18 @@ pub fn copy_files_except_ext(from: &Path, to: &Path, recursive: bool, ext_blackl } +/// +/// +/// Wrapper around the pulldown-cmark parser and renderer to render markdown + +pub fn render_markdown(text: &str) -> String { + let mut s = String::with_capacity(text.len() * 3 / 2); + let p = Parser::new(&text); + html::push_html(&mut s, p); + s +} + + // ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------