mdq 0.10.0

Select and render specific elements in a Markdown document
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
use crate::output;
use crate::output::{LinkTransform, ReferencePlacement};
use clap::error::ErrorKind;
use clap::{CommandFactory, Parser, ValueEnum};
use derive_builder::Builder;
use std::fmt::{Display, Formatter};

macro_rules! create_options_structs {
    (
        $(
            $(#[$meta:meta])*
            clap $clap:tt
            pub $name:ident : $ty:ty
        ),* $(,)?
        {
            // Properties that work a bit differently in CliOptions vs RunOptions, so we just use the macro to specify
            // the documentation.
            $(#[$add_breaks_meta:meta])*
            add_breaks,

            $(#[$selectors_meta:meta])*
            selectors,

            $(#[$md_file_paths_meta:meta])*
            markdown_file_paths,
        }
    ) => {
        #[derive(Clone, Default, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Parser)]
        #[command(version, about, long_about = None)]
        #[doc(hidden)]
        pub struct CliOptions {
            $(
            $(#[$meta])*
            #[arg$clap]
            pub(crate) $name: $ty,
            )*

            $(#[$add_breaks_meta])*
            // Note: this is a fake arg so, we have explicit validation below to ensure it isn't invoked. Clap doesn't let us
            // add boolean flags with 'no-' to disable, so I'm using this trick to fake that out. Basically, this fake arg is
            // only for the help text, and then --br and --no-br are fake but hidden args.
            #[arg(long = "[no]-br", action)]
            pub(crate) br_umbrella: bool,

            /// Negates the --br option.
            #[arg(long, hide = true)]
            pub(crate) br: bool,

            /// Negates the --br option.
            #[arg(long, conflicts_with = "br", hide = true)]
            pub(crate) no_br: bool,

            #[arg(
                short = ' ',
                hide = true,
                group = "selectors_group",
                value_name = "selectors starting with list"
            )]
            pub(crate) list_selector: Option<String>,

            $(#[$selectors_meta])*
            #[arg(group = "selectors_group", value_name = "selectors")]
            pub(crate) selectors: Option<String>,

            $(#[$md_file_paths_meta])*
            #[arg()]
            pub(crate) markdown_file_paths: Vec<String>,
        }

        /// Options analogous to the mdq CLI's switches.
        #[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Builder)]
        pub struct RunOptions {
            $(
            $(#[$meta])*
            pub $name: $ty,
            )*

            $(#[$add_breaks_meta])*
            ///
            /// This is analogous to the `--[no-]br` option in the CLI arguments.
            pub add_breaks: Option<bool>,

            $(#[$selectors_meta])*
            pub selectors: String,

            $(#[$md_file_paths_meta])*
            pub markdown_file_paths: Vec<String>
        }

        impl From<CliOptions> for RunOptions {
            fn from(mut value: CliOptions) -> Self {
                let add_breaks = match (value.br, value.no_br) {
                    (false, false) => None,
                    (true, false) => Some(true),
                    (false, true) => Some(false),
                    (true, true) => {
                        // Clap will prevent this from happening. See test [tests::both_br_and_no_br] below.
                        eprintln!("internal error when determining whether to add breaks between elements. will use default settings");
                        None
                    }
                };
                let selectors = match value.selectors.take() {
                    Some(s) => s,
                    None => match &value.list_selector {
                        Some(list_selectors) => {
                            let mut reconstructed = String::with_capacity(list_selectors.len() + 2);
                            reconstructed.push_str("- ");
                            reconstructed.push_str(list_selectors);
                            reconstructed
                        }
                        None => String::new(),
                    },
                };
                Self {
                    $($name: value.$name,)*
                    markdown_file_paths: value.markdown_file_paths,
                    add_breaks,
                    selectors,
                }
            }
        }
    };
}

create_options_structs! {
    /// Where to put link references.
    ///
    /// For links and images of style `[description][1]`, this flag controls where to put the `[1]: https://example.com`
    /// definition.
    clap(long, value_enum, default_value_t=ReferencePlacement::Section)
    pub link_pos: ReferencePlacement,

    /// Where to put footnote references. Defaults to be same as --link-pos
    clap(long, value_enum)
    pub footnote_pos: Option<ReferencePlacement>,

    clap(long, short, value_enum, default_value_t)
    pub link_format: LinkTransform,

    clap(long, default_value_t = true, action = clap::ArgAction::Set)
    pub renumber_footnotes: bool,

    /// Specifies the output format. Defaults to markdown.
    clap(long, short, default_value_t = OutputFormat::Markdown)
    pub output: OutputFormat,

    /// The number of characters to wrap text at. This is only valid when the output format is
    /// markdown.
    ///
    /// Certain elements (like section headings and link definitions) will never be wrapped, and the
    /// wrapping will never break a word; it will only ever be along existing whitespace. In
    /// particular, this means the wrapping will never add hyphens, and it will never break URLs.
    clap(long)
    pub wrap_width: Option<usize>,

    /// Quiet: do not print anything to stdout. The exit code will still be 0 if any elements match, and non-0 if none do.
    clap(long, short)
    pub quiet: bool,

    // See: tree.rs > Lookups::unknown_markdown.
    clap(long, hide = true)
    pub allow_unknown_markdown: bool,
    {

        /// Include breaks between elements in plain and markdown output mode.
        ///
        /// For plain, this will add a blank line between elements. For markdown, this will add a thematic break
        /// ("-----") between elements.
        ///
        /// This has no effect in JSON output mode.
        ///
        /// This defaults to true for Markdown output, and false for plain text output.
        add_breaks,

        /// The selectors string
        selectors,

        /// An optional list of Markdown files to parse, by path. If not provided, standard input will be used.
        ///
        /// If these are provided, mdq will act as if they were all concatenated into a single file. For example, if you
        /// use --link-pos=doc, the link definitions for all input files will be at the very end of the output.
        ///
        /// A path of "-" represents standard input.
        ///
        /// If these are provided, standard input will not be used unless one of the arguments is "-". Files will be
        /// processed in the order you provide them. If you provide the same file twice, mdq will process it twice, unless
        /// that file is "-"; all but the first "-" paths are ignored.
        markdown_file_paths,
    }
}

impl Default for RunOptions {
    fn default() -> Self {
        Self {
            link_pos: ReferencePlacement::Section,
            footnote_pos: None,
            link_format: LinkTransform::NeverInline,
            renumber_footnotes: true,
            output: OutputFormat::Markdown,
            add_breaks: None,
            wrap_width: None,
            selectors: "".to_string(),
            quiet: false,
            allow_unknown_markdown: false,
            markdown_file_paths: vec![],
        }
    }
}

impl From<&RunOptions> for output::MdWriterOptions {
    fn from(cli: &RunOptions) -> Self {
        output::MdWriterOptions {
            link_reference_placement: cli.link_pos,
            footnote_reference_placement: cli.footnote_pos.unwrap_or(cli.link_pos),
            inline_options: output::InlineElemOptions {
                link_format: cli.link_format,
                renumber_footnotes: cli.renumber_footnotes,
            },
            include_thematic_breaks: cli.should_add_breaks(),
            text_width: cli.wrap_width,
        }
    }
}

impl RunOptions {
    pub fn should_add_breaks(&self) -> bool {
        self.add_breaks.unwrap_or(match self.output {
            OutputFormat::Json => false,
            OutputFormat::Markdown | OutputFormat::Md => true,
            OutputFormat::Plain => false,
        })
    }
}

impl CliOptions {
    pub fn extra_validation(&self) -> bool {
        match self.output {
            OutputFormat::Json => {
                if self.wrap_width.is_some() {
                    let _ = CliOptions::command()
                        .error(
                            ErrorKind::ArgumentConflict,
                            "Can't set text width with JSON output format",
                        )
                        .print();
                    return false;
                }
            }
            OutputFormat::Markdown | OutputFormat::Md => {}
            OutputFormat::Plain => {}
        }
        if self.br_umbrella {
            let _ = CliOptions::command()
                .error(
                    ErrorKind::UnknownArgument,
                    r"invalid argument '--[no]-br'; use '--br' or '--no-br'.",
                )
                .print();
            return false;
        }
        true
    }
}

/// Output formats, analogous to `--output` in the CLI.
#[derive(Copy, Clone, Default, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, ValueEnum)]
#[non_exhaustive]
pub enum OutputFormat {
    /// Output results as Markdown.
    #[default]
    Markdown,

    /// Alias for markdown
    Md,

    /// Output results as JSON. Spans of inline elements (like within a single paragraph) will be rendered as a single string of
    /// Markdown, not as separate JSON elements.
    Json,

    /// Outputs just the plain text. This retrains the spacing between paragraphs and paragraph-like blocks (code
    /// blocks, block quotes, etc.) but removes all other formating, including inline formatting. Links are rendered as
    /// just their display text, and footnotes are removed entirely.
    ///
    /// The block:
    ///
    /// ````markdown
    /// hello from [the world](https://example.com)! It's _so easy_ to do do `hello world` in bash[^1]:
    ///
    /// ```bash
    /// echo 'hello world'
    /// ```
    ///
    /// 1. Here's an ordered list
    ///
    /// - Here's an unordered list.
    /// - With multiple items
    ///
    /// [^1]: assuming you have bash installed, of course
    /// ````
    ///
    /// would render as:
    ///
    /// ```text
    /// hello from the world! It's so easy to do do hello world in bash:
    ///
    /// echo 'hello world'
    ///
    /// Here's an ordered list
    /// With multiple items
    ///
    /// Here's an unordered list.
    /// ```
    Plain,
}

impl Display for OutputFormat {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        let self_str = match self {
            OutputFormat::Markdown | OutputFormat::Md => "markdown",
            OutputFormat::Json => "json",
            OutputFormat::Plain => "plain",
        };
        f.write_str(self_str)
    }
}

#[cfg(test)]
mod tests {
    use crate::run::cli::CliOptions;
    use crate::run::RunOptions;
    use crate::util::utils_for_test::*;
    use clap::{Error, Parser};

    #[test]
    fn verify_cli() {
        use clap::CommandFactory;
        CliOptions::command().debug_assert();
    }

    #[test]
    fn no_args() {
        let result = CliOptions::try_parse_from(["mdq"]);
        unwrap!(result, Ok(cli));
        assert!(cli.markdown_file_paths.is_empty());
        let run_opts: RunOptions = cli.into();
        assert_eq!(run_opts.selectors, "");
    }

    #[test]
    fn no_args_equals_default() {
        let result = CliOptions::try_parse_from(["mdq"]);
        unwrap!(result, Ok(cli));
        let default_run_options = RunOptions::default();
        let from_cli: RunOptions = cli.into();
        assert_eq!(from_cli, default_run_options);
    }

    #[test]
    fn standard_selectors() {
        let result = CliOptions::try_parse_from(["mdq", "# hello"]);
        unwrap!(result, Ok(cli));
        assert!(cli.markdown_file_paths.is_empty());
        let run_opts: RunOptions = cli.into();
        assert_eq!(run_opts.selectors, "# hello");
    }

    #[test]
    fn selector_and_file() {
        let result = CliOptions::try_parse_from(["mdq", "# hello", "file.txt"]);
        unwrap!(result, Ok(cli));
        assert_eq!(cli.markdown_file_paths, ["file.txt"]);
        let run_opts: RunOptions = cli.into();
        assert_eq!(run_opts.selectors, "# hello");
    }

    #[test]
    fn start_with_list_selectors() {
        let result = CliOptions::try_parse_from(["mdq", "- world"]);
        unwrap!(result, Ok(cli));
        let run_opts: RunOptions = cli.into();
        assert_eq!(run_opts.selectors, "- world");
    }

    #[test]
    fn start_with_list_selectors_twice() {
        let result = CliOptions::try_parse_from(["mdq", "- hello", "- world"]);
        check_err(
            &result,
            "the argument '-  <selectors starting with list>' cannot be used multiple times",
        );
    }

    #[test]
    fn both_list_and_std_selectors() {
        let result = CliOptions::try_parse_from(["mdq", "# hello", "- world"]);
        check_err(
            &result,
            "the argument '[selectors]' cannot be used with '-  <selectors starting with list>'",
        )
    }

    #[test]
    fn both_br_and_no_br() {
        let result = CliOptions::try_parse_from(["mdq", "--br", "--no-br"]);
        check_err(&result, "the argument '--br' cannot be used with '--no-br'")
    }

    fn check_err(result: &Result<CliOptions, Error>, expect: &str) {
        unwrap!(result, Err(e));
        let e_str = e.to_string();
        let first_line = e_str.split('\n').next().expect("no error string found");
        let mut expect_full = "error: ".to_string();
        expect_full.push_str(expect);
        assert_eq!(first_line, &expect_full);
    }
}