krik 0.1.27

A fast static site generator written in Rust with internationalization, theming, and modern web features
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
use crate::error::{KrikError, KrikResult, ThemeError, ThemeErrorKind};
use crate::i18n::I18nManager;
use crate::parser::Document;
use crate::site::SiteConfig;
use crate::theme::Theme;
use std::collections::HashMap;
use std::ffi::OsStr;
use std::path::{Path, PathBuf};
use tracing::{debug, error, info, warn};

#[derive(Debug)]
pub enum ChangeType {
    ThemeRelated,
    SiteConfig,
    Markdown { relative_path: String },
    Asset,
    Unrelated,
}

/// The main site generator that processes Markdown files and creates a static website.
///
/// `SiteGenerator` handles the entire process of creating a static site:
/// - Scanning and parsing Markdown files with front matter
/// - Processing internationalization and translations
/// - Applying themes and templates
/// - Generating HTML output with navigation features
/// - Creating Atom feeds
///
/// # Example
///
/// ```rust,no_run
/// use krik::generator::SiteGenerator;
/// use std::path::PathBuf;
///
/// let mut generator = SiteGenerator::new(
///     "content",           // Source directory
///     "_site",            // Output directory  
///     Some("themes/custom") // Optional theme directory
/// )?;
///
/// generator.scan_files()?;
/// generator.generate_site()?;
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
#[derive(Debug)]
pub struct SiteGenerator {
    /// Source directory containing Markdown files and assets
    pub source_dir: PathBuf,
    /// Output directory where the generated site will be written
    pub output_dir: PathBuf,
    /// Theme configuration and templates
    pub theme: Theme,
    /// Site-wide configuration loaded from site.toml
    pub site_config: SiteConfig,
    /// Parsed documents ready for processing
    pub documents: Vec<Document>,
    /// Incremental cache: map from relative file path to Document
    pub document_cache: HashMap<String, Document>,
}

impl SiteGenerator {
    /// Creates a new `SiteGenerator` instance.
    ///
    /// # Arguments
    ///
    /// * `source_dir` - Directory containing Markdown files and content
    /// * `output_dir` - Directory where generated HTML will be written  
    /// * `theme_dir` - Optional custom theme directory (defaults to `themes/default`)
    ///
    /// # Returns
    ///
    /// Returns a `Result` containing the `SiteGenerator` or an error if initialization fails.
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// use krik::generator::SiteGenerator;
    ///
    /// // Using default theme
    /// let generator = SiteGenerator::new("content", "_site", None::<&str>)?;
    ///
    /// // Using custom theme
    /// let generator = SiteGenerator::new("content", "_site", Some("my-theme"))?;
    /// # Ok::<(), Box<dyn std::error::Error>>(())
    /// ```
    pub fn new<P: AsRef<Path>>(
        source_dir: P,
        output_dir: P,
        theme_dir: Option<P>,
    ) -> KrikResult<Self> {
        // Normalize paths to avoid mismatches between absolute and relative paths
        let mut source_dir = source_dir.as_ref().to_path_buf();
        if let Ok(abs) = std::fs::canonicalize(&source_dir) {
            source_dir = abs;
        }

        let mut output_dir = output_dir.as_ref().to_path_buf();
        // Output directory might not exist yet; canonicalize only if it does
        if output_dir.exists() {
            if let Ok(abs) = std::fs::canonicalize(&output_dir) {
                output_dir = abs;
            }
        }

        let theme = if let Some(theme_path) = theme_dir {
            let mut path = theme_path.as_ref().to_path_buf();
            if let Ok(abs) = std::fs::canonicalize(&path) {
                path = abs;
            }
            Theme::builder()
                .theme_path(&path)
                .autoescape_html(false)
                .enable_reload(false)
                .build()
                .map_err(|_| {
                    KrikError::Theme(Box::new(ThemeError {
                        kind: ThemeErrorKind::NotFound,
                        theme_path: path.clone(),
                        context: format!("Loading custom theme from {}", path.display()),
                    }))
                })?
        } else {
            let default_path = PathBuf::from("themes/default");
            let default_path = if let Ok(abs) = std::fs::canonicalize(&default_path) {
                abs
            } else {
                default_path
            };
            Theme::builder()
                .theme_path(&default_path)
                .autoescape_html(false)
                .enable_reload(false)
                .build()
                .unwrap_or_else(|_| Theme {
                    config: crate::theme::ThemeConfig {
                        name: "default".to_string(),
                        version: "1.0.0".to_string(),
                        author: None,
                        description: None,
                        templates: HashMap::new(),
                    },
                    templates: tera::Tera::new("themes/default/templates/**/*").unwrap_or_default(),
                    theme_path: default_path,
                })
        };

        // Load site configuration with proper error handling
        let site_config = SiteConfig::load_from_path(&source_dir).unwrap_or_else(|e| {
            warn!(
                "Failed to load site configuration: {}. Falling back to defaults.",
                e
            );
            SiteConfig::default()
        });

        Ok(Self {
            source_dir,
            output_dir,
            theme,
            site_config,
            documents: Vec::new(),
            document_cache: HashMap::new(),
        })
    }

