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
482
483
484
485
486
487
488
489
490
491
use indexmap::IndexMap;
use md5::{Digest, Md5};
use mdbook::book::{Book, BookItem};
use mdbook::errors::*;
use mdbook::preprocess::{Preprocessor, PreprocessorContext};
use mdbook::MDBook;
use regex::Regex;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::fs;
use std::fs::DirEntry;
use std::io::prelude::*;
use std::io::{BufReader, BufWriter};
use std::ops::Range;
use std::path::Path;

const README_FILE: &str = "README.md";
const SUMMARY_FILE: &str = "SUMMARY.md";

const TITLE_WAY: &str = "title";

#[derive(Debug)]
pub struct MdFile {
    pub meta: Meta,
    pub file: String,
    pub path: String,
}

#[derive(Debug)]
pub struct MdGroup {
    pub name: String,
    pub path: String,
    pub has_readme: bool,
    pub md_list: Vec<MdFile>,
    pub group_list: Vec<MdGroup>,
    pub group_map: IndexMap<String, Vec<MdFile>>,
}

/// A preprocessor for reading YAML front matter from a markdown file.
/// - `author` - For setting the author meta tag.
/// - `title` - For overwritting the title tag.
/// - `description` - For setting the description meta tag.
/// - `keywords` - For setting the keywords meta tag.
/// - `dir` - For setting the dir meta tag.
#[derive(Debug, Default, Serialize, Deserialize)]
pub struct Meta {
    pub section: Option<String>,
    pub title: Option<String>,
    pub author: Option<String>,
    pub description: Option<String>,
    pub keywords: Option<Vec<String>>,
}

#[derive(Default)]
pub struct CMSPreprocessor;

impl CMSPreprocessor {
    pub(crate) const NAME: &'static str = "cms";

    /// Create a new `MetadataPreprocessor`.
    pub fn new() -> Self {
        CMSPreprocessor
    }
}

impl Preprocessor for CMSPreprocessor {
    fn name(&self) -> &str {
        Self::NAME
    }

    fn run(&self, ctx: &PreprocessorContext, mut _book: Book) -> Result<Book> {
        let mut title_way = "filename";

        // In testing we want to tell the preprocessor to blow up by setting a
        // particular config value
        if let Some(nop_cfg) = ctx.config.get_preprocessor(self.name()) {
            if nop_cfg.contains_key("blow-up") {
                anyhow::bail!("Boom!!1!");
            }
            if nop_cfg.contains_key(TITLE_WAY) {
                let v = nop_cfg.get(TITLE_WAY).unwrap();
                title_way = v.as_str().unwrap_or("filename");
            }
        }

        let source_dir = ctx
            .root
            .join(&ctx.config.book.src)
            .to_str()
            .unwrap()
            .to_string();

        gen_summary(&source_dir, title_way);

        match MDBook::load(&ctx.root) {
            Ok(mut mdbook) => {
                mdbook.book.for_each_mut(|section: &mut BookItem| {
                    if let BookItem::Chapter(ref mut ch) = *section {
                        if let Some(m) = Match::find_metadata(&ch.content) {
                            if let Ok(meta) = serde_yaml::from_str(&ch.content[m.range]) {
                                // 暂时不用
                                let _meta: Value = meta;
                                ch.content = String::from(&ch.content[m.end..]);
                            };
                        }
                    }
                });
                Ok(mdbook.book)
            }
            Err(e) => {
                panic!("{}", e);
            }
        }
    }

    fn supports_renderer(&self, renderer: &str) -> bool {
        renderer != "not-supported"
    }
}

pub(crate) struct Match {
    pub(crate) range: Range<usize>,
    pub(crate) end: usize,
}

impl Match {
    pub(crate) fn find_metadata(contents: &str) -> Option<Match> {
        // lazily compute following regex
        // r"\A-{3,}\n(?P<metadata>.*?)^{3,}\n"
        lazy_static::lazy_static! {
            static ref RE: Regex = Regex::new(
                r"(?xms)          # insignificant whitespace mode and multiline
                \A-{3,}\n         # match a horizontal rule at the start of the content
                (?P<metadata>.*?) # name the match between horizontal rules metadata
                ^-{3,}\n          # match a horizontal rule
                "
            )
            .unwrap();
        };
        if let Some(mat) = RE.captures(contents) {
            // safe to unwrap as we know there is a match
            let metadata = mat.name("metadata").unwrap();
            Some(Match {
                range: metadata.start()..metadata.end(),
                end: mat.get(0).unwrap().end(),
            })
        } else {
            None
        }
    }
}

