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
//! A simple file system based markdown flat page.
//!
//! ## Folder structure
//!
//! Only characters allowed in urls are ASCII, numbers and hyphen with underscore.
//! Urls map to files by simply substituting `/` to `^` and adding `.md` extension.
//! I believe it should eliminate all kinds of security issues.
//!
//! | url            | file name         |
//! |----------------|-------------------|
//! | `/`            | `^.md`            |
//! | `/foo/bar-baz` | `^foo^bar-baz.md` |
//!
//! ## Page format
//!
//! File could provide title and description in a yaml-based frontmatter, if there's no frontmatter
//! the first line would be considered the title (and cleaned from possible header marker `#`).
//!
//! | File content                                         | [`title`] | [`description`] | [`body`] | [`html()`]           |
//! |------------------------------------------------------|---------------------|---------------------------|--------------------|--------------------------------|
//! | `# Foo`<br>`Bar`                                     | `"Foo"`             | `None`                    | `"# Foo\nBar"`     | `"<h1>Foo</h1>\n<p>Bar</p>\n"` |
//! | `---`<br>`description: Bar`<br>`---`<br>`# Foo`      | `"Foo"`             | `Some("Bar")`             | `"# Foo"`          | `"<h1>Foo</h1>\n"`             |
//! | `---`<br>`title: Foo`<br>`description: Bar`<br>`---` | `"Foo"`             | `Some("Bar")`             | `""`               | `""`                           |
//!
//!
//! ## Reading a page
//!
//! ```rust
//! let root_folder = "./";
//! if let Some(home) = flatpage::FlatPage::by_url(root_folder, "/").unwrap() {
//!     println!("title: {}", home.title);
//!     println!("description: {:?}", home.description);
//!     println!("markdown body: {}", home.body);
//!     println!("html body: {}", home.html());
//! } else {
//!     println!("No home page");
//! }
//! ```
//!
//! ## Cached metadata
//!
//! It's a common for a page to have a list of related pages. To avoid reading all the files each
//! time, you can use [`FlatPageStore`] to cache pages [`metadata`] (titles and descriptions).
//!
//! ```rust
//! let root_folder = "./";
//! let store = flatpage::FlatPageStore::read_dir(root_folder).unwrap();
//! if let Some(meta) = store.meta_by_url("/") {
//!     println!("title: {}", meta.title);
//!     println!("description: {:?}", meta.description);
//! } else {
//!     println!("No home page");
//! }
//! ```
//!
//! [`title`]: FlatPage::title
//! [`description`]: FlatPage::description
//! [`body`]: FlatPage::body
//! [`html()`]: FlatPage::html()
//! [`metadata`]: FlatPageMeta
#![warn(clippy::all, missing_docs, nonstandard_style, future_incompatible)]
#![forbid(unsafe_code)]
#![cfg_attr(docsrs, feature(doc_cfg))]
use displaydoc::Display;
use std::{
    collections::HashMap,
    fs, io,
    path::{Path, PathBuf},
};
use thiserror::Error;

const ALLOWED_IN_URL: &str = "/_-";

