libimagmarkdowncmd/
lib.rs

1//
2// imag - the personal information management suite for the commandline
3// Copyright (C) 2015-2020 Matthias Beyer <mail@beyermatthias.de> and contributors
4//
5// This library is free software; you can redistribute it and/or
6// modify it under the terms of the GNU Lesser General Public
7// License as published by the Free Software Foundation; version
8// 2.1 of the License.
9//
10// This library is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13// Lesser General Public License for more details.
14//
15// You should have received a copy of the GNU Lesser General Public
16// License along with this library; if not, write to the Free Software
17// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
18//
19
20#![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
62/// Marker enum for implementing ImagApplication on
63///
64/// This is used by binaries crates to execute business logic
65/// or to build a CLI completion.
66pub 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}