fn md5(buf: &str) -> String {
    let mut hasher = Md5::new();
    hasher.update(buf.as_bytes());
    let f = hasher.finalize();
    let md5_vec = f.as_slice();
    hex::encode_upper(md5_vec)
}

pub fn gen_summary(source_dir: &str, title_way: &str) {
    let mut source_dir = source_dir.to_string();
    if !source_dir.ends_with('/') {
        source_dir.push('/')
    }
    let group = walk_dir(&source_dir, title_way);
    let lines = gen_summary_lines(&source_dir, &group, title_way);
    let buff: String = lines.join("\n");

    let new_md5_string = md5(&buff);

    let summary_file = std::fs::OpenOptions::new()
        .write(true)
        .read(true)
        .create(true)
        .open(source_dir.clone() + "/" + SUMMARY_FILE)
        .unwrap();

    let mut old_summary_file_content = String::new();
    let mut summary_file_reader = BufReader::new(summary_file);
    summary_file_reader
        .read_to_string(&mut old_summary_file_content)
        .unwrap();

    let old_md5_string = md5(&old_summary_file_content);

    if new_md5_string == old_md5_string {
        return;
    }

    let summary_file = std::fs::OpenOptions::new()
        .write(true)
        .read(true)
        .create(true)
        .truncate(true)
        .open(source_dir + "/" + SUMMARY_FILE)
        .unwrap();
    let mut summary_file_writer = BufWriter::new(summary_file);
    summary_file_writer.write_all(buff.as_bytes()).unwrap();
}

fn count(s: &str) -> usize {
    s.split('/').count()
}

fn gen_summary_lines(root_dir: &str, group: &MdGroup, title_way: &str) -> Vec<String> {
    let mut lines: Vec<String> = vec![];

    let path = group.path.replace(root_dir, "");
    let cnt = count(&path);

    let buff_spaces = " ".repeat(4 * (cnt - 1));
    let mut name = group.name.clone();

    let buff_link: String;
    if name == "src" {
        name = String::from("Welcome");
    }

    if path.is_empty() {
        lines.push(String::from("# SUMMARY"));
        buff_link = String::new();
    } else {
        buff_link = format!("{}* [{}]()", buff_spaces, name);
    }

    if buff_spaces.is_empty() {
        lines.push(String::from("\n"));
        if name != "Welcome" {
            lines.push(String::from("----"));
        }
    }

    lines.push(buff_link);

    for md in &group.md_list {
        let path = md.path.replace(root_dir, "");
        if path == SUMMARY_FILE {
            continue;
        }
        if path.ends_with(README_FILE) {
            continue;
        }

        let cnt = count(&path);
        let buff_spaces = " ".repeat(4 * (cnt - 1));

        let buff_link: String;

        let meta = &md.meta;
        let title = match meta.title.as_ref() {
            None => "",
            Some(title) => title,
        };

        if title_way != "filename" && title.is_empty() {
            buff_link = format!("{}* [{}]({})", buff_spaces, title, path);
        } else {
            buff_link = format!("{}* [{}]({})", buff_spaces, md.file, path);
        }

        lines.push(buff_link);
    }

    for (parent, ml) in &group.group_map {
        lines.push(format!("* [{}]()", parent));
        for md in ml {
            let path = md.path.replace(root_dir, "");
            if path == SUMMARY_FILE {
                continue;
            }
            if path.ends_with(README_FILE) {
                continue;
            }
            let buff_spaces = " ".repeat(4);

            let buff_link: String;

            let meta = &md.meta;
            let title = match meta.title.as_ref() {
                None => "",
                Some(title) => title,
            };

            if title_way != "filename" && title.is_empty() {
                buff_link = format!("{}* [{}]({})", buff_spaces, title, path);
            } else {
                buff_link = format!("{}* [{}]({})", buff_spaces, md.file, path);
            }

            lines.push(buff_link);
        }
    }

    for group in &group.group_list {
        let mut line = gen_summary_lines(root_dir, group, title_way);
        lines.append(&mut line);
    }

    lines
}

