template-fragments 0.2.0

template-fragments for jinja-like engines
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
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
//! Pre-process Jinja-like templates with fragment tags
//!
//! `template_fragments` offers a way to split a template that is annotated with
//! fragment tags (`{% fragment NAME %}`, `{% endfragment %}`) into fragments.
//! For example:
//!
//! ```html
//! <body>
//! # Header
//! {% fragment items %}
//! {% for item in items %}
//!     {% fragment item %}
//!         <div>{{ item }}</div>
//!     {% endfragment %}
//! {% endfor %}
//! {% endfragment %}
//! <body>
//! ```
//!
//! This template defines three fragments:
//!
//! - `""`: the whole template without any fragment markers
//! - `"items"`: the template looping over all items
//! - `"item"`: the innermost div
//!
//! `template_fragments` offers two ways to pre-process such a template:
//!
//! - [filter_template]: given a fragment name, only return those parts of the
//!   template that belong to the fragment. This function is designed to be used
//!   when templates are requested dynamically
//! - [split_templates]: split a template into all its fragments. This function
//!   is designed to be used when to extract all templates once at application
//!   startup
//!
//! # Syntax
//!
//! - Fragments start with `{% fragment NAMES... %}` or `{% fragment-block NAMES
//!   %}`
//! - `{% fragment-block NAME %}` and `{% endfragment-block %}` define fragment
//!   blocks: they are rendered as a block, if the fragment is included. This is
//!   equivalent to wrapping a block with a fragment of the same name.
//! - Fragments end with `{% endfragment %}` or `{% endfragment-block %}`
//! - Fragments can occur multiple times in the document
//! - Multiple fragments can be started in a single tag by using multiple
//!   whitespace separated names in the start tag
//! - Fragment tags must be contained in a single line and there must not be any
//!   other non-whitespace content on the same line
//! - Fragment names can contain any alphanumeric character and `'-'`, `'_'`.
//!
//! # Example using `minijinja`
//!
//! One way to use fragment tags with  `minijinja` is to build a template source
//! with the split templates at application start up like this
//!
//! ```
//! # mod minijinja {
//! #   pub struct Source;
//! #   impl Source {
//! #     pub fn new() -> Self { Source }
//! #     pub fn add_template(
//! #       &mut self,
//! #       path: String,
//! #       fragment: &str,
//! #     ) -> Result<(), template_fragments::ErrorWithLine> {
//! #       Ok(())
//! #     }
//! #   }
//! # }
//! # fn main() -> Result<(), template_fragments::ErrorWithLine> {
//! use template_fragments::{split_templates, join_path};
//!
//! let mut source = minijinja::Source::new();
//!
//! for (path, template) in [("index.html", include_str!("../examples/templates/index.html"))] {
//!     for (fragment_name, template_fragment) in split_templates(template)? {
//!         source.add_template(join_path(path, &fragment_name), &template_fragment)?;
//!     }
//! }
//! # Ok(())
//! # }
//! ```
//!
//! Note the different fragments can be rendered by requesting the relevant
//! template, e.g., `env.get_template("index.html")` or
//! `env.get_template("index.html#fragment")`.
//!
use std::collections::{HashMap, HashSet};

#[cfg(test)]
mod test;

const DEFAULT_TAG_MARKERS: (&str, &str) = ("{%", "%}");

/// Split a template path with optional fragment into the path and fragment
///
/// If no fragment is found, the fragment will be a empty string
///
/// ```rust
/// # use template_fragments::split_path;
/// #
/// assert_eq!(split_path("index.html"), ("index.html", ""));
/// assert_eq!(split_path("index.html#child"), ("index.html", "child"));
///
/// // whitespace is normalized
/// assert_eq!(split_path("  index.html  "), ("index.html", ""));
/// assert_eq!(split_path("  index.html  #  child  "), ("index.html", "child"));
/// ```
pub fn split_path(path: &str) -> (&str, &str) {
    if let Some((path, fragment)) = path.rsplit_once('#') {
        (path.trim(), fragment.trim())
    } else {
        (path.trim(), "")
    }
}

