Tholp's bespoke website generator

Add clear macro

Tholp1 2d1a770b 86bf06e7

+51 -25
+10
src/macros/clear.rs
··· 1 + use crate::{ 2 + stringtools::{split_keep_delimiters, strings_to_tokens}, 3 + types::{InputFile, Token}, 4 + }; 5 + 6 + pub fn macro_clear(_file: &mut InputFile, _args: &Vec<String>) -> Vec<Token> { 7 + _file.tokens = _file.tokens.split_off(_file.working_index); 8 + _file.working_index = 0; 9 + return Vec::new(); 10 + }
+1 -1
src/macros/include.rs
··· 5 5 types::{InputFile, Token}, 6 6 }; 7 7 8 - pub fn macro_include(_file: &InputFile, args: &Vec<String>) -> Vec<Token> { 8 + pub fn macro_include(_file: &mut InputFile, args: &Vec<String>) -> Vec<Token> { 9 9 print!("\nargs: {:?}\n", args); 10 10 let mut output = fs::read_to_string(args[0].clone()).expect("File unreadable or missing"); 11 11 if output.ends_with("\n") {
+12 -5
src/macros/mod.rs
··· 1 + pub mod clear; 1 2 pub mod include; 2 3 use super::types::Macro; 3 4 5 + use clear::macro_clear; 4 6 use include::macro_include; 5 7 6 - pub static MACRO_LIST: [Macro<'_>; 1] = [Macro { 7 - symbol: "include", 8 - expand: macro_include, 9 - //always_ephemeral: false, 10 - }]; 8 + pub static MACRO_LIST: [Macro<'_>; 2] = [ 9 + Macro { 10 + symbol: "include", 11 + expand: macro_include, 12 + }, 13 + Macro { 14 + symbol: "clear", 15 + expand: macro_clear, 16 + }, 17 + ];
+25 -18
src/main.rs
··· 3 3 mod stringtools; 4 4 mod types; 5 5 6 - use macros::{ 7 - include::{self, macro_include}, 8 - MACRO_LIST, 9 - }; 6 + use macros::MACRO_LIST; 10 7 use markdown::{to_html_with_options, CompileOptions, Options}; 11 8 use std::{ 12 9 env, ··· 44 41 //file.tokens = strings_to_tokens(split_keep_delimiters(contents), file.filename_input.clone()); 45 42 file.tokens = split_to_tokens(contents, file.filename_input.clone()); 46 43 47 - let mut index = 0; 48 - 49 - while index < file.tokens.len() { 44 + while file.working_index < file.tokens.len() { 50 45 //look for macros or blocks 51 46 //println!(">\"{}\"<", file.tokens[index].contents); 52 47 53 - if file.tokens[index].contents.starts_with(['!', '&']) { 54 - let mut matched = false; 48 + if file.tokens[file.working_index] 49 + .contents 50 + .starts_with(['!', '&']) 51 + { 52 + let mut matched: bool = false; 55 53 56 54 for m in &MACRO_LIST { 57 - if &file.tokens[index].contents.trim()[1..] == m.symbol { 55 + let symbol = file.tokens[file.working_index].contents.trim(); 56 + if symbol.len() < 2 57 + { 58 + continue; 59 + } 60 + if &symbol[1..] == m.symbol { 58 61 matched = true; 59 62 println!("Found a macro ({})", m.symbol); 60 63 let mut ephemeral = false; 61 - if file.tokens[index].contents.starts_with('&') 62 - && file.tokens[index].origin_file != file.filename_input 64 + if file.tokens[file.working_index].contents.starts_with('&') 65 + && file.tokens[file.working_index].origin_file != file.filename_input 63 66 { 64 67 println!("Skipping Ephermal macro from included file."); 65 68 ephemeral = true; 66 69 } 67 70 68 - let (args, tokcount) = collect_arguments(&file.tokens[index..]); 71 + let (args, tokcount) = collect_arguments(&file.tokens[file.working_index..]); 69 72 let expansion: Vec<Token>; 70 73 if ephemeral { 71 74 expansion = Vec::new(); 72 75 } else { 73 - expansion = (m.expand)(&file, &args); 76 + expansion = (m.expand)(file, &args); 74 77 } 75 - file.tokens.remove(index); 76 - file.tokens.splice(index..(index + tokcount - 1), expansion); 78 + file.tokens.remove(file.working_index); 79 + file.tokens.splice( 80 + file.working_index..(file.working_index + tokcount - 1), 81 + expansion, 82 + ); 77 83 } 78 84 } 79 85 ··· 82 88 if !matched { 83 89 println!( 84 90 "Token written as a function but no such function exists \"{}\"", 85 - file.tokens[index].contents.trim() 91 + file.tokens[file.working_index].contents.trim() 86 92 ); 87 93 } 88 94 } 89 95 90 - index += 1; 96 + file.working_index += 1; 91 97 } 92 98 //println!("{:?}", file.tokens); 93 99 let mut skid_output: String = "".to_string(); ··· 110 116 ) 111 117 .unwrap(); 112 118 fs::write(&file.filename_htmlout, &html_output).expect("Couldn't write html to file"); 119 + println!("{} written.", file.filename_htmlout); 113 120 }
+3 -1
src/types.rs
··· 24 24 pub filename_skidout: String, 25 25 pub filename_htmlout: String, 26 26 pub tokens: Vec<Token>, 27 + pub working_index: usize, 27 28 pub block_edges: Vec<BlockEdge>, 28 29 } 29 30 30 - type MacroExpansion = fn(&InputFile, &Vec<String>) -> Vec<Token>; 31 + type MacroExpansion = fn(&mut InputFile, &Vec<String>) -> Vec<Token>; 31 32 pub struct Macro<'a> { 32 33 pub symbol: &'a str, 33 34 pub expand: MacroExpansion, ··· 41 42 filename_skidout: "".to_string(), 42 43 filename_htmlout: "".to_string(), 43 44 tokens: Vec::new(), 45 + working_index: 0, 44 46 block_edges: Vec::new(), 45 47 } 46 48 }