mdbook_variables/
lib.rs

1#[macro_use]
2extern crate lazy_static;
3use mdbook_preprocessor::{
4    book::{Book, BookItem, Chapter},
5    errors::Result,
6    Preprocessor, PreprocessorContext,
7};
8use regex::{CaptureMatches, Captures, Regex};
9use toml::value::{Table, Value};
10#[derive(Default)]
11pub struct VariablesPreprocessor;
12
13impl VariablesPreprocessor {
14    pub(crate) const NAME: &'static str = "variables";
15
16    /// Create a new `LinkPreprocessor`.
17    pub fn new() -> Self {
18        VariablesPreprocessor
19    }
20}
21
22impl Preprocessor for VariablesPreprocessor {
23    fn name(&self) -> &str {
24        Self::NAME
25    }
26
27    fn run(&self, ctx: &PreprocessorContext, mut book: Book) -> Result<Book> {
28        let mut variables = None;
29        let mut use_env = false;
30        let mut warn_missing = true;
31        let preprocessors = ctx.config.preprocessors::<Value>()?;
32        if let Some(config) = preprocessors.get(VariablesPreprocessor::NAME) {
33            if let Some(vars) = config.get("variables") {
34                variables = Some(vars);
35            } else {
36                eprintln!(" not found variables in configuration {:?} ", config);
37            }
38            if let Some(env_config) = config.get("use_env") {
39                if let &Value::Boolean(enabled) = env_config {
40                    use_env = enabled;
41                } else {
42                    eprintln!(" variables preprocess use_env configuration must be a boolean ");
43                }
44            }
45
46            if let Some(warn_missing_config) = config.get("warn_missing") {
47                if let &Value::Boolean(enabled) = warn_missing_config {
48                    warn_missing = enabled;
49                } else {
50                    eprintln!(" variables preprocess use_env configuration must be a boolean ");
51                }
52            }
53        } else {
54            eprintln!(" not found {} configuration ", VariablesPreprocessor::NAME);
55        }
56        if let Some(&Value::Table(ref vars)) = variables {
57            book.for_each_mut(|section: &mut BookItem| {
58                if let BookItem::Chapter(ref mut ch) = *section {
59                    ch.content = replace_all(ch, vars, use_env, warn_missing);
60                }
61            });
62        }
63        Ok(book)
64    }
65}
66
67fn replace_all(ch: &Chapter, variables: &Table, use_env: bool, warn_missing: bool) -> String {
68    // When replacing one thing in a string by something with a different length,
69    // the indices after that will not correspond,
70    // we therefore have to store the difference to correct this
71    let mut previous_end_index = 0;
72    let mut replaced = String::new();
73    let start = Value::Table(variables.clone());
74    for variable in find_variables(&ch.content) {
75        replaced.push_str(&ch.content[previous_end_index..variable.start_index]);
76        let variable_path = variable.name.split('.');
77        let mut current_value = Some(&start);
78        for variable_name in variable_path {
79            current_value = if let Some(&Value::Table(ref table)) = current_value {
80                table.get(variable_name)
81            } else {
82                None
83            };
84        }
85        if let Some(value) = current_value {
86            if let Value::String(s) = value {
87                replaced.push_str(&s);
88            } else {
89                replaced.push_str(&value.to_string());
90            }
91        } else if use_env {
92            if let Ok(value) = std::env::var(&variable.name) {
93                replaced.push_str(&value);
94            } else {
95                eprintln!(
96                    "Not found value for variable '{}' from chapter '{}'",
97                    variable.name,
98                    ch.path.as_ref().map(|p| p.to_str()).flatten().unwrap_or("")
99                );
100            }
101        } else {
102            if warn_missing {
103                eprintln!(
104                    "Not found value for variable '{}' from chapter '{}'",
105                    variable.name,
106                    ch.path.as_ref().map(|p| p.to_str()).flatten().unwrap_or("")
107                );
108            }
109            replaced.push_str(&ch.content[variable.start_index..variable.end_index]);
110        }
111        previous_end_index = variable.end_index;
112    }
113
114    replaced.push_str(&ch.content[previous_end_index..]);
115    replaced
116}
117
118struct VariablesIter<'a>(CaptureMatches<'a, 'a>);
119
120struct Variable {
121    start_index: usize,
122    end_index: usize,
123    name: String,
124}
125
126impl Variable {
127    fn from_capture(cap: Captures) -> Option<Variable> {
128        let value = cap.get(1);
129        value.map(|v| {
130            cap.get(0)
131                .map(|mat| Variable {
132                    start_index: mat.start(),
133                    end_index: mat.end(),
134                    name: v.as_str().to_string(),
135                })
136                .expect("base match exists a this point ")
137        })
138    }
139}
140
141impl<'a> Iterator for VariablesIter<'a> {
142    type Item = Variable;
143    fn next(&mut self) -> Option<Variable> {
144        for cap in &mut self.0 {
145            return Variable::from_capture(cap);
146        }
147        None
148    }
149}
150
151fn find_variables(contents: &str) -> VariablesIter<'_> {
152    lazy_static! {
153        static ref RE: Regex = Regex::new(r"\{\{\s*([a-zA-Z0-9_.]+)\s*\}\}").unwrap();
154    }
155    VariablesIter(RE.captures_iter(contents))
156}
157
158#[cfg(test)]
159mod tests {
160    use super::replace_all;
161    use mdbook_preprocessor::book::Chapter;
162    use toml::value::{Table, Value};
163
164    #[test]
165    pub fn test_variable_replaced() {
166        let to_replace = r" # Text {{var1}} \
167            text \
168            text {{var2}} \
169            val  \
170            (text {{var3}})[{{var3}}/other] \
171        ";
172
173        let mut table = Table::new();
174        table.insert("var1".to_owned(), Value::String("first".to_owned()));
175        table.insert("var2".to_owned(), Value::String("second".to_owned()));
176        table.insert("var3".to_owned(), Value::String("third".to_owned()));
177
178        let result = replace_all(
179            &Chapter::new("", to_replace.to_owned(), "", vec![]),
180            &table,
181            false,
182            true,
183        );
184
185        assert_eq!(
186            result,
187            r" # Text first \
188            text \
189            text second \
190            val  \
191            (text third)[third/other] \
192        "
193        );
194    }
195    #[test]
196    pub fn test_variable_replaced_env() {
197        let to_replace = r" # Text {{var1}} \
198            text \
199            text {{var2}} \
200            val  \
201            (text {{var3}})[{{var3}}/other] \
202        ";
203
204        std::env::set_var("var1".to_owned(), "first".to_owned());
205        std::env::set_var("var2".to_owned(), "second".to_owned());
206        std::env::set_var("var3".to_owned(), "third".to_owned());
207
208        let table = Table::new();
209        let result = replace_all(
210            &Chapter::new("", to_replace.to_owned(), "", vec![]),
211            &table,
212            true,
213            true,
214        );
215
216        assert_eq!(
217            result,
218            r" # Text first \
219            text \
220            text second \
221            val  \
222            (text third)[third/other] \
223        "
224        );
225    }
226
227    #[test]
228    pub fn test_keep_variable_for_missing() {
229        let to_replace = "Text {{var1}} ";
230
231        let table = Table::new();
232
233        let result = replace_all(
234            &Chapter::new("", to_replace.to_owned(), "", vec![]),
235            &table,
236            false,
237            true,
238        );
239
240        assert_eq!(result, "Text {{var1}} ");
241    }
242}