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
use std::{
    io::{stdin, stdout},
    process::exit,
};

use anyhow::Result;
use clap::{Parser, Subcommand};
use derive_everything::derive_everything;
use mdbook_fork4ls::{
    book::{Book, Chapter},
    preprocess::CmdPreprocessor,
    BookItem,
};
use regex::Regex;
use serde::Deserialize;
use tracing::{debug, trace, warn};

pub fn run() -> Result<()> {
    let (ctx, mut book) = CmdPreprocessor::parse_input(stdin())?;
    let raw_config: RawConfig = match ctx.config.get("preprocessor.header-footer") {
        Some(raw) => raw.clone().try_into(),
        None => Ok(Default::default()),
    }?;
    let config = raw_config.compile()?;
    pad_book(&config, &mut book)?;
    serde_json::to_writer(stdout(), &book)?;
    exit(0);
}

pub fn pad_book(config: &Config, book: &mut Book) -> Result<()> {
    book.for_each_mut(|item| {
        if let BookItem::Chapter(Chapter {
            content,
            path: Some(path),
            ..
        }) = item
        {
            match path.to_str() {
                Some(path) => match config.pad_chapter(content, path) {
                    Some(new_content) => *content = new_content,
                    None => debug!(?path, "Did not pad chapter."),
                },
                _ => warn!(?path, "Chapter path is not a valid UTF-8 string."),
            }
        }
    });
    Ok(())
}

pub struct Config {
    headers: Vec<Matcher>,
    footers: Vec<Matcher>,
}

impl Config {
    pub fn pad_chapter(&self, content: &str, path: &str) -> Option<String> {
        let (mut headers, mut footers) = (Vec::new(), Vec::new());
        for header in &self.headers {
            if header.regex.is_match(path) {
                trace!(?header.regex_str, ?path, "Match");
                headers.push(&header.padding);
            }
        }
        for footer in &self.footers {
            if footer.regex.is_match(path) {
                trace!(?footer.regex_str, ?path, "Match");
                footers.push(&footer.padding);
            }
        }
        if headers.is_empty() && footers.is_empty() {
            None
        } else {
            let capacity = headers.iter().map(|s| s.len()).sum::<usize>()
                + content.len()
                + footers.iter().map(|s| s.len()).sum::<usize>();
            let mut result = String::with_capacity(capacity);
            for header in headers {
                result.push_str(header);
            }
            result.push_str(content);
            for footer in footers {
                result.push_str(footer);
            }
            Some(result)
        }
    }
}

pub struct Matcher {
    regex: Regex,
    regex_str: String,
    padding: String,
}

#[derive(Deserialize)]
#[derive_everything]
pub struct RawConfig {
    headers: Vec<RawMatcher>,
    footers: Vec<RawMatcher>,
}

impl RawConfig {
    pub fn compile(self) -> Result<Config> {
        Ok(Config {
            headers: self
                .headers
                .into_iter()
                .map(RawMatcher::compile)
                .collect::<Result<_>>()?,
            footers: self
                .footers
                .into_iter()
                .map(RawMatcher::compile)
                .collect::<Result<_>>()?,
        })
    }
}

#[derive(Deserialize)]
#[derive_everything]
#[serde(rename_all = "kebab-case")]
pub struct RawMatcher {
    #[serde(default = "default_regex_str")]
    regex: String,
    padding: String,
}

impl RawMatcher {
    pub fn compile(self) -> Result<Matcher> {
        Ok(Matcher {
            regex: Regex::new(&self.regex)?,
            regex_str: self.regex,
            padding: self.padding,
        })
    }
}

fn default_regex_str() -> String {
    ".*".into()
}

/// mdBook preprocessor to prepend header and append footer to certain chapters.
#[derive(Parser)]
#[command(version, about)]
pub struct App {
    #[command(subcommand)]
    pub command: Option<Command>,
}

/// Checks if renderer is supported.
#[derive(Subcommand)]
pub enum Command {
    Supports {
        /// The renderer to check support for.
        renderer: String,
    },
}