Refactor architecture: eliminate auto-discovery and consolidate packages
- Remove auto-discovery entirely (~450 lines) * Delete internal/content/discoverer.go * Simplify enhancer to single-phase processing * Remove duplicate container expansion logic - Consolidate repository implementations * Move internal/content/client.go → internal/db/http_client.go * Group all repository implementations in db/ package - Add file utilities to engine following Go stdlib patterns * Add engine.ProcessFile() and ProcessDirectory() methods * Engine now handles both content processing AND file operations - Move site management to dedicated package * Move internal/content/site_manager.go → internal/sites/manager.go * Clear separation of site lifecycle from content processing - Preserve container expansion (syntactic sugar) * .insertr on containers still auto-applies to viable children * Container detection logic consolidated in engine/utils.go Result: Clean architecture with single source of truth for .insertr processing
This commit is contained in:
97
internal/engine/file.go
Normal file
97
internal/engine/file.go
Normal file
@@ -0,0 +1,97 @@
|
||||
package engine
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"golang.org/x/net/html"
|
||||
)
|
||||
|
||||
// ProcessFile processes a single HTML file (following Go stdlib pattern like os.ReadFile/WriteFile)
|
||||
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)
|
||||
}
|
||||
Reference in New Issue
Block a user