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
#[macro_use]
extern crate serde_derive;
use anyhow::{Context, Result};
use std::env::current_dir;
use std::fs::{read_to_string, File};
use std::io::Write;
use std::path::{Path, PathBuf};
pub use structopt::StructOpt;
mod config;
mod parse;
use crate::config::ConfigAndPath;
use crate::parse::Parser;
pub use crate::{
    config::{Config, OutputTo},
    parse::ParserConfig,
};

#[cfg(test)]
mod test;

///
/// Include files in Markdown docs
/// Can be after from command-line arguments using `Args::from_args()` (uses the `StructOpt` trait)
///
#[derive(Debug, StructOpt, Default, Clone)]
#[structopt(name = "md-inc", about = "Include files in Markdown docs")]
pub struct Args {
    ///
    /// A list of files to transform
    ///
    #[structopt(parse(from_os_str))]
    files: Vec<PathBuf>,

    ///
    /// An optional path to output the generated file to.
    /// If not present, the files will be inserted inline
    ///
    #[structopt(long = "out", parse(from_os_str))]
    out_dir: Option<PathBuf>,

    ///
    /// Override the opening tag for a command block
    ///
    #[structopt(
        short = "O",
        long,
        help = "Tag used for opening commands (default: '<!--|')"
    )]
    open_tag: Option<String>,

    ///
    /// Override the closing tag for a command block
    ///
    #[structopt(
        short = "C",
        long,
        help = "Tag used for closing commands (default: '|-->')"
    )]
    close_tag: Option<String>,

    ///
    /// Override the 'end' command name
    ///
    #[structopt(short, long, help = "Command used to end a block (default: 'end')")]
    end_command: Option<String>,

    ///
    /// The base directory used to reference imported files
    ///
    #[structopt(
        short = "b",
        long = "base-dir",
        parse(from_os_str),
        help = "Base directory used when referencing imports"
    )]
    base_dir: Option<PathBuf>,

    ///
    /// Set 1 or more working directories that may contain a '.md-inc.toml' config file.
    ///
    #[structopt(
        short = "d",
        long = "dir",
        parse(from_os_str),
        help = "Working directories"
    )]
    working_dir: Vec<PathBuf>,

    ///
    /// Ignore automatic detection of '.md-inc.toml' config files in the working directory
    ///
    #[structopt(
        short,
        long = "ignore-config",
        help = "Ignore '.md-inc.toml' files in the directory"
    )]
    ignore_config: bool,

    ///
    /// A custom '.toml' config file
    ///
    #[structopt(short, long, parse(from_os_str), help = "Path to a config file")]
    config: Option<PathBuf>,

    ///
    /// If true, the output is not written back to the file
    ///
    #[structopt(short = "R", long = "read-only", help = "Skip writing output to file")]
    read_only: bool,

    ///
    /// Scans all subdirectories for '.md-inc.toml' files
    ///
    #[structopt(
        short = "r",
        long = "recursive",
        help = "Run for all subfolders containing '.md-inc.toml'"
    )]
    recursive: bool,

    ///
    /// Searches the working directory for all matching config files
    /// and transforms files using each config file
    ///
    #[structopt(
        short = "g",
        long = "glob",
        help = "Custom globs used to match config files"
    )]
    glob: Vec<String>,

    ///
    /// Prints the transformed files to stdout
    ///
    #[structopt(short, long, help = "Print output to stdout")]
    print: bool,
}

///
/// Transforms a list of input files
///
/// # Returns
/// A Result containing the transformed input and vec of directories to also check
///
/// # Parameters
/// * `args` An struct of configuration settings.
///
///
pub fn transform_files_with_args(args: Args, config: Option<ConfigAndPath>) -> Result<Vec<String>> {
    let mut out_dir: Option<PathBuf> = None;
    if let Some(x) = args.working_dir.first() {
        if x.exists() {
            std::env::set_current_dir(&x)
                .with_context(|| format!("Could not set working directory: {:?}", &x))?;
        }
    }
    let (mut parser, files) = if let Some(cfg) = config {
        let parent = cfg.parent_path()?;
        out_dir = cfg.config.out_dir.as_ref().map(|x| parent.join(x));
        cfg.into_parser()?
    } else {
        (ParserConfig::default(), args.files)
    };

    if let Some(x) = args.out_dir {
        out_dir = Some(x);
    }
    if let Some(x) = args.open_tag {
        parser.tags.opening = x;
    }
    if let Some(x) = args.close_tag {
        parser.tags.closing = x;
    }
    if let Some(x) = args.end_command {
        parser.end_command = x;
    }
    if let Some(x) = args.base_dir {
        parser.base_dir = x;
    }
    transform_files(
        parser,
        &files,
        OutputTo {
            read_only: args.read_only,
            print: args.print,
            out_dir,
        },
    )
}

