- Implement complete mushroom foraging blog with chanterelles article - Add rich demonstration of .insertr and .insertr-add functionality - Include comprehensive documentation for future .insertr-content vision - Update project styling and configuration to support blog demo - Enhance engine and API handlers for improved content management
97 lines
2.3 KiB
Go
97 lines
2.3 KiB
Go
package engine
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"golang.org/x/net/html"
|
|
)
|
|
|
|
func (e *ContentEngine) ProcessFile(inputPath, outputPath, siteID string, mode ProcessMode) error {
|
|
htmlContent, err := os.ReadFile(inputPath)
|
|
if err != nil {
|
|
return fmt.Errorf("reading file %s: %w", inputPath, err)
|
|
}
|
|
|
|
result, err := e.ProcessContent(ContentInput{
|
|
HTML: htmlContent,
|
|
FilePath: filepath.Base(inputPath),
|
|
SiteID: siteID,
|
|
Mode: mode,
|
|
})
|
|
if err != nil {
|
|
return fmt.Errorf("processing content: %w", err)
|
|
}
|
|
|
|
return writeHTMLDocument(outputPath, result.Document)
|
|
}
|
|
|
|
// ProcessDirectory processes all HTML files in a directory (following filepath.Walk pattern)
|
|
func (e *ContentEngine) ProcessDirectory(inputDir, outputDir, siteID string, mode ProcessMode) error {
|
|
// Create output directory if it doesn't exist
|
|
if err := os.MkdirAll(outputDir, 0755); err != nil {
|
|
return fmt.Errorf("failed to create output directory: %w", err)
|
|
}
|
|
|
|
return filepath.Walk(inputDir, func(path string, info os.FileInfo, err error) error {
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// Calculate relative path for output
|
|
relPath, err := filepath.Rel(inputDir, path)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
outputPath := filepath.Join(outputDir, relPath)
|
|
|
|
// Handle directories
|
|
if info.IsDir() {
|
|
return os.MkdirAll(outputPath, info.Mode())
|
|
}
|
|
|
|
// Process HTML files
|
|
if strings.HasSuffix(strings.ToLower(path), ".html") {
|
|
return e.ProcessFile(path, outputPath, siteID, mode)
|
|
}
|
|
|
|
// Copy non-HTML files as-is
|
|
return copyFile(path, outputPath)
|
|
})
|
|
}
|
|
|
|
// writeHTMLDocument writes an HTML document to a file
|
|
func writeHTMLDocument(outputPath string, doc *html.Node) error {
|
|
// Create output directory
|
|
if err := os.MkdirAll(filepath.Dir(outputPath), 0755); err != nil {
|
|
return fmt.Errorf("creating output directory: %w", err)
|
|
}
|
|
|
|
file, err := os.Create(outputPath)
|
|
if err != nil {
|
|
return fmt.Errorf("creating output file: %w", err)
|
|
}
|
|
defer file.Close()
|
|
|
|
return html.Render(file, doc)
|
|
}
|
|
|
|
// copyFile copies a file from src to dst
|
|
func copyFile(src, dst string) error {
|
|
// Create directory for destination
|
|
if err := os.MkdirAll(filepath.Dir(dst), 0755); err != nil {
|
|
return err
|
|
}
|
|
|
|
// Read source
|
|
data, err := os.ReadFile(src)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// Write destination
|
|
return os.WriteFile(dst, data, 0644)
|
|
}
|