Tholp's bespoke website generator

Add clear macro

Tholp1 2d1a770b 86bf06e7

Changed files
+51 -25
src
+10
src/macros/clear.rs
···
+
use crate::{
+
stringtools::{split_keep_delimiters, strings_to_tokens},
+
types::{InputFile, Token},
+
};
+
+
pub fn macro_clear(_file: &mut InputFile, _args: &Vec<String>) -> Vec<Token> {
+
_file.tokens = _file.tokens.split_off(_file.working_index);
+
_file.working_index = 0;
+
return Vec::new();
+
}
+1 -1
src/macros/include.rs
···
types::{InputFile, Token},
};
-
pub fn macro_include(_file: &InputFile, args: &Vec<String>) -> Vec<Token> {
+
pub fn macro_include(_file: &mut InputFile, args: &Vec<String>) -> Vec<Token> {
print!("\nargs: {:?}\n", args);
let mut output = fs::read_to_string(args[0].clone()).expect("File unreadable or missing");
if output.ends_with("\n") {
+12 -5
src/macros/mod.rs
···
+
pub mod clear;
pub mod include;
use super::types::Macro;
+
use clear::macro_clear;
use include::macro_include;
-
pub static MACRO_LIST: [Macro<'_>; 1] = [Macro {
-
symbol: "include",
-
expand: macro_include,
-
//always_ephemeral: false,
-
}];
+
pub static MACRO_LIST: [Macro<'_>; 2] = [
+
Macro {
+
symbol: "include",
+
expand: macro_include,
+
},
+
Macro {
+
symbol: "clear",
+
expand: macro_clear,
+
},
+
];
+25 -18
src/main.rs
···
mod stringtools;
mod types;
-
use macros::{
-
include::{self, macro_include},
-
MACRO_LIST,
-
};
+
use macros::MACRO_LIST;
use markdown::{to_html_with_options, CompileOptions, Options};
use std::{
env,
···
//file.tokens = strings_to_tokens(split_keep_delimiters(contents), file.filename_input.clone());
file.tokens = split_to_tokens(contents, file.filename_input.clone());
-
let mut index = 0;
-
-
while index < file.tokens.len() {
+
while file.working_index < file.tokens.len() {
//look for macros or blocks
//println!(">\"{}\"<", file.tokens[index].contents);
-
if file.tokens[index].contents.starts_with(['!', '&']) {
-
let mut matched = false;
+
if file.tokens[file.working_index]
+
.contents
+
.starts_with(['!', '&'])
+
{
+
let mut matched: bool = false;
for m in &MACRO_LIST {
-
if &file.tokens[index].contents.trim()[1..] == m.symbol {
+
let symbol = file.tokens[file.working_index].contents.trim();
+
if symbol.len() < 2
+
{
+
continue;
+
}
+
if &symbol[1..] == m.symbol {
matched = true;
println!("Found a macro ({})", m.symbol);
let mut ephemeral = false;
-
if file.tokens[index].contents.starts_with('&')
-
&& file.tokens[index].origin_file != file.filename_input
+
if file.tokens[file.working_index].contents.starts_with('&')
+
&& file.tokens[file.working_index].origin_file != file.filename_input
{
println!("Skipping Ephermal macro from included file.");
ephemeral = true;
}
-
let (args, tokcount) = collect_arguments(&file.tokens[index..]);
+
let (args, tokcount) = collect_arguments(&file.tokens[file.working_index..]);
let expansion: Vec<Token>;
if ephemeral {
expansion = Vec::new();
} else {
-
expansion = (m.expand)(&file, &args);
+
expansion = (m.expand)(file, &args);
}
-
file.tokens.remove(index);
-
file.tokens.splice(index..(index + tokcount - 1), expansion);
+
file.tokens.remove(file.working_index);
+
file.tokens.splice(
+
file.working_index..(file.working_index + tokcount - 1),
+
expansion,
+
);
}
}
···
if !matched {
println!(
"Token written as a function but no such function exists \"{}\"",
-
file.tokens[index].contents.trim()
+
file.tokens[file.working_index].contents.trim()
);
}
}
-
index += 1;
+
file.working_index += 1;
}
//println!("{:?}", file.tokens);
let mut skid_output: String = "".to_string();
···
)
.unwrap();
fs::write(&file.filename_htmlout, &html_output).expect("Couldn't write html to file");
+
println!("{} written.", file.filename_htmlout);
}
+3 -1
src/types.rs
···
pub filename_skidout: String,
pub filename_htmlout: String,
pub tokens: Vec<Token>,
+
pub working_index: usize,
pub block_edges: Vec<BlockEdge>,
}
-
type MacroExpansion = fn(&InputFile, &Vec<String>) -> Vec<Token>;
+
type MacroExpansion = fn(&mut InputFile, &Vec<String>) -> Vec<Token>;
pub struct Macro<'a> {
pub symbol: &'a str,
pub expand: MacroExpansion,
···
filename_skidout: "".to_string(),
filename_htmlout: "".to_string(),
tokens: Vec::new(),
+
working_index: 0,
block_edges: Vec::new(),
}
}