Auto merge of #60 - matthiasbeyer:notes-open-include-tags, r=matthiasbeyer

Notes open include tags

Include the tags in the browser view on `imag notes open`.

Some refactoring included.
This commit is contained in:
Homu 2016-01-05 23:49:40 +09:00
commit 5abdb8f022

View file

@ -125,16 +125,6 @@ impl<'a> Notes<'a> {
} }
fn command_open(&self, matches: &ArgMatches) -> bool { 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 parser = Parser::new(JsonHeaderParser::new(None));
let filter = { let filter = {
@ -157,48 +147,13 @@ impl<'a> Notes<'a> {
if matches.is_present("onepage") { if matches.is_present("onepage") {
let tmpcontent = files.fold(String::new(), |acc, file| { let tmpcontent = files.fold(String::new(), |acc, file| {
let heading = { let content = self.preprocess_file_for_markdown(file);
let name = get_name_from_header(file.deref().borrow().header()); format!("{}\n\n{}", acc, content)
if name.len() == 0 {
format!("# {}", file.deref().borrow().id)
} else {
format!("# {} <small>({})</small>", name, file.deref().borrow().id())
}
};
format!("{}\n\n{}\n\n{}", acc, heading, file.deref().borrow().data())
}); });
self.open_tmpcontent(tmpcontent)
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()
} else { } else {
let result = files.map(|file| { let result = files.map(|file| {
let (temppath, mut tempfile) = match get_tempfile("html") { self.open_tmpcontent(self.preprocess_file_for_markdown(file))
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()
}) })
.fold((0, 0), |acc, succeeded| { .fold((0, 0), |acc, succeeded| {
let (worked, failed) = acc; let (worked, failed) = acc;
@ -219,6 +174,51 @@ impl<'a> Notes<'a> {
} }
fn preprocess_file_for_markdown(&self, file: Rc<RefCell<File>>) -> 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!(" <small>(<i>{}</i>)</small>", tags.join(", "))
} else {
format!(" <small>(No Tags)</small>")
}
};
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!("<h1>{}</h1><small>{}</small>{}\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 { fn command_list(&self, matches: &ArgMatches) -> bool {
use ansi_term::Colour::{Red, Green}; use ansi_term::Colour::{Red, Green};
use ui::file::{FilePrinter, TablePrinter}; use ui::file::{FilePrinter, TablePrinter};