commit 4fa73ef56683d751e41362c714f7afbd227ebdf6
parent 362c6156dbf8d8cb402215b241898dfbc96ad4fd
Author: Demonstrandum <moi@knutsen.co>
Date: Thu, 5 Nov 2020 17:04:18 +0000
Fix bug where macros don't expand when reading from stream.
Diffstat:
5 files changed, 44 insertions(+), 49 deletions(-)
diff --git a/README.md b/README.md
@@ -9,7 +9,6 @@ Because all markup is terrible, especially XML/SGML and derivatives.
But mainly, for easier static markup code generation, such as with
macros, code includes and such.
-
## Try it out
Mainly this should be used as a library, such as from within a server,
@@ -17,17 +16,31 @@ generating HTML (or any other supported markup) before it is served to the
client.
### Current Formats
+
- XML
- HTML
- CSS
-### Using The Binary
+### Installation
-Providing you have installed `seam` with
+You may clone the repo, then build and install
+```sh
+git clone git://git.knutsen.co/seam
+cd seam
+cargo build --release
+cargo install --path .
+```
+
+Or install it from crates.io
```sh
cargo install seam
```
+Either way, you'll need the Rust (nightly) compiler and along
+with it, comes `cargo`.
+
+### Using The Binary
+
You may use it by doing
```sh
seam test.sex --html > test.html
@@ -36,11 +49,14 @@ seam test.sex --html > test.html
`test.sex` contains your symbolic-expressions, which is used to generate
HTML, saved in `test.html`.
-Likewise, you may do
+Likewise, you may read from `STDIN`
```sh
-cat test.sex | seam --html > test.html
+seam --html < example.sex > example.html
+# Which is the same as
+cat example.sex | seam --html > example.html
```
-or
+You may also very well use here-strings and here-docs, if your shell
+supports it.
```sh
seam --html <<< "(p Hello World)"
#stdout:
@@ -53,7 +69,13 @@ seam --html <<< "(p Hello World)"
# </body>
# </html>
```
-
+```sh
+seam --xml <<< '(para Today is a day in (%date "%B, year %Y").)'
+#stdout:
+# <?xml version="1.0" encoding="UTF-8" ?>
+# <para>Today is a day in November, year 2020.</para>
+# <!-- Generated by SEAM, from symbolic-expressions into XML. -->
+```
## TODO
- Caching or checking time-stamps as to not regenerate unmodified source files.
diff --git a/src/lib.rs b/src/lib.rs
@@ -2,10 +2,9 @@ pub mod parse;
pub mod assemble;
use parse::{expander, parser, lexer};
-pub use parse::parse_stream;
use std::error::Error;
-use std::{fs, path::Path};
+use std::{fs, io, path::Path};
pub const VERSION : (u8, u8, u8) = (0, 1, 3);
@@ -25,6 +24,13 @@ pub fn parse_file(path : &Path)
parse(contents, Some(&path))
}
+pub fn parse_stream(stream : &mut impl io::Read)
+ -> Result<parser::ParseTree, Box<dyn Error>> {
+ let mut contents = String::new();
+ stream.read_to_string(&mut contents)?;
+ parse(contents, Option::<&Path>::None)
+}
+
pub fn main() {
eprintln!("Library main function should not be used.");
std::process::exit(1);
diff --git a/src/parse/expander.rs b/src/parse/expander.rs
@@ -71,7 +71,7 @@ impl ExpansionContext {
};
// Open file, and parse contents!
- let tree = match super::parse_file(&Path::new(&path)) {
+ let tree = match super::parse_file_noexpand(&Path::new(&path)) {
Ok(tree) => tree,
Err(error) => {
eprintln!("{}", error);
@@ -82,6 +82,9 @@ impl ExpansionContext {
}
};
+ // Build new (expanded) tree, with result of previous
+ // parse, while recursively expanding each branch in the
+ // tree too, as they are added.
let mut expanded_tree = Vec::with_capacity(tree.len());
for branch in tree {
expanded_tree.extend(self.expand_node(branch)?);
diff --git a/src/parse/mod.rs b/src/parse/mod.rs
@@ -5,17 +5,10 @@ pub mod lexer;
pub mod parser;
pub use parser::ParseTree;
-use std::{fs, path::Path, io, error::Error};
+use std::{fs, path::Path, error::Error};
-pub fn parse_stream(stream : &mut impl io::Read) -> Result<ParseTree, Box<dyn Error>> {
- let mut contents = String::new();
- stream.read_to_string(&mut contents)?;
- let tokens = lexer::lex(contents, Option::<&Path>::None)?;
- let tree = parser::parse_stream(tokens)?;
- Ok(tree)
-}
-
-pub fn parse_file(path : &Path) -> Result<ParseTree, Box<dyn Error>> {
+/// Parse a file without expanding macros.
+pub fn parse_file_noexpand(path : &Path) -> Result<ParseTree, Box<dyn Error>> {
let contents = fs::read_to_string(&path)?;
let tokens = lexer::lex(contents, Some(path))?;
let tree = parser::parse_stream(tokens)?;
diff --git a/test.html b/test.html
@@ -1,29 +0,0 @@
-<!DOCTYPE html>
-<html lang="en"><head><title>Example HTML Document</title>
-<style>
-html {
- width: 100%;
- height: 100%;
-}
-html , body {
- margin: 0;
- padding: 0;
-}
-body {
- padding: 4em 6em;
-}
-#hello {
- color: rgb(24 calc((3 + (7 * 3) + 1)) 4);
- font-family: sans-serif;
-}
-img {
- border-radius: 5px;
-}
-</style></head>
-<body><p id="hello">Hello, World!</p>
-<p>something something text...</p>
-<h1>A (big) Header!</h1>
-<p>Yet some more <span style="color: red">text</span> <3</p>
-<p>Hello<span style="color: green">World</span>!</p>
-<img alt="Cute cat" src="https://static.insider.com/image/5d24d6b921a861093e71fef3.jpg" width="300"></img></body></html>
-<!-- Generated by SEAM, from symbolic-expressions into HTML. -->