Fixes 2 bugs with relative paths. Fixed by injecting a variable path_to_root into the json data for the handlebars template. Fixes #17
This commit is contained in:
parent
6a4b8d51b4
commit
1111ff3ceb
|
@ -55,6 +55,10 @@ impl Renderer for HtmlHandlebars {
|
|||
data.remove("content");
|
||||
data.insert("content".to_string(), content.to_json());
|
||||
|
||||
// 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());
|
||||
|
||||
// Rendere the handlebars template with the data
|
||||
let rendered = try!(handlebars.render("index", &data));
|
||||
|
||||
|
@ -225,22 +229,35 @@ fn path_to_link(path: &Path) -> Option<PathBuf> {
|
|||
Some(path)
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
impl HelperDef for RenderToc {
|
||||
fn call(&self, c: &Context, h: &Helper, _: &Handlebars, rc: &mut RenderContext) -> Result<(), RenderError> {
|
||||
let param = h.params().get(0).unwrap();
|
||||
fn call(&self, c: &Context, _h: &Helper, _: &Handlebars, rc: &mut RenderContext) -> Result<(), RenderError> {
|
||||
|
||||
// get value from context data
|
||||
// rc.get_path() is current json parent path, you should always use it like this
|
||||
// param is the key of value you want to display
|
||||
let value = c.navigate(rc.get_path(), param);
|
||||
let chapters = c.navigate(rc.get_path(), "chapters");
|
||||
let path_to_root = c.navigate(rc.get_path(), "path_to_root").to_string().replace("\"", "");
|
||||
try!(rc.writer.write("<ul class=\"chapter\">".as_bytes()));
|
||||
|
||||
// Decode json format
|
||||
let decoded: Vec<BTreeMap<String,String>> = json::decode(&value.to_string()).unwrap();
|
||||
let decoded: Vec<BTreeMap<String,String>> = json::decode(&chapters.to_string()).unwrap();
|
||||
|
||||
let mut current_level = 1;
|
||||
|
||||
|
@ -265,6 +282,8 @@ impl HelperDef for RenderToc {
|
|||
try!(rc.writer.write("<a href=\"".as_bytes()));
|
||||
|
||||
if let Some(link) = path_to_link(Path::new(item.get("path").expect("Error: path should be Some(_)"))) {
|
||||
try!(rc.writer.write(path_to_root.as_bytes()));
|
||||
println!("Path to root: {}", path_to_root);
|
||||
try!(rc.writer.write(link.to_str().unwrap().as_bytes()));
|
||||
} else {
|
||||
try!(rc.writer.write("#".as_bytes()));
|
||||
|
|
|
@ -7,14 +7,14 @@
|
|||
<meta name="description" content="{% block description %}{% endblock %}">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
|
||||
<link rel="stylesheet" href="book.css" media="screen" title="no title" charset="utf-8">
|
||||
<link rel="stylesheet" href="{{ path_to_root }}book.css" media="screen" title="no title" charset="utf-8">
|
||||
<link href='http://fonts.googleapis.com/css?family=Open+Sans:300italic,400italic,600italic,700italic,800italic,400,300,600,700,800' rel='stylesheet' type='text/css'>
|
||||
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css">
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<div id="sidebar" class="sidebar">
|
||||
{{#toc chapters}}{{/toc}}
|
||||
{{#toc}}{{/toc}}
|
||||
</div>
|
||||
|
||||
<div id="page-wrapper" class="page-wrapper">
|
||||
|
@ -31,6 +31,6 @@
|
|||
</div>
|
||||
|
||||
<script src="http://code.jquery.com/jquery-2.1.4.min.js"></script>
|
||||
<script src="book.js"></script>
|
||||
<script src="{{ path_to_root }}book.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
|
|
Loading…
Reference in New Issue