    /// Scan files in the source directory and parse markdown documents
    pub fn scan_files(&mut self) -> KrikResult<()> {
        info!(
            "Scanning for markdown files in: {}",
            self.source_dir.display()
        );
        // Full scan rebuilds the cache
        self.document_cache.clear();
        self.documents.clear();
        let result = super::markdown::scan_files(&self.source_dir, &mut self.documents).map_err(
            |e| match e {
                KrikError::Generation(gen_err) => KrikError::Generation(gen_err),
                other => other,
            },
        );

        match &result {
            Ok(_) => {
                // Populate cache from documents
                for doc in &self.documents {
                    self.document_cache
                        .insert(doc.file_path.clone(), doc.clone());
                }
                info!("Successfully scanned {} documents", self.documents.len())
            }
            Err(e) => error!("Failed to scan files: {}", e),
        }

        result
    }

    /// Generate the complete static site
    ///
    /// This orchestrates the entire site generation process:
    /// 1. Copy non-markdown files and theme assets
    /// 2. Generate HTML pages from documents
    /// 3. Generate index page with post listings
    /// 4. Generate Atom feed
    /// 5. Generate XML sitemap
    /// 6. Generate robots.txt
    /// 7. Generate PDFs (if pandoc and typst are available)
    pub fn generate_site(&self) -> KrikResult<()> {
        use super::pipeline::{EmitPhase, RenderPhase, ScanPhase, TransformPhase};

        info!("Starting site generation");
        debug!("Source directory: {}", self.source_dir.display());
        debug!("Output directory: {}", self.output_dir.display());

        let scan = ScanPhase;
        let transform = TransformPhase;
        let render = RenderPhase;
        let emit = EmitPhase;

        // Prepare output
        info!("Preparing output directory");
        emit.ensure_output_dir(&self.output_dir)?;

        // Scan
        info!("Scanning source files");
        let documents = scan.scan(&self.source_dir)?;
        debug!("Found {} documents to process", documents.len());

        // Transform
        info!("Transforming documents");
        let documents = transform.transform(documents, &self.source_dir);

        // Assets
        info!("Copying assets");
        emit.copy_assets(&self.source_dir, &self.theme, &self.output_dir)?;

        // Render
        info!("Rendering pages");
        render.render_pages(&documents, &self.theme, &self.site_config, &self.output_dir)?;
        render.render_index(
            &documents,
            &self.theme,
            &self.site_config,
            &self.output_dir,
        )?;

        // Emit ancillary artifacts
        info!("Generating ancillary files");
        emit.emit_feed(&documents, &self.site_config, &self.output_dir)?;
        emit.emit_sitemap(&documents, &self.site_config, &self.output_dir)?;
        emit.emit_robots(&self.site_config, &self.output_dir)?;

        // Generate PDFs if tools are available
        if super::pdf::PdfGenerator::is_available() {
            info!("PDF generation tools available, generating PDFs");
            match super::pdf::PdfGenerator::new() {
                Ok(pdf_generator) => {
                    match pdf_generator.generate_pdfs(
                        &documents,
                        &self.source_dir,
                        &self.output_dir,
                        &self.site_config,
                    ) {
                        Ok(generated_pdfs) => {
                            if !generated_pdfs.is_empty() {
                                info!(
                                    "Generated {} PDF files alongside their HTML counterparts",
                                    generated_pdfs.len()
                                );
                            }
                        }
                        Err(e) => warn!("PDF generation failed: {}", e),
                    }
                }
                Err(e) => warn!("Could not initialize PDF generator: {}", e),
            }
        } else {
            debug!("PDF generation skipped: pandoc and/or typst not available in PATH");
        }

        info!("Site generation completed successfully");
        Ok(())
    }

