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
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
use anyhow::anyhow;
use std::collections::HashMap;
use nom::{branch::alt, bytes::complete::{tag, take_till, take_until, take_while, take_while_m_n}, character::complete::{alphanumeric1, anychar, multispace0, space0}, combinator::{opt, recognize, rest}, multi::{many0, many1, many_till, separated_list1}, sequence::{delimited, preceded, separated_pair, terminated, tuple}, IResult, Parser};
use nom_locate::LocatedSpan;

////////////////////////////////////////////////////////////////////////////////
// Structs and types
pub type Span<'a> = LocatedSpan<&'a str>;

/// Predefined functions names that will be used within `render_filter` to
/// convert a value.
#[derive(Clone, Debug, PartialEq)]
pub enum Filter {
    /// Converts a string into lowercase.
    ///
    /// # Example
    /// ```rust
    /// "Hello, World!"
    /// // becomes
    /// "hello, world!"
    /// ```
    Lowercase,
    /// Converts a string into uppercase.
    ///
    /// # Example
    /// ```rust
    /// "Hello, World!"
    /// // becomes
    /// "HELLo, WORLD!"
    /// ```
    Uppercase,
    /// Converts a string from Markdown into HTML.
    ///
    /// # Examples
    /// ```html
    /// # Markdown Title
    /// First paragraph.
    ///
    /// [example.com](https://example.com)
    ///
    /// * Unordered list
    ///
    /// 1. Ordered list
    ///
    /// <!-- becomes -->
    ///
    /// <h1>Markdown Title</h1>
    /// <p>First paragraph.</p>
    ///
    /// <p><a href="https://example.com">example.com</a></p>
    ///
    /// <ul><li>Unordered list</li></ul>
    ///
    /// <ol><li>Unordered list</li></ol>
    /// ```
    Markdown,
    /// Reverse a string, character by character.
    ///
    /// # Example
    /// ```rust
    /// "Hello, World!"
    /// // becomes
    /// "!dlroW ,olleH"
    /// ```
    Reverse,
    /// Truncates a string to a given length, and applies a `trail`ing string,
    /// if the string was truncated.
    ///
    /// # Example
    /// ```rust
    /// "Hello, World!"
    /// // becomes this if characters is 5, and trail is "...".
    /// "Hello..."
    /// ```
    Truncate {
        characters: u8,
        trail: String,
    }
}

/// A simple struct to store the key value pair from within the meta section of
/// a Markdown file.
///
/// # Example
/// ```rust
/// use blogs_md_easy::{parse_meta_line, Meta, Span};
///
/// let input = Span::new("foo = bar");
/// let (_, meta) = parse_meta_line(input).unwrap();
/// // Unwrap because key-values are Some() and comments are None.
/// let meta = meta.unwrap();
/// assert_eq!(meta, Meta::new("foo", "bar"));
/// ```
#[derive(Debug, PartialEq)]
pub struct Meta {
    pub key: String,
    pub value: String,
}

impl Meta {
    /// Trims the `key` and `value` and stores them in the respective values in
    /// this struct.
    ///
    /// # Example
    /// ```rust
    /// use blogs_md_easy::Meta;
    ///
    /// let meta_with_space = Meta::new("  foo  ", "  bar  ");
    /// let meta_without_space = Meta::new("foo", "bar");
    /// assert_eq!(meta_with_space, meta_without_space);
    /// ```
    pub fn new(key: &str, value: &str) -> Self {
        Self {
            key: key.trim().to_string(),
            value: value.trim().to_string(),
        }
    }
}

/// A position for a Cursor within a `Span`.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Marker {
    pub line: u32,
    pub offset: usize,
}

impl Marker {
    /// Extracts the `location_line()` and `location_offset()` from the `Span`.
    pub fn new(span: Span) -> Self {
        Self {
            line: span.location_line(),
            offset: span.location_offset(),
        }
    }
}

impl Default for Marker {
    /// Create a `Marker` with a `line` of `1` and `offset` of `1`.
    ///
    /// # Example
    /// ```rust
    /// use blogs_md_easy::Marker;
    ///
    /// let marker_default = Marker::default();
    /// let marker_new = Marker { line: 1, offset: 1 };
    /// assert_eq!(marker_default, marker_new);
    /// ```
    fn default() -> Self {
        Self {
            line: 1,
            offset: 1,
        }
    }
}

