mdsh 0.7.0

Markdown shell pre-processor
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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
#[macro_use]
extern crate lazy_static;

use std::fs::File;
use std::io::prelude::*;
use std::io::{self, ErrorKind, Write};
use std::process::{Command, Output, Stdio};

use difference::Changeset;
use mdsh::cli::{FileArg, Opt, Parent};
use regex::{Captures, Regex};
use structopt::StructOpt;

fn run_command(command: &str, work_dir: &Parent) -> Output {
    let mut cli = Command::new("bash");
    cli.arg("-c")
        .arg(command)
        .stdin(Stdio::null()) // don't read from stdin
        .current_dir(work_dir.as_path_buf())
        .output()
        .expect(
            format!(
                "fatal: failed to execute command `{:?}` in {}",
                cli,
                work_dir.as_path_buf().display()
            )
            .as_str(),
        )
}

fn die<A>(msg: String) -> A {
    std::io::stderr()
        .write_all(format!("fatal: {}\n", msg).as_bytes())
        .unwrap();
    std::process::exit(1)
}

fn read_file(f: &FileArg) -> String {
    let mut buffer = String::new();

    match f {
        FileArg::StdHandle => {
            let stdin = io::stdin();
            let mut handle = stdin.lock();
            handle
                .read_to_string(&mut buffer)
                .unwrap_or_else(|err| die(format!("failed to read from stdin: {}", err)));
        }
        FileArg::File(path_buf) => {
            File::open(path_buf)
                .and_then(|mut file| file.read_to_string(&mut buffer))
                .unwrap_or_else(|err| {
                    die(format!(
                        "failed to read from {}: {}",
                        path_buf.display(),
                        err
                    ))
                });
        }
    }

    buffer
}

fn write_file(f: &FileArg, contents: String) {
    match f {
        FileArg::StdHandle => {
            let stdout = io::stdout();
            let mut handle = stdout.lock();
            write!(handle, "{}", contents)
                .unwrap_or_else(|err| die(format!("failed to write to stdout: {}", err)));
        }
        FileArg::File(path_buf) => {
            File::create(path_buf)
                .and_then(|mut file| {
                    write!(file, "{}", contents)?;
                    file.sync_all()
                })
                .unwrap_or_else(|err| {
                    die(format!(
                        "failed to write to {}: {}",
                        path_buf.display(),
                        err
                    ))
                });
        }
    }
}

fn trail_nl<T: AsRef<str>>(s: T) -> String {
    let r = s.as_ref();
    if r.ends_with('\n') {
        r.to_string()
    } else {
        format!("{}\n", r)
    }
}

// make sure that the string starts and ends with new lines
fn wrap_nl(s: String) -> String {
    if s.starts_with('\n') {
        trail_nl(s)
    } else if s.ends_with('\n') {
        format!("\n{}", s)
    } else {
        format!("\n{}\n", s)
    }
}

// remove all ANSI escape characters
fn filter_ansi(s: String) -> String {
    RE_ANSI_FILTER.replace_all(&s, "").to_string()
}

/// Link text block include of form `[$ description](./filename)`
static RE_FENCE_LINK_STR: &str = r"\[\$ [^\]]+\]\((?P<link>[^\)]+)\)";
/// Link markdown block include of form `[> description](./filename)`
static RE_MD_LINK_STR: &str = r"\[> [^\]]+\]\((?P<link>[^\)]+)\)";
/// Command text block include of form `\`$ command\``
static RE_FENCE_COMMAND_STR: &str = r"`\$ (?P<command>[^`]+)`";
/// Command markdown block include of form `\`> command\``
static RE_MD_COMMAND_STR: &str = r"`> (?P<command>[^`]+)`";
/// Command to set a variable
static RE_VAR_COMMAND_STR: &str = r"`! (?P<key>[\w_]+)=(?P<raw_value>[^`]+)`";
/// Delimiter block for marking automatically inserted text
static RE_FENCE_BLOCK_STR: &str = r"^```.+?^```";
/// Delimiter block for marking automatically inserted markdown
static RE_MD_BLOCK_STR: &str = r"^<!-- BEGIN mdsh -->.+?^<!-- END mdsh -->";

/// HTML comment wrappers
static RE_COMMENT_BEGIN_STR: &str = r"(?:<!-- +)?";
static RE_COMMENT_END_STR: &str = r"(?: +-->)?";

