Tholp's bespoke website generator
1// This file for implementations of short macros, im qualifying that as less than 30ish lines
2use std::process::exit;
3
4use chrono::Local;
5
6use crate::{
7 console::{error_skid, reminder_skid, warn_skid},
8 project::{Indexing, Project},
9 stringtools::split_to_tokens,
10 types::{SkidContext, Token},
11};
12
13pub fn macro_time(
14 origin_index: usize,
15 origin_line: usize,
16 context: &mut Project,
17 _skid_context: &mut SkidContext,
18 args: &Vec<String>,
19 _scope: &[Token],
20) -> Vec<Token> {
21 let t = Local::now();
22 let fmt = if args.len() == 0 {
23 &"%+".to_string() // RFC-3339
24 } else {
25 &args[0]
26 };
27
28 return split_to_tokens(t.format(fmt).to_string(), origin_index);
29}
30
31pub fn macro_filename(
32 origin_index: usize,
33 _origin_line: usize,
34 proj_context: &mut Project,
35 _skid_context: &mut SkidContext,
36 _args: &Vec<String>,
37 _scope: &[Token],
38) -> Vec<Token> {
39 return split_to_tokens(
40 proj_context
41 .file_for_index(origin_index)
42 .unwrap()
43 .to_str()
44 .unwrap()
45 .into(),
46 origin_index,
47 );
48}
49
50pub fn macro_output_filename(
51 origin_index: usize,
52 origin_line: usize,
53 proj_context: &mut Project,
54 _skid_context: &mut SkidContext,
55 args: &Vec<String>,
56 _scope: &[Token],
57) -> Vec<Token> {
58 let mut in_filepath = proj_context.input_folder.clone();
59 if args.len() == 0 {
60 in_filepath.push(proj_context.file_for_index(origin_index).unwrap());
61 } else {
62 in_filepath.push(&args[0]);
63 }
64
65 if in_filepath.exists() {
66 for g in &proj_context.filegroups {
67 if !g.process {
68 continue;
69 }
70 for f in &g.files {
71 if f.file_input == in_filepath {
72 let stripped = f
73 .file_out
74 .strip_prefix(&proj_context.output_folder)
75 .unwrap();
76 return split_to_tokens(stripped.to_str().unwrap().into(), origin_index);
77 }
78 }
79 }
80 }
81 warn_skid(
82 proj_context,
83 origin_index,
84 origin_line,
85 &format!(
86 "output_filename given a file with no matching output file ({:?}), returning empty",
87 in_filepath
88 ),
89 );
90 Vec::new()
91}
92
93pub fn macro_filename_canonical(
94 origin_index: usize,
95 _origin_line: usize,
96 context: &mut Project,
97 _skid_context: &mut SkidContext,
98 _args: &Vec<String>,
99 _scope: &[Token],
100) -> Vec<Token> {
101 return split_to_tokens(
102 context
103 .file_for_index_canonical(origin_index)
104 .unwrap()
105 .to_str()
106 .unwrap()
107 .into(),
108 origin_index,
109 );
110}
111
112pub fn macro_reminder(
113 origin_index: usize,
114 origin_line: usize,
115 context: &mut Project,
116 _skid_context: &mut SkidContext,
117 args: &Vec<String>,
118 _scope: &[Token],
119) -> Vec<Token> {
120 reminder_skid(context, origin_index, origin_line, &args[0]);
121 Vec::new()
122}