-
Notifications
You must be signed in to change notification settings - Fork 0
/
processor.go
90 lines (80 loc) · 2.26 KB
/
processor.go
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
package main
import (
"errors"
"os"
"strings"
"github.com/flyx/askew/data"
"github.com/flyx/askew/output"
"github.com/flyx/askew/units"
"github.com/flyx/askew/walker"
"github.com/flyx/net/html"
"golang.org/x/mod/modfile"
)
type processor struct {
syms data.Symbols
mod *modfile.File
}
func (p *processor) init(base *data.BaseDir) {
p.syms.BaseDir = *base
}
func (p *processor) processMacros(pkgName string) error {
p.syms.CurPkg = pkgName
pkg := p.syms.Packages[pkgName]
for _, file := range pkg.Files {
var err error
p.syms.SetAskewFile(file)
os.Stdout.WriteString("[info] processing macros: " + file.Path + "\n")
var dummyParent *html.Node
if dummyParent, err = processMacros(file.Content, &p.syms); err != nil {
return errors.New(file.Path + err.Error())
}
// we need to write out the nodes and parse it again since text nodes may
// be merged and additional elements may be created now with includes
// processed. If we don't do this, paths to access the dynamic objects will
// be wrong.
b := strings.Builder{}
for cur := dummyParent.FirstChild; cur != nil; cur = cur.NextSibling {
html.Render(&b, cur)
}
file.Content, err = html.ParseFragmentWithOptions(
strings.NewReader(b.String()), &data.BodyEnv,
html.ParseOptionCustomElements(walker.AskewElements))
if err != nil {
return errors.New(file.Path + ": " + err.Error())
}
}
return nil
}
func (p *processor) processComponents(pkgName string) error {
p.syms.CurPkg = pkgName
pkg := p.syms.Packages[pkgName]
for _, file := range pkg.Files {
if err := units.ProcessFile(file, &p.syms); err != nil {
return err
}
}
if pkg.Site != nil {
return units.ProcessSite(pkg.Site, &p.syms)
}
return nil
}
func (p *processor) dump(outputPath string, backend output.Backend) error {
for relPath, pkg := range p.syms.Packages {
w := output.PackageWriter{Syms: &p.syms, PackageName: pkg.Name, RelPath: relPath}
if err := os.MkdirAll(relPath, 0755); err != nil {
panic("failed to create package directory '" + relPath +
"': " + err.Error())
}
for _, f := range pkg.Files {
if err := w.WriteFile(f); err != nil {
return err
}
}
if pkg.Site != nil {
if err := w.WriteSite(pkg.Site, outputPath, backend); err != nil {
return err
}
}
}
return nil
}