fn get_meta(entry: &DirEntry, title_way: &str) -> Meta {
    let md_file = std::fs::File::open(entry.path().to_str().unwrap()).unwrap();
    let mut md_file_content = String::new();
    let mut md_file_reader = BufReader::new(md_file);
    md_file_reader.read_to_string(&mut md_file_content).unwrap();

    match title_way {
        "first-line" => {
            let lines = md_file_content.split('\n');

            let mut title: String = "".to_string();
            let mut first_h1_line = "";
            for line in lines {
                if line.starts_with("# ") {
                    first_h1_line = line.trim_matches('#').trim();
                    break;
                }
            }

            if first_h1_line.is_empty() {
                title = first_h1_line.to_string();
            }

            Meta {
                section: None,
                title: Some(title),
                author: None,
                description: None,
                keywords: None,
            }
        }
        "meta" => {
            if let Some(m) = Match::find_metadata(&md_file_content) {
                let meta_info = &md_file_content[m.range];

                match serde_yaml::from_str(meta_info) {
                    Ok(meta) => meta,
                    Err(_e) => Meta::default(),
                }
            } else {
                Meta::default()
            }
        }
        _ => Meta::default(),
    }
}

fn walk_dir(dir: &str, title_way: &str) -> MdGroup {
    let read_dir = fs::read_dir(dir).unwrap();
    let name = Path::new(dir)
        .file_name()
        .unwrap()
        .to_owned()
        .to_str()
        .unwrap()
        .to_string();
    let mut group = MdGroup {
        name,
        path: dir.to_string(),
        has_readme: false,
        group_list: vec![],
        md_list: vec![],
        group_map: Default::default(),
    };

    for entry in read_dir {
        let entry = entry.unwrap();
        // println!("{:?}", entry);
        if entry.file_type().unwrap().is_dir() {
            let g = walk_dir(entry.path().to_str().unwrap(), title_way);
            if g.has_readme {
                group.group_list.push(g);
            }
            continue;
        }
        let file_name = entry.file_name();
        let file_name = file_name.to_str().unwrap().to_string();
        if file_name == README_FILE {
            group.has_readme = true;
        }
        let arr: Vec<&str> = file_name.split('.').collect();
        if arr.len() < 2 {
            continue;
        }
        let file_name = arr[0];
        let file_ext = arr[1];
        if file_ext.to_lowercase() != "md" {
            continue;
        }

        let meta = get_meta(&entry, title_way);

        match &meta.section {
            None => {
                let md = MdFile {
                    meta,
                    file: file_name.to_string(),
                    path: entry.path().to_str().unwrap().to_string(),
                };
                group.md_list.push(md);
            }
            Some(meta_dir) => {
                let meta_dir = meta_dir.clone();
                let md = MdFile {
                    meta,
                    file: file_name.to_string(),
                    path: entry.path().to_str().unwrap().to_string(),
                };
                (*group.group_map.entry(meta_dir.clone()).or_default()).push(md);
            }
        }
    }

    group
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_find_metadata_not_at_start() {
        let s = "\
        content\n\
        ---
        author: \"Adam\"
        title: \"Blog Post #1\"
        keywords:
          : \"rust\"
          : \"blog\"
        date: \"2021/02/15\"
        modified: \"2021/02/16\"\n\
        ---
        content
        ";
        if let Some(_) = Match::find_metadata(s) {
            panic!()
        }
    }

    #[test]
    fn test_find_metadata_at_start() {
        let s = "\
        ---
        author: \"Adam\"
        title: \"Blog Post #1\"
        keywords:
          - \"rust\"
          - \"blog\"
        date: \"2021/02/15\"
        description: \"My rust blog.\"
        modified: \"2021/02/16\"\n\
        ---\n\
        content
        ";
        if let None = Match::find_metadata(s) {
            panic!()
        }
    }

    #[test]
    fn test_find_metadata_partial_metadata() {
        let s = "\
        ---
        author: \"Adam\n\
        content
        ";
        if let Some(_) = Match::find_metadata(s) {
            panic!()
        }
    }

    #[test]
    fn test_find_metadata_not_metadata() {
        type Map = serde_json::Map<String, serde_json::Value>;
        let s = "\
        ---
        This is just standard content that happens to start with a line break
        and has a second line break in the text.\n\
        ---
        followed by more content
        ";
        if let Some(m) = Match::find_metadata(s) {
            if let Ok(_) = serde_yaml::from_str::<Map>(&s[m.range]) {
                panic!()
            }
        }
    }
}