razorbill 0.1.0

A toolkit for bespoke static site generators.
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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
use std::collections::HashMap;
use std::convert::Infallible;
use std::net::SocketAddr;
use std::path::{Path, PathBuf};
use std::sync::{Arc, RwLock};
use std::{io, thread};

use auk::HtmlElement;
use http_body_util::combinators::BoxBody;
use http_body_util::{BodyExt, Empty, Full};
use hyper::body::Bytes;
use hyper::server::conn::http1;
use hyper::service::service_fn;
use hyper::{header, Method, Request, Response, StatusCode};
use hyper_util::rt::TokioIo;
use notify::{Event, RecommendedWatcher, RecursiveMode, Watcher};
use once_cell::sync::Lazy;
use serde_json::json;
use thiserror::Error;
use tokio::net::TcpListener;
use tokio::sync::mpsc::unbounded_channel;
use walkdir::WalkDir;
use ws::{Message, Sender, WebSocket};

use crate::content::{Page, ParsePageError, ParseSectionError, Section, SectionPath};
use crate::render::{PageToRender, SectionToRender};
use crate::storage::{DiskStorage, InMemoryStorage, Store};

#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Clone)]
pub enum TemplateKey {
    Default,
    Custom(String),
}

struct Templates {
    pub index: Arc<dyn Fn(&SectionToRender) -> HtmlElement + Send + Sync>,
    pub section: HashMap<TemplateKey, Arc<dyn Fn(&SectionToRender) -> HtmlElement + Send + Sync>>,
    pub page: HashMap<TemplateKey, Arc<dyn Fn(&PageToRender) -> HtmlElement + Send + Sync>>,
}