/// Join a path with a fragment (omitting empty fragments)
///
/// ```rust
/// # use template_fragments::join_path;
/// #
/// assert_eq!(join_path("index.html", ""), "index.html");
/// assert_eq!(join_path("index.html", "child"), "index.html#child");
///
/// // whitespace is normalized
/// assert_eq!(join_path("  index.html  ", "  "), "index.html");
/// assert_eq!(join_path("  index.html  ", "  child  "), "index.html#child");
/// ```
pub fn join_path(path: &str, fragment: &str) -> String {
    let path = path.trim();
    let fragment = fragment.trim();

    if fragment.is_empty() {
        path.to_string()
    } else {
        format!("{path}#{fragment}")
    }
}

/// Process the template and return all parts for the given fragment
///
/// To obtain the base template use an empty string for the fragment.
///
/// ```rust
/// # use template_fragments::filter_template;
/// let source = concat!(
///     "<body>\n",
///     "  {% fragment item %}\n",
///     "    <div>{{ item }}</div>\n",
///     "  {% endfragment %}\n",
///     "<body>\n",
/// );
///
/// assert_eq!(
///     filter_template(source, "").unwrap(),
///     concat!(
///         "<body>\n",
///         "    <div>{{ item }}</div>\n",
///         "<body>\n",
///     ),
/// );
///
/// assert_eq!(
///     filter_template(source, "item").unwrap(),
///     "    <div>{{ item }}</div>\n",
/// );
/// ```
///
pub fn filter_template(src: &str, fragment: &str) -> Result<String, ErrorWithLine> {
    let mut stack: FragmentStack<'_> = Default::default();
    let mut res = String::new();
    let mut last_line_idx = 0;

    for (line_idx, line) in iterate_with_endings(src).enumerate() {
        last_line_idx = line_idx;

        match parse_fragment_tag(line, DEFAULT_TAG_MARKERS).map_err(|err| err.at(line_idx))? {
            Some(Tag::Start(tag)) => stack.push(tag.fragments).map_err(|err| err.at(line_idx))?,
            Some(Tag::End(_)) => {
                stack.pop().map_err(|err| err.at(line_idx))?;
            }
            Some(Tag::StartBlock(tag)) => {
                stack
                    .push(HashSet::from([tag.fragment]))
                    .map_err(|err| err.at(line_idx))?;
                let line = format!(
                    "{}{{% block {} %}}{}",
                    tag.prefix,
                    tag.fragment,
                    get_ending(line)
                );
                if stack.is_active(fragment) {
                    res.push_str(&line);
                }
            }
            Some(Tag::EndBlock(tag)) => {
                let active = stack.pop().map_err(|err| err.at(line_idx))?;
                let line = format!("{}{{% endblock %}}{}", tag.prefix, get_ending(line));
                if active.contains(fragment) {
                    res.push_str(&line);
                }
            }
            None => {
                if stack.is_active(fragment) {
                    res.push_str(line);
                }
            }
        }
    }
    stack.done().map_err(|err| err.at(last_line_idx))?;

    Ok(res)
}

