summaryrefslogtreecommitdiff
path: root/src/collect_files.rs
blob: 9aa498388d7aeaece497048dda4062054dc472e1 (plain) (blame)
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
use crate::*;

use vagabond::*;

use std::collections::HashMap;


pub struct Website {
    pub name: String,
    pub config: HashMap<String, String>,
    pub pages: Vec<Page>,
    pub redirects: Vec<Redirect>,
    pub static_files: Vec<StaticItem>,
    pub static_dirs: Vec<StaticItem>,
}

pub struct Page {
    pub name: String,                 // Display name of this page
    pub name_url: String,             // URL name for this page, no extension
    pub full_url: String,             // Full URL for this page, no extension
    pub parents: Vec<String>,         // Parent directory components, unsafe
    pub parent_url: String,           // Base URL for links in this page
    pub source_path: PathBuf,         // Absolute path to source file
    pub document: MarkdownDocument,   // File content parsed as markdown
    pub headings: Vec<Heading>,       // Ordered list of all headings in page
}

pub struct Heading {
    pub name: String,
    pub url: String,
    pub level: Level,
}

pub struct StaticItem {
    pub full_url: String,             // Safe full URL, with extension
    pub source_path: PathBuf,         // Absolute path to source file
}

pub struct Redirect {
    pub name: String,                 // Display name of this redirect
    pub full_url: String,             // Safe full URL, no extension
    pub parents: Vec<String>,         // Parent directory components, unsafe
    pub parent_url: String,           // Base URL for relative redirects
    pub redirect: String,             // Page to redirect to, as an internal link
}

pub trait LinkFrom {
    fn name(&self) -> &str;
    fn parent_url(&self) -> &str;
    fn parents(&self) -> &[String];
    fn root(&self) -> String {
        let mut root = String::new();
        for _ in self.parents() {
            root.push_str("../");
        }
        return root;
    }
}


impl Page {
    pub fn root(&self) -> String {
        let mut root = String::new();
        for _ in &self.parents {
            root.push_str("../");
        }
        return root;
    }
}

impl LinkFrom for Page {
    fn name(&self) -> &str { &self.name }
    fn parent_url(&self) -> &str { &self.parent_url }
    fn parents(&self) -> &[String] { &self.parents }
}

impl LinkFrom for Redirect {
    fn name(&self) -> &str { &self.name }
    fn parent_url(&self) -> &str { &self.parent_url }
    fn parents(&self) -> &[String] { &self.parents }
}


impl Website {
    pub fn from_path(path: &Path) -> Self {
        let mut new = Self {
            pages: Vec::new(),
            redirects: Vec::new(),
            static_files: Vec::new(),
            static_dirs: Vec::new(),
            name: match Entry::from_path(path) {
                Ok(entry) => entry.name,
                Err(err) => error!("Couldn't open {:?}: {:?}", &path, err),
            },
            config: HashMap::new(),
        };
        new.collect_entry(path, path);
        return new;
    }

