bard 2.0.1

Creates PDF and HTML songbooks out of easy-to-write Markdown sources.
Documentation
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
use std::collections::HashMap;
use std::fmt;
use std::fs;
use std::io;
use std::str::FromStr;
use std::sync::{Arc, Mutex};

use handlebars::{self as hb, handlebars_helper, Handlebars, HelperDef, JsonValue, RenderError};
use image::image_dimensions;
use once_cell::sync::Lazy;
use regex::{Error as ReError, Regex};
use semver::Version;
use serde_json::Number;

use super::RenderContext;
use crate::prelude::*;
use crate::project::Format;
use crate::project::{Output, Project};
use crate::util::ImgCache;

type RegexCache = HashMap<String, Result<Regex, ReError>>;

static REGEX_CACHE: Lazy<Mutex<RegexCache>> = Lazy::new(|| Mutex::new(RegexCache::new()));

// Default templates

pub struct DefaultTemaplate {
    pub filename: &'static str,
    pub content: &'static str,
}

macro_rules! default_template {
    ($name:ident, $filename:expr) => {
        pub static $name: crate::render::template::DefaultTemaplate =
            crate::render::template::DefaultTemaplate {
                filename: $filename,
                content: include_str!(concat!("./templates/", $filename)),
            };
    };
}

// HB helpers

macro_rules! hb_err {
    ($msg:literal) => {
        RenderError::new($msg)
    };

    ($fmt:literal, $($field:expr),+) => {
        RenderError::new(format!($fmt, $($field),+))
    };

    ($e:ident, $fmt:literal, $($field:expr),+) => {
        RenderError::from_error(&format!($fmt, $($field),+), $e)
    };
}

trait HandlebarsExt {
    fn with_helper<T>(self, name: &str, helper: T) -> Self
    where
        T: HelperDef + Send + Sync + 'static;
}

impl HandlebarsExt for Handlebars<'static> {
    fn with_helper<T>(mut self, name: &str, helper: T) -> Self
    where
        T: HelperDef + Send + Sync + 'static,
    {
        self.register_helper(name, Box::new(helper));
        self
    }
}

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(),
    }
});

struct Cat<'a>(Vec<&'a JsonValue>);

impl<'a> fmt::Display for Cat<'a> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        for arg in self.0.iter() {
            match arg {
                JsonValue::Null => write!(f, "[null]")?,
                JsonValue::Bool(b) => write!(f, "{}", b)?,
                JsonValue::Number(n) => write!(f, "{}", n)?,
                JsonValue::String(s) => write!(f, "{}", s)?,
                JsonValue::Array(..) => write!(f, "[array]")?,
                JsonValue::Object(..) => write!(f, "[object]")?,
            }
        }
        Ok(())
    }
}

handlebars_helper!(hb_cat: |*args| {
    format!("{}", Cat(args))
});

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,
    cache: ImgCache,
}

impl ImgHelper {
    fn width(project: &Project, img_cache: &ImgCache) -> Self {
        let out_dir = project.settings.dir_output().to_owned();
        Self {
            out_dir,
            result_i: 0,
            name: "img_w",
            cache: img_cache.clone(),
        }
    }