/// A helper struct that contains a start and end `Marker` of a `Span`.
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct Selection {
    pub start: Marker,
    pub end: Marker,
}

impl Selection {
    /// Generate a new selection from two `Spans`.
    ///
    /// The `start` argument will simply extract the `location_line` and
    /// `location_offset` from the `Span`.
    /// The `end` argument will use the `location_line`, but will set the offset
    /// to the `location_offset` added to the `fragment` length to ensure we
    /// consume the entire match.
    pub fn from(start: Span, end: Span) -> Self {
        Self {
            start: Marker::new(start),
            // We cannot use `new` because we need to account for the string
            // fragment length.
            end: Marker {
                line: end.location_line(),
                offset: end.location_offset() + end.fragment().len()
            }
        }
    }
}

/// A Placeholder is a variable that is created within a Template file.
///
/// The syntax for a `Placeholder` is as below.
/// ```
/// {{ £variable_name[| filter_name[= [key: ]value]...] }}
/// ```
/// Breaking that down, it simply means that a Placeholder can just be a
/// variable, or can have a list of optional filters following the `|`
/// character.
/// In the case of some filters, the key can be ignored as a default will be
/// used; see the `Filter` enum for further documentation.
#[derive(Clone, Debug, Default, PartialEq)]
pub struct Placeholder {
    pub name: String,
    pub selection: Selection,
    pub filters: Vec<Filter>,
}


////////////////////////////////////////////////////////////////////////////////
// Parsers
/// Parse any character until the end of the line.
/// This will return all characters, except the newline which will be consumed
/// and discarded.
///
/// # Examples
/// When there is no newline.
/// ```rust
/// use blogs_md_easy::{parse_until_eol, Span};
///
/// let input = Span::new("Hello, World!");
/// let (input, until_eol) = parse_until_eol(input).unwrap();
/// assert_eq!(input.fragment(), &"");
/// assert_eq!(until_eol.fragment(), &"Hello, World!");
/// ```
///
/// There is a newline.
/// ```rust
/// use blogs_md_easy::{parse_until_eol, Span};
///
/// let input = Span::new("Hello, World!\nThis is Sparta!");
/// let (input, until_eol) = parse_until_eol(input).unwrap();
/// assert_eq!(input.fragment(), &"This is Sparta!");
/// assert_eq!(until_eol.fragment(), &"Hello, World!");
/// ```
pub fn parse_until_eol(input: Span) -> IResult<Span, Span> {
    terminated(
        alt((take_until("\n"), rest)),
        alt((tag("\n"), tag(""))),
    )(input)
}

/// Parse a comment starting with either a `#` or `//` and ending with a newline.
///
/// # Example
/// ```rust
/// use blogs_md_easy::{parse_meta_comment, Span};
///
/// let input = Span::new("# This is a comment");
/// let (input, meta_comment) = parse_meta_comment(input).unwrap();
/// assert_eq!(input.fragment(), &"");
/// assert_eq!(meta_comment.fragment(), &"This is a comment");
/// ```
pub fn parse_meta_comment(input: Span) -> IResult<Span, Span> {
    preceded(
        // All comments start with either a `#` or `//` followed by a space(s).
        tuple((alt((tag("#"), tag("//"))), space0)),
        parse_until_eol,
    )(input)
}

/// Parse a key, that starts with an optional `£`, followed by an alphabetic
/// character, then any number of alphanumeric characters, hyphens and
/// underscores.
///
/// # Examples
/// A valid variable, consisting of letters and underscores.
/// ```rust
/// use blogs_md_easy::{parse_meta_key, Span};
///
/// let input = Span::new("£publish_date");
/// let (_, variable) = parse_meta_key(input).unwrap();
/// assert_eq!(variable.fragment(), &"publish_date");
/// ```
/// An invalid example, variables cannot start with a number.
/// ```rust
/// use blogs_md_easy::{parse_meta_key, Span};
///
/// let input = Span::new("£1_to_2");
/// let variable = parse_meta_key(input);
/// assert!(variable.is_err());
/// ```
pub fn parse_meta_key(input: Span) -> IResult<Span, Span> {
    preceded(
        opt(tag("£")),
        parse_variable_name
    )(input)
}