/// Fenced code type specifier
static RE_FENCE_TYPE_STR: &str = r"(?: as (?P<fence_type>\w+))?";

lazy_static! {
    /// Match a whole text block (`$` command or link and then delimiter block)
    static ref RE_MATCH_FENCE_BLOCK_STR: String = format!(
        r"(?sm)(^{}(?:{}|{}){}{} *$)\n+({}|{})",
        RE_COMMENT_BEGIN_STR, RE_FENCE_COMMAND_STR, RE_FENCE_LINK_STR, RE_FENCE_TYPE_STR, RE_COMMENT_END_STR,
        RE_FENCE_BLOCK_STR, RE_MD_BLOCK_STR,
    );
    /// Match a whole markdown block (`>` command or link and then delimiter block)
    static ref RE_MATCH_MD_BLOCK_STR: String = format!(
        r"(?sm)(^{}(?:{}|{}){} *$)\n+({}|{})",
        RE_COMMENT_BEGIN_STR, RE_MD_COMMAND_STR, RE_MD_LINK_STR, RE_COMMENT_END_STR,
        RE_MD_BLOCK_STR, RE_FENCE_BLOCK_STR,
    );

    static ref RE_MATCH_ANY_COMMAND_STR: String = format!(r"(?sm)^{}(`[^`]+`){}{} *$", RE_COMMENT_BEGIN_STR, RE_FENCE_TYPE_STR, RE_COMMENT_END_STR);
    /// Match `RE_FENCE_COMMAND_STR`
    static ref RE_MATCH_FENCE_COMMAND_STR: String = format!(r"(?sm)^{}$", RE_FENCE_COMMAND_STR);
    /// Match `RE_MD_COMMAND_STR`
    static ref RE_MATCH_MD_COMMAND_STR: String = format!(r"(?sm)^{}$", RE_MD_COMMAND_STR);
    /// Match `RE_VAR_COMMAND_STR`
    static ref RE_MATCH_VAR_COMMAND_STR: String = format!(r"(?sm)^{}$", RE_VAR_COMMAND_STR);

    /// Match `RE_FENCE_LINK_STR`
    static ref RE_MATCH_FENCE_LINK_STR: String = format!(r"(?sm)^{}{}{}{} *$", RE_COMMENT_BEGIN_STR, RE_FENCE_LINK_STR, RE_FENCE_TYPE_STR, RE_COMMENT_END_STR);
    /// Match `RE_MD_LINK_STR`
    static ref RE_MATCH_MD_LINK_STR: String = format!(r"(?sm)^{}{}{} *$", RE_COMMENT_BEGIN_STR, RE_MD_LINK_STR, RE_COMMENT_END_STR);


    static ref RE_MATCH_ANY_COMMAND: Regex = Regex::new(&RE_MATCH_ANY_COMMAND_STR).unwrap();
    static ref RE_MATCH_FENCE_BLOCK: Regex = Regex::new(&RE_MATCH_FENCE_BLOCK_STR).unwrap();
    static ref RE_MATCH_MD_BLOCK: Regex = Regex::new(&RE_MATCH_MD_BLOCK_STR).unwrap();
    static ref RE_MATCH_FENCE_COMMAND: Regex = Regex::new(&RE_MATCH_FENCE_COMMAND_STR).unwrap();
    static ref RE_MATCH_MD_COMMAND: Regex = Regex::new(&RE_MATCH_MD_COMMAND_STR).unwrap();
    static ref RE_MATCH_VAR_COMMAND: Regex = Regex::new(&RE_MATCH_VAR_COMMAND_STR).unwrap();
    static ref RE_MATCH_FENCE_LINK: Regex = Regex::new(&RE_MATCH_FENCE_LINK_STR).unwrap();
    static ref RE_MATCH_MD_LINK: Regex = Regex::new(&RE_MATCH_MD_LINK_STR).unwrap();

    /// ANSI characters filter
    /// https://superuser.com/questions/380772/removing-ansi-color-codes-from-text-stream
    static ref RE_ANSI_FILTER: Regex = Regex::new(r"\x1b\[[0-9;]*[mGKH]").unwrap();
}

struct FailingCommand {
    output: Output,
    command: String,
    command_char: char,
}

