From 57caba426b113d5247391275c1179e8440107e44 Mon Sep 17 00:00:00 2001 From: Victor Freire Date: Thu, 3 Mar 2022 19:08:29 -0300 Subject: [PATCH] wip --- Cargo.lock | 57 +++++++++++++++++++++++ Cargo.toml | 1 + flake.nix | 2 +- src/blog.rs | 1 + src/builder.rs | 120 +++++++++++++++++++++++++++++++++++++++++++------ src/main.rs | 12 +++-- src/mod.rs | 1 - 7 files changed, 175 insertions(+), 19 deletions(-) create mode 100644 src/blog.rs delete mode 100644 src/mod.rs diff --git a/Cargo.lock b/Cargo.lock index 8304444..81c1a38 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,6 +2,63 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "bitflags" +version = "1.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" + +[[package]] +name = "getopts" +version = "0.2.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "14dbbfd5c71d70241ecf9e6f13737f7b5ce823821063188d7e46c41d371eebd5" +dependencies = [ + "unicode-width", +] + +[[package]] +name = "memchr" +version = "2.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "308cc39be01b73d0d18f82a0e7b2a3df85245f84af96fdddc5d202d27e47b86a" + +[[package]] +name = "pulldown-cmark" +version = "0.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34f197a544b0c9ab3ae46c359a7ec9cbbb5c7bf97054266fecb7ead794a181d6" +dependencies = [ + "bitflags", + "getopts", + "memchr", + "unicase", +] + [[package]] name = "sbg" version = "0.1.0" +dependencies = [ + "pulldown-cmark", +] + +[[package]] +name = "unicase" +version = "2.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "50f37be617794602aabbeee0be4f259dc1778fabe05e2d67ee8f79326d5cb4f6" +dependencies = [ + "version_check", +] + +[[package]] +name = "unicode-width" +version = "0.1.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3ed742d4ea2bd1176e236172c8429aaf54486e7ac098db29ffe6529e0ce50973" + +[[package]] +name = "version_check" +version = "0.9.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" diff --git a/Cargo.toml b/Cargo.toml index 5527714..bcc20ae 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,3 +6,4 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +pulldown-cmark = "0.9.1" diff --git a/flake.nix b/flake.nix index c70d0bb..10dff50 100644 --- a/flake.nix +++ b/flake.nix @@ -43,7 +43,7 @@ nativeBuildInputs = [ rustToolchain ]; - cargoSha256 = "sha256-4zJsD9zJ66ICGhVlQWVZH9A6fuc7/mCYFvojpD5xC/4="; + cargoSha256 = "sha256-91DYCDaHubS7V8UEgGD7yWCqkl8DmTAL0JFHlzM26AQ="; }; in rec { diff --git a/src/blog.rs b/src/blog.rs new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/src/blog.rs @@ -0,0 +1 @@ + diff --git a/src/builder.rs b/src/builder.rs index 71c4630..fbea33f 100644 --- a/src/builder.rs +++ b/src/builder.rs @@ -1,17 +1,35 @@ -use std::path::{Path, PathBuf}; +use std::{fs, path::PathBuf}; + +use pulldown_cmark::{html, Event, HeadingLevel, Parser, Tag}; + +#[derive(Default, Debug)] +pub struct Entry { + title: String, + content: String, +} + +#[derive(Default, Debug)] +pub struct Blog { + title: String, + style: String, + site_url: String, + entries: Vec, +} + +impl Blog {} #[derive(Debug)] pub struct BlogBuilder { - file: PathBuf, - site_url: String, + file: Option, + site_url: Option, style: Option, - title: String, + title: Option, } impl BlogBuilder { pub fn file(self, source: PathBuf) -> Self { Self { - file: source, + file: Some(source), ..self } } @@ -25,25 +43,101 @@ impl BlogBuilder { pub fn site_url(self, source: String) -> Self { Self { - site_url: source, + site_url: Some(source), ..self } } - pub fn build(&self) -> Result<(), Box> { - if let Some(style) = self.style { - Path::new(style).exists()? - } + pub fn build(self) -> Result { + let title = match self.title { + Some(title) => title, + None => "blog".into(), + }; + + let site_url = match self.site_url { + Some(site_url) => site_url, + None => "/".into(), + }; + + let style = match self.style { + // FIXME error handling + Some(path) => fs::read_to_string(path).unwrap(), + None => include_str!("assets/bamboo.css").to_string(), + }; + + let file_content = match self.file { + Some(path) => { + if path.exists() { + // FIXME error handling + fs::read_to_string(path).unwrap() + } else { + Err("file not found.")? + } + } + None => { + let path = "entries.md"; + // FIXME error handling + fs::read_to_string(path).unwrap() + } + }; + + let entries = { + let parser = Parser::new(&file_content); + let mut entries: Vec = Vec::new(); + + let mut inside_heading = false; + let mut current_entry = Entry::default(); + let mut current_events: Vec = Vec::new(); + for event in parser { + match event { + Event::Start(Tag::Heading(HeadingLevel::H1, _, _)) => { + if !current_events.is_empty() { + let mut content = String::new(); + // FIXME remove clone + html::push_html(&mut content, current_events.clone().into_iter()); + + current_entry.content = content; + entries.push(current_entry); + + current_entry = Entry::default(); + } + inside_heading = true; + + current_events.push(event); + } + Event::End(Tag::Heading(HeadingLevel::H1, _, _)) => { + inside_heading = false; + } + Event::Text(title) => { + if inside_heading { + current_entry.title = title.to_string(); + } + current_events.push(Event::Text(title)); + } + // FIXME value borrowed here after move + event => current_events.push(event), + } + } + + dbg!(entries) + }; + + Ok(Blog { + title, + style, + site_url, + entries, + }) } } impl Default for BlogBuilder { fn default() -> Self { Self { - file: "entries.md".into(), - site_url: "/".into(), + file: None, + site_url: None, style: None, - title: "blog".into(), + title: None, } } } diff --git a/src/main.rs b/src/main.rs index 151597b..11cf59d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,11 @@ -use builder::BlogBuilder; - +mod blog; mod builder; -fn main() { - dbg!(BlogBuilder::default()); +use std::error::Error; + +use builder::BlogBuilder; + +fn main() -> Result<(), Box> { + let blog = BlogBuilder::default().file("blog.md".into()).build()?; + Ok(()) } diff --git a/src/mod.rs b/src/mod.rs deleted file mode 100644 index 5575a85..0000000 --- a/src/mod.rs +++ /dev/null @@ -1 +0,0 @@ -pub mod builder; -- 2.45.2