diff --git a/src/lib.rs b/src/lib.rs index 1b421767..6fcdc70e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,6 +2,7 @@ mod book; mod parse; pub mod renderer; pub mod theme; +pub mod utils; pub use book::MDBook; pub use book::BookItem; diff --git a/src/renderer/html_handlebars.rs b/src/renderer/html_handlebars.rs index b1a18fb9..3c8abd4a 100644 --- a/src/renderer/html_handlebars.rs +++ b/src/renderer/html_handlebars.rs @@ -4,7 +4,7 @@ extern crate pulldown_cmark; use renderer::Renderer; use book::{BookItems, BookConfig}; -use theme; +use {theme, utils}; use std::path::{Path, PathBuf, Component}; use std::fs::{self, File, metadata}; @@ -14,8 +14,7 @@ use std::collections::BTreeMap; use self::handlebars::{Handlebars, HelperDef, RenderError, RenderContext, Helper, Context, JsonRender}; use self::rustc_serialize::json::{self, Json, ToJson}; -use self::pulldown_cmark::Parser; -use self::pulldown_cmark::html; +use self::pulldown_cmark::{Parser, html}; pub struct HtmlHandlebars; @@ -57,7 +56,7 @@ impl Renderer for HtmlHandlebars { // Remove path to root from previous file and render content for this one data.remove("path_to_root"); - data.insert("path_to_root".to_string(), path_to_root(&item.path).to_json()); + data.insert("path_to_root".to_string(), utils::path::path_to_root(&item.path).to_json()); // Rendere the handlebars template with the data let rendered = try!(handlebars.render("index", &data)); @@ -213,19 +212,6 @@ fn render_html(text: &str) -> String { s } -fn path_to_root(path: &Path) -> String { - // Remove filename and add "../" for every directory - - path.to_path_buf().parent().expect("") - .components().fold(String::new(), |mut s, c| { - match c { - Component::Normal(_) => s.push_str("../"), - _ => {} - } - s - }) -} - // Handlebars helper to construct TOC #[derive(Clone, Copy)] struct RenderToc; diff --git a/src/utils/mod.rs b/src/utils/mod.rs new file mode 100644 index 00000000..4da97892 --- /dev/null +++ b/src/utils/mod.rs @@ -0,0 +1 @@ +pub mod path; diff --git a/src/utils/path.rs b/src/utils/path.rs new file mode 100644 index 00000000..adbe5cb9 --- /dev/null +++ b/src/utils/path.rs @@ -0,0 +1,14 @@ +use std::path::{Path, Component}; + +pub fn path_to_root(path: &Path) -> String { + // Remove filename and add "../" for every directory + + path.to_path_buf().parent().expect("") + .components().fold(String::new(), |mut s, c| { + match c { + Component::Normal(_) => s.push_str("../"), + _ => {} + } + s + }) +}