-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathless_processor.go
More file actions
166 lines (141 loc) · 4.29 KB
/
Copy pathless_processor.go
File metadata and controls
166 lines (141 loc) · 4.29 KB
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
package vuego
import (
"bytes"
"io/fs"
"strings"
"github.com/titpetric/lessgo/dst"
"github.com/titpetric/lessgo/renderer"
"golang.org/x/net/html"
)
// LessProcessorError wraps processing errors with context.
type LessProcessorError struct {
// Err is the underlying error from the LESS processor.
Err error
// Reason is a descriptive message about the LESS processing failure.
Reason string
}
// Error returns the combined reason and underlying error message.
func (e *LessProcessorError) Error() string {
return e.Reason + ": " + e.Err.Error()
}
// LessProcessor implements vuego.NodeProcessor.
//
// It implements the functionality to compile Less CSS to standard css.
// It processes `script` tags with a `type="text/css+less"` attribute.
type LessProcessor struct {
// fs is optional filesystem for loading @import statements in LESS files
fs fs.FS
}
// NewLessProcessor creates a new LESS processor.
func NewLessProcessor(fsys ...fs.FS) *LessProcessor {
var fsVal fs.FS
if len(fsys) > 0 {
fsVal = fsys[0]
}
return &LessProcessor{fs: fsVal}
}
// New creates a new allocation of *LessProcessor.
func (lp *LessProcessor) New() NodeProcessor {
return &LessProcessor{
fs: lp.fs,
}
}
// PreProcess currently does nothing.
func (lp *LessProcessor) PreProcess(nodes []*html.Node) error {
return nil
}
// PostProcess walks the DOM tree and performs compilation.
func (lp *LessProcessor) PostProcess(nodes []*html.Node) error {
for _, node := range nodes {
if err := lp.processNode(node); err != nil {
return err
}
}
return nil
}
// processNode recursively processes a single node and its children.
func (lp *LessProcessor) processNode(node *html.Node) error {
if node == nil {
return nil
}
// Check if this is a style tag with type="text/css+less"
if node.Type == html.ElementNode && node.Data == "style" {
if lp.isLessStyleTag(node) {
if err := lp.compileLessTag(node); err != nil {
return err
}
}
}
// Recursively process all children
for c := node.FirstChild; c != nil; c = c.NextSibling {
if err := lp.processNode(c); err != nil {
return err
}
}
return nil
}
// isLessStyleTag checks if a style tag has type="text/css+less".
func (lp *LessProcessor) isLessStyleTag(node *html.Node) bool {
for _, attr := range node.Attr {
if attr.Key == "type" && attr.Val == "text/css+less" {
return true
}
}
return false
}
// compileLessTag extracts LESS content from the style tag, compiles it to CSS, and replaces the tag with a style tag.
func (lp *LessProcessor) compileLessTag(styleNode *html.Node) error {
// Extract the LESS content from the style tag's text content
lessContent := ""
for c := styleNode.FirstChild; c != nil; c = c.NextSibling {
if c.Type == html.TextNode {
lessContent += c.Data
}
}
if lessContent == "" {
return nil // Empty style tag, nothing to compile
}
// Parse and compile LESS to CSS
parser := dst.NewParser(bytes.NewReader([]byte(lessContent)))
if lp.fs != nil {
parser = dst.NewParserWithFS(bytes.NewReader([]byte(lessContent)), lp.fs)
}
file, err := parser.Parse()
if err != nil {
return &LessProcessorError{Err: err, Reason: "failed to parse LESS"}
}
// Render LESS to CSS
r := renderer.NewRenderer()
css, err := r.Render(file)
if err != nil {
return &LessProcessorError{Err: err, Reason: "failed to render LESS to CSS"}
}
// Replace the <style type="text/css+less"> tag with a <style> tag
lp.replaceWithStyleTag(styleNode, css)
return nil
}
// replaceWithStyleTag converts a style tag to a style tag with compiled CSS.
func (lp *LessProcessor) replaceWithStyleTag(styleNode *html.Node, css string) {
// Change tag name to "style"
styleNode.Data = "style"
// Keep only relevant attributes (remove type="text/css+less" and replace with type="text/css")
newAttrs := []html.Attribute{}
for _, attr := range styleNode.Attr {
if attr.Key != "type" {
newAttrs = append(newAttrs, attr)
}
}
// Add type="text/css" attribute
newAttrs = append(newAttrs, html.Attribute{Key: "type", Val: "text/css"})
styleNode.Attr = newAttrs
// Clear children and set CSS content
styleNode.FirstChild = nil
styleNode.LastChild = nil
// Create a text node with the compiled CSS
textNode := &html.Node{
Type: html.TextNode,
Data: "\n" + strings.TrimSpace(css) + "\n",
}
styleNode.FirstChild = textNode
styleNode.LastChild = textNode
}