    /// Incrementally (re)generate outputs affected by a single changed content or asset file.
    ///
    /// Behavior:
    /// - If a markdown file changed: re-scan just that file, update/emit its HTML, and re-render index/feed/sitemap.
    /// - If a non-markdown content asset changed: copy that single asset into the output.
    /// - If a content file was removed: remove the mirrored output file and refresh index/feed/sitemap.
    /// - If a theme file changed (templates/assets), fall back to full regeneration as templates affect many pages.
    pub fn generate_incremental_for_path<P: AsRef<Path>>(
        &mut self,
        changed_path: P,
        is_removed: bool,
    ) -> KrikResult<()> {
        let changed_path = changed_path.as_ref();
        let change_type =
            analyze_change_type(changed_path, &self.theme.theme_path, &self.source_dir)?;

        match change_type {
            ChangeType::ThemeRelated | ChangeType::SiteConfig => {
                debug!("Theme or site config change detected, triggering full regeneration");
                self.generate_site()
            }
            ChangeType::Markdown { relative_path } => {
                self.handle_markdown_change(&relative_path, changed_path, is_removed)
            }
            ChangeType::Asset => self.handle_asset_change(changed_path, is_removed),
            ChangeType::Unrelated => {
                debug!("Change not related to content or theme, triggering full regeneration");
                self.generate_site()
            }
        }
    }

    /// Find all documents that are language variants of the given document.
    /// Language variants share the same base name but have different language extensions.
    /// For example: "welcome.md", "welcome.it.md", "welcome.fr.md" are all variants.
    fn find_language_variants(&self, target_path: &str) -> Vec<String> {
        let mut variants = Vec::new();

        // Extract base name by removing language and extension
        // Example: "pages/welcome.it.md" -> "pages/welcome"
        let path_buf = std::path::PathBuf::from(target_path);
        let parent = path_buf
            .parent()
            .map(|p| p.to_string_lossy().to_string())
            .unwrap_or_default();

        if let Some(_filename) = path_buf.file_name().and_then(|n| n.to_str()) {
            let base_name = if let Some(stem) = path_buf.file_stem().and_then(|s| s.to_str()) {
                // Check if stem contains a language code (e.g., "welcome.it")
                if let Some(dot_pos) = stem.rfind('.') {
                    let potential_lang = &stem[dot_pos + 1..];
                    // Check if it's a known language code
                    if I18nManager::is_supported_language(&potential_lang)
                    {
                        &stem[..dot_pos] // Remove language part
                    } else {
                        stem // No language code found
                    }
                } else {
                    stem // No dots in stem
                }
            } else {
                return variants;
            };

            // Find all documents with the same base name
            for doc in &self.documents {
                let doc_path_buf = std::path::PathBuf::from(&doc.file_path);
                let doc_parent = doc_path_buf
                    .parent()
                    .map(|p| p.to_string_lossy().to_string())
                    .unwrap_or_default();

                // Must be in same directory
                if doc_parent != parent {
                    continue;
                }

                if let Some(_doc_filename) = doc_path_buf.file_name().and_then(|n| n.to_str()) {
                    if let Some(doc_stem) = doc_path_buf.file_stem().and_then(|s| s.to_str()) {
                        let doc_base_name = if let Some(dot_pos) = doc_stem.rfind('.') {
                            let potential_lang = &doc_stem[dot_pos + 1..];
                            if I18nManager::is_supported_language(&potential_lang) {
                                &doc_stem[..dot_pos]
                            } else {
                                doc_stem
                            }
                        } else {
                            doc_stem
                        };

                        // If base names match, this is a language variant
                        if doc_base_name == base_name {
                            variants.push(doc.file_path.clone());
                        }
                    }
                }
            }
        }

        variants
    }

