diff --git a/Cargo.lock b/Cargo.lock index 67d8bce..96211c0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4,4 +4,4 @@ version = 4 [[package]] name = "bberry" -version = "0.1.1" +version = "0.1.2" diff --git a/Cargo.toml b/Cargo.toml index 2e9aaaf..10b61e7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "bberry" description = "lisp-like dsl which \"compiles\" into html" -version = "0.1.1" +version = "0.1.2" edition = "2024" authors = ["trisuaso"] repository = "https://trisua.com/t/bberry.git" diff --git a/README.md b/README.md index 8389c89..cffb5b6 100644 --- a/README.md +++ b/README.md @@ -5,21 +5,21 @@ Super simple parser and "compiler" for turning a lisp-like DSL into HTML. Here's a small example: ```lisp -('"") ; using a raw string for the doctype is fine +(text "") ; using a raw string for the doctype is fine (html (head ; everything that belongs in the head element - (title'"Document") + (title (text "Document")) - (meta (: "charset" "UTF-8")) - (meta (: "name" "viewport") (: "content" "width=device-width, initial-scale=1.0")) - (meta (: "http-equiv" "X-UA-Compatible") (: "content" "ie=edge")) + (meta ("charset" "UTF-8")) + (meta ("name" "viewport") ("content" "width=device-width, initial-scale=1.0")) + (meta ("http-equiv" "X-UA-Compatible") ("content" "ie=edge")) - (link (: "rel" "stylesheet") (: "href" "#"))) + (link ("rel" "stylesheet") ("href" "#"))) (body ; the actual body only starts here - (span (: "style" "color: red") ('"Hello, world!")))) + (span ("style" "color: red") (text "Hello, world!")))) ``` This yields: @@ -45,9 +45,11 @@ This yields: bberry has some super simple syntax helpers: -- You can create raw HTML elements using `(text "...")` or `(' "...")` -- You can add attributes using `(attr "key" "value")` or `(: "key" "value")` -- You can quickly add text to an element by adding an apostrophe at the end of its tag (if it has no children) like `(h1'"Hello, world!")` +- You can create raw HTML elements using `(text "...")` or `(tag' "...")` +- You can add attributes using `(attr "key" "value")` or `("key" "value")` +- Whitespace is ignored (except for in parenthesis), so you can format however you'd like + - The only formatting rule is there must be a space after tag names, and everything must be in parenthesis! + - Parenthesis and strings must also be properly closed # License diff --git a/examples/boilerplate.lisp b/examples/boilerplate.lisp index 0957d98..6c17a22 100644 --- a/examples/boilerplate.lisp +++ b/examples/boilerplate.lisp @@ -1,15 +1,15 @@ -('"") ; using a raw string for the doctype is fine +(text "") ; using a raw string for the doctype is fine (html (head ; everything that belongs in the head element - (title'"Document") + (title (text "Document")) - (meta (: "charset" "UTF-8")) - (meta (: "name" "viewport") (: "content" "width=device-width, initial-scale=1.0")) - (meta (: "http-equiv" "X-UA-Compatible") (: "content" "ie=edge")) + (meta ("charset" "UTF-8")) + (meta ("name" "viewport") ("content" "width=device-width, initial-scale=1.0")) + (meta ("http-equiv" "X-UA-Compatible") ("content" "ie=edge")) - (link (: "rel" "stylesheet") (: "href" "#"))) + (link ("rel" "stylesheet") ("href" "#"))) (body ; the actual body only starts here - (span (: "style" "color: red") ('"Hello, world!")))) + (span ("style" "color: red") (text "Hello, world!")))) diff --git a/examples/string_escape.lisp b/examples/string_escape.lisp index 2e5ea26..a113b71 100644 --- a/examples/string_escape.lisp +++ b/examples/string_escape.lisp @@ -1,2 +1,5 @@ -('"\"Hello,\" world!") -(' "Hello, \"w\"orld!") +(text "\"Hello,\" world!") +(text "Hello, \"w\"orld!") +(span + ("x" "y") + (text "Hello")) diff --git a/src/core/parser.rs b/src/core/parser.rs index 1cf2951..881c724 100644 --- a/src/core/parser.rs +++ b/src/core/parser.rs @@ -37,12 +37,6 @@ pub fn string_parser(buf: impl Iterator, buffer: &mut String) { /// /// # Returns /// `(key, value)` -/// -/// # Example -/// -/// ```rust -/// attr_parser("(attr \"a\" \"b\")"); -/// ``` pub fn attr_parser(buf: &str) -> (String, String) { let mut key: String = String::new(); let mut value: String = String::new(); @@ -134,8 +128,15 @@ pub fn expr_parser(buf: &str) -> Element { } return element; - } else if (element.tag == "attr") | (element.tag == ":") { - let (k, v) = attr_parser(&buf[i..buf.len()]); + } else if (element.tag == "attr") | (element.tag == ":") | (element.tag.is_empty()) { + let mut chars = (&buf[i..buf.len()]).to_string(); + + if element.tag.is_empty() { + chars.insert_str(0, "\""); + } + + // parse + let (k, v) = attr_parser(&chars); element.attrs.insert(k, v); element.tag = "attr".to_string(); return element; @@ -143,7 +144,7 @@ pub fn expr_parser(buf: &str) -> Element { let mut buffer: String = String::new(); string_parser((&buf[i..buf.len()]).chars(), &mut buffer); - if (element.tag != "text") && (element.tag != "'") { + if element.tag != "text" { // allows us to write `(h1' "Hello, world!")` instead of `(h1 [(text "Hello, world!")])` element.tag = element.tag.replace("'", ""); @@ -154,10 +155,6 @@ pub fn expr_parser(buf: &str) -> Element { element.children.push(text_element); } else { // just add attr - if element.tag == "'" { - element.tag = "text".to_string(); - } - element.attrs.insert("content".to_string(), buffer); } diff --git a/src/lib.rs b/src/lib.rs index b682f4c..097f7a0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -14,5 +14,21 @@ mod test { .render(), ) .unwrap(); + + assert_eq!( + std::fs::read_to_string("string_escape.html").unwrap(), + "\"Hello,\" world!Hello, \"w\"orld!Hello" + ) + } + + #[test] + fn boilerplate() { + std::fs::write( + "boilerplate.html", + parse(&std::fs::read_to_string("examples/boilerplate.lisp").unwrap()) + .0 + .render(), + ) + .unwrap(); } }