///
/// Transforms files
///
/// # Parameters
/// * `parser` A parser which contains override configuration and a base directory
/// * `files` A list of files to be transformed
/// * `prefs` Output configuration settings
///
/// # Example
///
/// ```
/// use md_inc::{transform_files, OutputTo, ParserConfig};
/// transform_files(ParserConfig::default(), &["README.md"], OutputTo::stdout());
/// ```
///
pub fn transform_files<P: AsRef<Path>>(
    parser: ParserConfig,
    files: &[P],
    prefs: OutputTo,
) -> Result<Vec<String>> {
    let (read_only, print, out_dir) = (prefs.read_only, prefs.print, prefs.out_dir);
    Ok(files
        .iter()
        .map(|file| {
            let file = file.as_ref();
            print!(" {}", &file.to_str().unwrap_or_default());
            let file_parser = Parser::new(parser.clone(), read_to_string(file.clone())?);
            let res = file_parser.parse()?;
            if !read_only {
                match &out_dir {
                    Some(path) => {
                        let mut path = path.clone();
                        if path.is_dir() {
                            let name = file.file_name().and_then(|x| x.to_str()).unwrap_or("out");
                            path = path.join(name)
                        }
                        if path.is_file() {
                            // Check if contents has changed
                            let contents = read_to_string(&path)?;
                            if contents == res {
                                println!(" [[No changes]]");
                                return Ok(res); // Next file
                            }
                        }
                        let mut f = File::create(&path)?;
                        f.write_all(res.as_bytes())?;
                        println!(" [[Updated!]]")
                    }
                    _ => {
                        if res != file_parser.content {
                            let mut f = File::create(&file)?;
                            f.write_all(res.as_bytes())?;
                            println!(" [[Updated!]]")
                        } else {
                            println!(" [[No changes]]");
                        }
                    }
                }
            }
            if print {
                println!("\n{}", res);
            }
            Ok(res)
        })
        .collect::<Result<Vec<_>>>()?)
}

///
/// Transform files based on the arguments in `args`.
///
/// If `recursive` is true, the `glob` will be used to find matching config files,
/// (or "**/.md-inc.toml" if not set)
/// if `files` is set, they will be transformed, otherwise the `files` field in the config file(s)
/// will be used.
/// Similarly, any fields in the config file will be overridden if also set in `args`.
///
pub fn walk_transform(mut args: Args) -> Result<Vec<Vec<String>>> {
    if let Some(x) = &args.working_dir.first() {
        std::env::set_current_dir(x)?;
    }
    let mut subdirs: Vec<PathBuf> = args.working_dir.clone();
    if args.recursive {
        args.recursive = false;
        let find_glob = |g| {
            glob::glob(g)
                .expect("Failed to read glob pattern")
                .filter_map(|path| path.ok().and_then(|x| x.parent().map(|x| x.to_path_buf())))
                .collect::<Vec<_>>()
        };
        if args.glob.is_empty() {
            subdirs.append(&mut find_glob("**/.md-inc.toml"));
        }
        for g in &args.glob {
            subdirs.append(&mut find_glob(g.as_str()));
        }
        if subdirs.is_empty() {
            return Err(anyhow::anyhow!("Did not find any matches for globs"));
        }
    }

    let config: Option<ConfigAndPath> = if let Some(path) = &args.config {
        Some(ConfigAndPath {
            config: Config::try_from_path(&path)?,
            path: path.to_path_buf(),
        })
    } else if !args.ignore_config {
        Config::try_from_dir(current_dir()?)?
    } else {
        None
    };

    let config = if let Some(x) = config {
        let parent = x
            .path
            .parent()
            .context("Directory of config file could not be determined")?;
        if subdirs.is_empty() {
            subdirs = vec![current_dir()?];
        }
        subdirs = x
            .config
            .depend_dirs
            .iter()
            .map(|x| parent.join(x))
            .chain(subdirs.into_iter())
            .chain(x.config.next_dirs.iter().map(|x| parent.join(x)))
            .collect();
        Some(x)
    } else {
        None
    };

    let res = if subdirs.is_empty() {
        let res = transform_files_with_args(args.clone(), config.clone())?;
        vec![res]
    } else {
        let mut res = vec![];
        for x in subdirs {
            println!(">> {}", x.to_str().unwrap_or_default());
            match &config {
                Some(cfg) if cfg.path == x => {
                    args.working_dir = vec![x];
                    res.push(transform_files_with_args(args.clone(), config.clone())?);
                }
                _ => {
                    if let Some(config) = Config::try_from_dir(&x)? {
                        args.working_dir = vec![x];
                        res.push(transform_files_with_args(args.clone(), Some(config))?);
                    }
                }
            }
        }
        res
    };
    Ok(res)
}