This repository has been archived by the owner on Oct 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathread.go
144 lines (109 loc) · 2.4 KB
/
read.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
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
package main
import (
"bufio"
"log"
"os"
"regexp"
"strings"
)
type classMap struct {
m map[string]int
classes []string
suffixes []string
errors []error
}
type inputFile struct {
path string
contents []string
}
type inputFiles []inputFile
func read(s string) (inputFile, error) {
inFile := inputFile{
path: s,
}
file, err := os.Open(s)
if err != nil {
log.Fatal(err)
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
inFile.contents = append(inFile.contents, scanner.Text())
}
return inFile, err
}
func readFile(file string) (classMap, error) {
var classes classMap
classes.m = make(map[string]int)
var fls inputFiles
fl, err := read(file)
fls = append(fls, fl)
classes.parseClasses(fls)
return classes, err
}
func readFiles(dir string) (classMap, error) {
var classes classMap
classes.m = make(map[string]int)
entries, err := os.ReadDir(dir)
if err != nil {
return classes, err
}
var fls inputFiles
for _, entry := range entries {
file, err := read(dir + "/" + entry.Name())
if err != nil {
continue
}
fls = append(fls, file)
}
classes.parseClasses(fls)
return classes, err
}
func (classes *classMap) parseClasses(files inputFiles) {
for _, file := range files {
classLines := file.findClassLines()
classes.getClasses(classLines)
}
}
func (file inputFile) findClassLines() []string {
var classes []string
r, _ := regexp.Compile(`class\s*=\s*(['"])([^'"]+)`)
for _, line := range file.contents {
withClass := r.FindAllString(line, -1)
if len(withClass) == 0 {
continue
}
classes = append(classes, withClass...)
}
return classes
}
func (classes *classMap) getClasses(lines []string) {
r, _ := regexp.Compile(`^.*"`)
for _, line := range lines {
split := strings.Split(line, "class")
for _, s := range split {
trimmed := r.ReplaceAllString(s, "")
trimmed = strings.TrimSpace(trimmed)
class := strings.Split(trimmed, " ")
classes.parse(class)
}
}
}
func (classes *classMap) parse(classString []string) {
for _, class := range classString {
// it's an empty class
if class == "" {
continue
}
// it's already mapped
_, ok := classes.m[class]
if ok {
continue
}
classes.m[class] = 0
m, suffix, err := match(class)
classes.classes = append(classes.classes, m)
classes.suffixes = append(classes.suffixes, suffix)
classes.errors = append(classes.errors, err)
}
}