    fn height(project: &Project, img_cache: &ImgCache) -> Self {
        let out_dir = project.settings.dir_output().to_owned();
        Self {
            out_dir,
            result_i: 1,
            name: "img_h",
            cache: img_cache.clone(),
        }
    }
}

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<hb::ScopedJson<'reg, 'rc>, RenderError> {
        let path: &str = h
            .param(0)
            .map(|x| x.value())
            .ok_or_else(|| hb_err!("{}: Image path not supplied", self.name))
            .and_then(|x| {
                x.as_str().ok_or_else(|| {
                    hb_err!(
                        "{}: 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) = self.cache.try_get(&pathbuf, || {
            image_dimensions(&pathbuf)
                .map_err(|e| hb_err!(e, "{}: Couldn't read image at {:?}", self.name, pathbuf))
        })?;

        let res = [w, h][self.result_i];
        Ok(hb::ScopedJson::Derived(JsonValue::from(res)))
    }
}

pub struct DpiHelper {
    dpi: f32,
    format: Format,
    name: &'static str,
}

impl DpiHelper {
    const INCH_MM: f64 = 25.4;

    pub fn new(output: &Output, name: &'static str) -> Box<Self> {
        Box::new(Self {
            dpi: output.dpi(),
            format: output.format(),
            name,
        })
    }
}

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<hb::ScopedJson<'reg, 'rc>, RenderError> {
        let value: f64 = h
            .param(0)
            .map(|x| x.value())
            .ok_or_else(|| hb_err!("{}: Input value not supplied", self.name))
            .and_then(|x| {
                x.as_f64().ok_or_else(|| {
                    hb_err!(
                        "{}: Input value not a number, it's {:?} as JSON.",
                        self.name,
                        x
                    )
                })
            })?;

        let res = match self.format {
            Format::Html => JsonValue::from((self.dpi as f64 * value).round() as u32),
            _ => JsonValue::from((value / self.dpi as f64) * Self::INCH_MM),
        };

        Ok(hb::ScopedJson::Derived(res))
    }
}

struct VersionCheckHelper {
    version: Arc<Mutex<Option<Version>>>,
}

impl VersionCheckHelper {
    fn new() -> (Self, Arc<Mutex<Option<Version>>>) {
        let version = Arc::new(Mutex::new(None));
        let this = Self {
            version: version.clone(),
        };
        (this, version)
    }
}

impl HelperDef for VersionCheckHelper {
    fn call_inner<'reg: 'rc, 'rc>(
        &self,
        h: &hb::Helper<'reg, 'rc>,
        _: &'reg Handlebars<'reg>,
        _: &'rc hb::Context,
        _: &mut hb::RenderContext<'reg, 'rc>,
    ) -> Result<hb::ScopedJson<'reg, 'rc>, RenderError> {
        let version = h
            .param(0)
            .map(|x| x.value())
            .ok_or_else(|| hb_err!("version_check: No version number supplied"))
            .and_then(|x| match x {
                JsonValue::String(s) => Ok(s.as_str()),
                _ => Err(hb_err!("version_check: Input value not a string")),
            })
            .and_then(|s| {
                Version::parse(s)
                    .map_err(|e| hb_err!(e, "version_check: Could not parse version '{}'", s))
            })?;

        *self.version.lock().unwrap() = Some(version);
        Ok(hb::ScopedJson::Derived(JsonValue::String(String::new())))
    }
}

/// Simple math helper, which can do the usual arithmetic operations on integers and floats. Tries to handle most edge-cases and provide useful error message to the user.
///
/// Usage: `{{ math 5 "+" 3 }}`, `{{ math 23.8 "/" -1.5 }}`, `{{ math "3" "*" 8.5 }}`
///
/// Supported operations:
///    - \+ addition
///    - \- subtraction
///    - \* multiplication
///    - / **decimal** division (integers are converted to floats)
///    - // integer division (both numbers must be integers)
///    - % modulo
///    - & bitwise and (integers only)
///    - | bitwise or (integers only)
///    - ^ bitwise xor (integers only)
///    - << bitwise shift left (integers only)
///    - \>> bitwise shift right (integers only)
struct MathHelper;

impl MathHelper {
    fn hb_math_int(a: i64, operation: &str, b: i64) -> Result<i64, RenderError> {
        Ok(match operation {
            "+" => a + b,
            "-" => a - b,
            "*" => a * b,
            "//" => a / b, // normal division is done using floats to make it simples for inexperienced users. For integer division, use //.
            "%" => a % b,
            "&" => a & b,
            "|" => a | b,
            "^" => a ^ b,
            "<<" => a << b,
            ">>" => a >> b,
            _ => return Err(hb_err!("math: Operation \"{}\" is not possible with integers. Available operations on integers: +, -, *, /, //, %, &, |, ^, <<, >>", operation)),
        })
    }

    fn hb_math_float(a: f64, operation: &str, b: f64) -> Result<f64, RenderError> {
        Ok(match operation {
            "+" => a + b,
            "-" => a - b,
            "*" => a * b,
            "/" => a / b,
            "%" => a % b,
            _ => return Err(hb_err!("math: Operation \"{}\" is not possible with a decimal number. Available operations: +, -, *, /, %. (Also //, |, ^, <<, >>, but only if both numbers are integers)", operation)),
        })
    }
}