/// Split the template into all fragments available
///
/// The base template is included as the fragment `""`.
///
/// ```rust
/// # use template_fragments::split_templates;
/// let source = concat!(
///     "<body>\n",
///     "  {% fragment item %}\n",
///     "    <div>{{ item }}</div>\n",
///     "  {% endfragment %}\n",
///     "<body>\n",
/// );
/// let templates = split_templates(source).unwrap();
///
/// assert_eq!(
///     templates[""],
///     concat!(
///         "<body>\n",
///         "    <div>{{ item }}</div>\n",
///         "<body>\n",
///     ),
/// );
///
/// assert_eq!(
///     templates["item"],
///     "    <div>{{ item }}</div>\n",
/// );
/// ```
pub fn split_templates(src: &str) -> Result<HashMap<String, String>, ErrorWithLine> {
    let mut stack: FragmentStack<'_> = Default::default();
    let mut res: HashMap<String, String> = Default::default();
    let mut last_line_idx = 0;

    for (line_idx, line) in iterate_with_endings(src).enumerate() {
        last_line_idx = line_idx;

        match parse_fragment_tag(line, DEFAULT_TAG_MARKERS).map_err(|err| err.at(line_idx))? {
            Some(Tag::Start(tag)) => stack.push(tag.fragments).map_err(|err| err.at(line_idx))?,
            Some(Tag::End(_)) => {
                stack.pop().map_err(|err| err.at(line_idx))?;
            }
            Some(Tag::StartBlock(tag)) => {
                stack
                    .push(HashSet::from([tag.fragment]))
                    .map_err(|err| err.at(line_idx))?;
                let line = format!(
                    "{}{{% block {} %}}{}",
                    tag.prefix,
                    tag.fragment,
                    get_ending(line)
                );
                for fragment in &stack.active_fragments {
                    push_line(&mut res, fragment, &line);
                }
            }
            Some(Tag::EndBlock(tag)) => {
                let fragments = stack.pop().map_err(|err| err.at(line_idx))?;
                let line = format!("{}{{% endblock %}}{}", tag.prefix, get_ending(line));

                for fragment in fragments {
                    push_line(&mut res, fragment, &line);
                }
            }
            None => {
                for fragment in &stack.active_fragments {
                    push_line(&mut res, fragment, line);
                }
            }
        }
    }
    stack.done().map_err(|err| err.at(last_line_idx))?;

    Ok(res)
}

fn push_line(res: &mut HashMap<String, String>, fragment: &str, line: &str) {
    if let Some(target) = res.get_mut(fragment) {
        target.push_str(line);
    } else {
        res.insert(fragment.to_owned(), line.to_owned());
    }
}

fn get_ending(line: &str) -> &str {
    if line.ends_with("\r\n") {
        "\r\n"
    } else if line.ends_with('\n') {
        "\n"
    } else {
        ""
    }
}

#[derive(Debug)]
struct FragmentStack<'a> {
    stack: Vec<HashSet<&'a str>>,
    active_fragments: HashSet<&'a str>,
}

impl<'a> std::default::Default for FragmentStack<'a> {
    fn default() -> Self {
        Self {
            stack: Vec::new(),
            active_fragments: HashSet::from([""]),
        }
    }
}

impl<'a> FragmentStack<'a> {
    /// Add new fragments to the currently active fragments
    fn push(&mut self, fragments: HashSet<&'a str>) -> Result<(), Error> {
        let mut reentrant_fragments = Vec::new();

        for &fragment in &fragments {
            let not_seen = self.active_fragments.insert(fragment);
            if !not_seen {
                reentrant_fragments.push(fragment);
            }
        }
        if !reentrant_fragments.is_empty() {
            return Err(Error::ReentrantFragment(sorted_fragments(
                reentrant_fragments,
            )));
        }

        self.stack.push(fragments);
        Ok(())
    }

    /// Pop the last addeed fragments and return the active fragments before
    /// this op
    fn pop(&mut self) -> Result<HashSet<&'a str>, Error> {
        let fragments = self.active_fragments.clone();
        for fragment in self.stack.pop().ok_or(Error::UnbalancedEndTag)? {
            self.active_fragments.remove(fragment);
        }

        Ok(fragments)
    }

    fn done(&self) -> Result<(), Error> {
        if !self.stack.is_empty() {
            let fragments: HashSet<&str> = self.stack.iter().flatten().copied().collect();
            Err(Error::UnclosedTag(sorted_fragments(fragments)))
        } else {
            Ok(())
        }
    }

    fn is_active(&self, fragment: &str) -> bool {
        self.active_fragments.contains(fragment)
    }
}

