marmite 0.4.2

Marmite is the easiest static site generator.
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
use indexmap::IndexMap;
use serde::Serialize;
use tera::{Kwargs, State, TeraResult, Value};
use url::Url;

use crate::content::Content;
use crate::site::Data;
use crate::workspace::CrossSiteData;

fn parse_site_param(kwargs: &Kwargs) -> Option<String> {
    kwargs
        .get::<&str>("site")
        .ok()
        .flatten()
        .filter(|s| !s.is_empty())
        .map(ToString::to_string)
}

fn prefix_slug(slug: &str, output_path: &str) -> String {
    if output_path.is_empty() {
        slug.to_string()
    } else {
        format!("{output_path}/{slug}")
    }
}

fn collect_cross_site_posts(site_param: &str, cross_site_data: &CrossSiteData) -> Vec<Content> {
    let site_names: Vec<&str> = if site_param == "all" {
        cross_site_data.sites.keys().map(String::as_str).collect()
    } else {
        site_param.split(',').map(str::trim).collect()
    };

    let mut posts = Vec::new();
    for name in site_names {
        if let Some(site_data) = cross_site_data.sites.get(name) {
            for post in &site_data.data.posts {
                let mut prefixed = post.clone();
                prefixed.slug = prefix_slug(&post.slug, &site_data.output_path);
                posts.push(prefixed);
            }
        }
    }
    posts
}

fn collect_cross_site_pages(site_param: &str, cross_site_data: &CrossSiteData) -> Vec<Content> {
    let site_names: Vec<&str> = if site_param == "all" {
        cross_site_data.sites.keys().map(String::as_str).collect()
    } else {
        site_param.split(',').map(str::trim).collect()
    };

    let mut pages = Vec::new();
    for name in site_names {
        if let Some(site_data) = cross_site_data.sites.get(name) {
            for page in &site_data.data.pages {
                let mut prefixed = page.clone();
                prefixed.slug = prefix_slug(&page.slug, &site_data.output_path);
                pages.push(prefixed);
            }
        }
    }
    pages
}

#[derive(Serialize)]
pub struct SlugData {
    pub image: String,
    pub slug: String,
    pub title: String,
    pub text: String,
    pub content_type: String,
}

#[derive(Default)]
pub struct UrlFor {
    pub base_url: String,
    pub path_prefix: String,
    pub all_site_prefixes: Vec<String>,
}

impl UrlFor {
    fn has_site_prefix(&self, path: &str) -> bool {
        self.all_site_prefixes
            .iter()
            .filter(|p| !p.is_empty())
            .any(|p| path.starts_with(&format!("/{p}/")))
    }

    pub fn resolve(&self, path: &str, abs: bool) -> String {
        let mut path = path.trim_start_matches("./").to_string();

        let abs_prefixes = ["http", "https", "mailto"];
        if abs_prefixes.iter().any(|&prefix| path.starts_with(prefix)) {
            return path;
        }

        // Ensure the path starts with "/" by adding it if necessary
        if !path.starts_with('/') {
            path = format!("/{path}");
        }

        // Apply workspace path prefix for non-root sites,
        // but skip if the path already belongs to another site
        if !self.path_prefix.is_empty()
            && !path.starts_with(&format!("/{}/", self.path_prefix))
            && !self.has_site_prefix(&path)
        {
            path = format!("/{}{path}", self.path_prefix);
        }

        // Trim trailing slashes from base_url if it's not empty
        let base_url = if self.base_url.is_empty() {
            String::new()
        } else {
            self.base_url.trim_end_matches('/').to_string()
        };

        // Parse the base_url to extract the path part if not empty.
        // When path_prefix is set (workspace non-default site), the base_url
        // already contains the prefix path (e.g. https://example.com/en),
        // so we must NOT also use it as base_path to avoid double-prefixing.
        let base_path = if !self.path_prefix.is_empty() || base_url.is_empty() {
            String::new()
        } else {
            Url::parse(&base_url)
                .map(|parsed_url| parsed_url.path().trim_end_matches('/').to_string())
                .unwrap_or_default()
        };

        // Construct the URL based on the presence of base_url and abs flag
        if abs && !base_url.is_empty() {
            // Absolute URL with base_url.
            // When path_prefix is set, strip the path part from base_url since
            // path_prefix is already included in the resolved path.
            let abs_base = if self.path_prefix.is_empty() {
                base_url.clone()
            } else {
                Url::parse(&base_url).map_or_else(
                    |_| base_url.clone(),
                    |parsed_url| {
                        let host = parsed_url.host_str().unwrap_or("");
                        let scheme = parsed_url.scheme();
                        if let Some(port) = parsed_url.port() {
                            format!("{scheme}://{host}:{port}")
                        } else {
                            format!("{scheme}://{host}")
                        }
                    },
                )
            };
            format!("{}/{}", abs_base, path.trim_start_matches('/'))
        } else if !base_path.is_empty() {
            // Relative URL with base path from base_url
            format!("{base_path}{path}")
        } else {
            // Just the path if no base_url or base_path
            path
        }
    }
}