    /// Handle markdown file changes by updating the document cache and re-rendering affected pages
    fn handle_markdown_change(
        &mut self,
        relative_path: &str,
        changed_path: &Path,
        is_removed: bool,
    ) -> KrikResult<()> {
        use super::pipeline::{EmitPhase, RenderPhase, TransformPhase};

        let transform = TransformPhase;
        let render = RenderPhase;
        let emit = EmitPhase;

        emit.ensure_output_dir(&self.output_dir)?;

        let mut documents = self.documents.clone();

        if is_removed {
            self.handle_markdown_removal(relative_path, &mut documents)
        } else {
            self.handle_markdown_update(relative_path, changed_path, &mut documents)?
        };

        // Transform documents for correct dates before rendering
        let documents = transform.transform(documents, &self.source_dir);

        if !is_removed {
            self.render_language_variants(relative_path, &documents)?;
        }

        // Persist updated working set back into generator state
        self.documents = documents;

        // Update global artifacts that depend on full document set
        debug!("updating global artifacts (index/feed/sitemap/robots) after single-page change");
        render.render_index(
            &self.documents,
            &self.theme,
            &self.site_config,
            &self.output_dir,
        )?;
        emit.emit_feed(&self.documents, &self.site_config, &self.output_dir)?;
        emit.emit_sitemap(&self.documents, &self.site_config, &self.output_dir)?;
        emit.emit_robots(&self.site_config, &self.output_dir)?;

        Ok(())
    }

    /// Handle asset file changes by copying or removing the file
    fn handle_asset_change(&self, changed_path: &Path, is_removed: bool) -> KrikResult<()> {
        use super::pipeline::EmitPhase;

        let emit = EmitPhase;
        emit.ensure_output_dir(&self.output_dir)?;

        if is_removed {
            debug!("removing single asset {}", changed_path.display());
            super::assets::remove_single_asset(&self.source_dir, &self.output_dir, changed_path)
                .map_err(|e| {
                    create_asset_error(
                        "Removing single changed asset",
                        &self.source_dir,
                        &self.output_dir,
                        Box::new(e),
                    )
                })
        } else {
            debug!("copying single asset {}", changed_path.display());
            super::assets::copy_single_asset(&self.source_dir, &self.output_dir, changed_path)
                .map_err(|e| {
                    create_asset_error(
                        "Copying single changed asset",
                        &self.source_dir,
                        &self.output_dir,
                        Box::new(e),
                    )
                })
        }
    }

    /// Handle markdown file removal by cleaning up cache and output files
    fn handle_markdown_removal(&mut self, relative_path: &str, documents: &mut Vec<Document>) {
        debug!("removing generated page for {}", relative_path);
        // Remove from cache and the mirrored HTML file
        if let Some(rel_removed) = self.document_cache.remove(relative_path) {
            // Also remove from current documents vector copy
            documents.retain(|d| d.file_path != rel_removed.file_path);
            // If removed document had a PDF generated, remove corresponding PDF file
            if rel_removed.front_matter.pdf.unwrap_or(false) {
                let mut pdf_rel_path = std::path::PathBuf::from(relative_path);
                pdf_rel_path.set_extension("pdf");
                let pdf_output_path = self.output_dir.join(pdf_rel_path);
                if pdf_output_path.exists() {
                    let _ = std::fs::remove_file(pdf_output_path);
                }
            }
        }
        let mut output_path = std::path::PathBuf::from(relative_path);
        output_path.set_extension("html");
        let output_path = self.output_dir.join(output_path);
        if output_path.exists() {
            let _ = std::fs::remove_file(output_path);
        }
    }

