Add Go CLI with container expansion parser and development server

- Implement Go-based CLI for build-time HTML enhancement
- Add container expansion: div.insertr auto-expands to viable children
- Create servedev command with live development workflow
- Add Air configuration for automatic rebuilds and serving
- Enable transition from runtime JS to build-time enhancement approach
This commit is contained in:
2025-09-02 22:58:59 +02:00
parent afd4879cef
commit 0e84af98bc
13 changed files with 1429 additions and 1 deletions

View File

@@ -0,0 +1,177 @@
package parser
import (
"crypto/sha1"
"fmt"
"regexp"
"strings"
"golang.org/x/net/html"
)
// IDGenerator generates unique content IDs for elements
type IDGenerator struct {
usedIDs map[string]bool
}
// NewIDGenerator creates a new ID generator
func NewIDGenerator() *IDGenerator {
return &IDGenerator{
usedIDs: make(map[string]bool),
}
}
// Generate creates a content ID for an HTML element
func (g *IDGenerator) Generate(node *html.Node) string {
context := g.getSemanticContext(node)
purpose := g.getPurpose(node)
content := g.getContentSample(node)
baseID := g.createBaseID(context, purpose, content)
return g.ensureUnique(baseID)
}
// getSemanticContext determines the semantic context from parent elements
func (g *IDGenerator) getSemanticContext(node *html.Node) string {
// Walk up the tree to find semantic containers
parent := node.Parent
for parent != nil && parent.Type == html.ElementNode {
classes := getClasses(parent)
// Check for common semantic section classes
for _, class := range []string{"hero", "services", "nav", "navbar", "footer", "about", "contact", "testimonial"} {
if containsClass(classes, class) {
return class
}
}
// Check for semantic HTML elements
switch parent.Data {
case "nav":
return "nav"
case "header":
return "header"
case "footer":
return "footer"
case "main":
return "main"
case "aside":
return "aside"
}
parent = parent.Parent
}
return "content"
}
// getPurpose determines the purpose/role of the element
func (g *IDGenerator) getPurpose(node *html.Node) string {
tag := strings.ToLower(node.Data)
classes := getClasses(node)
// Check for specific CSS classes that indicate purpose
for _, class := range classes {
switch {
case strings.Contains(class, "title"):
return "title"
case strings.Contains(class, "headline"):
return "headline"
case strings.Contains(class, "description"):
return "description"
case strings.Contains(class, "subtitle"):
return "subtitle"
case strings.Contains(class, "cta"):
return "cta"
case strings.Contains(class, "button"):
return "button"
case strings.Contains(class, "logo"):
return "logo"
case strings.Contains(class, "lead"):
return "lead"
}
}
// Infer purpose from HTML tag
switch tag {
case "h1":
return "title"
case "h2":
return "subtitle"
case "h3", "h4", "h5", "h6":
return "heading"
case "p":
return "text"
case "a":
return "link"
case "button":
return "button"
default:
return "content"
}
}
// getContentSample gets a sample of content for ID generation
func (g *IDGenerator) getContentSample(node *html.Node) string {
text := extractTextContent(node)
// Clean and normalize text
text = strings.ToLower(text)
text = regexp.MustCompile(`[^a-z0-9\s]+`).ReplaceAllString(text, "")
text = regexp.MustCompile(`\s+`).ReplaceAllString(text, " ")
text = strings.TrimSpace(text)
// Take first few words
words := strings.Fields(text)
if len(words) > 3 {
words = words[:3]
}
return strings.Join(words, "-")
}
// createBaseID creates the base ID from components
func (g *IDGenerator) createBaseID(context, purpose, content string) string {
parts := []string{}
// Add context if meaningful
if context != "content" {
parts = append(parts, context)
}
// Add purpose
parts = append(parts, purpose)
// Add content sample if available and meaningful
if content != "" && content != purpose {
parts = append(parts, content)
}
baseID := strings.Join(parts, "-")
// Clean up the ID
baseID = regexp.MustCompile(`-+`).ReplaceAllString(baseID, "-")
baseID = strings.Trim(baseID, "-")
// Ensure it's not empty
if baseID == "" {
baseID = "content"
}
return baseID
}
// ensureUnique makes sure the ID is unique by adding a suffix if needed
func (g *IDGenerator) ensureUnique(baseID string) string {
if !g.usedIDs[baseID] {
g.usedIDs[baseID] = true
return baseID
}
// If base ID is taken, add a hash suffix
hash := fmt.Sprintf("%x", sha1.Sum([]byte(baseID)))[:6]
uniqueID := fmt.Sprintf("%s-%s", baseID, hash)
g.usedIDs[uniqueID] = true
return uniqueID
}