add: improve syntax
This commit is contained in:
parent
55130db04a
commit
149025f9e4
7 changed files with 52 additions and 34 deletions
2
Cargo.lock
generated
2
Cargo.lock
generated
|
@ -4,4 +4,4 @@ version = 4
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "bberry"
|
name = "bberry"
|
||||||
version = "0.1.1"
|
version = "0.1.2"
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
[package]
|
[package]
|
||||||
name = "bberry"
|
name = "bberry"
|
||||||
description = "lisp-like dsl which \"compiles\" into html"
|
description = "lisp-like dsl which \"compiles\" into html"
|
||||||
version = "0.1.1"
|
version = "0.1.2"
|
||||||
edition = "2024"
|
edition = "2024"
|
||||||
authors = ["trisuaso"]
|
authors = ["trisuaso"]
|
||||||
repository = "https://trisua.com/t/bberry.git"
|
repository = "https://trisua.com/t/bberry.git"
|
||||||
|
|
22
README.md
22
README.md
|
@ -5,21 +5,21 @@ Super simple parser and "compiler" for turning a lisp-like DSL into HTML.
|
||||||
Here's a small example:
|
Here's a small example:
|
||||||
|
|
||||||
```lisp
|
```lisp
|
||||||
('"<!DOCTYPE html>") ; using a raw string for the doctype is fine
|
(text "<!DOCTYPE html>") ; using a raw string for the doctype is fine
|
||||||
(html
|
(html
|
||||||
(head
|
(head
|
||||||
; everything that belongs in the head element
|
; everything that belongs in the head element
|
||||||
(title'"Document")
|
(title (text "Document"))
|
||||||
|
|
||||||
(meta (: "charset" "UTF-8"))
|
(meta ("charset" "UTF-8"))
|
||||||
(meta (: "name" "viewport") (: "content" "width=device-width, initial-scale=1.0"))
|
(meta ("name" "viewport") ("content" "width=device-width, initial-scale=1.0"))
|
||||||
(meta (: "http-equiv" "X-UA-Compatible") (: "content" "ie=edge"))
|
(meta ("http-equiv" "X-UA-Compatible") ("content" "ie=edge"))
|
||||||
|
|
||||||
(link (: "rel" "stylesheet") (: "href" "#")))
|
(link ("rel" "stylesheet") ("href" "#")))
|
||||||
|
|
||||||
(body
|
(body
|
||||||
; the actual body only starts here
|
; the actual body only starts here
|
||||||
(span (: "style" "color: red") ('"Hello, world!"))))
|
(span ("style" "color: red") (text "Hello, world!"))))
|
||||||
```
|
```
|
||||||
|
|
||||||
This yields:
|
This yields:
|
||||||
|
@ -45,9 +45,11 @@ This yields:
|
||||||
|
|
||||||
bberry has some super simple syntax helpers:
|
bberry has some super simple syntax helpers:
|
||||||
|
|
||||||
- You can create raw HTML elements using `(text "...")` or `(' "...")`
|
- You can create raw HTML elements using `(text "...")` or `(tag' "...")`
|
||||||
- You can add attributes using `(attr "key" "value")` or `(: "key" "value")`
|
- 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!")`
|
- 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
|
# License
|
||||||
|
|
||||||
|
|
|
@ -1,15 +1,15 @@
|
||||||
('"<!DOCTYPE html>") ; using a raw string for the doctype is fine
|
(text "<!DOCTYPE html>") ; using a raw string for the doctype is fine
|
||||||
(html
|
(html
|
||||||
(head
|
(head
|
||||||
; everything that belongs in the head element
|
; everything that belongs in the head element
|
||||||
(title'"Document")
|
(title (text "Document"))
|
||||||
|
|
||||||
(meta (: "charset" "UTF-8"))
|
(meta ("charset" "UTF-8"))
|
||||||
(meta (: "name" "viewport") (: "content" "width=device-width, initial-scale=1.0"))
|
(meta ("name" "viewport") ("content" "width=device-width, initial-scale=1.0"))
|
||||||
(meta (: "http-equiv" "X-UA-Compatible") (: "content" "ie=edge"))
|
(meta ("http-equiv" "X-UA-Compatible") ("content" "ie=edge"))
|
||||||
|
|
||||||
(link (: "rel" "stylesheet") (: "href" "#")))
|
(link ("rel" "stylesheet") ("href" "#")))
|
||||||
|
|
||||||
(body
|
(body
|
||||||
; the actual body only starts here
|
; the actual body only starts here
|
||||||
(span (: "style" "color: red") ('"Hello, world!"))))
|
(span ("style" "color: red") (text "Hello, world!"))))
|
||||||
|
|
|
@ -1,2 +1,5 @@
|
||||||
('"\"Hello,\" world!")
|
(text "\"Hello,\" world!")
|
||||||
(' "Hello, \"w\"orld!")
|
(text "Hello, \"w\"orld!")
|
||||||
|
(span
|
||||||
|
("x" "y")
|
||||||
|
(text "Hello"))
|
||||||
|
|
|
@ -37,12 +37,6 @@ pub fn string_parser(buf: impl Iterator<Item = char>, buffer: &mut String) {
|
||||||
///
|
///
|
||||||
/// # Returns
|
/// # Returns
|
||||||
/// `(key, value)`
|
/// `(key, value)`
|
||||||
///
|
|
||||||
/// # Example
|
|
||||||
///
|
|
||||||
/// ```rust
|
|
||||||
/// attr_parser("(attr \"a\" \"b\")");
|
|
||||||
/// ```
|
|
||||||
pub fn attr_parser(buf: &str) -> (String, String) {
|
pub fn attr_parser(buf: &str) -> (String, String) {
|
||||||
let mut key: String = String::new();
|
let mut key: String = String::new();
|
||||||
let mut value: String = String::new();
|
let mut value: String = String::new();
|
||||||
|
@ -134,8 +128,15 @@ pub fn expr_parser(buf: &str) -> Element {
|
||||||
}
|
}
|
||||||
|
|
||||||
return element;
|
return element;
|
||||||
} else if (element.tag == "attr") | (element.tag == ":") {
|
} else if (element.tag == "attr") | (element.tag == ":") | (element.tag.is_empty()) {
|
||||||
let (k, v) = attr_parser(&buf[i..buf.len()]);
|
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.attrs.insert(k, v);
|
||||||
element.tag = "attr".to_string();
|
element.tag = "attr".to_string();
|
||||||
return element;
|
return element;
|
||||||
|
@ -143,7 +144,7 @@ pub fn expr_parser(buf: &str) -> Element {
|
||||||
let mut buffer: String = String::new();
|
let mut buffer: String = String::new();
|
||||||
|
|
||||||
string_parser((&buf[i..buf.len()]).chars(), &mut buffer);
|
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!")])`
|
// allows us to write `(h1' "Hello, world!")` instead of `(h1 [(text "Hello, world!")])`
|
||||||
element.tag = element.tag.replace("'", "");
|
element.tag = element.tag.replace("'", "");
|
||||||
|
|
||||||
|
@ -154,10 +155,6 @@ pub fn expr_parser(buf: &str) -> Element {
|
||||||
element.children.push(text_element);
|
element.children.push(text_element);
|
||||||
} else {
|
} else {
|
||||||
// just add attr
|
// just add attr
|
||||||
if element.tag == "'" {
|
|
||||||
element.tag = "text".to_string();
|
|
||||||
}
|
|
||||||
|
|
||||||
element.attrs.insert("content".to_string(), buffer);
|
element.attrs.insert("content".to_string(), buffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
16
src/lib.rs
16
src/lib.rs
|
@ -14,5 +14,21 @@ mod test {
|
||||||
.render(),
|
.render(),
|
||||||
)
|
)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
std::fs::read_to_string("string_escape.html").unwrap(),
|
||||||
|
"\"Hello,\" world!Hello, \"w\"orld!<span x=\"y\">Hello</span>"
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn boilerplate() {
|
||||||
|
std::fs::write(
|
||||||
|
"boilerplate.html",
|
||||||
|
parse(&std::fs::read_to_string("examples/boilerplate.lisp").unwrap())
|
||||||
|
.0
|
||||||
|
.render(),
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue