mdpage 0.1.3

Simple documentation tool
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
//! Data serves both as the configuration data for mdPage
//! as well as the actual template data for generating content.

use std::env;
use std::error::Error;

use std::fs;
use std::fs::File;

use std::io::prelude::*;
use std::io::BufReader;
use std::path::Path;
use std::path::PathBuf;

use serde::{Deserialize, Serialize};

use crate::content::{
    init_dir_contents, init_dir_sections, init_entry_contents, Content, ContentType,
};
use crate::utils::{build_title_for_dir, is_ext};

/// Data serves both as the configuration data for mdPage
/// as well as the actual template data for generating content.
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
pub struct Data {
    /// Whether to do full page or not.
    pub full_page: Option<bool>,
    /// Title used in header and title of the document.
    pub title: Option<String>,
    /// Subtitle used in header.
    pub subtitle: Option<String>,
    /// Author used in metadata.
    pub author: Option<String>,
    /// The favicon link.
    pub icon: Option<String>,
    /// The main content used for the front page.
    pub main: Option<Content>,
    /// The content of the document.
    pub contents: Option<Vec<Content>>,
    /// The custom JavaScript to be added in the `script` tag.
    pub script: Option<String>,
    /// The custom CSS to be added in the `style` tag.
    pub style: Option<String>,
    /// The custom style and script links.
    pub links: Option<Vec<Link>>,
    /// Custom header content.
    pub header: Option<Content>,
    /// Custom footer content.
    pub footer: Option<Content>,
}

/// Link represents a link we can insert into the head of the generated document.
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
pub struct Link {
    /// Link type.
    pub link_type: Option<LinkType>,
    /// The link source.
    pub src: Option<String>,
    /// Optional integrity for SRI.
    pub integrity: Option<String>,
    /// Optional crossorigin for SRI.
    pub crossorigin: Option<String>,
}

/// Link Type enum.
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
#[serde(rename_all = "lowercase")]
pub enum LinkType {
    Style,
    Script,
}

impl LinkType {
    pub fn from_str(s: &str) -> Option<LinkType> {
        match s {
            "style" => Some(LinkType::Style),
            "stylesheet" => Some(LinkType::Style),
            "script" => Some(LinkType::Script),
            _ => None,
        }
    }

    pub fn as_str(&self) -> &'static str {
        match self {
            LinkType::Style => "stylesheet",
            LinkType::Script => "script",
        }
    }
}

impl Default for Data {
    fn default() -> Data {
        Data {
            full_page: Some(false),
            title: None,
            subtitle: None,
            author: None,
            icon: None,
            main: None,
            contents: None,
            script: None,
            style: None,
            links: None,
            footer: None,
            header: None,
        }
    }
}

impl Data {
    fn build(&mut self, root: &Path) -> Result<(), Box<dyn Error>> {
        self.init(root)?;

        self.build_contents(root)?;

        Ok(())
    }

    fn init(&mut self, root: &Path) -> Result<(), Box<dyn Error>> {
        if self.title.is_none() {
            self.title = Some(build_title_for_dir(
                root,
                fs::read_dir(root).map_err(|err| {
                    format!("Error reading dir: {}. {}", root.display(), err.to_string())
                })?,
                true,
            )?);
        }

        if self.main.is_some() {
            self.main.as_mut().unwrap().init_from_file(root);
        }

        if self.header.is_some() {
            self.header.as_mut().unwrap().init_from_file(root);
        }

        if self.footer.is_some() {
            self.footer.as_mut().unwrap().init_from_file(root);
        }

        let mut main = None;
        let mut header = None;
        let mut footer = None;

        let paths = fs::read_dir(root)
            .map_err(|err| format!("Error reading dir: {}. {}", root.display(), err.to_string()))?;

        let mut res = paths
            .filter_map(|p| {
                if let Ok(entry) = p {
                    if let Ok(file_type) = entry.file_type() {
                        if file_type.is_file() {
                            return init_entry_contents(root, entry, true).and_then(|(c, ct)| {
                                match ct {
                                    ContentType::Main => {
                                        main = Some(c);
                                        None
                                    }
                                    ContentType::Footer => {
                                        footer = Some(c);
                                        None
                                    }
                                    ContentType::Header => {
                                        header = Some(c);
                                        None
                                    }
                                    _ => Some(vec![c]),
                                }
                            });
                        }
                    }
                }
                None
            })
            .flatten()
            .collect::<Vec<Content>>();

        res.sort_by(|a, b| a.file.cmp(&b.file));

        let mut sections = init_dir_sections(root)?;

        if !res.is_empty() {
            res.push(Content::new_break());
        }
        res.append(&mut sections);

        if self.main.is_none() {
            self.main = main;
        }

        if self.footer.is_none() {
            self.footer = footer;
        }

        if self.header.is_none() {
            self.header = header;
        }

        if self.contents.is_none() {
            self.contents = Some(res);
        }

        Ok(())
    }

