libimagmarkdowncmd/
lib.rs1#![forbid(unsafe_code)]
21
22#![deny(
23 non_camel_case_types,
24 non_snake_case,
25 path_statements,
26 trivial_numeric_casts,
27 unstable_features,
28 unused_allocation,
29 unused_import_braces,
30 unused_imports,
31 unused_must_use,
32 unused_mut,
33 unused_qualifications,
34 while_true,
35)]
36
37extern crate clap;
38#[macro_use] extern crate log;
39extern crate failure;
40extern crate resiter;
41
42extern crate libimagerror;
43extern crate libimagrt;
44extern crate libimagstore;
45
46use std::io::Write;
47
48use failure::Error;
49use failure::err_msg;
50use failure::Fallible as Result;
51use resiter::AndThen;
52use resiter::Map;
53use resiter::IterInnerOkOrElse;
54use clap::App;
55
56use libimagrt::runtime::Runtime;
57use libimagrt::application::ImagApplication;
58use libimagstore::iter::get::StoreIdGetIteratorExtension;
59
60mod ui;
61
62pub enum ImagMarkdown {}
67impl ImagApplication for ImagMarkdown {
68 fn run(rt: Runtime) -> Result<()> {
69 let only_links = rt.cli().is_present("links");
70 let out = rt.stdout();
71 let mut outlock = out.lock();
72
73 let iter = rt
74 .ids::<crate::ui::PathProvider>()?
75 .ok_or_else(|| err_msg("No ids supplied"))?
76 .into_iter()
77 .map(Ok)
78 .into_get_iter(rt.store())
79 .map_inner_ok_or_else(|| err_msg("Entry does not exist but is in store. This is a BUG, please report!"));
80
81 if only_links {
82 debug!("Printing only links");
83 iter.map_ok(|fle| libimagentrymarkdown::link::extract_links(fle.get_content()))
84 .and_then_ok(|links| {
85 links.iter()
86 .map(|link| {
87 writeln!(outlock, "{title}: {link}", title = link.title, link = link.link).map_err(Error::from)
88 })
89 .collect()
90 })
91 .collect()
92
93 } else {
94 iter.and_then_ok(|fle| libimagentrymarkdown::html::to_html(fle.get_content()))
95 .and_then_ok(|html| {
96 writeln!(outlock, "{}", html).map_err(Error::from).map_err(Error::from)
97 })
98 .collect()
99 }
100 }
101
102 fn build_cli<'a>(app: App<'a, 'a>) -> App<'a, 'a> {
103 ui::build_ui(app)
104 }
105
106 fn name() -> &'static str {
107 env!("CARGO_PKG_NAME")
108 }
109
110 fn description() -> &'static str {
111 "Print one or more imag entries after processing them with a markdown parser"
112 }
113
114 fn version() -> &'static str {
115 env!("CARGO_PKG_VERSION")
116 }
117}