Added function to clean links before inserting in toc

This commit is contained in:
Mathieu David 2015-07-29 12:10:12 +02:00
parent a55a676bbe
commit ecdcc31e9e
3 changed files with 43 additions and 3 deletions

View File

@ -196,8 +196,26 @@ fn render_html(text: &str) -> String {
s s
} }
fn path_to_link(path: &Path) -> Result<PathBuf, Box<Error>> {
// Extract filename
let mut file_name;
if let Some(name) = path.file_stem() {
file_name = String::from(name.to_str().unwrap());
}
else { return Err(Box::new(io::Error::new(io::ErrorKind::Other, "No filename"))) }
file_name.push_str(".html");
// Change file name to .html
let mut path = path.to_path_buf();
path.set_file_name(file_name);
// Clean paths with './'
Ok(path)
}
// Handlebars helper to construct TOC // Handlebars helper to construct TOC
// implement by a structure impls HelperDef
#[derive(Clone, Copy)] #[derive(Clone, Copy)]
struct RenderToc; struct RenderToc;
@ -235,7 +253,15 @@ impl HelperDef for RenderToc {
let path_exists = if let Some(path) = item.get("path") { let path_exists = if let Some(path) = item.get("path") {
if path.len() > 0 { if path.len() > 0 {
try!(rc.writer.write("<a href=\"".as_bytes())); try!(rc.writer.write("<a href=\"".as_bytes()));
try!(rc.writer.write(item.get("path").expect("Error: path should be Some(_)").as_bytes()));
match path_to_link(Path::new(item.get("path")
.expect("Error: path should be Some(_)")
)) {
Ok(link) => { try!(rc.writer.write(link.to_str().unwrap().as_bytes())); },
Err(_) => { try!(rc.writer.write("#".as_bytes())); },
}
try!(rc.writer.write("\">".as_bytes())); try!(rc.writer.write("\">".as_bytes()));
true true
} else { } else {

View File

@ -89,10 +89,21 @@ html, body {
} }
.chapter li a:hover { .chapter li a:hover {
/* Animate color change */
color: #008cff; color: #008cff;
text-decoration: none; text-decoration: none;
} }
.menu-bar {
height: 50px;
color: #CCC;
}
.menu-bar :hover {
/* Animate color change */
color: #333;
}
pre { pre {
padding: 16px; padding: 16px;
overflow: auto; overflow: auto;

View File

@ -19,7 +19,10 @@
<div id="page-wrapper" class="page-wrapper"> <div id="page-wrapper" class="page-wrapper">
<div id="menu-bar" class="menu-bar"></div> <div id="menu-bar" class="menu-bar">
<i class="fa fa-bars left"></i>
<h1 class="menu-title">{{ title }}</h1>
</div>
<div id="page" class="page"> <div id="page" class="page">
{{{ content }}} {{{ content }}}