fn main() -> std::io::Result<()> {
    let opt = Opt::from_args();
    let clean = opt.clean;
    let frozen = opt.frozen;
    let inputs = opt.inputs;

    if inputs.len() == 0 {
        // Nothing to do
        return Ok(());
    } else if inputs.len() == 1 {
        let input = inputs.first().unwrap();
        let output = opt.output.unwrap_or_else(|| input.clone());
        let work_dir: Parent = opt.work_dir.map_or_else(
            || {
                input
                    .clone()
                    .parent()
                    .expect("fatal: your input file has no parent directory.")
            },
            |buf| Parent::from_parent_path_buf(buf),
        );
        process_file(&input, &output, &work_dir, clean, frozen)?;
    } else {
        if opt.output.is_some() {
            return Err(std::io::Error::new(
                ErrorKind::Other,
                "--output is not compatible with multiple inputs",
            ));
        }
        if opt.work_dir.is_some() {
            return Err(std::io::Error::new(
                ErrorKind::Other,
                "--work-dir is not compatible with multiple inputs",
            ));
        }
        for input in inputs {
            let work_dir = input
                .clone()
                .parent()
                .expect("fatal: your input file has no parent directory.");
            let output = input.clone();
            process_file(&input, &output, &work_dir, clean, frozen)?;
        }
    }

    Ok(())
}

