-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathanalyzer.go
More file actions
352 lines (308 loc) · 7.71 KB
/
analyzer.go
File metadata and controls
352 lines (308 loc) · 7.71 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
package html
import (
"bytes"
"context"
"fmt"
"io/fs"
"os"
"path/filepath"
"strings"
"github.com/PuerkitoBio/goquery"
pkgParser "github.com/doITmagic/rag-code-mcp/pkg/parser"
"github.com/doITmagic/rag-code-mcp/pkg/parser/html/gotemplate"
)
func init() {
pkgParser.Register(NewAnalyzer())
}
// Analyzer implements the pkgParser.Analyzer interface for HTML.
type Analyzer struct {
ca *CodeAnalyzer
}
// NewAnalyzer creates a new HTML analyzer.
func NewAnalyzer() *Analyzer {
return &Analyzer{
ca: NewCodeAnalyzer(),
}
}
// Name returns "html".
func (a *Analyzer) Name() string {
return "html"
}
// CanHandle returns true for .html, .htm, .tmpl, and .gohtml files.
func (a *Analyzer) CanHandle(filePath string) bool {
ext := strings.ToLower(filepath.Ext(filePath))
switch ext {
case ".html", ".htm", ".tmpl", ".gohtml":
return true
default:
return false
}
}
// Analyze extracts symbols from an HTML or Go template file.
// For files with {{ }} syntax: uses both GoTemplate and HTML analysis.
// For plain HTML files: uses goquery HTML analysis only.
func (a *Analyzer) Analyze(ctx context.Context, path string) (*pkgParser.Result, error) {
var symbols []pkgParser.Symbol
// For single files: detect Go template syntax and run GoTemplate analysis
info, err := os.Stat(path)
if err != nil {
return nil, err
}
if !info.IsDir() {
data, err := os.ReadFile(path)
if err != nil {
return nil, err
}
// If Go template syntax detected, run Go template analysis first
if bytes.Contains(data, []byte("{{")) {
goTplAnalyzer := &gotemplate.GoTemplateAnalyzer{}
templates := goTplAnalyzer.Analyze([]string{path})
symbols = append(symbols, gotemplate.ConvertToSymbols(templates)...)
}
}
// Always run HTML DOM analysis too (Go templates contain HTML)
chunks, err := a.ca.AnalyzePaths([]string{path})
if err != nil {
// If HTML parsing fails but we got Go template symbols, return those
if len(symbols) > 0 {
return &pkgParser.Result{
Symbols: symbols,
Language: "html",
}, nil
}
return nil, err
}
for _, ch := range chunks {
symbols = append(symbols, pkgParser.Symbol{
Name: ch.Name,
Type: pkgParser.SymbolType(ch.Type),
FilePath: ch.FilePath,
Language: "html",
Docstring: ch.Docstring,
Content: ch.Content,
Signature: ch.Signature,
Metadata: ch.Metadata,
})
}
return &pkgParser.Result{
Symbols: symbols,
Language: "html",
}, nil
}
// CodeAnalyzer handles the heavy lifting of HTML analysis.
type CodeAnalyzer struct{}
func NewCodeAnalyzer() *CodeAnalyzer {
return &CodeAnalyzer{}
}
func (ca *CodeAnalyzer) AnalyzePaths(paths []string) ([]CodeChunk, error) {
var chunks []CodeChunk
for _, root := range paths {
info, err := os.Stat(root)
if err != nil {
return nil, fmt.Errorf("html analyzer: stat %s: %w", root, err)
}
if info.IsDir() {
err = filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if d.IsDir() {
if ca.shouldSkipDir(path, root) {
return filepath.SkipDir
}
return nil
}
if !ca.isHTMLFile(d.Name()) {
return nil
}
fileChunks, ferr := ca.analyzeFile(path)
if ferr != nil {
return ferr
}
chunks = append(chunks, fileChunks...)
return nil
})
if err != nil {
return nil, err
}
continue
}
if !ca.isHTMLFile(root) {
continue
}
fileChunks, err := ca.analyzeFile(root)
if err != nil {
return nil, err
}
chunks = append(chunks, fileChunks...)
}
return chunks, nil
}
func (ca *CodeAnalyzer) analyzeFile(path string) ([]CodeChunk, error) {
data, err := os.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("html analyzer: read %s: %w", path, err)
}
doc, err := goquery.NewDocumentFromReader(bytes.NewReader(data))
if err != nil {
return nil, fmt.Errorf("html analyzer: parse %s: %w", path, err)
}
title := strings.TrimSpace(doc.Find("title").First().Text())
sections := ca.buildSections(doc, path, title)
if len(sections) > 0 {
return sections, nil
}
// Fallback: treat entire body as a single chunk.
body := doc.Find("body")
if body.Length() == 0 {
body = doc.Selection
}
bodyText := normalizeWhitespace(body.Text())
if bodyText == "" {
return nil, nil
}
name := title
if name == "" {
base := filepath.Base(path)
name = strings.TrimSuffix(base, filepath.Ext(base))
}
chunk := CodeChunk{
Type: "type", // Mapped to section/type
Name: name,
Language: "html",
FilePath: path,
Signature: title,
Docstring: bodyText,
Content: bodyText,
}
if title != "" {
chunk.Metadata = map[string]any{"page_title": title}
}
return []CodeChunk{chunk}, nil
}
func (ca *CodeAnalyzer) buildSections(doc *goquery.Document, path, pageTitle string) []CodeChunk {
headingSelector := "h1,h2,h3,h4,h5,h6"
body := doc.Find("body")
if body.Length() == 0 {
body = doc.Selection
}
var chunks []CodeChunk
body.Find(headingSelector).Each(func(i int, sel *goquery.Selection) {
title := strings.TrimSpace(sel.Text())
if title == "" {
title = fmt.Sprintf("Section %d", i+1)
}
level := headingLevel(goquery.NodeName(sel))
if level == 0 {
level = 1
}
content := sel.NextUntil(headingSelector)
bodyText := normalizeWhitespace(content.Text())
codeBlocks := extractCodeBlocks(content)
metadata := map[string]any{
"heading_level": level,
}
if pageTitle != "" {
metadata["page_title"] = pageTitle
}
if id, ok := sel.Attr("id"); ok && strings.TrimSpace(id) != "" {
metadata["html_id"] = strings.TrimSpace(id)
}
if class, ok := sel.Attr("class"); ok && strings.TrimSpace(class) != "" {
metadata["class"] = strings.TrimSpace(class)
}
if len(codeBlocks) > 0 {
metadata["code_blocks"] = codeBlocks
}
chunk := CodeChunk{
Type: "type", // Map sections to type for consistency
Name: title,
Language: "html",
FilePath: path,
Signature: fmt.Sprintf("<h%d>%s</h%d>", level, title, level),
Docstring: bodyText,
Content: buildSectionCode(title, bodyText, codeBlocks),
Metadata: metadata,
}
chunks = append(chunks, chunk)
})
return chunks
}
func (ca *CodeAnalyzer) shouldSkipDir(path, root string) bool {
if path == root {
return false
}
base := filepath.Base(path)
if strings.HasPrefix(base, ".") {
return true
}
switch base {
case "node_modules", "vendor", "dist", "build":
return true
default:
return false
}
}
func (ca *CodeAnalyzer) isHTMLFile(name string) bool {
lower := strings.ToLower(name)
for _, ext := range []string{".html", ".htm", ".tmpl", ".gohtml"} {
if strings.HasSuffix(lower, ext) {
return true
}
}
return false
}
func headingLevel(tag string) int {
if len(tag) == 2 && tag[0] == 'h' {
switch tag[1] {
case '1':
return 1
case '2':
return 2
case '3':
return 3
case '4':
return 4
case '5':
return 5
case '6':
return 6
}
}
return 0
}
func normalizeWhitespace(text string) string {
text = strings.ReplaceAll(text, "\r\n", "\n")
lines := strings.Split(text, "\n")
var cleaned []string
for _, line := range lines {
line = strings.TrimSpace(line)
if line != "" {
cleaned = append(cleaned, line)
}
}
return strings.Join(cleaned, "\n")
}
func extractCodeBlocks(sel *goquery.Selection) []string {
var blocks []string
sel.Find("pre,code").Each(func(_ int, s *goquery.Selection) {
block := normalizeWhitespace(s.Text())
if block != "" {
blocks = append(blocks, block)
}
})
return blocks
}
func buildSectionCode(title, body string, codeBlocks []string) string {
var parts []string
if title != "" {
parts = append(parts, title)
}
if body != "" {
parts = append(parts, body)
}
if len(codeBlocks) > 0 {
parts = append(parts, strings.Join(codeBlocks, "\n\n"))
}
return strings.Join(parts, "\n\n")
}