/// Parse any number of characters until the end of the line or string.
///
/// # Example
/// ```rust
/// use blogs_md_easy::{parse_meta_value, Span};
///
/// let input = Span::new("This is a value");
/// let (_, value) = parse_meta_value(input).unwrap();
/// assert_eq!(value.fragment(), &"This is a value");
/// ```
pub fn parse_meta_value(input: Span) -> IResult<Span, Span> {
    // The value of the variable, everything after the equals sign.
    // Continue to a newline or the end of the string.
    parse_until_eol(input)
}

/// Parse a key-value pair of meta_key and meta_value.
///
/// # Example
/// ```rust
/// use blogs_md_easy::{parse_meta_key_value, Span};
///
/// let input = Span::new("£publish_date = 2021-01-01");
/// let (_, meta) = parse_meta_key_value(input).unwrap();
/// assert_eq!(meta.key, "publish_date");
/// assert_eq!(meta.value, "2021-01-01");
/// ```
pub fn parse_meta_key_value(input: Span) -> IResult<Span, Meta> {
    separated_pair(
        parse_meta_key,
        recognize(tuple((space0, tag("="), space0))),
        parse_meta_value
    )(input)
    .map(|(input, (key, value))| {
        (input, Meta::new(key.fragment(), value.fragment()))
    })
}

/// Parse a line of meta data. This can either be a comment or a key-value pair.
///
/// # Examples
/// Parsing of a comment returns None.
/// ```rust
/// use blogs_md_easy::{parse_meta_line, Span};
///
/// let input = Span::new("# This is a comment");
/// let (_, meta) = parse_meta_line(input).unwrap();
/// assert!(meta.is_none());
/// ```
/// Parsing of a key-value pair returns a Meta object.
/// ```rust
/// use blogs_md_easy::{parse_meta_line, Span};
///
/// let input = Span::new("£publish_date = 2021-01-01");
/// let (_, meta) = parse_meta_line(input).unwrap();
/// assert!(&meta.is_some());
/// let meta = meta.unwrap();
/// assert_eq!(&meta.key, "publish_date");
/// assert_eq!(&meta.value, "2021-01-01");
/// ```
pub fn parse_meta_line(input: Span) -> IResult<Span, Option<Meta>> {
    let (input, _) = space0(input)?;
    let (input, res) = alt((
        parse_meta_comment.map(|_| None),
        parse_meta_key_value.map(Some),
    ))(input)?;
    let (input, _) = multispace0(input)?;
    Ok((input, res))
}

/// Parse the meta section. This is either a `:meta` or `<meta>` tag surrounding
/// a Vector of parse_meta_line.
///
/// # Example
/// ```rust
/// use blogs_md_easy::{parse_meta_section, Meta, Span};
///
/// let input = Span::new(":meta\n// This is the published date\npublish_date = 2021-01-01\n:meta\n# Markdown title");
/// let (input, meta) = parse_meta_section(input).unwrap();
/// // Comments are ignored and removed from the Vector.
/// assert_eq!(meta.len(), 1);
/// assert_eq!(meta, vec![
///     Meta {
///         key: "publish_date".to_string(),
///         value: "2021-01-01".to_string(),
///     },
/// ]);
/// assert_eq!(input.fragment(), &"# Markdown title");
/// ```
pub fn parse_meta_section(input: Span) -> IResult<Span, Vec<Meta>> {
    alt((
        // I can't think of a more elegant solution for ensuring the pairs match
        // one another. The previous solution could open with `:meta` and close
        // with `</meta>` for example.
        delimited(
            tuple((multispace0, tag(":meta"), multispace0)),
            many1(parse_meta_line),
            tuple((multispace0, tag(":meta"), multispace0)),
        ),
        delimited(
            tuple((multispace0, tag("<meta>"), multispace0)),
            many1(parse_meta_line),
            tuple((multispace0, tag("</meta>"), multispace0)),
        ),
    ))(input)
    // Filter out None values, leaving only legitimate meta values.
    .map(|(input, res)| {
        // When calling flatten on Option<> types, None values are considered
        // empty iterators and removed, Some values are considered iterators
        // with a single element and are therefore unwrapped and returned.
        (input, res.into_iter().flatten().collect())
    })
}

