mdbook_journal/mdbook/preprocessor/
naive.rs1use super::prelude::*;
2use crate::prelude::*;
3
4pub struct NaivePreprocessor<T>
5where
6 T: JournalLoaderTrait,
7{
8 journal: Journal<T>,
9}
10
11impl<T> NaivePreprocessor<T>
12where
13 T: JournalLoaderTrait,
14{
15 pub fn new<J>(journal: J) -> Self
16 where
17 J: Into<Journal<T>>,
18 {
19 Self {
20 journal: journal.into(),
21 }
22 }
23}
24
25impl<T> Preprocessor for NaivePreprocessor<T>
26where
27 T: JournalLoaderTrait,
28{
29 fn name(&self) -> &str {
30 "Naive Journal Preprocessor"
31 }
32
33 fn run(&self, _ctx: &PreprocessorContext, mut book: Book) -> Result<Book> {
34 let journal = &self.journal;
35 let writing = &mut book;
36
37 for topic in journal.each_topic() {
38 let mut entries = journal.entries_for_topic(&topic.name())?;
39 entries.sort_by(|a, b| b.created_at().cmp(a.created_at()));
40 let book = topic_chapter(topic, &entries);
41 writing.push_item(book);
42 }
43
44 Ok(book)
45 }
46}
47
48fn topic_chapter(topic: &Topic, entries: &[Entry]) -> BookItem {
49 let sub_items = entries.iter().map(|e| entry_chapter(topic, e)).collect();
50
51 BookItem::Chapter(Chapter {
52 sub_items,
53 name: topic.name().to_owned(),
54 ..Default::default()
55 })
56}
57
58fn entry_chapter(topic: &Topic, entry: &Entry) -> BookItem {
59 let name = match entry.meta_value(&"title") {
60 Some(MetaValue::String(title)) => title.to_owned(),
61 _ => String::from("Untitled"),
62 };
63 let content = entry.content().to_owned();
64
65 BookItem::Chapter(Chapter {
66 name,
67 content,
68 path: topic.virtual_path(entry).ok(),
69 source_path: entry.file_location().cloned(),
70 ..Default::default()
71 })
72}