From 34a2b75b89c44037ed12173b60eb660b674b9175 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Tue, 5 Jan 2016 15:35:20 +0100 Subject: [PATCH 1/2] Include tags in rendered output --- src/module/notes/mod.rs | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/src/module/notes/mod.rs b/src/module/notes/mod.rs index cd86f118..93386907 100644 --- a/src/module/notes/mod.rs +++ b/src/module/notes/mod.rs @@ -131,6 +131,7 @@ impl<'a> Notes<'a> { use open; use self::header::get_name_from_header; + use self::header::get_tags_from_header; use ui::external::get_tempfile; use module::helpers::content::markdown::MarkdownParser; @@ -159,10 +160,19 @@ impl<'a> Notes<'a> { let tmpcontent = files.fold(String::new(), |acc, file| { let heading = { let name = get_name_from_header(file.deref().borrow().header()); + let tagsstr = { + let tags = get_tags_from_header(file.deref().borrow().header()); + if tags.len() != 0 { + format!(" ({})", tags.join(", ")) + } else { + format!(" (No Tags)") + } + }; if name.len() == 0 { - format!("# {}", file.deref().borrow().id) + format!("

{}

{}", file.deref().borrow().id, tagsstr) } else { - format!("# {} ({})", name, file.deref().borrow().id()) + format!("

{}

({}){}", + name, file.deref().borrow().id(), tagsstr) } }; @@ -191,8 +201,18 @@ impl<'a> Notes<'a> { } }; - let content = format!("# {}\n\n{}", + let tagsstr = { + let tags = get_tags_from_header(file.deref().borrow().header()); + if tags.len() != 0 { + format!(" ({})", tags.join(", ")) + } else { + format!(" (No Tags)") + } + }; + + let content = format!("

{}

{}\n\n{}", get_name_from_header(file.deref().borrow().header()), + tagsstr, file.deref().borrow().data()); let html = MarkdownParser::new(&content).to_html_page(); From 8abb740c13f8b7a17b1c81b99f28c9f23ab26999 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Tue, 5 Jan 2016 15:48:00 +0100 Subject: [PATCH 2/2] Refactor: tmp-content creation, file opening tmp-content creation is now done by a helper function, as is the markdown parsing, the tmp-file creation and the xdg-open call. --- src/module/notes/mod.rs | 118 +++++++++++++++++----------------------- 1 file changed, 49 insertions(+), 69 deletions(-) diff --git a/src/module/notes/mod.rs b/src/module/notes/mod.rs index 93386907..004c47e8 100644 --- a/src/module/notes/mod.rs +++ b/src/module/notes/mod.rs @@ -125,17 +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 self::header::get_tags_from_header; - use ui::external::get_tempfile; - - use module::helpers::content::markdown::MarkdownParser; - let parser = Parser::new(JsonHeaderParser::new(None)); let filter = { @@ -158,67 +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()); - let tagsstr = { - let tags = get_tags_from_header(file.deref().borrow().header()); - if tags.len() != 0 { - format!(" ({})", tags.join(", ")) - } else { - format!(" (No Tags)") - } - }; - if name.len() == 0 { - format!("

{}

{}", file.deref().borrow().id, tagsstr) - } else { - format!("

{}

({}){}", - name, file.deref().borrow().id(), tagsstr) - } - }; - - 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 tagsstr = { - let tags = get_tags_from_header(file.deref().borrow().header()); - if tags.len() != 0 { - format!(" ({})", tags.join(", ")) - } else { - format!(" (No Tags)") - } - }; - - let content = format!("

{}

{}\n\n{}", - get_name_from_header(file.deref().borrow().header()), - tagsstr, - 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; @@ -239,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};