Newer
Older
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
package html
import (
"fmt"
"github.com/andybalholm/cascadia"
"gitlab.schukai.com/oss/bob.git/types"
"gitlab.schukai.com/oss/libraries/go/markup/html/engine"
"golang.org/x/net/html"
"gopkg.in/yaml.v3"
"os"
"path/filepath"
)
type Specification struct {
Selector string
}
func readHTML(p string) (*html.Node, error) {
htmlFile, err := os.Open(p)
if err != nil {
return nil, err
}
defer htmlFile.Close()
return html.Parse(htmlFile)
}
func SyncHtml(p string) error {
content, err := os.ReadFile(p)
if err != nil {
return err
}
currentDir, _ := os.Getwd()
os.Chdir(filepath.Dir(p))
defer os.Chdir(currentDir)
specification := types.SyncSpecification{}
if err := yaml.Unmarshal(content, &specification); err != nil {
return err
}
sourceFiles := make(map[string]*html.Node)
destinationFiles := make(map[string]*html.Node)
destinationFile := make(map[string][]string)
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
for _, r := range specification.Sync {
source := r.Source
destination := r.Destination
exclude := r.Destination.Exclude
absSource, err := filepath.Abs(source.Path)
if err != nil {
return err
}
if err != nil {
return err
}
if sourceFiles[absSource], err = readHTML(absSource); err != nil {
return err
}
for _, d := range destination.Path {
d, err := filepath.Abs(d)
if err != nil {
return err
}
fileinfo, err := os.Stat(d)
if err != nil {
return err
}
if fileinfo.IsDir() {
filepath.Walk(d, func(pp string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
if exclude != nil {
for _, e := range exclude {
e, err := filepath.Abs(e)
if err != nil {
return err
}
if e == pp {
return nil
}
}
}
ext := filepath.Ext(pp)
if ext == ".html" {
var dd *html.Node
if dd, err = readHTML(pp); err != nil {
return err
}
destinationFiles[pp] = dd
destinationFile[absSource] = append(destinationFile[absSource], pp)
}
return nil
})
} else if filepath.Ext(d) == ".html" {
if exclude != nil {
for _, e := range exclude {
e, err := filepath.Abs(e)
if err != nil {
return err
}
if e == d {
return nil
}
}
}
var dd *html.Node
if dd, err = readHTML(d); err != nil {
return err
}
destinationFiles[d] = dd
destinationFile[absSource] = append(destinationFile[absSource], d)
}
}
for _, r := range specification.Sync {
source := r.Source
absSource, err := filepath.Abs(source.Path)
if err != nil {
return err
}
sourceSelector := source.Selector
query, err := cascadia.Compile(sourceSelector)
if err != nil {
return err
}
sourceNode := query.MatchFirst(sourceFiles[absSource])
dp := destinationFile[absSource]
for _, d := range dp {
destinationSelector := r.Destination.Selector
if destinationSelector == "" {
destinationSelector = sourceSelector
}
keepMap := make(map[string][]*html.Node)
for _, n := range r.Destination.Keep {
q, err := cascadia.Compile(n)
if err != nil {
return err
}
kNode := q.MatchAll(destinationFiles[d])
if kNode == nil {
return fmt.Errorf("keep node not found: %s", n)
}
for _, k := range kNode {
keepMap[n] = append(keepMap[n], engine.CloneNode(k))
}
}
query, err := cascadia.Compile(destinationSelector)
if err != nil {
return err
}
destinationData := query.MatchFirst(destinationFiles[d])
if destinationData == nil {
return fmt.Errorf("could not find selector %s in %s", destinationSelector, d)
}
n := engine.CloneNode(sourceNode)
destinationData.Parent.InsertBefore(n, destinationData)
destinationData.Parent.RemoveChild(destinationData)
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
for sel, k := range keepMap {
cas, err := cascadia.Compile(sel)
if err != nil {
return err
}
x := cas.MatchAll(destinationFiles[d])
if x == nil {
return fmt.Errorf("could not find selector %s in %s", sel, d)
}
for _, n1 := range x {
for _, kk := range k {
// node already removed, for example, by a previous keep
if n1.Parent == nil {
continue
}
n1.Parent.InsertBefore(kk, n1)
n1.Parent.RemoveChild(n1)
}
}
}
}
}
for p, d := range destinationFiles {
fp, err := os.Create(p)
if err != nil {
return err
}
err = html.Render(fp, d)
err2 := fp.Close()
if err2 != nil {
return err2
}
if err != nil {
return err
}
}
}
return nil
}