#[derive(Error, Debug)]
pub enum LoadSiteError {
    #[error("failed to walk content directory: {0}")]
    Io(#[from] walkdir::Error),

    #[error("failed to parse section: {0}")]
    ParseSection(#[from] ParseSectionError),

    #[error("failed to parse page: {0}")]
    ParsePage(#[from] ParsePageError),
}

#[derive(Error, Debug)]
pub enum RenderSiteError {
    #[error("IO error: {0}")]
    Io(#[from] io::Error),

    #[error("render error: {0}")]
    RenderPage(#[from] std::fmt::Error),

    #[error("template not found: {0:?}")]
    TemplateNotFound(TemplateKey),

    #[error("storage error: {0}")]
    Storage(String),
}

#[derive(Error, Debug)]
pub enum ServeSiteError {
    #[error("async IO error: {0}")]
    AsyncIo(#[from] tokio::io::Error),
}

static SITE_CONTENT: Lazy<Arc<RwLock<HashMap<String, String>>>> =
    Lazy::new(|| Arc::new(RwLock::new(HashMap::new())));

pub struct Site {
    root_path: PathBuf,
    content_path: PathBuf,
    output_path: PathBuf,
    templates: Templates,
    sections: HashMap<PathBuf, Section>,
    pages: HashMap<PathBuf, Page>,
    is_serving: bool,
}

impl Site {
    pub fn builder() -> SiteBuilder<()> {
        SiteBuilder::new()
    }

    pub fn load(&mut self) -> Result<(), LoadSiteError> {
        let walker = WalkDir::new(&self.content_path)
            .follow_links(true)
            .into_iter();

        let mut pages = Vec::new();
        let mut sections = Vec::new();

        for entry in walker {
            let entry = entry?;
            let path = entry.path();

            let Some(filename) = entry.file_name().to_str() else {
                continue;
            };

            if !path.is_dir() {
                if !filename.ends_with(".md")
                    || filename.starts_with(".")
                    || filename == "_index.md"
                {
                    continue;
                }

                pages.push(Page::from_path(&self.content_path, path)?);
            } else {
                let section = Section::from_path(&self.content_path, path)?;
                sections.push(section);
            }
        }

        for section in sections {
            self.sections.insert(section.file.path.clone(), section);
        }

        for page in pages {
            self.pages.insert(page.file.path.clone(), page);
        }

        for (path, page) in self.pages.iter_mut() {
            let parent_section_path = page.file.parent.join("_index.md");

            if let Some(parent_section) = self.sections.get_mut(&parent_section_path) {
                parent_section.pages.push(path.clone());
            }
        }

        Ok(())
    }

    pub fn render(&mut self) -> Result<(), RenderSiteError> {
        if self.is_serving {
            self.render_to(InMemoryStorage::new(SITE_CONTENT.clone()))
        } else {
            self.render_to(DiskStorage::new(self.output_path.clone()))
        }
    }

    fn render_to(&mut self, storage: impl Store) -> Result<(), RenderSiteError> {
        for section in self.sections.values() {
            let section_template = if section.path == SectionPath("/_index".to_string()) {
                &self.templates.index
            } else {
                let template_name = section
                    .meta
                    .template
                    .clone()
                    .map(TemplateKey::Custom)
                    .unwrap_or(TemplateKey::Default);

                let section_template = self
                    .templates
                    .section
                    .get(&template_name)
                    .ok_or_else(|| RenderSiteError::TemplateNotFound(template_name))?;

                section_template
            };

            let rendered = section_template(&SectionToRender::from_section(section, &self.pages))
                .render_to_string()?;

            storage
                .store_rendered_section(&section, rendered)
                .map_err(|err| RenderSiteError::Storage(err.to_string()))?;
        }

        for page in self.pages.values() {
            let template_name = page
                .meta
                .template
                .clone()
                .map(TemplateKey::Custom)
                .unwrap_or(TemplateKey::Default);

            let page_template = self
                .templates
                .page
                .get(&template_name)
                .ok_or_else(|| RenderSiteError::TemplateNotFound(template_name))?;

            let rendered = page_template(&PageToRender::from_page(page)).render_to_string()?;

            storage
                .store_rendered_page(&page, rendered)
                .map_err(|err| RenderSiteError::Storage(err.to_string()))?;
        }

        Ok(())
    }

    pub async fn serve(self) -> Result<(), ServeSiteError> {
        let addr = SocketAddr::from(([127, 0, 0, 1], 3000));

        let listener = TcpListener::bind(addr).await?;

        /// [v4.0.2](https://github.com/livereload/livereload-js/blob/v4.0.2/dist/livereload.min.js)
        const LIVE_RELOAD_JS: &'static str = include_str!("../assets/livereload.min.js");

        let live_reload_server = WebSocket::new(|output: Sender| {
            move |message: Message| {
                if message.into_text().unwrap().contains("\"hello\"") {
                    let handshake_response = json!({
                        "command": "hello",
                        "protocols": ["http://livereload.com/protocols/official-7"],
                        "serverName": "Razorbill"
                    });

                    return output.send(Message::text(
                        serde_json::to_string(&handshake_response).unwrap(),
                    ));
                }

                Ok(())
            }
        })
        .unwrap();

        let live_reload_broadcaster = live_reload_server.broadcaster();

        let live_reload_address = "127.0.0.1:35729";

        let live_reload_server = live_reload_server
            .bind(&*live_reload_address)
            .expect("failed to bind live reload server");

        thread::spawn(move || {
            live_reload_server.run().unwrap();
        });

        fn empty() -> BoxBody<Bytes, hyper::Error> {
            Empty::<Bytes>::new()
                .map_err(|never| match never {})
                .boxed()
        }

        fn full<T: Into<Bytes>>(chunk: T) -> BoxBody<Bytes, hyper::Error> {
            Full::new(chunk.into())
                .map_err(|never| match never {})
                .boxed()
        }

        async fn handle_request(
            req: Request<hyper::body::Incoming>,
        ) -> Result<Response<BoxBody<Bytes, hyper::Error>>, Infallible> {
            match (req.method(), req.uri().path()) {
                (&Method::GET, path) => {
                    if path == "/livereload.js" {
                        return Ok(Response::builder()
                            .header(header::CONTENT_TYPE, "application/javascript")
                            .status(StatusCode::OK)
                            .body(full(LIVE_RELOAD_JS.to_owned()))
                            .unwrap());
                    }

                    if let Some(content) = SITE_CONTENT.read().unwrap().get(path) {
                        return Ok(Response::builder()
                            .header(header::CONTENT_TYPE, "text/html")
                            .status(StatusCode::OK)
                            .body(full(content.to_owned()))
                            .unwrap());
                    }

                    let mut not_found = Response::new(empty());
                    *not_found.status_mut() = StatusCode::NOT_FOUND;
                    Ok(not_found)
                }
                _ => {
                    let mut not_found = Response::new(empty());
                    *not_found.status_mut() = StatusCode::NOT_FOUND;
                    Ok(not_found)
                }
            }
        }

        let site = Arc::new(RwLock::new(self));

        {
            let mut site = site.write().unwrap();
            site.is_serving = true;
            site.render().unwrap();
        }

        let (watcher_tx, mut watcher_rx) = unbounded_channel();

        let mut watcher = RecommendedWatcher::new(
            move |result: Result<Event, notify::Error>| {
                let event = result.unwrap();

                watcher_tx.send(event).unwrap();
            },
            notify::Config::default(),
        )
        .unwrap();

        watcher
            .watch(&site.read().unwrap().content_path, RecursiveMode::Recursive)
            .unwrap();

        tokio::task::spawn(async move {
            use notify::EventKind;

            loop {
                let Some(event) = watcher_rx.recv().await else {
                    continue;
                };

                match event.kind {
                    EventKind::Create(_) | EventKind::Modify(_) | EventKind::Remove(_) => {
                        dbg!(&event.paths);

                        let mut site = site.write().unwrap();
                        site.load().unwrap();
                        site.render().unwrap();

                        let reload_message = json!({
                            "command": "reload",
                            "path": "/",
                            "originalPath": "",
                            "liveCSS": true,
                            "liveImg": true,
                            "protocol": ["http://livereload.com/protocols/official-7"]
                        });

                        live_reload_broadcaster
                            .send(serde_json::to_string(&reload_message).unwrap())
                            .unwrap();
                    }
                    _ => {}
                }
            }
        });

        loop {
            let (stream, _) = listener.accept().await?;

            let io = TokioIo::new(stream);

            tokio::task::spawn(async move {
                if let Err(err) = http1::Builder::new()
                    .serve_connection(io, service_fn(handle_request))
                    .await
                {
                    eprintln!("Error serving connection: {err:?}");
                }
            });
        }
    }
}

pub struct SiteBuilder<T> {
    state: T,
}

impl SiteBuilder<()> {
    pub fn new() -> Self {
        Self { state: () }
    }

    pub fn root(self, root_path: impl AsRef<Path>) -> SiteBuilder<WithRootPath> {
        SiteBuilder {
            state: WithRootPath {
                root_path: root_path.as_ref().to_owned(),
            },
        }
    }
}

pub struct WithRootPath {
    root_path: PathBuf,
}

impl SiteBuilder<WithRootPath> {
    pub fn templates(
        self,
        index: impl Fn(&SectionToRender) -> HtmlElement + Send + Sync + 'static,
        section: impl Fn(&SectionToRender) -> HtmlElement + Send + Sync + 'static,
        page: impl Fn(&PageToRender) -> HtmlElement + Send + Sync + 'static,
    ) -> SiteBuilder<WithTemplates> {
        SiteBuilder {
            state: WithTemplates {
                root_path: self.state.root_path,
                templates: Templates {
                    index: Arc::new(index),
                    section: HashMap::from_iter([(
                        TemplateKey::Default,
                        Arc::new(section)
                            as Arc<dyn Fn(&SectionToRender) -> HtmlElement + Send + Sync>,
                    )]),
                    page: HashMap::from_iter([(
                        TemplateKey::Default,
                        Arc::new(page) as Arc<dyn Fn(&PageToRender) -> HtmlElement + Send + Sync>,
                    )]),
                },
            },
        }
    }
}

pub struct WithTemplates {
    root_path: PathBuf,
    templates: Templates,
}

impl SiteBuilder<WithTemplates> {
    pub fn add_section_template(
        mut self,
        name: impl Into<String>,
        template: impl Fn(&SectionToRender) -> HtmlElement + Send + Sync + 'static,
    ) -> Self {
        self.state
            .templates
            .section
            .insert(TemplateKey::Custom(name.into()), Arc::new(template));
        self
    }

    pub fn add_page_template(
        mut self,
        name: impl Into<String>,
        template: impl Fn(&PageToRender) -> HtmlElement + Send + Sync + 'static,
    ) -> Self {
        self.state
            .templates
            .page
            .insert(TemplateKey::Custom(name.into()), Arc::new(template));
        self
    }

    pub fn build(self) -> Site {
        let root_path = self.state.root_path;

        Site {
            root_path: root_path.to_owned(),
            content_path: root_path.join("content"),
            output_path: root_path.join("public"),
            templates: self.state.templates,
            sections: HashMap::new(),
            pages: HashMap::new(),
            is_serving: false,
        }
    }
}