impl HelperDef for MathHelper {
    fn call_inner<'reg: 'rc, 'rc>(
        &self,
        h: &hb::Helper<'reg, 'rc>,
        _: &'reg Handlebars<'reg>,
        _: &'rc hb::Context,
        _: &mut hb::RenderContext<'reg, 'rc>,
    ) -> Result<hb::ScopedJson<'reg, 'rc>, RenderError> {
        let wrong_param_count = || {
            hb_err!("math: Found {} parameters, but math helper requires 3 parameters: number, operator as a string, number. Example: {{{{ math 1 \"+\" 2.5 }}}}.", h.params().len())
        };

        let a = h.param(0).ok_or_else(wrong_param_count)?.value();
        let operation = h.param(1).ok_or_else(wrong_param_count)?.value();
        let b = h.param(2).ok_or_else(wrong_param_count)?.value();
        let operation = operation.as_str().ok_or_else(|| {
            hb_err!("math: Second argument must be a string. Example: {{ math 1 \"+\" 2 }}.")
        })?;

        let aint = a
            .as_i64()
            .or_else(|| a.as_str().and_then(|s| i64::from_str(s).ok()));
        let bint = b
            .as_i64()
            .or_else(|| b.as_str().and_then(|s| i64::from_str(s).ok()));

        // try integer arithmetics
        if let (Some(aint), Some(bint)) = (aint, bint) {
            if operation != "/" {
                // normal division is done using floats to make it simpler for inexperienced users. For integer division, use //.
                return Ok(hb::ScopedJson::Derived(JsonValue::Number(Number::from(
                    Self::hb_math_int(aint, operation, bint)?,
                ))));
            }
        };

        // try float arithmetics
        let afloat = a
            .as_f64()
            .or_else(|| a.as_str().and_then(|s| f64::from_str(s).ok()))
            .ok_or_else(|| hb_err!("math: First number is not in valid format. Valid examples: 5, -62.53. Got this: {:?}", a))?;
        let bfloat = b
            .as_f64()
            .or_else(|| b.as_str().and_then(|s| f64::from_str(s).ok()))
            .ok_or_else(|| hb_err!("math: Second number is not in valid format. Valid examples: 5, -62.53. Got this: {:?}", b))?;

        let res = Self::hb_math_float(afloat, operation, bfloat)?;
        let res = Number::from_f64(res).ok_or_else(|| {
            hb_err!(
                "math: Calculation result is {}, which cannot be converted to JSON number.",
                res
            )
        })?;
        Ok(hb::ScopedJson::Derived(JsonValue::Number(res)))
    }
}

#[derive(Debug)]
pub(crate) struct HbRender {
    pub(crate) hb: Handlebars<'static>,
    pub(crate) tpl_name: String,
    pub(crate) version: Arc<Mutex<Option<Version>>>,
}

impl HbRender {
    /// Version of the template to assume if it specifies none.
    const ASSUMED_FIRST_VERSION: Version = Version::new(1, 0, 0);

    pub(crate) fn new(
        project: &Project,
        output: &Output,
        default: &DefaultTemaplate,
        img_cache: &ImgCache,
    ) -> Result<Self> {
        let (version_helper, version) = VersionCheckHelper::new();
        let mut hb = Handlebars::new()
            .with_helper("eq", hb_eq)
            .with_helper("contains", hb_contains)
            .with_helper("cat", hb_cat)
            .with_helper("default", hb_default)
            .with_helper("matches", hb_matches)
            .with_helper("math", MathHelper)
            .with_helper("img_w", ImgHelper::width(project, img_cache))
            .with_helper("img_h", ImgHelper::height(project, img_cache))
            .with_helper("version_check", version_helper);

        let tpl_name = output
            .template
            .as_ref()
            .map(|t| t.to_string_lossy().to_string())
            .unwrap_or_else(|| default.filename.to_string());

        if let Some(template) = output.template.as_ref() {
            if template.exists() {
                hb.register_template_file(&tpl_name, template)
                    .with_context(|| format!("Error in template file {:?}", template))?;
            } else {
                let parent = template.parent().unwrap(); // The temaplate should've been resolved as absolute in Project
                fs::create_dir_all(parent)
                    .and_then(|_| fs::write(template, default.content.as_bytes()))
                    .with_context(|| {
                        format!("Error writing default template to file: {:?}", template)
                    })?;

                hb.register_template_string(&tpl_name, default.content)
                    .expect("Internal error: Could not load default template");
            }
        } else {
            hb.register_template_string(&tpl_name, default.content)
                .expect("Internal error: Could not load default template");
        }

        // Render with no data to an IO Sink.
        // This will certainly fail, but if the version_check() helper is used on top
        // of the template, we will get the version in self.version.
        let _ = hb.render_to_write(&tpl_name, &(), io::sink());

        Ok(Self {
            hb,
            tpl_name,
            version,
        })
    }

    pub(crate) fn render(&self, output: &Path, context: RenderContext) -> Result<()> {
        let rendered = self.hb.render(&self.tpl_name, &context)?;

        fs::write(output, rendered.as_bytes())
            .with_context(|| format!("Error writing output file: {:?}", output))?;

        Ok(())
    }

    pub(crate) fn version(&self) -> Option<Version> {
        Some(
            self.version
                .lock()
                .unwrap()
                .clone()
                .unwrap_or(Self::ASSUMED_FIRST_VERSION),
        )
    }
}

#[cfg(test)]
mod tests;