1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
use super::*;
use crate::{
ResourceDir,
fs::hist::{HistoryAction, HistoryRef},
templates,
};
use std::{fs, path::PathBuf};
/// String for embedding into files
const LUA_WEFTER_TEMPLATE_EMBEDDING_POINT: &str = "@wefter.embed";
// Create a table for the 'template' submodule
pub fn module(l: &Lua, profile: ResourceDir, history: HistoryRef) -> Result<WefterModuleTable<'_>> {
Ok(vec![
("get", {
let profile = profile.clone();
l.create_function(move |lua, (template, params): (PathBuf, Table)| {
let template = profile.build_template_path(template)?;
let params = serialize_table(lua, params)?;
log::debug!("[wefter.template.get] template {:?}", template);
let rendered = templates::render_file(template, params)?;
Ok(rendered)
})?
}),
("create", {
let profile = profile.clone();
let history = history.clone();
l.create_function(
move |lua, (dst, template, params): (PathBuf, PathBuf, Table)| {
let template = profile.build_template_path(template)?;
let params = serialize_table(lua, params)?;
log::debug!(
"[wefter.template.create] Creating file {:?} with template file {:?}",
dst,
template
);
// Get file contents
let rendered = templates::render_file(template, params)?;
// Create parent directory
if let Some(parent) = dst.as_path().parent()
&& !parent.exists()
{
fs::create_dir_all(parent)?;
}
// Create file
fs::write(dst.clone(), rendered)?;
history.borrow_mut().push(HistoryAction::CreateFile(dst));
Ok(())
},
)?
}),
("create_inline", {
let history = history.clone();
l.create_function(
move |lua, (dst, template_str, params): (PathBuf, String, Table)| {
let params = serialize_table(lua, params)?;
log::debug!(
"[wefter.template.create] Creating file {:?} with inline template",
dst,
);
// Get file contents
let rendered = templates::render_inline(template_str, params)?;
// Create parent directory
if let Some(parent) = dst.as_path().parent()
&& !parent.exists()
{
fs::create_dir_all(parent)?;
}
// Create file
fs::write(dst.clone(), rendered)?;
history.borrow_mut().push(HistoryAction::CreateFile(dst));
Ok(())
},
)?
}),
("embed", {
let profile = profile.clone();
let history = history.clone();
l.create_function(
move |lua,
(dst, ipoint, template, params): (
PathBuf,
Option<String>,
PathBuf,
Table,
)| {
// Insertion point builder
let lookup = match ipoint {
Some(e) => format!("{}:{}", LUA_WEFTER_TEMPLATE_EMBEDDING_POINT, e),
None => format!("{}", LUA_WEFTER_TEMPLATE_EMBEDDING_POINT),
};
let template = profile.build_template_path(template)?;
let params = serialize_table(lua, params)?;
log::debug!(
"[wefter.template.embed] template file {:?} into {:?} at {:?}",
template,
dst,
lookup
);
// Add contents to file
templates::embed_file(dst.clone(), lookup.clone(), template, params)?;
history
.borrow_mut()
.push(HistoryAction::ModifyFile(dst, lookup));
Ok(())
},
)?
}),
("embed_inline", {
let history = history.clone();
l.create_function(
move |lua,
(dst, ipoint, template_str, params): (
PathBuf,
Option<String>,
String,
Table,
)| {
// Insertion point builder
let lookup = match ipoint {
Some(e) => format!("{}:{}", LUA_WEFTER_TEMPLATE_EMBEDDING_POINT, e),
None => format!("{}", LUA_WEFTER_TEMPLATE_EMBEDDING_POINT),
};
let params = serialize_table(lua, params)?;
log::debug!(
"[wefter.template.embed] inline template into {:?} at {:?}",
dst,
lookup
);
// Add contents to file
templates::embed_inline(dst.clone(), lookup.clone(), template_str, params)?;
history
.borrow_mut()
.push(HistoryAction::ModifyFile(dst, lookup));
Ok(())
},
)?
}),
/* @wefter.embed:template */
])
}