fn process_file(
    input: &FileArg,
    output: &FileArg,
    work_dir: &Parent,
    clean: bool,
    frozen: bool,
) -> std::io::Result<()> {
    let original_contents = read_file(&input);
    let mut contents = original_contents.clone();

    eprintln!(
        "Using input={:?} output={:?} work_dir={:?} clean={:?} frozen={:?}",
        &input, output, work_dir, clean, frozen
    );

    /// Remove all outputs of blocks
    fn clean_blocks(file: &mut String, block_regex: &Regex) {
        *file = block_regex
            .replace_all(file, |caps: &Captures| {
                // the 1 group is our command,
                // the 2nd is the block, which we ignore and thus erase
                caps[1].to_string()
            })
            .into_owned()
    }

    clean_blocks(&mut contents, &RE_MATCH_FENCE_BLOCK);
    clean_blocks(&mut contents, &RE_MATCH_MD_BLOCK);

    // Write the contents and return if --clean is passed
    if clean {
        write_file(&output, contents.to_string());
        return Ok(());
    }

    // Return either the captures fence type with a whitespace in front,
    // or an empty string.
    // That way if the fence type doesn't apply, nothing is being added.
    fn get_fence_type(caps: &Captures) -> String {
        if let Some(name) = &caps.name("fence_type") {
            format!("{}", name.as_str())
        } else {
            format!("")
        }
    }

    let mut failures = Vec::new();

    // Run all commands and fill their blocks.
    let fill_commands =
        |data: &mut String, command_regex: &Regex| -> Result<(), Vec<FailingCommand>> {
            *data = command_regex
                .replace_all(data, |caps: &Captures| {
                    let original_line = &caps[0];
                    let command_line = &caps[1];
                    let fence_type = get_fence_type(caps);
                    eprintln!("{}", command_line);
                    // eprintln!("command_line: {}", command_line);
                    // eprintln!("fence_type: {}", fence_type);

                    if let Some(caps) = RE_MATCH_FENCE_COMMAND.captures(command_line) {
                        let command = &caps["command"];
                        // eprintln!("command: {}", command);
                        let start_delimiter = "```";
                        let end_delimiter = "```";
                        let command_char = '$';

                        // TODO: now match on any of the known commands

                        let result = run_command(command, &work_dir);
                        if result.status.success() {
                            let stdout = String::from_utf8_lossy(&result.stdout);
                            // remove ANSI escape sequences
                            let stdout = filter_ansi(stdout.to_string());
                            // we can leave the output block if stdout was empty
                            if stdout.trim().is_empty() {
                                format!("{}", trail_nl(&original_line))
                            } else {
                                format!(
                                    "{}{}{}{}{}",
                                    trail_nl(&original_line),
                                    start_delimiter,
                                    fence_type,
                                    wrap_nl(stdout.to_string()),
                                    end_delimiter
                                )
                            }
                        } else {
                            failures.push(FailingCommand {
                                output: result,
                                command: command.to_string(),
                                command_char: command_char,
                            });
                            // re-insert what was there before
                            original_line.to_string()
                        }
                    } else if let Some(caps) = RE_MATCH_MD_COMMAND.captures(command_line) {
                        let command = &caps["command"];
                        // eprintln!("command: {}", command);
                        let start_delimiter = "<!-- BEGIN mdsh -->";
                        let end_delimiter = "<!-- END mdsh -->";
                        let command_char = '>';

                        let result = run_command(command, &work_dir);
                        if result.status.success() {
                            let stdout = String::from_utf8_lossy(&result.stdout);
                            // remove ANSI escape sequences
                            let stdout = filter_ansi(stdout.to_string());
                            // we can leave the output block if STDOUT was empty
                            if stdout.trim().is_empty() {
                                format!("{}", trail_nl(&original_line))
                            } else {
                                format!(
                                    "{}{}{}{}{}",
                                    trail_nl(&original_line),
                                    start_delimiter,
                                    fence_type,
                                    wrap_nl(stdout.to_string()),
                                    end_delimiter
                                )
                            }
                        } else {
                            failures.push(FailingCommand {
                                output: result,
                                command: command.to_string(),
                                command_char: command_char,
                            });
                            // re-insert what was there before
                            original_line.to_string()
                        }
                    } else if let Some(caps) = RE_MATCH_VAR_COMMAND.captures(command_line) {
                        let key = &caps["key"];
                        let raw_value = &caps["raw_value"];
                        // eprintln!("key: {}", key);
                        // eprintln!("raw_value: {}", raw_value);
                        let command = format!("echo {}", raw_value.trim());
                        let result = run_command(&command, &work_dir);
                        if result.status.success() {
                            let stdout = String::from_utf8_lossy(&result.stdout);
                            // remove ANSI escape sequences
                            let stdout = filter_ansi(stdout.to_string());
                            // set the environment variable
                            std::env::set_var(key, stdout.trim());
                        } else {
                            failures.push(FailingCommand {
                                output: result,
                                command: command.to_string(),
                                command_char: '!',
                            });
                        };

                        // re-insert what was there before
                        original_line.to_string()
                    } else {
                        panic!("WTF, not supported")
                    }
                })
                .into_owned();
            if failures.is_empty() {
                Ok(())
            } else {
                Err(failures)
            }
        };

    fn print_failures(fs: Vec<FailingCommand>) {
        eprintln!("\nERROR: some commands failed:\n");
        for f in fs {
            let stderr = match String::from_utf8_lossy(&f.output.stderr)
                .into_owned()
                .as_str()
            {
                "" => String::from(""),
                s => String::from("\nIts stderr was:\n") + s.trim_end(),
            };
            eprintln!(
                "`{} {}` failed with status {}.{}\n",
                f.command_char, f.command, f.output.status, stderr
            );
        }
    }

    fill_commands(&mut contents, &RE_MATCH_ANY_COMMAND).or_else(
        |failures| -> std::io::Result<()> {
            print_failures(failures);
            std::process::exit(1);
        },
    )?;

    /// Run all link includes and fill their blocks
    fn fill_includes(
        file: &mut String,
        link_regex: &Regex,
        link_char: char,
        start_delimiter: &str,
        end_delimiter: &str,
    ) {
        *file = link_regex
            .replace_all(file, |caps: &Captures| {
                let link = &caps["link"];
                let fence_type = get_fence_type(caps);

                eprintln!("[{} {}]", link_char, link);

                let result = read_file(&FileArg::from_str_unsafe(link));

                format!(
                    "{}{}{}{}{}",
                    trail_nl(&caps[0]),
                    start_delimiter,
                    fence_type,
                    wrap_nl(result.to_owned()),
                    end_delimiter
                )
            })
            .into_owned()
    }

    fill_includes(&mut contents, &RE_MATCH_FENCE_LINK, '$', "```", "```");
    fill_includes(
        &mut contents,
        &RE_MATCH_MD_LINK,
        '>',
        "<!-- BEGIN mdsh -->",
        "<!-- END mdsh -->",
    );

    // If there is no change, these is nothing left to do.
    if original_contents == contents {
        return Ok(());
    }

    // Let the user know where things have changed
    let changeset = Changeset::new(&original_contents, &contents, "\n");
    eprintln!("{}", changeset);

    // If there are changes and the file is frozen, abort
    if frozen {
        return Err(std::io::Error::new(
            ErrorKind::Other,
            "--frozen: output is not the same",
        ));
    }

    // Write the file
    write_file(&output, contents.to_string());

    return Ok(());
}