diff --git a/src/module/notes/mod.rs b/src/module/notes/mod.rs index cd86f118..004c47e8 100644 --- a/src/module/notes/mod.rs +++ b/src/module/notes/mod.rs @@ -125,16 +125,6 @@ impl<'a> Notes<'a> { } fn command_open(&self, matches: &ArgMatches) -> bool { - use std::io::Write; - use std::process::exit; - - use open; - - use self::header::get_name_from_header; - use ui::external::get_tempfile; - - use module::helpers::content::markdown::MarkdownParser; - let parser = Parser::new(JsonHeaderParser::new(None)); let filter = { @@ -157,48 +147,13 @@ impl<'a> Notes<'a> { if matches.is_present("onepage") { let tmpcontent = files.fold(String::new(), |acc, file| { - let heading = { - let name = get_name_from_header(file.deref().borrow().header()); - if name.len() == 0 { - format!("# {}", file.deref().borrow().id) - } else { - format!("# {} ({})", name, file.deref().borrow().id()) - } - }; - - format!("{}\n\n{}\n\n{}", acc, heading, file.deref().borrow().data()) + let content = self.preprocess_file_for_markdown(file); + format!("{}\n\n{}", acc, content) }); - - let (temppath, mut tempfile) = match get_tempfile("html") { - Some(tpl) => tpl, - None => { - error!("Could not create tempfile"); - exit(1); - } - }; - - let html = MarkdownParser::new(&tmpcontent).to_html_page(); - - tempfile.write_all(html.as_ref()); - open::that(&temppath[..]).is_ok() + self.open_tmpcontent(tmpcontent) } else { let result = files.map(|file| { - let (temppath, mut tempfile) = match get_tempfile("html") { - Some(tpl) => tpl, - None => { - error!("Could not create tempfile"); - exit(1); - } - }; - - let content = format!("# {}\n\n{}", - get_name_from_header(file.deref().borrow().header()), - file.deref().borrow().data()); - - let html = MarkdownParser::new(&content).to_html_page(); - - tempfile.write_all(html.as_ref()); - open::that(&temppath[..]).is_ok() + self.open_tmpcontent(self.preprocess_file_for_markdown(file)) }) .fold((0, 0), |acc, succeeded| { let (worked, failed) = acc; @@ -219,6 +174,51 @@ impl<'a> Notes<'a> { } + fn preprocess_file_for_markdown(&self, file: Rc>) -> String { + use self::header::get_name_from_header; + use self::header::get_tags_from_header; + + let tagsstr = { + let tags = get_tags_from_header(file.deref().borrow().header()); + if tags.len() != 0 { + format!(" ({})", tags.join(", ")) + } else { + format!(" (No Tags)") + } + }; + + let (name, id) = { + let notename = get_name_from_header(file.deref().borrow().header()); + if notename.len() == 0 { + (format!("{}", file.deref().borrow().id()), String::new()) + } else { + (notename, format!("{}", file.deref().borrow().id())) + } + }; + + format!("

{}

{}{}\n\n{}", name, id, tagsstr, + file.deref().borrow().data()) + } + + fn open_tmpcontent(&self, s: String) -> bool { + use std::process::exit; + use std::io::Write; + use open; + use ui::external::get_tempfile; + use module::helpers::content::markdown::MarkdownParser; + + let (temppath, mut tempfile) = match get_tempfile("html") { + Some(tpl) => tpl, + None => { + error!("Could not create tempfile"); + exit(1); + } + }; + + tempfile.write_all(MarkdownParser::new(&s).to_html_page().as_ref()); + open::that(&temppath[..]).is_ok() + } + fn command_list(&self, matches: &ArgMatches) -> bool { use ansi_term::Colour::{Red, Green}; use ui::file::{FilePrinter, TablePrinter};