Add support for formatted markdown output

This patch also removes the (unused) "compile" subcommand and replaces
it with a flag "-c" / "--compile" which prints the output in formatted
to the terminal.

Compiling the output from markdown to HTML and showing in the browser is
not yet possible.
This commit is contained in:
Matthias Beyer 2018-04-24 15:23:32 +02:00
parent d12844aff0
commit c2fd3fa94e
3 changed files with 44 additions and 42 deletions

View file

@ -31,9 +31,12 @@ tempfile = "2.1"
libimagstore = { version = "0.8.0", path = "../../../lib/core/libimagstore" } libimagstore = { version = "0.8.0", path = "../../../lib/core/libimagstore" }
libimagrt = { version = "0.8.0", path = "../../../lib/core/libimagrt" } libimagrt = { version = "0.8.0", path = "../../../lib/core/libimagrt" }
libimagerror = { version = "0.8.0", path = "../../../lib/core/libimagerror" } libimagerror = { version = "0.8.0", path = "../../../lib/core/libimagerror" }
libimagentryview = { version = "0.8.0", path = "../../../lib/entry/libimagentryview" }
libimagutil = { version = "0.8.0", path = "../../../lib/etc/libimagutil" } libimagutil = { version = "0.8.0", path = "../../../lib/etc/libimagutil" }
[dependencies.libimagentryview]
version = "0.8.0"
path = "../../../lib/entry/libimagentryview"
[dependencies.clap] [dependencies.clap]
version = "^2.29" version = "^2.29"
default-features = false default-features = false

View file

@ -62,6 +62,7 @@ use libimagerror::str::ErrFromStr;
use libimagerror::trace::MapErrTrace; use libimagerror::trace::MapErrTrace;
use libimagerror::iter::TraceIterator; use libimagerror::iter::TraceIterator;
use libimagentryview::builtin::stdout::StdoutViewer; use libimagentryview::builtin::stdout::StdoutViewer;
use libimagentryview::builtin::md::MarkdownViewer;
use libimagentryview::viewer::Viewer; use libimagentryview::viewer::Viewer;
use libimagentryview::error::ViewError as VE; use libimagentryview::error::ViewError as VE;
use libimagstore::storeid::IntoStoreId; use libimagstore::storeid::IntoStoreId;
@ -171,24 +172,7 @@ fn main() {
drop(files); drop(files);
} else { } else {
let mut viewer = StdoutViewer::new(view_header, !hide_content); let iter = entry_ids
if rt.cli().occurrences_of("autowrap") != 0 {
let width = rt.cli().value_of("autowrap").unwrap(); // ensured by clap
let width = usize::from_str(width).unwrap_or_else(|e| {
error!("Failed to parse argument to number: autowrap = {:?}",
rt.cli().value_of("autowrap").map(String::from));
error!("-> {:?}", e);
::std::process::exit(1)
});
viewer.wrap_at(width);
}
let output = rt.stdout();
let mut lockout = output.lock();
entry_ids
.into_iter() .into_iter()
.into_get_iter(rt.store()) .into_get_iter(rt.store())
.map(|e| { .map(|e| {
@ -196,10 +180,35 @@ fn main() {
.ok_or_else(|| String::from("Entry not found")) .ok_or_else(|| String::from("Entry not found"))
.map_err(StoreError::from) .map_err(StoreError::from)
.map_err_trace_exit_unwrap(1) .map_err_trace_exit_unwrap(1)
})
.for_each(|e| {
viewer.view_entry(&e, &mut lockout).map_err_trace_exit_unwrap(1);
}); });
let out = rt.stdout();
let mut outlock = out.lock();
if rt.cli().is_present("compile-md") {
let viewer = MarkdownViewer::new(&rt);
for entry in iter {
viewer.view_entry(&entry, &mut outlock).map_err_trace_exit_unwrap(1);
}
} else {
let mut viewer = StdoutViewer::new(view_header, !hide_content);
if rt.cli().occurrences_of("autowrap") != 0 {
let width = rt.cli().value_of("autowrap").unwrap(); // ensured by clap
let width = usize::from_str(width).unwrap_or_else(|e| {
error!("Failed to parse argument to number: autowrap = {:?}",
rt.cli().value_of("autowrap").map(String::from));
error!("-> {:?}", e);
::std::process::exit(1)
});
viewer.wrap_at(width);
}
for entry in iter {
viewer.view_entry(&entry, &mut outlock).map_err_trace_exit_unwrap(1);
}
}
} }
} }

View file

@ -17,7 +17,7 @@
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
// //
use clap::{Arg, ArgGroup, App, SubCommand}; use clap::{Arg, ArgGroup, App};
pub fn build_ui<'a>(app: App<'a, 'a>) -> App<'a, 'a> { pub fn build_ui<'a>(app: App<'a, 'a>) -> App<'a, 'a> {
app app
@ -64,6 +64,15 @@ pub fn build_ui<'a>(app: App<'a, 'a>) -> App<'a, 'a> {
.required(false) .required(false)
.help("Do not view content")) .help("Do not view content"))
.arg(Arg::with_name("compile-md")
.long("compile")
.short("c")
.takes_value(false)
.required(false)
.help("Do compile markdown to be nice")
.conflicts_with("not-view-content")
.conflicts_with("autowrap")) // markdown viewer does not support wrapping
.arg(Arg::with_name("in") .arg(Arg::with_name("in")
.long("in") .long("in")
.takes_value(true) .takes_value(true)
@ -71,23 +80,4 @@ pub fn build_ui<'a>(app: App<'a, 'a>) -> App<'a, 'a> {
.multiple(false) .multiple(false)
.help("View content. If no value is given, this fails. Possible viewers are configured via the config file.")) .help("View content. If no value is given, this fails. Possible viewers are configured via the config file."))
.subcommand(SubCommand::with_name("compile")
.about("Compile content to other format for viewing, implies that the entry gets copied to /tmp")
.version("0.1")
.arg(Arg::with_name("from")
.long("from")
.short("f")
.takes_value(true) // "markdown" or "textile" or "restructuredtex"
.required(true)
.help("Compile from")
.value_name("FORMAT"))
.arg(Arg::with_name("to")
.long("to")
.short("t")
.takes_value(true) // "html" or "HTML" or ... json maybe?
.required(true)
.help("Compile to")
.value_name("FORMAT"))
)
} }