diff --git a/src/main.rs b/src/main.rs index 5b492b4..07076c0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,10 +5,12 @@ mod text; mod ts; mod utils; mod watch; +mod website; use std::collections::{HashMap, HashSet}; use std::fs; use std::process::Command; +use std::rc::Rc; use build::Hashed; use camino::{Utf8Path, Utf8PathBuf}; @@ -19,6 +21,7 @@ use gray_matter::Matter; use hypertext::{Raw, Renderable}; use pipeline::{Asset, AssetKind, Content, FileItemKind, Output, OutputKind, PipelineItem}; use serde::Deserialize; +use website::WebsiteDesigner; use crate::pipeline::Virtual; @@ -83,41 +86,38 @@ fn main() { .into(), }; - let sources = &[ - Source { + let website = WebsiteDesigner::default() + .add_source(Source { path: "content/about.md", exts: ["md"].into(), func: process_content::, - }, - Source { + }) + .add_source(Source { path: "content/posts/**/*", exts: ["md", "mdx"].into(), func: process_content::, - }, - Source { + }) + .add_source(Source { path: "content/slides/**/*", exts: ["md", "lhs"].into(), func: process_content::, - }, - Source { + }) + .add_source(Source { path: "content/wiki/**/*", exts: ["md"].into(), func: process_content::, - }, - ]; - - let special = vec![ - Output { + }) + .add_output(Output { kind: Virtual::new(|sack| crate::html::map(sack).render().to_owned().into()).into(), path: "map/index.html".into(), link: None, - }, - Output { + }) + .add_output(Output { kind: Virtual::new(|sack| crate::html::search(sack).render().to_owned().into()).into(), path: "search/index.html".into(), link: None, - }, - Output { + }) + .add_output(Output { kind: Asset { kind: pipeline::AssetKind::html(|sack| { let data = std::fs::read_to_string("content/index.md").unwrap(); @@ -135,16 +135,16 @@ fn main() { .into(), path: "index.html".into(), link: None, - }, - Output { + }) + .add_output(Output { kind: Virtual::new(|sack| { crate::html::to_list(sack, sack.get_links("posts/**/*.html"), "Posts".into()) }) .into(), path: "posts/index.html".into(), link: None, - }, - Output { + }) + .add_output(Output { kind: Virtual::new(|sack| { crate::html::to_list( sack, @@ -155,17 +155,12 @@ fn main() { .into(), path: "slides/index.html".into(), link: None, - }, - ]; + }) + .finish(); match args.mode { - Mode::Build => { - let _ = build(&ctx, sources, special); - } - Mode::Watch => { - let state = build(&ctx, sources, special); - watch::watch(&ctx, sources, state).unwrap() - } + Mode::Build => website.build(&ctx), + Mode::Watch => website.watch(&ctx), } } @@ -199,7 +194,7 @@ impl Source { } } -fn build(ctx: &BuildContext, sources: &[Source], special: Vec) -> Vec { +fn build(ctx: &BuildContext, sources: &[Source], special: &[Rc]) -> Vec> { crate::build::clean_dist(); let content: Vec = sources @@ -218,9 +213,9 @@ fn build(ctx: &BuildContext, sources: &[Source], special: Vec) -> Vec = content.iter().chain(special.iter()).collect(); + let assets: Vec<_> = content.iter().chain(special.iter().map(AsRef::as_ref)).collect(); crate::build::build_content(ctx, &assets, &assets, Some(hashes)); crate::build::build_static(); @@ -228,7 +223,7 @@ fn build(ctx: &BuildContext, sources: &[Source], special: Vec) -> Vec(raw: &str) -> (D, String) diff --git a/src/watch.rs b/src/watch.rs index 775f52d..deedb25 100644 --- a/src/watch.rs +++ b/src/watch.rs @@ -71,7 +71,7 @@ fn new_thread_ws_reload( (tx, thread) } -pub fn watch(ctx: &BuildContext, sources: &[Source], state: Vec) -> Result<()> { +pub fn watch(ctx: &BuildContext, sources: &[Source], mut state: Vec>) -> Result<()> { let root = env::current_dir().unwrap(); let server = TcpListener::bind("127.0.0.1:1337")?; let client = Arc::new(Mutex::new(vec![])); @@ -92,8 +92,6 @@ pub fn watch(ctx: &BuildContext, sources: &[Source], state: Vec) -> Resu let thread_i = new_thread_ws_incoming(server, client.clone()); let (tx_reload, thread_o) = new_thread_ws_reload(client.clone()); - let mut state: Vec> = state.into_iter().map(Rc::new).collect(); - while let Ok(events) = rx.recv().unwrap() { let paths: Vec = events .into_iter() diff --git a/src/website.rs b/src/website.rs new file mode 100644 index 0000000..d061c91 --- /dev/null +++ b/src/website.rs @@ -0,0 +1,49 @@ +use std::rc::Rc; + +use crate::{build, pipeline::Output, watch, BuildContext, Source}; + +#[derive(Debug)] +pub(crate) struct Website { + sources: Vec, + special: Vec>, +} + +impl Website { + pub(crate) fn designer() -> WebsiteDesigner { + WebsiteDesigner::default() + } + + pub(crate) fn build(&self, ctx: &BuildContext) { + let _ = build(ctx, &self.sources, &self.special.clone()); + } + + pub(crate) fn watch(&self, ctx: &BuildContext) { + let state = build(&ctx, &self.sources, &self.special.clone()); + watch::watch(&ctx, &self.sources, state).unwrap() + } +} + +#[derive(Debug, Default)] +pub(crate) struct WebsiteDesigner { + sources: Vec, + special: Vec>, +} + +impl WebsiteDesigner { + pub(crate) fn add_source(mut self, source: Source) -> WebsiteDesigner { + self.sources.push(source); + self + } + + pub(crate) fn add_output(mut self, output: Output) -> WebsiteDesigner { + self.special.push(Rc::new(output)); + self + } + + pub(crate) fn finish(self) -> Website { + Website { + sources: self.sources, + special: self.special, + } + } +}