From 3e5fdde1f3ed8a1ad1d39e4bcccd40bc18b13843 Mon Sep 17 00:00:00 2001 From: annieversary Date: Fri, 13 May 2022 14:29:40 +0100 Subject: [PATCH] example and stuff --- .gitignore | 1 + examples/html.rs | 36 ++++++++++++++++++++++++++++++++++++ src/lib.rs | 42 ++++++++++++++++++++++++++++++++++++++++-- src/parse.rs | 8 ++++++++ 4 files changed, 85 insertions(+), 2 deletions(-) create mode 100644 examples/html.rs diff --git a/.gitignore b/.gitignore index 4c790d0..6a48023 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ /target /Cargo.lock .DS_Store +examples/index.html diff --git a/examples/html.rs b/examples/html.rs new file mode 100644 index 0000000..92ec0fb --- /dev/null +++ b/examples/html.rs @@ -0,0 +1,36 @@ +fn main() { + // this list would ideally be generated on the fly out of the written html, + // but i don't want to unneeded dependencies to this crate + let classes = [ + "mt[10rem]", + "color[#e20f00]", + "color[green]hover", + "content[attr(after)]$after", + "content['*']$before", + "color[red]$after", + ]; + + let css = dbg!(zephyr::generate_css(&classes)); + + let html = format!( + r#" + + + + + + + +

+ this text is red, but green on hover +

+

+ this text has a lot of margin +

+ + +"# + ); + + std::fs::write("./examples/index.html", html).unwrap(); +} diff --git a/src/lib.rs b/src/lib.rs index f32bdd8..841d2df 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -7,7 +7,7 @@ use crate::parse::*; mod modifiers; mod parse; -pub fn generate(classes: &[&str], path: impl AsRef) -> Result<(), Error> { +pub fn generate_and_write(classes: &[&str], path: impl AsRef) -> Result<(), Error> { let out = generate_css(classes); std::fs::write(path, out)?; @@ -31,13 +31,19 @@ pub fn generate_class(class: &str) -> Option { static RULES: Lazy> = Lazy::new(|| { let mut m = HashMap::new(); m.insert("m", &Margin as &dyn Rule); + m.insert("mt", &MarginTop as &dyn Rule); + m.insert("color", &Color as &dyn Rule); + m.insert("content", &Content as &dyn Rule); m }); +// TODO maybe we can skip rules and make it just be a general rewritter + trait Rule: Sync { fn generate<'a>(&self, class: &Class<'a>) -> String; } +struct Margin; impl Rule for Margin { fn generate<'a>(&self, class: &Class<'a>) -> String { format!( @@ -47,7 +53,39 @@ impl Rule for Margin { ) } } -struct Margin; + +struct MarginTop; +impl Rule for MarginTop { + fn generate<'a>(&self, class: &Class<'a>) -> String { + format!( + "{selector} {{ margin-top: {value}; }}", + selector = class.selector(), + value = class.value + ) + } +} + +struct Color; +impl Rule for Color { + fn generate<'a>(&self, class: &Class<'a>) -> String { + format!( + "{selector} {{ color: {value}; }}", + selector = class.selector(), + value = class.value + ) + } +} + +struct Content; +impl Rule for Content { + fn generate<'a>(&self, class: &Class<'a>) -> String { + format!( + "{selector} {{ content: {value}; }}", + selector = class.selector(), + value = class.value + ) + } +} #[derive(Error, Debug)] pub enum Error { diff --git a/src/parse.rs b/src/parse.rs index b231fde..c1d5555 100644 --- a/src/parse.rs +++ b/src/parse.rs @@ -60,6 +60,14 @@ impl<'a> Class<'a> { } format!(".{original}{rest}") + .replace('[', "\\[") + .replace(']', "\\]") + .replace('(', "\\(") + .replace(')', "\\)") + .replace('#', "\\#") + .replace('$', "\\$") + .replace('\'', "\\'") + .replace('*', "\\*") } }