Skip to content

Commit

Permalink
Fix #70 render inline code blocks in the sidebar
Browse files Browse the repository at this point in the history
  • Loading branch information
azerupi committed Dec 29, 2015
1 parent b40688c commit e40b293
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 12 deletions.
12 changes: 2 additions & 10 deletions src/renderer/html_handlebars/hbs_renderer.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
extern crate handlebars;
extern crate rustc_serialize;
extern crate pulldown_cmark;

use renderer::html_handlebars::helpers;
use renderer::Renderer;
Expand All @@ -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;

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
21 changes: 20 additions & 1 deletion src/renderer/html_handlebars/helpers/toc.rs
Original file line number Diff line number Diff line change
@@ -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)]
Expand Down Expand Up @@ -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 {
Expand Down
18 changes: 17 additions & 1 deletion src/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}



// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
Expand Down

0 comments on commit e40b293

Please sign in to comment.