fn iterate_with_endings(mut s: &str) -> impl Iterator<Item = &str> {
    std::iter::from_fn(move || {
        let res;
        match s.find('\n') {
            Some(new_line_idx) => {
                let split_idx = new_line_idx + '\n'.len_utf8();
                res = Some(&s[..split_idx]);
                s = &s[split_idx..];
            }
            None if !s.is_empty() => {
                res = Some(s);
                s = "";
            }
            None => {
                res = None;
            }
        }
        res
    })
}

#[derive(Debug, Clone, PartialEq, Eq)]
enum Tag<'a> {
    Start(StartTag<'a>),
    End(EndTag),
    StartBlock(StartBlockTag<'a>),
    EndBlock(EndBlockTag<'a>),
}

#[derive(Debug, Clone, PartialEq, Eq)]
struct StartTag<'a> {
    fragments: HashSet<&'a str>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
struct StartBlockTag<'a> {
    prefix: &'a str,
    fragment: &'a str,
}

#[derive(Debug, Clone, PartialEq, Eq)]
struct EndBlockTag<'a> {
    prefix: &'a str,
}

#[derive(Debug, Clone, PartialEq, Eq)]
struct EndTag;

fn parse_fragment_tag<'l>(
    line: &'l str,
    tag_markers: (&str, &str),
) -> Result<Option<Tag<'l>>, Error> {
    let parts = match parse_base(line, tag_markers) {
        Some(parts) => parts,
        None => return Ok(None),
    };

    if !parts.head.trim().is_empty() {
        return Err(Error::LeadingContent(parts.head.to_owned()));
    }

    if !parts.tail.trim().is_empty() {
        return Err(Error::TrailingContent(parts.tail.to_owned()));
    }

    match parts.fragment_type {
        FragmentType::Start | FragmentType::BlockStart => {
            let data = parts.data.trim();
            if data.is_empty() {
                return Err(Error::StartTagWithoutData);
            }

            let block = matches!(parts.fragment_type, FragmentType::BlockStart);

            let fragments: HashSet<&str> = data.split_whitespace().collect();

            let mut invalid_fragments = Vec::new();
            for &fragment in &fragments {
                if !is_valid_fragment_name(fragment) {
                    invalid_fragments.push(fragment);
                }
            }
            if !invalid_fragments.is_empty() {
                return Err(Error::InvalidFragmentName(sorted_fragments(
                    invalid_fragments,
                )));
            }

            if !block {
                Ok(Some(Tag::Start(StartTag { fragments })))
            } else {
                if fragments.len() > 1 {
                    return Err(Error::MultipleNamesBlock(sorted_fragments(fragments)));
                } else if fragments.is_empty() {
                    return Err(Error::UnnamedBlock);
                }

                let fragment = fragments.into_iter().next().unwrap();
                Ok(Some(Tag::StartBlock(StartBlockTag {
                    prefix: parts.head,
                    fragment,
                })))
            }
        }
        FragmentType::End => {
            if !parts.data.trim().is_empty() {
                return Err(Error::EndTagWithData(parts.data.to_owned()));
            }
            Ok(Some(Tag::End(EndTag)))
        }
        FragmentType::BlockEnd => {
            if !parts.data.trim().is_empty() {
                return Err(Error::EndTagWithData(parts.data.to_owned()));
            }
            Ok(Some(Tag::EndBlock(EndBlockTag { prefix: parts.head })))
        }
    }
}