/// Parse the title of the document. This is either a Markdown title or an HTML
/// heading with the `h1` tag.
///
/// # Examples
/// Using a Markdown heading.
/// ```rust
/// use blogs_md_easy::{parse_title, Span};
///
/// let input = Span::new("# This is the title");
/// let (_, title) = parse_title(input).unwrap();
/// assert_eq!(title.fragment(), &"This is the title");
/// ```
/// Using an HTML heading.
/// ```rust
/// use blogs_md_easy::{parse_title, Span};
///
/// let input = Span::new("<h1>This is the title</h1>");
/// let (_, title) = parse_title(input).unwrap();
/// assert_eq!(title.fragment(), &"This is the title");
/// ```
pub fn parse_title(input: Span) -> IResult<Span, Span> {
    let (input, _) = multispace0(input)?;

    let (input, title) = alt((
        // Either a Markdown title...
        preceded(tuple((tag("#"), space0)), take_till(|c| c == '\n' || c == '\r')),
        // ... or an HTML title.
        delimited(tag("<h1>"), take_until("</h1>"), tag("</h1>"))
    ))(input)?;

    Ok((input.to_owned(), title.to_owned()))
}

/// Rewrite of the nom::is_alphabetic function that takes a char instead.
///
/// # Example
/// ```rust
/// use blogs_md_easy::is_alphabetic;
///
/// assert!(is_alphabetic('a'));
/// assert!(is_alphabetic('A'));
/// assert!(!is_alphabetic('1'));
/// assert!(!is_alphabetic('-'));
/// ```
pub fn is_alphabetic(input: char) -> bool {
    vec!['a'..='z', 'A'..='Z'].into_iter().flatten().any(|c| c == input)
}

/// A function that checks if a character is valid for a filter name.
/// The filter name is the value before the `=`.
///
/// # Example
/// ```rust
/// use blogs_md_easy::is_filter_name;
///
/// assert!(is_filter_name('a'));
/// assert!(is_filter_name('A'));
/// assert!(is_filter_name('1'));
/// assert!(!is_filter_name('-'));
/// assert!(!is_filter_name(' '));
/// ```
pub fn is_filter_name(input: char) -> bool {
    input.is_alphanumeric() || ['_'].contains(&input)
}

/// A function that checks if a character is valid for a filter argument name.
///
/// # Example
/// ```rust
/// use blogs_md_easy::is_filter_arg;
///
/// assert!(is_filter_arg('a'));
/// assert!(is_filter_arg('A'));
/// assert!(is_filter_arg('1'));
/// assert!(!is_filter_arg('-'));
/// assert!(!is_filter_arg(' '));
/// ```
pub fn is_filter_arg(input: char) -> bool {
    input.is_alphanumeric() || ['_'].contains(&input)
}

/// A function that checks if a character is valid for a filter argument name.
///
/// # Example
/// ```rust
/// use blogs_md_easy::is_filter_value;
///
/// assert!(is_filter_value('a'));
/// assert!(is_filter_value('A'));
/// assert!(is_filter_value('1'));
/// assert!(!is_filter_value('|'));
/// assert!(!is_filter_value(','));
/// assert!(!is_filter_value('{'));
/// assert!(!is_filter_value('}'));
/// ```
pub fn is_filter_value(input: char) -> bool {
    input.is_alphanumeric()
    || ![' ', '|', ',', '{', '}'].contains(&input)
}