    /// Handle markdown file update by parsing and updating cache
    fn handle_markdown_update(
        &mut self,
        relative_path: &str,
        changed_path: &Path,
        documents: &mut Vec<Document>,
    ) -> KrikResult<()> {
        match super::markdown::parse_single_file(&self.source_dir, changed_path) {
            Ok(doc) => {
                let prev_pdf = self
                    .document_cache
                    .get(&doc.file_path)
                    .and_then(|d| d.front_matter.pdf)
                    .unwrap_or(false);

                // Replace or insert into cache and working set
                self.document_cache
                    .insert(doc.file_path.clone(), doc.clone());
                if let Some(slot) = documents.iter_mut().find(|d| d.file_path == doc.file_path) {
                    *slot = doc;
                } else {
                    documents.push(doc);
                }

                // Handle PDF generation/removal
                self.handle_pdf_change(relative_path, documents, prev_pdf)?;
                Ok(())
            }
            Err(e) => {
                warn!(
                    "Failed to parse changed file {}: {}. Falling back to full rescan.",
                    changed_path.display(),
                    e
                );
                documents.clear();
                super::markdown::scan_files(&self.source_dir, documents)?;
                // rebuild cache from full scan
                self.document_cache.clear();
                for d in documents {
                    self.document_cache.insert(d.file_path.clone(), d.clone());
                }
                Ok(())
            }
        }
    }

    /// Handle PDF generation or removal based on document settings
    fn handle_pdf_change(
        &self,
        relative_path: &str,
        documents: &[Document],
        prev_pdf: bool,
    ) -> KrikResult<()> {
        if let Some(current_doc) = documents.iter().find(|d| d.file_path == relative_path) {
            let current_pdf = current_doc.front_matter.pdf.unwrap_or(false);
            let mut pdf_rel_path = std::path::PathBuf::from(&current_doc.file_path);
            pdf_rel_path.set_extension("pdf");
            let pdf_output_path = self.output_dir.join(pdf_rel_path);

            if current_pdf {
                // Generate or regenerate PDF
                if super::pdf::PdfGenerator::is_available() {
                    match super::pdf::PdfGenerator::new() {
                        Ok(pdf_gen) => {
                            let input_path = self.source_dir.join(&current_doc.file_path);
                            let _ = pdf_gen.generate_pdf_from_file(
                                &input_path,
                                &pdf_output_path,
                                &self.source_dir,
                                &self.site_config,
                                &current_doc.language,
                            );
                        }
                        Err(e) => {
                            warn!("PDF generator init failed during incremental: {}", e);
                        }
                    }
                } else if prev_pdf && pdf_output_path.exists() {
                    // Tools no longer available; remove stale PDF to reflect state
                    let _ = std::fs::remove_file(&pdf_output_path);
                }
            } else if prev_pdf {
                // PDF flag was removed; delete existing PDF if present
                if pdf_output_path.exists() {
                    let _ = std::fs::remove_file(&pdf_output_path);
                }
            }
        }
        Ok(())
    }

