mirror of
https://github.com/kjanat/articulate-parser.git
synced 2026-10-13 01:35:27 +02:00
refactor: extract HTMLCleaner interface and consolidate constants
- Add interfaces.HTMLCleaner interface for better testability - Add CleanAndTrim() method to simplify repeated pattern - Move item/lesson type constants to models/item_types.go - Update all exporters to use interface instead of concrete type - Replace strings.TrimSpace(CleanHTML(...)) with CleanAndTrim()
This commit is contained in:
@@ -7,13 +7,19 @@ import (
|
||||
"strings"
|
||||
|
||||
"golang.org/x/net/html"
|
||||
|
||||
"github.com/kjanat/articulate-parser/internal/interfaces"
|
||||
)
|
||||
|
||||
// HTMLCleaner provides utilities for converting HTML content to plain text.
|
||||
// It removes HTML tags while preserving their content and converts HTML entities
|
||||
// to their plain text equivalents using proper HTML parsing instead of regex.
|
||||
// Implements interfaces.HTMLCleaner.
|
||||
type HTMLCleaner struct{}
|
||||
|
||||
// Compile-time check that HTMLCleaner implements interfaces.HTMLCleaner.
|
||||
var _ interfaces.HTMLCleaner = (*HTMLCleaner)(nil)
|
||||
|
||||
// NewHTMLCleaner creates a new HTML cleaner instance.
|
||||
// This service is typically injected into exporters that need to handle
|
||||
// HTML content from Articulate Rise courses.
|
||||
@@ -46,6 +52,13 @@ func (h *HTMLCleaner) CleanHTML(htmlStr string) string {
|
||||
return strings.TrimSpace(cleaned)
|
||||
}
|
||||
|
||||
// CleanAndTrim removes HTML tags, decodes entities, and trims whitespace.
|
||||
// Convenience method that combines CleanHTML with strings.TrimSpace.
|
||||
// Returns empty string if input is empty or contains only whitespace/tags.
|
||||
func (h *HTMLCleaner) CleanAndTrim(htmlStr string) string {
|
||||
return strings.TrimSpace(h.CleanHTML(htmlStr))
|
||||
}
|
||||
|
||||
// extractText recursively traverses the HTML node tree and extracts text content.
|
||||
// It skips script and style tags to avoid including their content in the output.
|
||||
func extractText(w io.Writer, n *html.Node) {
|
||||
|
||||
Reference in New Issue
Block a user