Skip to content

Commit e40b293

Browse files
committed
Fix #70 render inline code blocks in the sidebar
1 parent b40688c commit e40b293

File tree

3 files changed

+39
-12
lines changed

3 files changed

+39
-12
lines changed

src/renderer/html_handlebars/hbs_renderer.rs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
extern crate handlebars;
22
extern crate rustc_serialize;
3-
extern crate pulldown_cmark;
43

54
use renderer::html_handlebars::helpers;
65
use renderer::Renderer;
@@ -16,7 +15,7 @@ use std::collections::BTreeMap;
1615

1716
use self::handlebars::{Handlebars, JsonRender};
1817
use self::rustc_serialize::json::{Json, ToJson};
19-
use self::pulldown_cmark::{Parser, html};
18+
2019

2120
pub struct HtmlHandlebars;
2221

@@ -73,7 +72,7 @@ impl Renderer for HtmlHandlebars {
7372
try!(f.read_to_string(&mut content));
7473

7574
// Render markdown using the pulldown-cmark crate
76-
content = render_html(&content);
75+
content = utils::render_markdown(&content);
7776
print_content.push_str(&content);
7877

7978
// 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>> {
241240
debug!("[*]: JSON constructed");
242241
Ok(data)
243242
}
244-
245-
fn render_html(text: &str) -> String {
246-
let mut s = String::with_capacity(text.len() * 3 / 2);
247-
let p = Parser::new(&text);
248-
html::push_html(&mut s, p);
249-
s
250-
}

src/renderer/html_handlebars/helpers/toc.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
extern crate handlebars;
22
extern crate rustc_serialize;
3+
extern crate pulldown_cmark;
34

45
use std::path::Path;
56
use std::collections::BTreeMap;
67

78
use self::rustc_serialize::json;
89
use self::handlebars::{Handlebars, HelperDef, RenderError, RenderContext, Helper, Context};
10+
use self::pulldown_cmark::{Parser, html, Event, Tag};
911

1012
// Handlebars helper to construct TOC
1113
#[derive(Clone, Copy)]
@@ -93,7 +95,24 @@ impl HelperDef for RenderToc {
9395
}
9496

9597
if let Some(name) = item.get("name") {
96-
try!(rc.writer.write(name.as_bytes()));
98+
// Render only inline code blocks
99+
100+
// filter all events that are not inline code blocks
101+
let parser = Parser::new(&name).filter(|event|{
102+
match event {
103+
&Event::Start(Tag::Code) | &Event::End(Tag::Code) => true,
104+
&Event::InlineHtml(_) => true,
105+
&Event::Text(_) => true,
106+
_ => false,
107+
}
108+
});
109+
110+
// render markdown to html
111+
let mut markdown_parsed_name = String::with_capacity(name.len() * 3 / 2);
112+
html::push_html(&mut markdown_parsed_name, parser);
113+
114+
// write to the handlebars template
115+
try!(rc.writer.write(markdown_parsed_name.as_bytes()));
97116
}
98117

99118
if path_exists {

src/utils/mod.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1+
extern crate pulldown_cmark;
2+
13
use std::path::{Path, PathBuf, Component};
24
use std::error::Error;
35
use std::fs::{self, metadata, File};
46

7+
use self::pulldown_cmark::{Parser, html};
8+
59
/// This is copied from the rust source code until Path_ Ext stabilizes.
610
/// You can use it, but be aware that it will be removed when those features go to rust stable
711
pub trait PathExt {
@@ -131,7 +135,7 @@ pub fn remove_dir_content(dir: &Path) -> Result<(), Box<Error>> {
131135
Ok(())
132136
}
133137

134-
/// **Untested!**
138+
///
135139
///
136140
/// Copies all files of a directory to another one except the files with the extensions given in the
137141
/// `ext_blacklist` array
@@ -178,6 +182,18 @@ pub fn copy_files_except_ext(from: &Path, to: &Path, recursive: bool, ext_blackl
178182
}
179183

180184

185+
///
186+
///
187+
/// Wrapper around the pulldown-cmark parser and renderer to render markdown
188+
189+
pub fn render_markdown(text: &str) -> String {
190+
let mut s = String::with_capacity(text.len() * 3 / 2);
191+
let p = Parser::new(&text);
192+
html::push_html(&mut s, p);
193+
s
194+
}
195+
196+
181197

182198
// ------------------------------------------------------------------------------------------------
183199
// ------------------------------------------------------------------------------------------------

0 commit comments

Comments
 (0)