    fn build_contents(&mut self, root: &Path) -> Result<(), Box<dyn Error>> {
        if self.contents.is_some() {
            let mut contents = self.contents.as_mut().unwrap();

            let has_dir = contents.iter().any(|c| c.dir.is_some());
            if has_dir {
                let mut expanded_contents = Vec::new();
                let mut index = 0;
                while index < contents.len() {
                    // fix dir entries
                    if contents[index].dir.is_some() {
                        let mut pathbuf = contents[index].dir.clone().unwrap();

                        if root.has_root() && pathbuf.is_relative() {
                            pathbuf = root.join(&pathbuf).canonicalize().unwrap_or_else(|_| {
                                panic!(
                                    "could not resolve path. root: {} path: {}",
                                    root.display(),
                                    pathbuf.display()
                                )
                            });
                        }

                        if pathbuf.is_dir() {
                            let mut dir_contents = Vec::new();

                            // get the base files
                            if let Some(mut root_dir_contents) = init_dir_contents(root, &pathbuf) {
                                dir_contents.append(&mut root_dir_contents);
                            }

                            // do subdirs
                            let mut sub_dir_contents = init_dir_sections(&pathbuf)?;
                            dir_contents.append(&mut sub_dir_contents);

                            // add into the overall
                            let mut di = 0;
                            while di < dir_contents.len() {
                                expanded_contents.push(dir_contents[di].clone());
                                di += 1;
                            }
                        }
                    } else {
                        expanded_contents.push(contents[index].clone());
                    }

                    index += 1;
                }

                self.contents = Some(expanded_contents);
            }

            contents = self.contents.as_mut().unwrap();

            for c in contents {
                crate::content::fill_content(c, root)?;
            }
        }

        if self.main.is_some() {
            crate::content::fill_content(self.main.as_mut().unwrap(), root)?;
        }

        if self.header.is_some() {
            crate::content::fill_content(self.header.as_mut().unwrap(), root)?;
        }

        if self.footer.is_some() {
            crate::content::fill_content(self.footer.as_mut().unwrap(), root)?;
        }

        Ok(())
    }
}

fn config_file(root: &Path) -> Option<PathBuf> {
    let mut r = Path::new(root);
    let json_config = r.join("mdpage.json");
    if json_config.as_path().exists() {
        Some(json_config)
    } else {
        r = Path::new(root);
        let toml_config = r.join("mdpage.toml");
        if toml_config.as_path().exists() {
            Some(toml_config)
        } else {
            None
        }
    }
}

/// Build the content data from a root directory path and optional initial value.
pub fn build(root: &Path, initial_value: Option<Data>) -> Result<Data, Box<dyn Error>> {
    let mut r = root;
    let current_dir = env::current_dir()?;
    let abs;
    if root.is_relative() {
        abs = current_dir
            .as_path()
            .join(root)
            .canonicalize()
            .map_err(|err| {
                format!(
                    "could not join current dir {} with path: {}. {}",
                    current_dir.display(),
                    root.display(),
                    err.to_string()
                )
            })?;
        r = abs.as_path();
    }

    let path = config_file(r);
    let mut data = initial_value.unwrap_or_default();

    if let Some(file_path) = path {
        info!("reading config: {}", file_path.display());
        let mut file = File::open(file_path.as_path()).map_err(|err| {
            format!(
                "Error reading file: {}. {}",
                file_path.display(),
                err.to_string()
            )
        })?;
        if is_ext(&file_path, "json") {
            let reader = BufReader::new(file);
            data = serde_json::from_reader(reader).map_err(|err| {
                format!(
                    "Error reading json: {}. {}",
                    file_path.display(),
                    err.to_string()
                )
            })?;
        } else if is_ext(&file_path, "toml") {
            let mut content = String::new();
            file.read_to_string(&mut content).map_err(|err| {
                format!(
                    "Error reading file: {}. {}",
                    file_path.display(),
                    err.to_string()
                )
            })?;
            data = toml::from_str(&content).map_err(|err| {
                format!(
                    "Error reading toml: {}. {}",
                    file_path.display(),
                    err.to_string()
                )
            })?;
        }
    }

    match data.build(r) {
        Ok(()) => Ok(data),
        Err(e) => Err(e),
    }
}

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

    #[test]
    fn test_init() {
        // empty
        let mut root = PathBuf::from("tests");
        let mut data = Data::default();
        assert!(data.init(&root).is_ok());
        let mut expected = Data::default();
        expected.title = Some(String::from("tests"));
        expected.contents = Some(vec![]); // initialized to empty
        assert_eq!(data, expected);

        // with subdirs
        root = PathBuf::from("tests/fixtures/data");
        data = Data::default();
        assert!(data.init(&root).is_ok());
        let mut expected_file =
            File::open("tests/fixtures/data/init_expected1.json").expect("could not open file");
        let mut reader = BufReader::new(expected_file);
        expected = serde_json::from_reader(reader).expect("could not read expected data");
        assert_eq!(data, expected);

        // with header and footer
        root = PathBuf::from("tests/fixtures/data/dir2");
        data = Data::default();
        assert!(data.init(&root).is_ok());
        expected_file =
            File::open("tests/fixtures/data/init_expected2.json").expect("could not open file");
        reader = BufReader::new(expected_file);
        expected = serde_json::from_reader(reader).expect("could not read expected data");
        assert_eq!(data, expected);

        // with pre-seeded data
        let seed_file =
            File::open("tests/fixtures/data/init_seed1.json").expect("could not open file");
        reader = BufReader::new(seed_file);
        data = serde_json::from_reader(reader).expect("could not read seed data");
        root = PathBuf::from("tests/fixtures/data/dir1");
        assert!(data.init(&root).is_ok());
        expected_file =
            File::open("tests/fixtures/data/init_expected3.json").expect("could not open file");
        reader = BufReader::new(expected_file);
        expected = serde_json::from_reader(reader).expect("could not read expected data");
        assert_eq!(data, expected);

        // just single index
        root = PathBuf::from("docs/examples/single_index");
        data = Data::default();
        assert!(data.init(&root).is_ok());
        expected_file = File::open("tests/fixtures/data/init_expected_single.json")
            .expect("could not open file");
        reader = BufReader::new(expected_file);
        expected = serde_json::from_reader(reader).expect("could not read expected data");
        assert_eq!(data, expected);
    }
}