example and stuff

This commit is contained in:
annieversary 2022-05-13 14:29:40 +01:00
parent 4c799091b4
commit 3e5fdde1f3
4 changed files with 85 additions and 2 deletions

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
/target
/Cargo.lock
.DS_Store
examples/index.html

36
examples/html.rs Normal file
View File

@ -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#"
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<style>{css}</style>
</head>
<body>
<p class="color[#e20f00] color[green]hover content['*']$before">
this text is red, but green on hover
</p>
<p class="mt[10rem] content[attr(after)]$after color[red]$after" after="hi, this is an after text">
this text has a lot of margin
</p>
</body>
</html>
"#
);
std::fs::write("./examples/index.html", html).unwrap();
}

View File

@ -7,7 +7,7 @@ use crate::parse::*;
mod modifiers;
mod parse;
pub fn generate(classes: &[&str], path: impl AsRef<Path>) -> Result<(), Error> {
pub fn generate_and_write(classes: &[&str], path: impl AsRef<Path>) -> Result<(), Error> {
let out = generate_css(classes);
std::fs::write(path, out)?;
@ -31,13 +31,19 @@ pub fn generate_class(class: &str) -> Option<String> {
static RULES: Lazy<HashMap<&str, &dyn Rule>> = 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 {

View File

@ -60,6 +60,14 @@ impl<'a> Class<'a> {
}
format!(".{original}{rest}")
.replace('[', "\\[")
.replace(']', "\\]")
.replace('(', "\\(")
.replace(')', "\\)")
.replace('#', "\\#")
.replace('$', "\\$")
.replace('\'', "\\'")
.replace('*', "\\*")
}
}