impl tera::Function<TeraResult<Value>> for UrlFor {
    fn call(&self, kwargs: Kwargs, _: &State) -> TeraResult<Value> {
        let path: &str = kwargs.must_get("path")?;
        let abs: bool = kwargs.get::<bool>("abs")?.unwrap_or(false);

        Ok(Value::from(self.resolve(path, abs)))
    }
}

/// Tera template function that takes a `kind` argument and returns the grouped content
/// based on the kind. The function is used to group the content by tags or archive.
/// The grouped content is built using the `site_data` field from the `Group` struct.
/// and converted to an `IndexMap` to preserve the order of insertion that is
/// determined by the iter on `GroupedContent`.
pub struct Group {
    pub site_data: Data,
    pub cross_site_data: Option<CrossSiteData>,
}

fn get_grouped_content_from_data<'a>(
    data: &'a Data,
    kind: &str,
) -> Option<&'a crate::content::GroupedContent> {
    match kind {
        "tag" => Some(&data.tag),
        "archive" => Some(&data.archive),
        "author" => Some(&data.author),
        "stream" => Some(&data.stream),
        "series" => Some(&data.series),
        "language" => Some(&data.language),
        _ => None,
    }
}

fn sort_group_list(group_list: &mut [(String, Vec<Content>)], kind: &str, ord: &str) {
    match kind {
        "archive" => {
            if ord == "asc" {
                group_list.reverse();
            }
        }
        _ => {
            if ord == "asc" {
                group_list.sort_by(|a, b| a.0.cmp(&b.0));
            } else {
                group_list.sort_by_key(|a| std::cmp::Reverse(a.1.len()));
            }
        }
    }
}

fn merge_grouped_content(
    site_param: &str,
    kind: &str,
    cross_site_data: &CrossSiteData,
) -> std::collections::HashMap<String, Vec<Content>> {
    let site_names: Vec<&str> = if site_param == "all" {
        cross_site_data.sites.keys().map(String::as_str).collect()
    } else {
        site_param.split(',').map(str::trim).collect()
    };

    let mut merged: std::collections::HashMap<String, Vec<Content>> =
        std::collections::HashMap::new();
    for name in site_names {
        if let Some(site_data) = cross_site_data.sites.get(name) {
            if let Some(grouped) = get_grouped_content_from_data(&site_data.data, kind) {
                for (key, posts) in grouped.iter() {
                    let entry = merged.entry(key.clone()).or_default();
                    for post in posts {
                        let mut prefixed = post.clone();
                        prefixed.slug = prefix_slug(&post.slug, &site_data.output_path);
                        entry.push(prefixed);
                    }
                }
            }
        }
    }
    merged
}