    fn collect_entry(&mut self, path: &Path, prefix: &Path) {
        let entry = Entry::from_path(path).unwrap();
        // Ignore dotted entries.
        if entry.name.starts_with('.') { return }
        // Get name and extension.
        let (mut name, extension) = entry.split_name();
        if let Some((prefix, suffix)) = name.split_once(' ') {
            if prefix.chars().all(|c| "0123456789-".contains(c)) {
                name = suffix.to_string();
            }
        }
        let name_url = make_url_safe(&name);
        // Generate parent URL, used only for files.
        let source_path = entry.original_path.clone();
        let relative_path = source_path.strip_prefix(prefix).unwrap_or_else(
            |_| error!("Path doesn't start with {prefix:?}: {source_path:?}"));
        let mut parents: Vec<_> = relative_path.components()
            .map(|c| c.as_os_str().to_string_lossy().to_string()).collect();
        parents.pop();  // Remove file segment.

        // Process each entry.
        if entry.is_directory() {
            if let Some(stripped) = entry.name.strip_prefix("!") {
                // Track all files inside the static directory.
                for child in traverse_directory(&entry).unwrap() {
                    let source_path = child.original_path;
                    let relative_path = source_path.strip_prefix(&entry.original_path).unwrap_or_else(
                        |_| error!("Path doesn't start with {prefix:?}: {source_path:?}"))
                        .as_os_str().to_string_lossy().to_string();
                    let full_url = format!("{stripped}/{relative_path}");
                    self.static_files.push(StaticItem { full_url, source_path })
                }
                let full_url = make_url_safe(stripped);
                self.static_dirs.push(StaticItem { full_url, source_path });
            } else {
                for child in list_directory(entry.original_path).unwrap() {
                    self.collect_entry(&child.original_path, prefix);
                }
            }
        } else if parents.is_empty() && entry.name.to_lowercase() == "toaster.conf" {
            // Parse the config file.
            let config = std::fs::read_to_string(&source_path).unwrap();
            let mut key = None;
            let mut value = String::new();
            for line in config.lines() {
                if line.starts_with(" ") {
                    value.push_str(line.trim());
                    value.push('\n');
                } else {
                    if let Some(key) = key {
                        self.config.insert(key, std::mem::take(&mut value));
                    }
                    key = Some(line.trim().to_lowercase().to_string());
                }
            }
            if let Some(key) = key {
                self.config.insert(key, std::mem::take(&mut value));
            }
        } else {
            match extension.as_str() {
                "md" => {
                    let markdown = std::fs::read_to_string(&source_path).unwrap();
                    let document = MarkdownDocument::from_str(&markdown);
                    let mut heading_set = HashSet::new();
                    let mut duplicates = HashSet::new();
                    let headings = document.blocks.iter()
                        .filter_map(|block| if let Block::Heading { line, level } = block {
                            let name = line.to_string();
                            let url = make_url_safe(&name);
                            let level = level.to_owned();
                            if !heading_set.insert(url.clone()) {
                                duplicates.insert(url.clone());
                            }
                            Some(Heading { name, url, level })
                        } else {
                            None
                        }).collect();
                    for url in duplicates {
                        warn!("Page {name:?} contains multiple headings with ID \"#{url}\"");
                    }
                    if name_url == "+index" {
                        if parents.is_empty() {
                            // This is the index file for the whole site.
                            self.pages.push(Page {
                                name: String::from("Home"),
                                name_url: String::from("index"),
                                full_url: String::from("index"),
                                parents,
                                parent_url: String::from(""),
                                source_path,
                                document,
                                headings,
                            });
                        } else {
                            // This is an index file for a directory.
                            let name = parents[parents.len()-1].clone();
                            let name_url = make_url_safe(&name);
                            let mut full_url = String::new();
                            for parent in &parents {
                                full_url.push_str(&make_url_safe(parent));
                                full_url.push('/');
                            }
                            let parent_url = full_url.clone();
                            full_url.pop();
                            parents.pop();
                            self.pages.push(Page {
                                name,
                                name_url,
                                full_url,
                                parents,
                                parent_url,
                                source_path,
                                document,
                                headings,
                            });
                        }
                    } else {
                        let mut full_url = String::new();
                        for parent in &parents {
                                full_url.push_str(&make_url_safe(parent));
                                full_url.push('/');
                        }
                        full_url.push_str(&name_url);
                        let mut parent_url = full_url.clone();
                        parent_url.push('/');
                        self.pages.push(Page {
                            name, name_url, full_url,
                            parents, parent_url,
                            source_path,
                            document, headings,
                        });
                    }
                },
                "redirect" => {
                    let mut full_url = String::new();
                    for parent in &parents {
                            full_url.push_str(&make_url_safe(parent));
                            full_url.push('/');
                    }
                    let parent_url = full_url.clone();
                    full_url.push_str(&name_url);
                    let redirect = std::fs::read_to_string(&source_path)
                        .unwrap().trim().to_string();
                    self.redirects.push(Redirect { name, full_url, parents, parent_url, redirect });
                }
                _ => {
                    let mut parent_url = String::new();
                    for parent in &parents {
                            parent_url.push_str(&make_url_safe(parent));
                            parent_url.push('/');
                    }
                    let full_url = format!("{parent_url}{name_url}.{extension}");
                    self.static_files.push(StaticItem { full_url, source_path });
                },
            }
        }
    }

    // Ext is extension without a dot.
    // Checks if a relative link to an internal page name can be reached from
    // the current page, and returns a resolved absolute link to the page with extension.
    pub fn has_page(&self, from: &impl LinkFrom, path: &str, ext: &str) -> Option<String> {
        // Remove heading fragment and file extension.
        let (path, heading) = match path.rsplit_once('#') {
            Some((path, heading)) => match heading.is_empty() {
                true => (path, None),
                false => (path, Some(heading)),
            }
            None => (path, None),
        };
        let mut path = path.strip_suffix(&format!(".{ext}")).unwrap_or(path).to_string();
        // Attach parent if not an absolute path.
        if !path.starts_with('/') {
            path = format!("{}{path}", from.parent_url());
        }
        let path = make_url_safe(&collapse_path(&path));

        // Find page with this path in website.
        for page in &self.pages {
            if page.full_url == path {
                let root = from.root();
                if let Some(heading) = heading {
                    if !page.headings.iter().any(|h| h.url == make_url_safe(heading)) {
                        warn!("Page {:?} contains link to nonexistent heading {heading:?} on page {path:?}", from.name());
                    }
                    return Some(format!("{root}{path}.{ext}#{heading}"));
                } else {
                    return Some(format!("{root}{path}.{ext}"));
                }
            }
        }
        return None;
    }

    pub fn has_static(&self, from: &impl LinkFrom, path: &str) -> Option<String> {
        // Attach parent if not an absolute path.
        let path = match !path.starts_with('/') {
            true => collapse_path(&format!("{}{path}", make_url_safe(from.parent_url()))),
            false => collapse_path(path),
        };
        for file in &self.static_files {
            if file.full_url == path {
                let root = from.root();
                return Some(format!("{root}{path}"));
            }
        }
        return None;
    }

    pub fn has_image(&self, file_name: &str) -> bool {
        let image_path = format!("images/thumb/{file_name}");
        self.static_files.iter().any(|s| s.full_url == image_path)
    }

    pub fn get_config(&self, key: &str) -> String {
        self.config.get(key).map(String::to_owned).unwrap_or_else(String::new)
    }
}


fn collapse_path(path: &str) -> String {
    // Iteratively collapse ".." segments.
    let mut segments: Vec<&str> = path.split('/')
        .filter(|s| !s.is_empty() && *s != ".")
        .collect();
    'outer: loop {
        for i in 0..(segments.len().saturating_sub(1)) {
            if segments[i] == ".." {
                if i == 0 {
                    segments.remove(0);
                } else {
                    segments.remove(i-1);
                    segments.remove(i-1);
                }
                continue 'outer;
            }
        }
        return segments.join("/");
    }
}