/// Variable names must start with an alphabetic character, then any number of
/// alphanumeric characters, hyphens and underscores.
///
/// # Examples
/// Variables can consist of letters and underscores.
/// ```rust
/// use blogs_md_easy::{parse_variable_name, Span};
///
/// let input = Span::new("publish_date");
/// let (_, variable) = parse_variable_name(input).unwrap();
/// assert_eq!(variable.fragment(), &"publish_date");
/// ```
/// Variables cannot start with a number or underscore.
/// ```rust
/// use blogs_md_easy::{parse_variable_name, Span};
///
/// let input = Span::new("1_to_2");
/// let variable = parse_variable_name(input);
/// assert!(variable.is_err());
/// ```
pub fn parse_variable_name(input: Span) -> IResult<Span, Span> {
    recognize(tuple((
        take_while_m_n(1, 1, is_alphabetic),
        many0(alt((alphanumeric1, tag("-"), tag("_")))),
    )))(input)
}

/// Parse a template placeholder variable. This is a `£` followed by a variable
/// name.
///
/// # Examples
/// Variables must start with a `£`.
/// ```rust
/// use blogs_md_easy::{parse_variable, Span};
///
/// let input = Span::new("£variable");
/// let (_, variable) = parse_variable(input).unwrap();
/// assert_eq!(variable.fragment(), &"variable");
/// ```
/// Failing to start with a `£` will return an error.
/// ```rust
/// use blogs_md_easy::{parse_variable, Span};
///
/// let input = Span::new("variable");
/// let variable = parse_variable(input);
/// assert!(variable.is_err());
/// ```
pub fn parse_variable(input: Span) -> IResult<Span, Span> {
    preceded(
        tag("£"),
        parse_variable_name
    )(input)
}

/// Parser that will parse exclusively the key-values from after a filter.  \
/// This will return the key (before the `:`) and the value (after the `:`). It
/// will also return a key of `_` if no key was provided.
///
/// # Returns
/// A tuple of the filter name and a vector of key-value pairs.  \
/// If only a value was provided, then the key will be `_`.
///
/// # Examples
/// Ensure that a key-value pair, separated by a colon, can be parsed into a
/// tuple.
/// ```rust
/// use blogs_md_easy::{parse_filter_key_value, Span};
///
/// let input = Span::new("trail: ...");
/// let (_, args) = parse_filter_key_value(input).unwrap();
/// assert_eq!(args, ("trail", "..."));
/// ```
///
/// Ensure that a single value can be parsed into a tuple with a key of `_`.
/// ```rust
/// use blogs_md_easy::{parse_filter_key_value, Span};
///
/// let input = Span::new("20");
/// let (_, args) = parse_filter_key_value(input).unwrap();
/// assert_eq!(args, ("_", "20"));
/// ```
pub fn parse_filter_key_value(input: Span) -> IResult<Span, (&str, &str)> {
    alt((
        // This matches a key-value separated by a colon.
        // Example: `truncate = characters: 20`
        separated_pair(
            take_while(is_filter_arg).map(|arg: Span| *arg.fragment()),
            tuple((space0, tag(":"), space0)),
            take_while(is_filter_value).map(|value: Span| *value.fragment()),
        ),
        // But it's also possible to just provide a value.
        // Example: `truncate = 20`
        take_while(is_filter_value)
        .map(|value: Span| ("_", *value.fragment()))
    ))(input)
}

/// Parser that will parse exclusively the key-values from after a filter.  \
/// The signature of a filter is `filter_name = key1: value1, key2: value2,...`,
/// or just `filter_name = value`.
///
/// # Examples
/// Ensure that a key-value pair, separated by a colon, can be parsed into a
/// tuple.
/// ```rust
/// use blogs_md_easy::{parse_filter_args, Span};
///
/// let input = Span::new("characters: 20, trail: ...");
/// let (_, args) = parse_filter_args(input).unwrap();
/// assert_eq!(args, vec![
///     ("characters", "20"),
///     ("trail", "..."),
/// ]);
/// ```
///
/// Ensure that a single value can be parsed into a tuple with a key of `_`.
/// ```rust
/// use blogs_md_easy::{parse_filter_args, Span};
///
/// let input = Span::new("20");
/// let (_, args) = parse_filter_args(input).unwrap();
/// assert_eq!(args, vec![
///     ("_", "20")
/// ]);
/// ```
pub fn parse_filter_args(input: Span) -> IResult<Span, Vec<(&str, &str)>> {
    separated_list1(
        tuple((space0, tag(","), space0)),
        parse_filter_key_value
    )(input)
}