#[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
impl tera::Function<TeraResult<Value>> for Group {
    fn call(&self, kwargs: Kwargs, _: &State) -> TeraResult<Value> {
        let kind: &str = kwargs.must_get("kind")?;
        let ord: &str = kwargs.get::<&str>("ord")?.unwrap_or("desc");
        let items: usize = kwargs
            .get::<i64>("items")
            .ok()
            .flatten()
            .map(|n| n as usize)
            .or_else(|| {
                kwargs
                    .get::<&str>("items")
                    .ok()
                    .flatten()
                    .and_then(|s| s.parse::<usize>().ok())
            })
            .unwrap_or(0);

        let site_param = parse_site_param(&kwargs);

        if let (Some(ref site_param), Some(ref csd)) = (&site_param, &self.cross_site_data) {
            let merged = merge_grouped_content(site_param, kind, csd);
            let mut group_list: Vec<(String, Vec<Content>)> = merged.into_iter().collect();
            sort_group_list(&mut group_list, kind, ord);
            if items > 0 && items < group_list.len() {
                group_list.truncate(items);
            }
            let mut ordered_map = IndexMap::new();
            for (name, posts) in group_list {
                ordered_map.insert(name, posts);
            }
            return Value::try_from_serializable(&ordered_map);
        }

        let grouped_content = match kind {
            "tag" => &self.site_data.tag,
            "archive" => &self.site_data.archive,
            "author" => &self.site_data.author,
            "stream" => &self.site_data.stream,
            "series" => &self.site_data.series,
            "language" => &self.site_data.language,
            _ => return Err(tera::Error::message("Invalid `kind` argument")),
        };

        // Convert to vector for sorting.
        // For tags, filter out backward-compat duplicate keys and recover original names.
        let mut group_list: Vec<(String, Vec<Content>)> = grouped_content
            .iter()
            .filter(|(key, _)| kind != "tag" || crate::slugify::slugify(key) == key.as_str())
            .map(|(name, posts)| {
                if kind == "tag" {
                    let original_name = posts
                        .iter()
                        .find_map(|content| {
                            content
                                .tags
                                .iter()
                                .find(|t| crate::slugify::slugify(t) == name.as_str())
                                .cloned()
                        })
                        .unwrap_or_else(|| name.clone());
                    (original_name, posts.clone())
                } else {
                    (name.clone(), posts.clone())
                }
            })
            .collect();

        sort_group_list(&mut group_list, kind, ord);

        if kind == "language" {
            if let Some(pos) = group_list
                .iter()
                .position(|(name, _)| name == &self.site_data.site.language)
            {
                let default_lang = group_list.remove(pos);
                group_list.push(default_lang);
            }
        }

        if items > 0 && items < group_list.len() {
            group_list.truncate(items);
        }

        // Convert back to IndexMap to preserve order
        let mut ordered_map = IndexMap::new();
        for (name, posts) in group_list {
            ordered_map.insert(name, posts);
        }

        Value::try_from_serializable(&ordered_map)
    }
}

/// Tera template function that generates source links for content
/// It takes a `content` argument and returns a URL pointing to the source markdown file
/// If `source_repository` is configured, it generates a link to the repository
/// If `publish_md` is true, it generates a link to the local markdown file
pub struct SourceLink {
    pub site_data: Data,
}

impl tera::Function<TeraResult<Value>> for SourceLink {
    fn call(&self, kwargs: Kwargs, _: &State) -> TeraResult<Value> {
        let content: &Value = kwargs.must_get("content")?;

        // Extract the source_path from the content
        let source_path = content
            .get_from_path("source_path")
            .and_then(Value::as_str)
            .ok_or_else(|| tera::Error::message("Missing `source_path` in content"))?;

        // If source_repository is configured, generate repository link
        if let Some(source_repository) = &self.site_data.site.source_repository {
            let source_path_buf = std::path::Path::new(source_path);
            let file_name = source_path_buf
                .file_name()
                .and_then(|n| n.to_str())
                .unwrap_or("unknown.md");

            let repo_url = format!("{}/{}", source_repository.trim_end_matches('/'), file_name);
            return Ok(Value::from(repo_url));
        }

        // If publish_md is true and no source_repository, generate local link
        if self.site_data.site.publish_md {
            let source_path_buf = std::path::Path::new(source_path);
            let file_name = source_path_buf
                .file_name()
                .and_then(|n| n.to_str())
                .unwrap_or("unknown.md");

            let local_url = format!("./{file_name}");
            return Ok(Value::from(local_url));
        }

        // Return empty string if neither option is enabled
        Ok(Value::from(""))
    }
}

/// Tera template function that returns the display name for a stream or series
/// It takes a `stream` or `series` argument and returns the configured display name
/// If no display name is configured, returns the stream/series name itself
pub struct DisplayName {
    pub site_data: Data,
    pub kind: String,
}

impl DisplayName {
    pub fn resolve(&self, name: &str) -> String {
        let display_name = match self.kind.as_str() {
            "stream" => self
                .site_data
                .site
                .streams
                .get(name)
                .map(|config| &config.display_name)
                .or_else(|| {
                    self.site_data
                        .site
                        .languages
                        .get(name)
                        .map(|config| &config.display_name)
                }),
            "series" => self
                .site_data
                .site
                .series
                .get(name)
                .map(|config| &config.display_name),
            "language" => self
                .site_data
                .site
                .languages
                .get(name)
                .map(|config| &config.display_name),
            _ => None,
        };

        display_name.cloned().unwrap_or_else(|| name.to_string())
    }
}

impl tera::Function<TeraResult<Value>> for DisplayName {
    fn call(&self, kwargs: Kwargs, _: &State) -> TeraResult<Value> {
        let name: &str = kwargs.must_get(&self.kind)?;
        Ok(Value::from(self.resolve(name)))
    }
}

/// Tera function to get filtered and sorted posts
/// Args: ord (optional, default="desc"), items (optional, default=0 for all),
///       site (optional, workspace cross-site query)
pub struct GetPosts {
    pub site_data: Data,
    pub cross_site_data: Option<CrossSiteData>,
}

#[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
impl tera::Function<TeraResult<Value>> for GetPosts {
    fn call(&self, kwargs: Kwargs, _: &State) -> TeraResult<Value> {
        let ord: &str = kwargs.get::<&str>("ord")?.unwrap_or("desc");
        let items: usize = kwargs
            .get::<i64>("items")
            .ok()
            .flatten()
            .map(|n| n as usize)
            .or_else(|| {
                kwargs
                    .get::<&str>("items")
                    .ok()
                    .flatten()
                    .and_then(|s| s.parse::<usize>().ok())
            })
            .unwrap_or(0);

        let mut posts = match (parse_site_param(&kwargs), &self.cross_site_data) {
            (Some(site_param), Some(csd)) => collect_cross_site_posts(&site_param, csd),
            _ => self.site_data.posts.clone(),
        };

        posts.sort_by_key(|a| std::cmp::Reverse(a.date));
        if ord == "asc" {
            posts.reverse();
        }

        if items > 0 && items < posts.len() {
            posts.truncate(items);
        }

        Value::try_from_serializable(&posts)
    }
}

/// Tera function to get filtered and sorted pages
/// Args: ord (optional, default="asc"), items (optional, default=0 for all),
///       site (optional, workspace cross-site query)
pub struct GetPages {
    pub site_data: Data,
    pub cross_site_data: Option<CrossSiteData>,
}

#[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
impl tera::Function<TeraResult<Value>> for GetPages {
    fn call(&self, kwargs: Kwargs, _: &State) -> TeraResult<Value> {
        let ord: &str = kwargs.get::<&str>("ord")?.unwrap_or("asc");
        let items: usize = kwargs
            .get::<i64>("items")
            .ok()
            .flatten()
            .map(|n| n as usize)
            .or_else(|| {
                kwargs
                    .get::<&str>("items")
                    .ok()
                    .flatten()
                    .and_then(|s| s.parse::<usize>().ok())
            })
            .unwrap_or(0);

        let mut pages = match (parse_site_param(&kwargs), &self.cross_site_data) {
            (Some(site_param), Some(csd)) => collect_cross_site_pages(&site_param, csd),
            _ => self.site_data.pages.clone(),
        };

        pages.sort_by(|a, b| a.title.cmp(&b.title));
        if ord == "desc" {
            pages.reverse();
        }

        if items > 0 && items < pages.len() {
            pages.truncate(items);
        }

        Value::try_from_serializable(&pages)
    }
}

fn resolve_slug_in_data(slug: &str, data: &Data) -> Option<SlugData> {
    if let Some(page) = data.pages.iter().find(|p| p.slug == slug) {
        return Some(SlugData {
            image: page
                .banner_image
                .as_ref()
                .unwrap_or(&data.site.banner_image)
                .clone(),
            slug: slug.to_string(),
            title: page.title.clone(),
            text: page.description.as_ref().unwrap_or(&String::new()).clone(),
            content_type: "page".to_string(),
        });
    }
    if let Some(post) = data.posts.iter().find(|p| p.slug == slug) {
        return Some(SlugData {
            image: post
                .banner_image
                .as_ref()
                .unwrap_or(&data.site.banner_image)
                .clone(),
            slug: slug.to_string(),
            title: post.title.clone(),
            text: post
                .date
                .map_or_else(String::new, |d| d.format("%Y-%m-%d").to_string()),
            content_type: "post".to_string(),
        });
    }
    None
}

/// Tera function to get data by slug for card display
/// Takes a slug and resolves which content type it refers to, returning `SlugData`
pub struct GetDataBySlug {
    pub site_data: Data,
    pub cross_site_data: Option<CrossSiteData>,
}

impl tera::Function<TeraResult<Value>> for GetDataBySlug {
    #[allow(clippy::too_many_lines)]
    fn call(&self, kwargs: Kwargs, _: &State) -> TeraResult<Value> {
        let raw_slug: &str = kwargs.must_get("slug")?;
        let site_param = parse_site_param(&kwargs);

        if let Some(ref csd) = self.cross_site_data {
            let sep = &csd.separator;
            if let Some(sep_pos) = raw_slug.find(sep.as_str()) {
                let site_name = &raw_slug[..sep_pos];
                let inner_slug = &raw_slug[sep_pos + sep.len()..];
                if let Some(sd) = csd.sites.get(site_name) {
                    let target_data = &sd.data;
                    let slug_data = resolve_slug_in_data(inner_slug, target_data);
                    if let Some(mut sd_result) = slug_data {
                        sd_result.slug = prefix_slug(&sd_result.slug, &sd.output_path);
                        return Value::try_from_serializable(&sd_result);
                    }
                    return Err(tera::Error::message(format!(
                        "Content not found for slug: {raw_slug}"
                    )));
                }
            }

            if let Some(ref site_name) = site_param {
                if let Some(sd) = csd.sites.get(site_name.as_str()) {
                    let target_data = &sd.data;
                    let slug_data = resolve_slug_in_data(raw_slug, target_data);
                    if let Some(mut sd_result) = slug_data {
                        sd_result.slug = prefix_slug(&sd_result.slug, &sd.output_path);
                        return Value::try_from_serializable(&sd_result);
                    }
                    return Err(tera::Error::message(format!(
                        "Content not found for slug: {raw_slug} in site: {site_name}"
                    )));
                }
            }
        }

        let slug = raw_slug;

        // Check what kind of content this slug refers to
        let slug_data = if slug.starts_with("series-") {
            // Series slug: series-{name}
            let series_name = slug
                .strip_prefix("series-")
                .ok_or_else(|| tera::Error::message("Invalid series slug format"))?;
            if let Some(series_contents) = self.site_data.series.map.get(series_name) {
                let display_name_fn = DisplayName {
                    site_data: self.site_data.clone(),
                    kind: "series".to_string(),
                };
                let title = display_name_fn.resolve(series_name);

                let description = self
                    .site_data
                    .site
                    .series
                    .get(series_name)
                    .and_then(|config| config.description.as_ref())
                    .cloned()
                    .unwrap_or_else(|| format!("{} posts", series_contents.len()));

                let image = series_contents
                    .first()
                    .and_then(|content| content.banner_image.as_ref())
                    .unwrap_or(&self.site_data.site.banner_image)
                    .clone();

                SlugData {
                    image,
                    slug: slug.to_string(),
                    title,
                    text: description,
                    content_type: "series".to_string(),
                }
            } else {
                return Err(tera::Error::message(format!(
                    "Series not found: {series_name}"
                )));
            }
        } else if slug.starts_with("stream-") {
            // Stream slug: stream-{name}
            // This is a special case, because streams are not prefixed with stream-
            let stream_name = slug
                .strip_prefix("stream-")
                .ok_or_else(|| tera::Error::message("Invalid stream slug format"))?;
            if let Some(stream_contents) = self.site_data.stream.map.get(stream_name) {
                let display_name_fn = DisplayName {
                    site_data: self.site_data.clone(),
                    kind: "stream".to_string(),
                };
                let title = display_name_fn.resolve(stream_name);

                let description = format!("{} posts", stream_contents.len());

                let image = stream_contents
                    .first()
                    .and_then(|content| content.banner_image.as_ref())
                    .unwrap_or(&self.site_data.site.banner_image)
                    .clone();

                SlugData {
                    image,
                    slug: stream_name.to_string(),
                    title,
                    text: description,
                    content_type: "stream".to_string(),
                }
            } else {
                return Err(tera::Error::message(format!(
                    "Stream not found: {stream_name}"
                )));
            }
        } else if slug.starts_with("tag-") {
            // Tag slug: tag-{name}
            let tag_name = slug
                .strip_prefix("tag-")
                .ok_or_else(|| tera::Error::message("Invalid tag slug format"))?;
            if let Some(tag_contents) = self.site_data.tag.map.get(tag_name) {
                let image = tag_contents
                    .first()
                    .and_then(|content| content.banner_image.as_ref())
                    .unwrap_or(&self.site_data.site.banner_image)
                    .clone();

                SlugData {
                    image,
                    slug: slug.to_string(),
                    title: tag_name.to_string(),
                    text: format!("{} posts", tag_contents.len()),
                    content_type: "tag".to_string(),
                }
            } else {
                return Err(tera::Error::message(format!("Tag not found: {tag_name}")));
            }
        } else if slug.starts_with("author-") {
            // Author slug: author-{name}
            let author_name = slug
                .strip_prefix("author-")
                .ok_or_else(|| tera::Error::message("Invalid author slug format"))?;
            if let Some(author_contents) = self.site_data.author.map.get(author_name) {
                let author_info = self.site_data.site.authors.get(author_name);
                let title = author_info
                    .map_or(&author_name.to_string(), |a| &a.name)
                    .clone();

                let image = author_info
                    .and_then(|a| a.avatar.as_ref())
                    .unwrap_or(&self.site_data.site.banner_image)
                    .clone();

                SlugData {
                    image,
                    slug: slug.to_string(),
                    title,
                    text: format!("{} posts", author_contents.len()),
                    content_type: "author".to_string(),
                }
            } else {
                return Err(tera::Error::message(format!(
                    "Author not found: {author_name}"
                )));
            }
        } else if slug.starts_with("archive-") {
            // Archive slug: archive-{year}
            let year = slug
                .strip_prefix("archive-")
                .ok_or_else(|| tera::Error::message("Invalid archive slug format"))?;
            if let Some(archive_contents) = self.site_data.archive.map.get(year) {
                let image = archive_contents
                    .first()
                    .and_then(|content| content.banner_image.as_ref())
                    .unwrap_or(&self.site_data.site.banner_image)
                    .clone();

                SlugData {
                    image,
                    slug: slug.to_string(),
                    title: format!("Posts from {year}"),
                    text: format!("{} posts", archive_contents.len()),
                    content_type: "archive".to_string(),
                }
            } else {
                return Err(tera::Error::message(format!(
                    "Archive year not found: {year}"
                )));
            }
        } else {
            // Check if it's a page
            if let Some(page) = self.site_data.pages.iter().find(|p| p.slug == slug) {
                SlugData {
                    image: page
                        .banner_image
                        .as_ref()
                        .unwrap_or(&self.site_data.site.banner_image)
                        .clone(),
                    slug: slug.to_string(),
                    title: page.title.clone(),
                    text: page.description.as_ref().unwrap_or(&String::new()).clone(),
                    content_type: "page".to_string(),
                }
            } else if let Some(post) = self.site_data.posts.iter().find(|p| p.slug == slug) {
                // Check if it's a post
                SlugData {
                    image: post
                        .banner_image
                        .as_ref()
                        .unwrap_or(&self.site_data.site.banner_image)
                        .clone(),
                    slug: slug.to_string(),
                    title: post.title.clone(),
                    text: post
                        .date
                        .map_or_else(String::new, |d| d.format("%Y-%m-%d").to_string()),
                    content_type: "post".to_string(),
                }
            } else if self.site_data.stream.map.contains_key(slug) {
                // Check if it's a Stream (streams does not start with stream-, those are just bare slugs)
                // to identify we must look to the site data streams map
                let stream_name = slug;
                let stream_contents =
                    self.site_data.stream.map.get(stream_name).ok_or_else(|| {
                        tera::Error::message(format!("Stream not found: {stream_name}"))
                    })?;
                let display_name_fn = DisplayName {
                    site_data: self.site_data.clone(),
                    kind: "stream".to_string(),
                };
                let title = display_name_fn.resolve(stream_name);
                SlugData {
                    image: stream_contents
                        .first()
                        .and_then(|content| content.banner_image.as_ref())
                        .unwrap_or(&self.site_data.site.banner_image)
                        .clone(),
                    slug: slug.to_string(),
                    title,
                    text: format!("{} posts", stream_contents.len()),
                    content_type: "stream".to_string(),
                }
            } else {
                return Err(tera::Error::message(format!(
                    "Content not found for slug: {slug}"
                )));
            }
        };

        Value::try_from_serializable(&slug_data)
    }
}

/// Tera function to get gallery data by path
/// Takes a gallery path and returns the Gallery struct if it exists
pub struct GetGallery {
    pub site_data: Data,
}

impl tera::Function<TeraResult<Value>> for GetGallery {
    fn call(&self, kwargs: Kwargs, _: &State) -> TeraResult<Value> {
        let path: &str = kwargs.must_get("path")?;

        log::info!("GetGallery called with path: {path}");
        log::info!(
            "Available galleries: {:?}",
            self.site_data.galleries.keys().collect::<Vec<_>>()
        );

        // Get the gallery from site_data
        if let Some(gallery) = self.site_data.galleries.get(path) {
            log::info!("Gallery found for path: {path}");
            Ok(Value::try_from_serializable(gallery)?)
        } else {
            log::info!("Gallery not found for path: {path}");
            // Return null if gallery not found
            Ok(Value::none())
        }
    }
}

#[cfg(test)]
#[path = "tests/tera_functions.rs"]
mod tests;