1 use super::*;
2 use lazy_static::lazy_static;
3 use spawning::SpawningFileAdapter;
4 use std::process::Command;
5 
6 // from https://github.com/jgm/pandoc/blob/master/src/Text/Pandoc/App/FormatHeuristics.hs
7 // excluding formats that could cause problems (.db ?= sqlite) or that are already text formats (e.g. xml-based)
8 //"db"       -> Just "docbook"
9 //"adoc"     -> Just "asciidoc"
10 //"asciidoc" -> Just "asciidoc"
11 //"context"  -> Just "context"
12 //"ctx"      -> Just "context"
13 //"dokuwiki" -> Just "dokuwiki"
14 //"htm"      -> Just "html"
15 //"html"     -> Just "html"
16 //"json"     -> Just "json"
17 //"latex"    -> Just "latex"
18 //"lhs"      -> Just "markdown+lhs"
19 //"ltx"      -> Just "latex"
20 //"markdown" -> Just "markdown"
21 //"md"       -> Just "markdown"
22 //"ms"       -> Just "ms"
23 //"muse"     -> Just "muse"
24 //"native"   -> Just "native"
25 //"opml"     -> Just "opml"
26 //"org"      -> Just "org"
27 //"roff"     -> Just "ms"
28 //"rst"      -> Just "rst"
29 //"s5"       -> Just "s5"
30 //"t2t"      -> Just "t2t"
31 //"tei"      -> Just "tei"
32 //"tei.xml"  -> Just "tei"
33 //"tex"      -> Just "latex"
34 //"texi"     -> Just "texinfo"
35 //"texinfo"  -> Just "texinfo"
36 //"textile"  -> Just "textile"
37 //"text"     -> Just "markdown"
38 //"txt"      -> Just "markdown"
39 //"xhtml"    -> Just "html"
40 //"wiki"     -> Just "mediawiki"
41 
42 static EXTENSIONS: &[&str] = &["epub", "odt", "docx", "fb2", "ipynb"];
43 
44 lazy_static! {
45     static ref METADATA: AdapterMeta = AdapterMeta {
46         name: "pandoc".to_owned(),
47         version: 3,
48         description:
49             "Uses pandoc to convert binary/unreadable text documents to plain markdown-like text"
50                 .to_owned(),
51         recurses: false,
52         fast_matchers: EXTENSIONS
53             .iter()
54             .map(|s| FastMatcher::FileExtension(s.to_string()))
55             .collect(),
56         slow_matchers: None
57     };
58 }
59 #[derive(Default)]
60 pub struct PandocAdapter;
61 
62 impl PandocAdapter {
new() -> PandocAdapter63     pub fn new() -> PandocAdapter {
64         PandocAdapter
65     }
66 }
67 impl GetMetadata for PandocAdapter {
metadata(&self) -> &AdapterMeta68     fn metadata(&self) -> &AdapterMeta {
69         &METADATA
70     }
71 }
72 impl SpawningFileAdapter for PandocAdapter {
get_exe(&self) -> &str73     fn get_exe(&self) -> &str {
74         "pandoc"
75     }
command(&self, filepath_hint: &Path, mut cmd: Command) -> Command76     fn command(&self, filepath_hint: &Path, mut cmd: Command) -> Command {
77         cmd.arg("--from")
78             .arg(filepath_hint.extension().unwrap())
79             // simpler markown (with more information loss but plainer text)
80             //.arg("--to=commonmark-header_attributes-link_attributes-fenced_divs-markdown_in_html_blocks-raw_html-native_divs-native_spans-bracketed_spans")
81             .arg("--to=plain")
82             .arg("--wrap=none")
83             .arg("--atx-headers");
84         cmd
85     }
86 }
87