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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
use std::fs;
use std::collections::HashMap;
use std::path::{Path, PathBuf};
use std::sync::Mutex;
use handlebars::{self as hb, Handlebars, HelperDef, JsonValue, handlebars_helper};
use regex::{Regex, Error as ReError};
use image::image_dimensions;
use lazy_static::lazy_static;
use serde::Serialize;
use crate::book::Song;
use crate::project::{Metadata, Output, Project};
use crate::util::PathBufExt;
use crate::{PROGRAM_META, ProgramMeta};
use crate::error::*;
use super::Render;
type RegexCache = HashMap<String, Result<Regex, ReError>>;
lazy_static! {
static ref REGEX_CACHE: Mutex<RegexCache> = Mutex::new(RegexCache::new());
}
pub trait DefaultTemaplate {
const TPL_NAME: &'static str;
const TPL_CONTENT: &'static str;
}
fn latex_escape(input: &str, pre_spaces: bool) -> String {
let mut res = String::with_capacity(input.len());
for c in input.chars() {
match c {
' ' if pre_spaces => res.push('~'),
'&' | '%' | '$' | '#' | '_' | '{' | '}' => {
res.push('\\');
res.push(c);
}
'[' => res.push_str("{\\lbrack}"),
']' => res.push_str("{\\rbrack}"),
'~' => res.push_str("{\\textasciitilde}"),
'^' => res.push_str("{\\textasciicircum}"),
'\\' => res.push_str("{\\textbackslash}"),
c => res.push(c),
}
}
res
}
fn hb_latex_escape(input: &str) -> String {
latex_escape(input, false)
}
handlebars_helper!(hb_eq: |v1: Json, v2: Json| {
v1 == v2
});
handlebars_helper!(hb_contains: |obj: object, key: str| {
obj.contains_key(key)
});
handlebars_helper!(hb_default: |value: Json, def: Json| {
match value {
JsonValue::Null => def.clone(),
other => other.clone(),
}
});
handlebars_helper!(hb_pre: |input: str| {
latex_escape(input, true)
});
handlebars_helper!(hb_matches: |value: str, regex: str| {
let mut cache = REGEX_CACHE.lock().unwrap();
if !cache.contains_key(regex) {
let res = Regex::new(regex);
if res.is_err() {
eprintln!("Warning: `matches` helper: Invalid regular expression: `{}`", regex);
}
cache.insert(regex.into(), res);
}
match cache.get(regex) {
Some(Ok(re)) => re.is_match(value),
_ => false,
}
});
struct ImgHelper {
out_dir: PathBuf,
result_i: usize,
name: &'static str,
}
impl ImgHelper {
fn width(project: &Project) -> Box<Self> {
let out_dir = project.settings.dir_output().to_owned();
Box::new(Self {
out_dir,
result_i: 0,
name: "img_w",
})
}
fn height(project: &Project) -> Box<Self> {
let out_dir = project.settings.dir_output().to_owned();
Box::new(Self {
out_dir,
result_i: 1,
name: "img_h",
})
}
}
impl HelperDef for ImgHelper {
fn call_inner<'reg: 'rc, 'rc>(
&self, h: &hb::Helper<'reg, 'rc>, _: &'reg Handlebars<'reg>, _: &'rc hb::Context,
_: &mut hb::RenderContext<'reg, 'rc>,
) -> Result<Option<hb::ScopedJson<'reg, 'rc>>, hb::RenderError> {
let path: &str = h
.param(0)
.map(|x| x.value())
.ok_or_else(|| hb::RenderError::new(format!("{}: Image path not supplied", self.name)))
.and_then(|x| {
x.as_str().ok_or_else(|| {
hb::RenderError::new(&format!(
"{}: Image path not a string, it's {:?} as JSON.",
self.name, x,
))
})
})?;
let pathbuf = Path::new(&path).to_owned().resolved(&self.out_dir);
let (w, h) = image_dimensions(&pathbuf).map_err(|e| {
hb::RenderError::new(&format!(
"{}: Couldn't read image at `{}`: {}",
self.name,
pathbuf.display(),
e
))
})?;
let res = [w, h][self.result_i];
Ok(Some(hb::ScopedJson::Derived(JsonValue::from(res))))
}
}
struct DpiHelper {
dpi: f64,
}
impl DpiHelper {
const INCH_MM: f64 = 25.4;
fn new(output: &Output) -> Box<Self> {
Box::new(Self { dpi: output.dpi() })
}
}
impl HelperDef for DpiHelper {
fn call_inner<'reg: 'rc, 'rc>(
&self, h: &hb::Helper<'reg, 'rc>, _: &'reg Handlebars<'reg>, _: &'rc hb::Context,
_: &mut hb::RenderContext<'reg, 'rc>,
) -> Result<Option<hb::ScopedJson<'reg, 'rc>>, hb::RenderError> {
let value: f64 = h
.param(0)
.map(|x| x.value())
.ok_or_else(|| hb::RenderError::new("px2mm: Input value not supplied"))
.and_then(|x| {
x.as_f64().ok_or_else(|| {
hb::RenderError::new(&format!(
"px2mm: Input value not a number, it's {:?} as JSON.",
x,
))
})
})?;
let res = (value / self.dpi) * Self::INCH_MM;
Ok(Some(hb::ScopedJson::Derived(JsonValue::from(res))))
}
}
#[derive(Serialize, Debug)]
struct HbContext<'a> {
book: &'a Metadata,
songs: &'a [Song],
output: &'a Metadata,
program: &'a ProgramMeta,
}
#[derive(Debug)]
struct HbRender<'a> {
hb: Handlebars<'static>,
tpl_name: String,
project: &'a Project,
output: &'a Output,
}
impl<'a> HbRender<'a> {
fn new<DT: DefaultTemaplate>(project: &'a Project, output: &'a Output) -> Result<Self> {
let mut hb = Handlebars::new();
hb.register_helper("eq", Box::new(hb_eq));
hb.register_helper("contains", Box::new(hb_contains));
hb.register_helper("default", Box::new(hb_default));
hb.register_helper("matches", Box::new(hb_matches));
hb.register_helper("px2mm", DpiHelper::new(output));
hb.register_helper("img_w", ImgHelper::width(project));
hb.register_helper("img_h", ImgHelper::height(project));
let tpl_name = if let Some(template) = output.template.as_ref() {
let tpl_name = template.to_str().unwrap().to_string();
if template.exists() {
hb.register_template_file(&tpl_name, &template)
.with_context(|| format!("Error in template file `{}`", template.display()))?;
} else {
let parent = template.parent().unwrap();
fs::create_dir_all(parent)
.and_then(|_| fs::write(&template, DT::TPL_CONTENT.as_bytes()))
.with_context(|| {
format!(
"Error writing default template to file: `{}`",
template.display()
)
})?;
hb.register_template_string(&tpl_name, DT::TPL_CONTENT)
.expect("Internal error: Could not load default template");
}
tpl_name
} else {
hb.register_template_string(DT::TPL_NAME, DT::TPL_CONTENT)
.expect("Internal error: Could not load default template");
DT::TPL_NAME.to_string()
};
Ok(Self {
hb,
tpl_name,
project,
output,
})
}
fn render(&self) -> Result<&'a Output> {
let context = HbContext {
book: self.project.metadata(),
songs: self.project.songs(),
output: &self.output.metadata,
program: &PROGRAM_META,
};
let html = self.hb.render(&self.tpl_name, &context)?;
fs::write(&self.output.file, html.as_bytes()).with_context(|| {
format!(
"Error writing output file: `{}`",
self.output.file.display()
)
})?;
Ok(self.output)
}
}
pub struct RHtml;
impl DefaultTemaplate for RHtml {
const TPL_NAME: &'static str = "html.hbs";
const TPL_CONTENT: &'static str = include_str!("../../default/templates/html.hbs");
}
impl Render for RHtml {
fn render<'a>(project: &'a Project, output: &'a Output) -> Result<&'a Output> {
let render = HbRender::new::<Self>(project, output)?;
render.render()
}
}
pub struct RTex;
impl DefaultTemaplate for RTex {
const TPL_NAME: &'static str = "pdf.hbs";
const TPL_CONTENT: &'static str = include_str!("../../default/templates/pdf.hbs");
}
impl Render for RTex {
fn render<'a>(project: &'a Project, output: &'a Output) -> Result<&'a Output> {
let mut render = HbRender::new::<Self>(project, output)?;
render.hb.register_escape_fn(hb_latex_escape);
render.hb.register_helper("pre", Box::new(hb_pre));
render.render()
}
}
pub struct RHovorka;
impl DefaultTemaplate for RHovorka {
const TPL_NAME: &'static str = "hovorka.hbs";
const TPL_CONTENT: &'static str = include_str!("../../example/templates/hovorka.hbs");
}
impl Render for RHovorka {
fn render<'a>(project: &'a Project, output: &'a Output) -> Result<&'a Output> {
let render = HbRender::new::<Self>(project, output)?;
render.render()
}
}