/// The crates error type
#[derive(Debug, Display, Error)]
pub enum Error {
    /// broken frontmatter yaml in `{1}`
    Frontmatter(#[source] serde_yaml::Error, String),
    /// readdir `{1}`
    ReadDir(#[source] io::Error, PathBuf),
    /// readdir entry
    DirEntry(#[source] io::Error),
}

/// The crates result type
pub type Result<T> = std::result::Result<T, Error>;

/// A store for [`FlatPageMeta`]
#[derive(Debug)]
pub struct FlatPageStore {
    /// The folder containing flat pages
    root: PathBuf,
    /// Maps file stems to pages metadata
    pages: HashMap<String, FlatPageMeta>,
}

/// Flat page metadata
#[derive(Debug)]
pub struct FlatPageMeta {
    /// Page title
    pub title: String,
    /// Page description
    pub description: Option<String>,
}

/// Flat page
#[derive(Debug)]
pub struct FlatPage {
    /// Title - for html title tag, `og:title`, etc
    pub title: String,
    /// Description - for html meta description, `og:description`, etc
    pub description: Option<String>,
    /// Raw markdown version of the body
    pub body: String,
}

/// Flat page yaml-based frontmatter
#[derive(Debug, serde::Deserialize)]
#[serde(deny_unknown_fields)]
struct Frontmatter {
    title: Option<String>,
    description: Option<String>,
}

impl FlatPageStore {
    /// Creates a store from the folder
    pub fn read_dir(root: impl Into<PathBuf>) -> Result<Self> {
        let root = root.into();
        let mut pages = HashMap::new();
        let md_ext = Some(std::ffi::OsStr::new("md"));
        for entry in fs::read_dir(&root).map_err(|e| Error::ReadDir(e, root.clone()))? {
            let entry = entry.map_err(Error::DirEntry)?;
            let path = entry.path();
            if !path.is_file() || path.extension() != md_ext {
                continue;
            }
            let stem = match path.file_stem().and_then(|x| x.to_str()) {
                Some(s) => s,
                None => continue,
            };
            let page = match FlatPage::by_path(&path)? {
                Some(p) => p,
                None => continue,
            };
            pages.insert(stem.into(), page.into());
        }
        Ok(Self { root, pages })
    }

    /// Returns a page metadata by its url
    pub fn meta_by_url(&self, url: &str) -> Option<&FlatPageMeta> {
        let stem = Self::url_to_stem(url);
        self.meta_by_stem(&stem)
    }

    /// Returns a page by its url
    pub fn page_by_url(&self, url: &str) -> Result<Option<FlatPage>> {
        let stem = Self::url_to_stem(url);
        self.page_by_stem(&stem)
    }

    /// Returns a page metadata by the file stem
    pub fn meta_by_stem(&self, stem: &str) -> Option<&FlatPageMeta> {
        self.pages.get(stem)
    }

    /// Returns a page by the file stem
    pub fn page_by_stem(&self, stem: &str) -> Result<Option<FlatPage>> {
        if self.pages.contains_key(stem) {
            let mut path = self.root.clone();
            path.push(format!("{stem}.md"));
            FlatPage::by_path(path)
        } else {
            Ok(None)
        }
    }

    /// Converts url to file stem
    fn url_to_stem(url: &str) -> String {
        url.replace('/', "^")
    }
}

impl FlatPage {
    /// Returns a page by its url
    pub fn by_url(root: impl Into<PathBuf>, url: &str) -> Result<Option<Self>> {
        let filename = match url_to_filename(url) {
            Some(f) => f,
            None => return Ok(None),
        };
        let mut path: PathBuf = root.into();
        path.push(&filename);
        Self::by_path(&path)
    }

    /// Returns a page by its file path
    pub fn by_path(path: impl AsRef<Path>) -> Result<Option<Self>> {
        let path = path.as_ref();
        let content = match fs::read_to_string(path) {
            Ok(c) => c,
            Err(_) => return Ok(None),
        };
        Self::from_content(&content)
            .map(Some)
            .map_err(|e| Error::Frontmatter(e, path.display().to_string()))
    }

    /// [`FlatPage::body`] rendered to html
    pub fn html(&self) -> String {
        markdown(&self.body)
    }

    /// Parses a page from text
    fn from_content(content: &str) -> serde_yaml::Result<Self> {
        let (maybe_matter, body) = Frontmatter::parse(content)?;
        let page = if let Some(matter) = maybe_matter {
            let title = matter
                .title
                .unwrap_or_else(|| title_from_markdown(body).into());
            let description = matter.description;
            Self {
                title,
                description,
                body: body.to_string(),
            }
        } else {
            let title = title_from_markdown(content).into();
            Self {
                title,
                description: None,
                body: content.to_string(),
            }
        };
        Ok(page)
    }
}

impl From<FlatPage> for FlatPageMeta {
    fn from(p: FlatPage) -> Self {
        Self {
            title: p.title,
            description: p.description,
        }
    }
}

impl Frontmatter {
    /// Parses frontmatter from the file content, returns the frontmatter and the rest of the
    /// content (page body)
    fn parse(content: &str) -> serde_yaml::Result<(Option<Self>, &str)> {
        let content = content.trim();
        let mut parts = content.splitn(3, "---");

        let prefix = parts.next().unwrap(); // `splitn` should always return at least one item
        if !prefix.is_empty() {
            // content doesn't start from the delimeter
            return Ok((None, content));
        }

        let matter_str = if let Some(s) = parts.next() {
            s
        } else {
            // no first opening delimeter
            return Ok((None, content));
        };

        let body = if let Some(s) = parts.next() {
            s
        } else {
            // no closing delimiter
            return Ok((None, content));
        };

        let matter = serde_yaml::from_str(matter_str)?;
        Ok((Some(matter), body.trim()))
    }
}

/// Considers the first line to be the page title, removes markdown header prefix `#`
fn title_from_markdown(body: &str) -> &str {
    body.lines()
        .next()
        .unwrap_or_default()
        .trim_start_matches('#')
        .trim()
}

/// Tries to convert the url into a filename
fn url_to_filename(url: &str) -> Option<String> {
    if url.is_empty() {
        None
    } else if url
        .chars()
        .all(|c| c.is_ascii_alphanumeric() || ALLOWED_IN_URL.contains(c))
    {
        Some(format!("{}.md", url.replace('/', "^")))
    } else {
        None
    }
}

fn markdown(text: &str) -> String {
    use pulldown_cmark::{html, Options, Parser};

    let mut options = Options::empty();
    options.insert(Options::ENABLE_FOOTNOTES);
    options.insert(Options::ENABLE_STRIKETHROUGH);
    options.insert(Options::ENABLE_TABLES);
    options.insert(Options::ENABLE_TASKLISTS);
    let parser = Parser::new_ext(text, options);
    let mut html = String::new();
    html::push_html(&mut html, parser);
    html
}

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

    #[test]
    fn test_url_to_filename() {
        assert_eq!(url_to_filename(""), None);
        assert_eq!(url_to_filename("#"), None);
        assert_eq!(url_to_filename("ы"), None);
        assert_eq!(
            url_to_filename("/foo-bar/baz/").unwrap(),
            "^foo-bar^baz^.md"
        );
    }

    #[test]
    fn test_title_from_markdown() {
        assert_eq!(title_from_markdown(""), "");
        assert_eq!(title_from_markdown("## foo\nbar"), "foo");
    }

    #[test]
    fn test_frontmatter_from_content() {
        println!("No starting delimiter");
        let text = "foo";
        let (m, b) = Frontmatter::parse(text).unwrap();
        assert!(m.is_none());
        assert_eq!(b, text);

        println!("Starting delimiter inside content");
        let text = "foo\n---\nfoo: bar\n---\nbody";
        let (m, b) = Frontmatter::parse(text).unwrap();
        assert!(m.is_none());
        assert_eq!(b, text);

        println!("Just the starting delimiter");
        let text = "---";
        let (m, b) = Frontmatter::parse(text).unwrap();
        assert!(m.is_none());
        assert_eq!(b, text);

        println!("No closing delimeter");
        let text = "---\ntitle: bar\nbaz";
        let (m, b) = Frontmatter::parse(text).unwrap();
        assert!(m.is_none());
        assert_eq!(b, text);

        println!("Empty frontmatter");
        assert!(Frontmatter::parse("---\n\n---").is_err());

        println!("Unknown field");
        assert!(Frontmatter::parse("---\nunknown_field: bar\n---").is_err());

        println!("Empty body");
        let text = "---\ntitle: foo\n---";
        let (m, b) = Frontmatter::parse(text).unwrap();
        assert_eq!(m.unwrap().title.unwrap(), "foo");
        assert_eq!(b, "");

        println!("Title with body");
        let text = "---\ntitle: foo\n---\nbar";
        let (m, b) = Frontmatter::parse(text).unwrap();
        assert_eq!(m.unwrap().title.unwrap(), "foo");
        assert_eq!(b, "bar");
    }

    #[test]
    fn test_flatpage_title() {
        let page = FlatPage::from_content("# Foo").unwrap();
        assert_eq!(page.title, "Foo");
        assert_eq!(page.body, "# Foo");
        assert_eq!(
            FlatPage::from_content("---\ntitle: Bar\n---\n# Foo")
                .unwrap()
                .title,
            "Bar"
        );
    }

    #[test]
    fn test_flatpage_description() {
        assert_eq!(FlatPage::from_content("").unwrap().description, None);
        assert_eq!(
            FlatPage::from_content("---\ndescription: Bar\n---")
                .unwrap()
                .description
                .unwrap(),
            "Bar"
        );
    }

    #[test]
    fn test_doc_table() {
        let page = FlatPage::from_content("# Foo\nBar").unwrap();
        assert_eq!(page.title, "Foo");
        assert!(page.description.is_none());
        assert_eq!(page.body, "# Foo\nBar");
        assert_eq!(page.html(), "<h1>Foo</h1>\n<p>Bar</p>\n");

        let page = FlatPage::from_content("---\ndescription: Bar\n---\n# Foo").unwrap();
        assert_eq!(page.title, "Foo");
        assert_eq!(page.description.as_deref().unwrap(), "Bar");
        assert_eq!(page.body, "# Foo");
        assert_eq!(page.html(), "<h1>Foo</h1>\n");

        let page = FlatPage::from_content("---\ntitle: Foo\ndescription: Bar\n---").unwrap();
        assert_eq!(page.title, "Foo");
        assert_eq!(page.description.as_deref().unwrap(), "Bar");
        assert_eq!(page.body, "");
        assert_eq!(page.html(), "");
    }
}