/// Parse a filter, and optionally its arguments if present.
///
/// # Examples
/// A filter with no arguments.
/// ```rust
/// use blogs_md_easy::{parse_filter, Filter, Span};
///
/// let input = Span::new("lowercase");
/// let (_, filter) = parse_filter(input).unwrap();
/// assert!(matches!(filter, Filter::Lowercase));
/// ```
///
/// A filter with just a value, but no key.  \
/// This will be parsed as a key of `_`, which will then be set to a key of the
/// given enum Struct variant that is deemed the default.  \
/// In the case of `Filter::Truncate`, this will be the `characters`.
/// ```rust
/// use blogs_md_easy::{parse_filter, Filter, Span};
///
/// let input = Span::new("truncate = 20");
/// let (_, filter) = parse_filter(input).unwrap();
/// assert_eq!(filter, Filter::Truncate { characters: 20, trail: "...".to_string() });
/// ```
///
/// A filter with multiple arguments, and given keys.
/// ```rust
/// use blogs_md_easy::{parse_filter, Filter, Span};
///
/// let input = Span::new("truncate = characters: 15, trail:...");
/// let (_, filter) = parse_filter(input).unwrap();
/// assert!(matches!(filter, Filter::Truncate { .. }));
/// assert_eq!(filter, Filter::Truncate {
///     characters: 15,
///     trail: "...".to_string(),
/// });
/// ```
///
/// For some filters, default values are provided, if not present.
/// ```rust
/// use blogs_md_easy::{parse_filter, Filter, Span};
///
/// let input = Span::new("truncate = trail:...");
/// let (_, filter) = parse_filter(input).unwrap();
/// assert!(matches!(filter, Filter::Truncate { .. }));
/// assert_eq!(filter, Filter::Truncate {
///     characters: 20,
///     trail: "...".to_string(),
/// });
/// ```
pub fn parse_filter(input: Span) -> IResult<Span, Filter> {
    separated_pair(
        take_while(is_filter_name),
        opt(tuple((space0, tag("="), space0))),
        opt(parse_filter_args)
    )(input)
    .map(|(input, (name, args))| {
        let args: HashMap<&str, &str> = args.unwrap_or_default().into_iter().collect();

        (input, match name.fragment().to_lowercase().trim() {
            "lowercase" => Filter::Lowercase,
            "uppercase" => Filter::Uppercase,
            "markdown" => Filter::Markdown,
            "reverse" => Filter::Reverse,
            "truncate" => Filter::Truncate {
                // Attempt to get the characters, but if we can't then we use
                // the unnamed value, defined as "_".
                characters: args.get("characters").unwrap_or(
                    args.get("_").unwrap_or(&"20")
                ).parse::<u8>().unwrap_or(20),
                trail: args.get("trail").unwrap_or(&"...").to_string()
            },
            _ => unreachable!(),
        })
    })
}

/// Parsers a list of Filters.
///
/// # Examples
/// A single filter.
/// ```rust
/// use blogs_md_easy::{parse_filters, Filter, Span};
///
/// // As in {{ £my_variable | lowercase }}
/// let input = Span::new("| lowercase");
/// let (_, filters) = parse_filters(input).unwrap();
/// assert!(matches!(filters[0], Filter::Lowercase));
/// ```
///
/// Multiple filters chained together with `|`.
/// ```rust
/// use blogs_md_easy::{parse_filters, Filter, Span};
///
/// // As in {{ £my_variable | lowercase | truncate = trail: ..! }}
/// let input = Span::new("| lowercase | truncate = trail: ..!");
/// let (_, filters) = parse_filters(input).unwrap();
/// assert!(matches!(filters[0], Filter::Lowercase));
/// assert!(matches!(filters[1], Filter::Truncate { .. }));
/// assert_eq!(filters[1], Filter::Truncate {
///     characters: 20,
///     trail: "..!".to_string(),
/// });
/// ```
pub fn parse_filters(input: Span) -> IResult<Span, Vec<Filter>> {
    preceded(
        tuple((space0, tag("|"), space0)),
        separated_list1(tuple((space0, tag("|"), space0)), parse_filter)
    )(input)
}

