Fix #70 render inline code blocks in the sidebar

This commit is contained in:
Mathieu David 2015-12-30 00:46:55 +01:00
parent b40688c880
commit e40b293336
3 changed files with 39 additions and 12 deletions

View File

@ -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<BTreeMap<String,Json>, Box<Error>> {
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
}

View File

@ -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 {

View File

@ -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<Error>> {
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
}
// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------