    /// Render all language variants of a document
    fn render_language_variants(
        &self,
        relative_path: &str,
        documents: &[Document],
    ) -> KrikResult<()> {
        let variant_paths = self.find_language_variants(relative_path);
        debug!(
            "found {} language variants for {}: {:?}",
            variant_paths.len(),
            relative_path,
            variant_paths
        );

        // Render all language variants (including the changed one)
        let mut rendered_any = false;
        for variant_path in &variant_paths {
            if let Some(doc) = documents.iter().find(|d| d.file_path == *variant_path) {
                debug!("re-rendering language variant page {}", variant_path);
                super::templates::generate_page(
                    doc,
                    documents,
                    &self.theme,
                    &self.site_config,
                    &self.output_dir,
                )
                .map_err(|e| {
                    KrikError::Generation(Box::new(crate::error::GenerationError {
                        kind: crate::error::GenerationErrorKind::OutputDirError(
                            std::io::Error::new(
                                std::io::ErrorKind::Other,
                                format!("Page generation failed for {}: {e}", variant_path),
                            ),
                        ),
                        context: "Incremental language variant page generation".to_string(),
                    }))
                })?;
                rendered_any = true;
            }
        }

        if !rendered_any {
            debug!(
                "changed page {} and its variants not present in scanned documents; triggering full regeneration",
                relative_path
            );
            return Err(KrikError::Generation(Box::new(
                crate::error::GenerationError {
                    kind: crate::error::GenerationErrorKind::OutputDirError(std::io::Error::new(
                        std::io::ErrorKind::NotFound,
                        "Document variants not found",
                    )),
                    context: "Language variant rendering".to_string(),
                },
            )));
        }

        Ok(())
    }
}

/// Analyze the type of change and determine how to handle it
pub fn analyze_change_type(
    changed_path: &Path,
    theme_path: &Path,
    source_dir: &Path,
) -> KrikResult<ChangeType> {
    // If change is inside theme dir or is an HTML template, do full regen.
    let is_theme_related = theme_path.is_dir() && changed_path.starts_with(theme_path);
    let is_template_ext = is_html_template(changed_path);

    if is_theme_related || is_template_ext {
        return Ok(ChangeType::ThemeRelated);
    }

    // Changes within content directory
    let canonical_changed =
        std::fs::canonicalize(changed_path).unwrap_or_else(|_| changed_path.to_path_buf());
    let canonical_source =
        std::fs::canonicalize(source_dir).unwrap_or_else(|_| source_dir.to_path_buf());

    if canonical_changed.starts_with(&canonical_source) {
        let is_markdown = changed_path.extension().is_some_and(|ext| ext == "md");
        let is_site_toml = changed_path.file_name() == Some(OsStr::new("site.toml"));

        if is_site_toml {
            return Ok(ChangeType::SiteConfig);
        }

        return if is_markdown {
            let relative_path = canonical_changed
                .strip_prefix(&canonical_source)
                .map(|rel| rel.to_string_lossy().to_string())
                .map_err(|_| {
                    debug!(
                        "failed to compute relative path for {}",
                        changed_path.display()
                    );
                    KrikError::Generation(Box::new(crate::error::GenerationError {
                        kind: crate::error::GenerationErrorKind::OutputDirError(
                            std::io::Error::new(
                                std::io::ErrorKind::Other,
                                "Failed to compute relative path",
                            ),
                        ),
                        context: "Path canonicalization".to_string(),
                    }))
                })?;
            Ok(ChangeType::Markdown { relative_path })
        } else {
            Ok(ChangeType::Asset)
        };
    }

    Ok(ChangeType::Unrelated)
}

/// Check if a path is an HTML template
pub fn is_html_template(path: &Path) -> bool {
    path.extension()
        .and_then(OsStr::to_str)
        .map(|ext| ext.eq_ignore_ascii_case("html"))
        .unwrap_or(false)
        && path
            .components()
            .any(|c| c.as_os_str() == OsStr::new("templates"))
}

/// Create a standardized asset error
pub fn create_asset_error(
    context: &str,
    source_dir: &Path,
    output_dir: &Path,
    error: Box<dyn std::error::Error + Send + Sync>,
) -> KrikError {
    KrikError::Generation(Box::new(crate::error::GenerationError {
        kind: crate::error::GenerationErrorKind::AssetCopyError {
            source: source_dir.to_path_buf(),
            target: output_dir.to_path_buf(),
            error: std::io::Error::new(
                std::io::ErrorKind::Other,
                format!("Asset operation failed: {error}"),
            ),
        },
        context: context.to_string(),
    }))
}