example and stuff
This commit is contained in:
parent
4c799091b4
commit
3e5fdde1f3
|
@ -1,3 +1,4 @@
|
||||||
/target
|
/target
|
||||||
/Cargo.lock
|
/Cargo.lock
|
||||||
.DS_Store
|
.DS_Store
|
||||||
|
examples/index.html
|
||||||
|
|
|
@ -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();
|
||||||
|
}
|
42
src/lib.rs
42
src/lib.rs
|
@ -7,7 +7,7 @@ use crate::parse::*;
|
||||||
mod modifiers;
|
mod modifiers;
|
||||||
mod parse;
|
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);
|
let out = generate_css(classes);
|
||||||
std::fs::write(path, out)?;
|
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(|| {
|
static RULES: Lazy<HashMap<&str, &dyn Rule>> = Lazy::new(|| {
|
||||||
let mut m = HashMap::new();
|
let mut m = HashMap::new();
|
||||||
m.insert("m", &Margin as &dyn Rule);
|
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
|
m
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// TODO maybe we can skip rules and make it just be a general rewritter
|
||||||
|
|
||||||
trait Rule: Sync {
|
trait Rule: Sync {
|
||||||
fn generate<'a>(&self, class: &Class<'a>) -> String;
|
fn generate<'a>(&self, class: &Class<'a>) -> String;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct Margin;
|
||||||
impl Rule for Margin {
|
impl Rule for Margin {
|
||||||
fn generate<'a>(&self, class: &Class<'a>) -> String {
|
fn generate<'a>(&self, class: &Class<'a>) -> String {
|
||||||
format!(
|
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)]
|
#[derive(Error, Debug)]
|
||||||
pub enum Error {
|
pub enum Error {
|
||||||
|
|
|
@ -60,6 +60,14 @@ impl<'a> Class<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
format!(".{original}{rest}")
|
format!(".{original}{rest}")
|
||||||
|
.replace('[', "\\[")
|
||||||
|
.replace(']', "\\]")
|
||||||
|
.replace('(', "\\(")
|
||||||
|
.replace(')', "\\)")
|
||||||
|
.replace('#', "\\#")
|
||||||
|
.replace('$', "\\$")
|
||||||
|
.replace('\'', "\\'")
|
||||||
|
.replace('*', "\\*")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue