typstify-generator 0.1.3

Static site generation engine
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
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
//! HTML generation from parsed content.
//!
//! Converts parsed content into final HTML pages using templates.

use std::path::{Path, PathBuf};

use chrono::{Datelike, Utc};
use thiserror::Error;
use tracing::debug;
use typstify_core::{Config, Page};

use crate::template::{Template, TemplateContext, TemplateError, TemplateRegistry};

/// HTML generation errors.
#[derive(Debug, Error)]
pub enum HtmlError {
    /// Template error.
    #[error("template error: {0}")]
    Template(#[from] TemplateError),

    /// IO error.
    #[error("IO error: {0}")]
    Io(#[from] std::io::Error),

    /// Invalid page data.
    #[error("invalid page data: {0}")]
    InvalidPage(String),
}

/// Result type for HTML generation.
pub type Result<T> = std::result::Result<T, HtmlError>;

/// HTML page generator.
#[derive(Debug)]
pub struct HtmlGenerator {
    templates: TemplateRegistry,
    config: Config,
}

impl HtmlGenerator {
    /// Create a new HTML generator with the given configuration.
    #[must_use]
    pub fn new(config: Config) -> Self {
        Self {
            templates: TemplateRegistry::new(),
            config,
        }
    }

    /// Create a generator with custom templates.
    #[must_use]
    pub fn with_templates(config: Config, templates: TemplateRegistry) -> Self {
        Self { templates, config }
    }

    /// Register a custom template.
    pub fn register_template(&mut self, template: Template) {
        self.templates.register(template);
    }

    /// Generate HTML for a page.
    pub fn generate_page(&self, page: &Page, alternates: &[(&str, &str)]) -> Result<String> {
        debug!(url = %page.url, "generating HTML for page");

        // Determine which template to use
        let template_name =
            page.template
                .as_deref()
                .unwrap_or(if page.date.is_some() { "post" } else { "page" });

        // Build inner content context
        let inner_ctx = self.build_page_context(page)?;
        let inner_html = self.templates.render(template_name, &inner_ctx)?;

        // Build outer (base) context
        let base_ctx = self.build_base_context(page, &inner_html, alternates)?;
        Ok(self.templates.render("base", &base_ctx)?)
    }

    /// Generate redirect HTML for URL aliases.
    pub fn generate_redirect(&self, redirect_url: &str) -> Result<String> {
        let ctx = TemplateContext::new().with_var("redirect_url", redirect_url);
        self.templates
            .render("redirect", &ctx)
            .map_err(HtmlError::from)
    }

    /// Generate a list page HTML.
    pub fn generate_list_page(
        &self,
        title: &str,
        items_html: &str,
        pagination_html: Option<&str>,
    ) -> Result<String> {
        let mut ctx = TemplateContext::new()
            .with_var("title", title)
            .with_var("items", items_html);

        if let Some(pagination) = pagination_html {
            ctx.insert("pagination", pagination);
        }

        let inner_html = self.templates.render("list", &ctx)?;

        // Wrap in base template
        let base_ctx = TemplateContext::new()
            .with_var("lang", &self.config.site.default_language)
            .with_var("title", title)
            .with_var(
                "site_title_suffix",
                format!(" | {}", self.config.site.title),
            )
            .with_var("canonical_url", &self.config.site.base_url)
            .with_var("content", &inner_html)
            .with_var("site_title", &self.config.site.title)
            .with_var("year", Utc::now().year().to_string())
            // Navigation URLs
            .with_var("nav_home_url", "/")
            .with_var("nav_posts_url", "/posts")
            .with_var("nav_archives_url", "/archives")
            .with_var("nav_tags_url", "/tags")
            .with_var("nav_about_url", "/about");

        Ok(self.templates.render("base", &base_ctx)?)
    }

    /// Generate a taxonomy term page HTML.
    pub fn generate_taxonomy_page(
        &self,
        taxonomy_name: &str,
        term: &str,
        items_html: &str,
        pagination_html: Option<&str>,
    ) -> Result<String> {
        let mut ctx = TemplateContext::new()
            .with_var("taxonomy_name", taxonomy_name)
            .with_var("term", term)
            .with_var("items", items_html);

        if let Some(pagination) = pagination_html {
            ctx.insert("pagination", pagination);
        }

        let inner_html = self.templates.render("taxonomy", &ctx)?;
        let title = format!("{taxonomy_name}: {term}");

        // Wrap in base template
        let base_ctx = TemplateContext::new()
            .with_var("lang", &self.config.site.default_language)
            .with_var("title", &title)
            .with_var(
                "site_title_suffix",
                format!(" | {}", self.config.site.title),
            )
            .with_var(
                "canonical_url",
                format!(
                    "{}/{}/{}",
                    self.config.site.base_url,
                    taxonomy_name.to_lowercase(),
                    term
                ),
            )
            .with_var("content", &inner_html)
            .with_var("site_title", &self.config.site.title)
            .with_var("year", Utc::now().year().to_string())
            // Navigation URLs
            .with_var("nav_home_url", "/")
            .with_var("nav_posts_url", "/posts")
            .with_var("nav_archives_url", "/archives")
            .with_var("nav_tags_url", "/tags")
            .with_var("nav_about_url", "/about");

        Ok(self.templates.render("base", &base_ctx)?)
    }

    /// Build template context for page content.
    fn build_page_context(&self, page: &Page) -> Result<TemplateContext> {
        let mut ctx = TemplateContext::new()
            .with_var("title", &page.title)
            .with_var("content", &page.content);

        // Add date if present
        if let Some(date) = page.date {
            ctx.insert("date_iso", date.format("%Y-%m-%d").to_string());
            ctx.insert("date_formatted", date.format("%B %d, %Y").to_string());
        }

        // Add tags HTML if present
        if !page.tags.is_empty() {
            let tags_html = page
                .tags
                .iter()
                .map(|tag| {
                    format!(
                        r#"<a href="/tags/{}" rel="tag">{}</a>"#,
                        slug_from_str(tag),
                        tag
                    )
                })
                .collect::<Vec<_>>()
                .join(" ");
            ctx.insert(
                "tags_html",
                format!(r#"<div class="tags">{tags_html}</div>"#),
            );
        }

        Ok(ctx)
    }

    /// Build template context for base HTML wrapper.
    fn build_base_context(
        &self,
        page: &Page,
        inner_html: &str,
        alternates: &[(&str, &str)],
    ) -> Result<TemplateContext> {
        // Determine language prefix for URLs
        let lang_prefix = if page.is_default_lang {
            String::new()
        } else {
            format!("/{}", page.lang)
        };

        let mut ctx = TemplateContext::new()
            .with_var("lang", &page.lang)
            .with_var("title", &page.title)
            .with_var(
                "site_title_suffix",
                format!(" | {}", self.config.title_for_language(&page.lang)),
            )
            .with_var(
                "canonical_url",
                format!("{}{}", self.config.site.base_url, page.url),
            )
            .with_var("content", inner_html)
            .with_var("site_title", self.config.title_for_language(&page.lang))
            .with_var("year", Utc::now().year().to_string())
            // Navigation URLs with language prefix
            .with_var("nav_home_url", format!("{lang_prefix}/"))
            .with_var("nav_posts_url", format!("{lang_prefix}/posts"))
            .with_var("nav_archives_url", format!("{lang_prefix}/archives"))
            .with_var("nav_tags_url", format!("{lang_prefix}/tags"))
            .with_var("nav_about_url", format!("{lang_prefix}/about"));

        // Add description if present
        if let Some(desc) = &page.description {
            ctx.insert("description", desc);
        } else if let Some(site_desc) = self.config.description_for_language(&page.lang) {
            ctx.insert("description", site_desc);
        }

        // Add author if present
        if let Some(author) = &self.config.site.author {
            ctx.insert("author", author);
        }

        // Add custom CSS
        if !page.custom_css.is_empty() {
            let css_links = page
                .custom_css
                .iter()
                .map(|href| format!(r#"<link rel="stylesheet" href="{href}">"#))
                .collect::<Vec<_>>()
                .join("\n");
            ctx.insert("custom_css", css_links);
        }

        // Add custom JS
        if !page.custom_js.is_empty() {
            let js_scripts = page
                .custom_js
                .iter()
                .map(|src| format!(r#"<script src="{src}"></script>"#))
                .collect::<Vec<_>>()
                .join("\n");
            ctx.insert("custom_js", js_scripts);
        }

        // Generate language switcher HTML
        let lang_switcher = self.generate_lang_switcher(&page.lang, &page.canonical_id);
        if !lang_switcher.is_empty() {
            ctx.insert("lang_switcher", lang_switcher);
        }

        // Add hreflang tags
        if !alternates.is_empty() {
            let hreflang = alternates
                .iter()
                .map(|(lang, url)| {
                    format!(
                        r#"<link rel="alternate" hreflang="{}" href="{}{}" />"#,
                        lang, self.config.site.base_url, url
                    )
                })
                .collect::<Vec<_>>()
                .join("\n");
            ctx.insert("hreflang", hreflang);
        }

        Ok(ctx)
    }

    /// Generate language switcher HTML dropdown.
    fn generate_lang_switcher(&self, current_lang: &str, canonical_id: &str) -> String {
        let all_langs = self.config.all_languages();
        if all_langs.len() <= 1 {
            return String::new();
        }

        let mut options = Vec::new();

        for lang in &all_langs {
            let name = self.config.language_name(lang);
            let url = if *lang == self.config.site.default_language {
                // Default language: no prefix
                if canonical_id.is_empty() {
                    "/".to_string()
                } else {
                    format!("/{canonical_id}")
                }
            } else {
                // Non-default language: add prefix
                if canonical_id.is_empty() {
                    format!("/{lang}/")
                } else {
                    format!("/{lang}/{canonical_id}")
                }
            };

            let selected_class = if *lang == current_lang { " active" } else { "" };
            options.push(format!(
                r#"<a href="{url}" class="lang-option{selected_class}">{name}</a>"#,
            ));
        }

        // Get the language code for display (uppercase, max 2 chars)
        let display_code = current_lang
            .chars()
            .take(2)
            .collect::<String>()
            .to_uppercase();

        format!(
            r#"<div class="lang-switcher" tabindex="0" role="button" aria-label="Switch language" aria-haspopup="true">
    <span class="lang-code">{}</span>
    <div class="lang-dropdown">{}</div>
</div>"#,
            display_code,
            options.join("\n        ")
        )
    }

    /// Get the output path for a page.
    #[must_use]
    pub fn output_path(&self, page: &Page, output_dir: &Path) -> PathBuf {
        let relative = page.url.trim_start_matches('/');

        if relative.is_empty() {
            output_dir.join("index.html")
        } else {
            output_dir.join(relative).join("index.html")
        }
    }

    /// Generate a tags index page listing all tags with their counts.
    pub fn generate_tags_index_page(
        &self,
        tags: &std::collections::HashMap<String, Vec<String>>,
        lang: &str,
    ) -> Result<String> {
        let is_default_lang = lang == self.config.site.default_language;
        let lang_prefix = if is_default_lang {
            String::new()
        } else {
            format!("/{lang}")
        };

        let mut items: Vec<_> = tags.iter().collect();
        items.sort_by(|a, b| b.1.len().cmp(&a.1.len())); // Sort by count descending

        let items_html: String = items
            .iter()
            .map(|(tag, pages)| {
                format!(
                    r#"<a href="{}/tags/{}" class="tag-item"><span class="tag-name">{}</span><span class="tag-count">{}</span></a>"#,
                    lang_prefix,
                    slug_from_str(tag),
                    tag,
                    pages.len()
                )
            })
            .collect::<Vec<_>>()
            .join("\n");

        let ctx = TemplateContext::new().with_var("items", &items_html);
        let inner_html = self.templates.render("tags_index", &ctx)?;

        let mut base_ctx = TemplateContext::new()
            .with_var("lang", lang)
            .with_var("title", "Tags")
            .with_var(
                "site_title_suffix",
                format!(" | {}", self.config.title_for_language(lang)),
            )
            .with_var(
                "canonical_url",
                format!("{}{}/tags", self.config.site.base_url, lang_prefix),
            )
            .with_var("content", &inner_html)
            .with_var("site_title", self.config.title_for_language(lang))
            .with_var("year", Utc::now().year().to_string())
            // Navigation URLs
            .with_var("nav_home_url", format!("{lang_prefix}/"))
            .with_var("nav_posts_url", format!("{lang_prefix}/posts"))
            .with_var("nav_archives_url", format!("{lang_prefix}/archives"))
            .with_var("nav_tags_url", format!("{lang_prefix}/tags"))
            .with_var("nav_about_url", format!("{lang_prefix}/about"));

        // Generate language switcher
        let lang_switcher = self.generate_lang_switcher(lang, "tags");
        if !lang_switcher.is_empty() {
            base_ctx.insert("lang_switcher", lang_switcher);
        }

        Ok(self.templates.render("base", &base_ctx)?)
    }

    /// Generate a categories index page listing all categories with their counts.
    pub fn generate_categories_index_page(
        &self,
        categories: &std::collections::HashMap<String, Vec<String>>,
        lang: &str,
    ) -> Result<String> {
        let is_default_lang = lang == self.config.site.default_language;
        let lang_prefix = if is_default_lang {
            String::new()
        } else {
            format!("/{lang}")
        };

        let mut items: Vec<_> = categories.iter().collect();
        items.sort_by(|a, b| a.0.cmp(b.0)); // Sort alphabetically

        let items_html: String = items
            .iter()
            .map(|(category, pages)| {
                format!(
                    r#"<li><a href="{}/categories/{}">{}</a> <span class="count">({})</span></li>"#,
                    lang_prefix,
                    slug_from_str(category),
                    category,
                    pages.len()
                )
            })
            .collect::<Vec<_>>()
            .join("\n");

        let ctx = TemplateContext::new().with_var("items", &items_html);
        let inner_html = self.templates.render("categories_index", &ctx)?;

        let mut base_ctx = TemplateContext::new()
            .with_var("lang", lang)
            .with_var("title", "Categories")
            .with_var(
                "site_title_suffix",
                format!(" | {}", self.config.title_for_language(lang)),
            )
            .with_var(
                "canonical_url",
                format!("{}{}/categories", self.config.site.base_url, lang_prefix),
            )
            .with_var("content", &inner_html)
            .with_var("site_title", self.config.title_for_language(lang))
            .with_var("year", Utc::now().year().to_string())
            // Navigation URLs
            .with_var("nav_home_url", format!("{lang_prefix}/"))
            .with_var("nav_posts_url", format!("{lang_prefix}/posts"))
            .with_var("nav_archives_url", format!("{lang_prefix}/archives"))
            .with_var("nav_tags_url", format!("{lang_prefix}/tags"))
            .with_var("nav_about_url", format!("{lang_prefix}/about"));

        // Generate language switcher
        let lang_switcher = self.generate_lang_switcher(lang, "categories");
        if !lang_switcher.is_empty() {
            base_ctx.insert("lang_switcher", lang_switcher);
        }

        Ok(self.templates.render("base", &base_ctx)?)
    }

    /// Generate an archives page listing all posts grouped by year.
    pub fn generate_archives_page(&self, pages: &[&Page], lang: &str) -> Result<String> {
        use std::collections::BTreeMap;

        let is_default_lang = lang == self.config.site.default_language;
        let lang_prefix = if is_default_lang {
            String::new()
        } else {
            format!("/{lang}")
        };

        // Group pages by year
        let mut by_year: BTreeMap<i32, Vec<&Page>> = BTreeMap::new();
        for page in pages {
            if let Some(date) = page.date {
                by_year.entry(date.year()).or_default().push(page);
            }
        }

        // Sort pages within each year by date (newest first)
        for pages in by_year.values_mut() {
            pages.sort_by(|a, b| b.date.cmp(&a.date));
        }

        // Generate HTML (years in descending order)
        let items_html: String = by_year
            .iter()
            .rev()
            .map(|(year, year_pages)| {
                let posts_html: String = year_pages
                    .iter()
                    .map(|p| {
                        let date_str = p
                            .date
                            .map(|d| d.format("%m-%d").to_string())
                            .unwrap_or_default();
                        format!(
                            r#"<li><span class="archive-date">{}</span><a href="{}">{}</a></li>"#,
                            date_str, p.url, p.title
                        )
                    })
                    .collect::<Vec<_>>()
                    .join("\n");

                format!(r#"<div class="archive-year"><h2>{year}</h2><ul>{posts_html}</ul></div>"#,)
            })
            .collect::<Vec<_>>()
            .join("\n");

        let ctx = TemplateContext::new().with_var("items", &items_html);
        let inner_html = self.templates.render("archives", &ctx)?;

        let mut base_ctx = TemplateContext::new()
            .with_var("lang", lang)
            .with_var("title", "Archives")
            .with_var(
                "site_title_suffix",
                format!(" | {}", self.config.title_for_language(lang)),
            )
            .with_var(
                "canonical_url",
                format!("{}{}/archives", self.config.site.base_url, lang_prefix),
            )
            .with_var("content", &inner_html)
            .with_var("site_title", self.config.title_for_language(lang))
            .with_var("year", Utc::now().year().to_string())
            // Navigation URLs
            .with_var("nav_home_url", format!("{lang_prefix}/"))
            .with_var("nav_posts_url", format!("{lang_prefix}/posts"))
            .with_var("nav_archives_url", format!("{lang_prefix}/archives"))
            .with_var("nav_tags_url", format!("{lang_prefix}/tags"))
            .with_var("nav_about_url", format!("{lang_prefix}/about"));

        // Generate language switcher
        let lang_switcher = self.generate_lang_switcher(lang, "archives");
        if !lang_switcher.is_empty() {
            base_ctx.insert("lang_switcher", lang_switcher);
        }

        Ok(self.templates.render("base", &base_ctx)?)
    }

    /// Generate a section index page (e.g., /posts/).
    pub fn generate_section_page(
        &self,
        section: &str,
        description: Option<&str>,
        items_html: &str,
        pagination_html: Option<&str>,
        lang: &str,
    ) -> Result<String> {
        let is_default_lang = lang == self.config.site.default_language;
        let lang_prefix = if is_default_lang {
            String::new()
        } else {
            format!("/{lang}")
        };

        // Convert section name to title case
        let title = section
            .chars()
            .next()
            .map(|c| c.to_uppercase().collect::<String>() + &section[1..])
            .unwrap_or_else(|| section.to_string());

        let mut ctx = TemplateContext::new()
            .with_var("title", &title)
            .with_var("items", items_html);

        if let Some(desc) = description {
            ctx.insert("description", desc);
        }

        if let Some(pagination) = pagination_html {
            ctx.insert("pagination", pagination);
        }

        let inner_html = self.templates.render("section", &ctx)?;

        let mut base_ctx = TemplateContext::new()
            .with_var("lang", lang)
            .with_var("title", &title)
            .with_var(
                "site_title_suffix",
                format!(" | {}", self.config.title_for_language(lang)),
            )
            .with_var(
                "canonical_url",
                format!("{}{}/{}", self.config.site.base_url, lang_prefix, section),
            )
            .with_var("content", &inner_html)
            .with_var("site_title", self.config.title_for_language(lang))
            .with_var("year", Utc::now().year().to_string())
            // Navigation URLs
            .with_var("nav_home_url", format!("{lang_prefix}/"))
            .with_var("nav_posts_url", format!("{lang_prefix}/posts"))
            .with_var("nav_archives_url", format!("{lang_prefix}/archives"))
            .with_var("nav_tags_url", format!("{lang_prefix}/tags"))
            .with_var("nav_about_url", format!("{lang_prefix}/about"));

        // Generate language switcher
        let lang_switcher = self.generate_lang_switcher(lang, section);
        if !lang_switcher.is_empty() {
            base_ctx.insert("lang_switcher", lang_switcher);
        }

        Ok(self.templates.render("base", &base_ctx)?)
    }
}

/// Generate a URL-safe slug from a string.
fn slug_from_str(s: &str) -> String {
    s.to_lowercase()
        .chars()
        .map(|c| if c.is_alphanumeric() { c } else { '-' })
        .collect::<String>()
        .split('-')
        .filter(|s| !s.is_empty())
        .collect::<Vec<_>>()
        .join("-")
}

/// Generate HTML for a list item (used in list pages).
pub fn list_item_html(page: &Page) -> String {
    let date_html = page
        .date
        .map(|d| {
            format!(
                r#"<time datetime="{}">{}</time>"#,
                d.format("%Y-%m-%d"),
                d.format("%Y-%m-%d")
            )
        })
        .unwrap_or_default();

    let description_html = page
        .description
        .as_ref()
        .filter(|d| !d.is_empty())
        .map(|d| format!(r#"<p class="post-description">{d}</p>"#))
        .unwrap_or_default();

    format!(
        r#"<li class="post-item">
    <div class="post-item-header">
        <a href="{}" class="post-title">{}</a>
        {}
    </div>
    {}
</li>"#,
        page.url, page.title, date_html, description_html
    )
}

/// Generate pagination HTML.
pub fn pagination_html(current: usize, total: usize, base_url: &str) -> Option<String> {
    if total <= 1 {
        return None;
    }

    let mut parts = Vec::new();

    if current > 1 {
        let prev_url = if current == 2 {
            base_url.to_string()
        } else {
            format!("{}/page/{}", base_url, current - 1)
        };
        parts.push(format!(r#"<a href="{prev_url}" rel="prev">← Previous</a>"#));
    }

    parts.push(format!("Page {current} of {total}"));

    if current < total {
        parts.push(format!(
            r#"<a href="{}/page/{}" rel="next">Next →</a>"#,
            base_url,
            current + 1
        ));
    }

    Some(format!(
        r#"<nav class="pagination">{}</nav>"#,
        parts.join(" ")
    ))
}

#[cfg(test)]
mod tests {
    use std::collections::HashMap;

    use super::*;

    fn test_config() -> Config {
        Config {
            site: typstify_core::config::SiteConfig {
                title: "Test Site".to_string(),
                base_url: "https://example.com".to_string(),
                default_language: "en".to_string(),
                description: Some("A test site".to_string()),
                author: Some("Test Author".to_string()),
            },
            languages: HashMap::new(),
            build: typstify_core::config::BuildConfig::default(),
            search: typstify_core::config::SearchConfig::default(),
            rss: typstify_core::config::RssConfig::default(),
            robots: typstify_core::config::RobotsConfig::default(),
            taxonomies: typstify_core::config::TaxonomyConfig::default(),
        }
    }

    fn test_page() -> Page {
        Page {
            url: "/test-page".to_string(),
            title: "Test Page".to_string(),
            description: Some("A test page".to_string()),
            date: None,
            updated: None,
            draft: false,
            lang: "en".to_string(),
            is_default_lang: true,
            canonical_id: "test-page".to_string(),
            tags: vec![],
            categories: vec![],
            content: "<p>Hello, World!</p>".to_string(),
            summary: None,
            reading_time: None,
            word_count: None,
            toc: vec![],
            custom_js: vec![],
            custom_css: vec![],
            aliases: vec![],
            template: None,
            weight: 0,
            source_path: Some(PathBuf::from("test-page.md")),
        }
    }

    #[test]
    fn test_generate_page() {
        let generator = HtmlGenerator::new(test_config());
        let page = test_page();

        let html = generator.generate_page(&page, &[]).unwrap();

        assert!(html.contains("<!DOCTYPE html>"));
        assert!(html.contains("<title>Test Page | Test Site</title>"));
        assert!(html.contains("<p>Hello, World!</p>"));
        assert!(html.contains("Test Site"));
    }

    #[test]
    fn test_generate_redirect() {
        let generator = HtmlGenerator::new(test_config());

        let html = generator
            .generate_redirect("https://example.com/new-url")
            .unwrap();

        assert!(html.contains("Redirecting"));
        assert!(html.contains("https://example.com/new-url"));
        assert!(html.contains(r#"http-equiv="refresh""#));
    }

    #[test]
    fn test_slug_from_str() {
        assert_eq!(slug_from_str("Hello World"), "hello-world");
        assert_eq!(slug_from_str("Rust & Go"), "rust-go");
        assert_eq!(slug_from_str("  multiple   spaces  "), "multiple-spaces");
        assert_eq!(slug_from_str("CamelCase"), "camelcase");
    }

    #[test]
    fn test_list_item_html() {
        let page = test_page();
        let html = list_item_html(&page);

        assert!(html.contains(r#"<li class="post-item">"#));
        assert!(html.contains("post-title"));
        assert!(html.contains("Test Page"));
        assert!(html.contains("/test-page"));
    }

    #[test]
    fn test_pagination_html() {
        // Single page - no pagination
        assert!(pagination_html(1, 1, "/blog").is_none());

        // First page of many
        let html = pagination_html(1, 5, "/blog").unwrap();
        assert!(html.contains("Page 1 of 5"));
        assert!(html.contains("Next →"));
        assert!(!html.contains("Previous"));

        // Middle page
        let html = pagination_html(3, 5, "/blog").unwrap();
        assert!(html.contains("Page 3 of 5"));
        assert!(html.contains("Previous"));
        assert!(html.contains("Next →"));

        // Last page
        let html = pagination_html(5, 5, "/blog").unwrap();
        assert!(html.contains("Page 5 of 5"));
        assert!(html.contains("Previous"));
        assert!(!html.contains("Next →"));
    }

    #[test]
    fn test_output_path() {
        let generator = HtmlGenerator::new(test_config());
        let output_dir = Path::new("public");

        let page = test_page();
        let path = generator.output_path(&page, output_dir);
        assert_eq!(path, PathBuf::from("public/test-page/index.html"));

        // Root page
        let mut root_page = test_page();
        root_page.url = "/".to_string();
        let path = generator.output_path(&root_page, output_dir);
        assert_eq!(path, PathBuf::from("public/index.html"));
    }
}