/// Parse a template placeholder. This is a variable name, surrounded by `{{`
/// and `}}`.
/// Whitespace is optional.
///
/// # Examples
/// A simple placeholder.
/// ```rust
/// use blogs_md_easy::{parse_placeholder, Span};
///
/// let input = Span::new("{{ £variable }}");
/// let (_, placeholder) = parse_placeholder(input).unwrap();
/// assert_eq!(placeholder.name.as_str(), "variable");
/// assert_eq!(placeholder.selection.start.offset, 0);
/// assert_eq!(placeholder.selection.end.offset, 16);
/// ```
///
/// A placeholder without whitespace.
/// ```rust
/// use blogs_md_easy::{parse_placeholder, Span};
///
/// let input = Span::new("{{£variable}}");
/// let (_, placeholder) = parse_placeholder(input).unwrap();
/// assert_eq!(placeholder.name.as_str(), "variable");
/// assert_eq!(placeholder.selection.start.offset, 0);
/// assert_eq!(placeholder.selection.end.offset, 14);
/// ```
pub fn parse_placeholder(input: Span) -> IResult<Span, Placeholder> {
    tuple((
        tuple((tag("{{"), multispace0)),
        parse_variable,
        opt(parse_filters),
        tuple((multispace0, tag("}}"))),
    ))(input)
    .map(|(input, (start, variable, filters, end))| {
        let mut filters = filters.unwrap_or_default();

        // By default, £content will always be parsed as Markdown.
        if variable.to_ascii_lowercase().as_str() == "content" && !filters.contains(&Filter::Markdown) {
            filters.push(Filter::Markdown);
        }

        (input, Placeholder {
            name: variable.to_string(),
            filters,
            selection: Selection::from(start.0, end.1)
        })
    })
}

/// Parse a string consuming - and discarding - any character, and stopping at
/// the first matched placeholder, returning a Placeholder struct.
///
/// # Example
/// ```rust
/// use blogs_md_easy::{take_till_placeholder, Marker, Placeholder, Selection, Span};
///
/// let input = Span::new("Hello, {{ £name }}!");
/// let (input, placeholder) = take_till_placeholder(input).expect("to parse input");
/// assert_eq!(input.fragment(), &"!");
/// assert_eq!(placeholder, Placeholder {
///     name: "name".to_string(),
///     selection: Selection {
///         start: Marker {
///             line: 1,
///             offset: 7,
///         },
///         end: Marker {
///             line: 1,
///             offset: 19,
///         },
///     },
///     filters: vec![],
/// });
/// ```
pub fn take_till_placeholder(input: Span) -> IResult<Span, Placeholder> {
    many_till(anychar, parse_placeholder)(input)
    // Map to remove anychar's captures.
    .map(|(input, (_, placeholder))| (input, placeholder))
}

/// Consume an entire string, and return a Vector of a tuple; where the first
/// element is a String of the variable name, and the second element is the
/// Placeholder.
///
/// # Example
/// ```rust
/// use blogs_md_easy::{parse_placeholder_locations, Span};
///
/// let input = Span::new("Hello, {{ £name }}!");
/// let placeholders = parse_placeholder_locations(input).unwrap();
/// assert_eq!(placeholders.len(), 1);
/// assert_eq!(placeholders[0].name.as_str(), "name");
/// assert_eq!(placeholders[0].selection.start.offset, 7);
/// assert_eq!(placeholders[0].selection.end.offset, 19);
/// ```
pub fn parse_placeholder_locations(input: Span) -> Result<Vec<Placeholder>, anyhow::Error> {
    let (_, mut placeholders) = many0(take_till_placeholder)(input).unwrap_or((input, Vec::new()));

    // Sort in reverse so that when we replace each placeholder, the offsets do
    // not affect offsets after this point.
    placeholders.sort_by(|a, b| b.selection.start.offset.cmp(&a.selection.start.offset));

    Ok(placeholders)
}

////////////////////////////////////////////////////////////////////////////////
// Functions

/// Replaces a substring in the original string with a replacement string.
///
/// # Arguments
///
/// * `original` - The original string.
/// * `start` - The start position of the substring in the original string.
/// * `end` - The end position of the substring in the original string.
/// * `replacement` - The string to replace the substring.
///
/// # Returns
///
/// * A new string with the replacement in place of the original substring.
///
/// # Example
///
/// ```
/// use blogs_md_easy::replace_substring;
///
/// let original = "Hello, World!";
/// let start = 7;
/// let end = 12;
/// let replacement = "Rust";
/// let result = replace_substring(original, start, end, replacement);
/// println!("{}", result);  // Prints: "Hello, Rust!"
/// ```
pub fn replace_substring(original: &str, start: usize, end: usize, replacement: &str) -> String {
    let mut result = String::new();
    result.push_str(&original[..start]);
    result.push_str(replacement);
    result.push_str(&original[end..]);
    result
}

/// Creates a HashMap of key-value pairs from meta values.
///
/// # Arguments
/// * `markdown` - A LocatedSpan of the markdown file.
/// * `meta_values` - An optional vector of Meta values.
///
/// # Returns
/// Convert the meta_values into a HashMap, then parse the title and content
/// from the markdown file.
///
/// # Example
/// ```
/// use blogs_md_easy::{create_variables, parse_meta_section, Span};
///
/// let markdown = Span::new(":meta\nauthor = John Doe\n:meta\n# Markdown title\nContent paragraph");
/// let (markdown, meta_values) = parse_meta_section(markdown).unwrap_or((markdown, vec![]));
/// let variables = create_variables(markdown, meta_values).expect("to create variables");
/// assert_eq!(variables.get("title").unwrap(), "Markdown title");
/// assert_eq!(variables.get("author").unwrap(), "John Doe");
/// assert_eq!(variables.get("content").unwrap(), "# Markdown title\nContent paragraph");
/// ```
pub fn create_variables(markdown: Span, meta_values: Vec<Meta>) -> Result<HashMap<String, String>, anyhow::Error> {
    let mut variables: HashMap<String, String> = meta_values
        .into_iter()
        .map(|meta| (meta.key.to_owned(), meta.value.to_owned()))
        .collect();

    // Make sure that we have a title and content variable.
    if !variables.contains_key("title") {
        if let Ok(title) = parse_title(markdown) {
            let (_, title) = title;
            variables.insert("title".to_string(), title.to_string());
        } else {
            return Err(anyhow!("Missing title"));
        }
    }
    if !variables.contains_key("content") {
        let content = markdown.fragment().trim().to_string();
        variables.insert("content".to_string(), content);
    }

    Ok(variables)
}

/// Take a variable, and run it through a Filter function to get the new
/// output.
///
/// # Examples
/// Filter that has no arguments.
/// ```rust
/// use blogs_md_easy::{render_filter, Filter};
///
/// let variable = "hello, world!".to_string();
/// assert_eq!("HELLO, WORLD!", render_filter(variable, &Filter::Uppercase));
/// ```
///
/// Filter that has arguments.
/// ```rust
/// use blogs_md_easy::{render_filter, Filter};
///
/// let variable = "hello, world!".to_string();
/// assert_eq!("hello...", render_filter(variable, &Filter::Truncate { characters: 5, trail: "...".to_string() }));
/// ```
pub fn render_filter(variable: String, filter: &Filter) -> String {
    match filter {
        Filter::Lowercase => variable.to_lowercase(),
        Filter::Uppercase => variable.to_uppercase(),
        Filter::Markdown  => {
            markdown::to_html_with_options(&variable, &markdown::Options {
                compile: markdown::CompileOptions {
                    allow_dangerous_html: true,
                    allow_dangerous_protocol: false,
                    ..Default::default()
                },
                ..Default::default()
            }).unwrap_or_default()
        },
        Filter::Reverse => variable.chars().rev().collect(),
        Filter::Truncate { characters, trail } => {
            let mut new_variable = variable.to_string();
            new_variable.truncate(*characters as usize);
            // Now truncate and append the trail.
            if (variable.len() as u8) > *characters {
                new_variable.push_str(trail);
            }
            new_variable
        },
    }
}