fn parse_base<'l>(line: &'l str, tag_markers: (&str, &str)) -> Option<LineParts<'l>> {
    // "(?P<head>[^\{]*)\{%\s+(?P<tag>fragment|endfragment)(?P<data>[^%]+)%\}(?P<tail>.*)
    let (head, line) = line.split_once(tag_markers.0)?;
    let line = line.strip_prefix(char::is_whitespace)?;

    use FragmentType as T;

    // NOTE: the order is important: the -block suffixes must come first
    let (fragment_type, line) = None
        .or_else(|| {
            line.strip_prefix("fragment-block")
                .map(|l| (T::BlockStart, l))
        })
        .or_else(|| {
            line.strip_prefix("endfragment-block")
                .map(|l| (T::BlockEnd, l))
        })
        .or_else(|| line.strip_prefix("fragment").map(|l| (T::Start, l)))
        .or_else(|| line.strip_prefix("endfragment").map(|l| (T::End, l)))?;

    let line = line.strip_prefix(char::is_whitespace)?;
    let (data, line) = line.split_once(tag_markers.1)?;
    let tail = line;

    Some(LineParts {
        head,
        fragment_type,
        data,
        tail,
    })
}

fn is_valid_fragment_name(name: &str) -> bool {
    let is_reserved = matches!(name, "block");
    let only_valid_chars = name
        .chars()
        .all(|c| c.is_alphanumeric() || matches!(c, '-' | '_'));

    !is_reserved && only_valid_chars
}

#[derive(Debug, Clone, PartialEq, Eq)]
struct LineParts<'a> {
    head: &'a str,
    fragment_type: FragmentType,
    data: &'a str,
    tail: &'a str,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum FragmentType {
    Start,
    End,
    BlockStart,
    BlockEnd,
}

fn sorted_fragments<'a, I: IntoIterator<Item = &'a str>>(fragments: I) -> String {
    let mut fragments = fragments.into_iter().collect::<Vec<_>>();
    fragments.sort();

    let mut res = String::new();
    for fragment in fragments {
        push_join(&mut res, fragment);
    }
    res
}

/// Errors that can occurs during processing
///
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Error {
    /// Non-whitespace content before the fragment tag
    LeadingContent(String),
    /// None-whitespace content after the fragment tag
    TrailingContent(String),
    /// Endfragment tag with fragment names
    EndTagWithData(String),
    /// Fragment tag without names
    StartTagWithoutData,
    /// Fragment tag with a fragment that is already active
    ReentrantFragment(String),
    /// Tag without end tag
    UnclosedTag(String),
    /// End tag without corresponding start
    UnbalancedEndTag,
    /// Reserved fragment names (at the moment only `block`) or invalid characters
    InvalidFragmentName(String),
    /// A block fragment without a name
    UnnamedBlock,
    /// A block fragmen with too many names
    MultipleNamesBlock(String),
}

impl Error {
    pub fn at(self, line: usize) -> ErrorWithLine {
        ErrorWithLine(line, self)
    }
}

impl std::fmt::Display for Error {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::LeadingContent(content) => write!(f, "Error::LeadingContent({content:?})"),
            Self::TrailingContent(content) => write!(f, "Error::TrailingContent({content:?})"),
            Self::EndTagWithData(data) => write!(f, "Error::EndTagWithData({data:?})"),
            Self::StartTagWithoutData => write!(f, "Error::StartTagWithoutData"),
            Self::ReentrantFragment(fragments) => write!(f, "Error::ReentrantFragment({fragments}"),
            Self::UnbalancedEndTag => write!(f, "Error::UnbalancedTags"),
            Self::UnclosedTag(fragments) => write!(f, "Error::UnclosedTag({fragments})"),
            Self::InvalidFragmentName(fragments) => {
                write!(f, "Error::InvalidFragmentName({fragments}")
            }
            Self::UnnamedBlock => write!(f, "Error::UnnamedBlock"),
            Self::MultipleNamesBlock(fragments) => {
                write!(f, "Error::MultipleNamesBlock({fragments}")
            }
        }
    }
}

impl std::error::Error for Error {}

/// An error with the line of the template it occurred on
///
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ErrorWithLine(pub usize, pub Error);

impl std::fmt::Display for ErrorWithLine {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{} at line {}", self.1, self.0 + 1)
    }
}

impl std::error::Error for ErrorWithLine {}

fn push_join(s: &mut String, t: &str) {
    if !s.is_empty() {
        s.push_str(", ");
    }
    s.push_str(t);
}