-
Notifications
You must be signed in to change notification settings - Fork 0
/
hdoc.go
193 lines (168 loc) · 4.33 KB
/
hdoc.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
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
package main
import (
"encoding/xml"
"fmt"
"github.com/beevik/etree"
"github.com/spf13/cobra"
"os"
"regexp"
"strings"
)
var reWhitespace = regexp.MustCompile(`\s+`)
var reInvalidId = regexp.MustCompile(`[^\p{L}\p{N}_-]+`)
func panicIfErr(err error) {
if err != nil {
panic(err)
}
}
// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "hdoc [source]",
Args: cobra.ExactArgs(1),
Short: "A simple HTML shorthand processor",
// Long: `A longer description that spans multiple lines and likely contains
// examples and usage of using your application. For example:
// Cobra is a CLI library for Go that empowers applications.
// This application is a tool to generate the needed files
// to quickly create a Cobra application.`,
Run: realMain,
}
func main() {
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
func init() {
// cobra.OnInitialize(initConfig)
rootCmd.Flags().StringP("output", "o", "", "Output file")
rootCmd.Flags().StringP("style", "", "", "Set document style options")
}
func realMain(cmd *cobra.Command, args []string) {
var outFile *os.File
var err error
// Open output
outfn := cmd.Flag("output").Value.String()
if outfn != "" {
outFile, err = os.OpenFile(outfn, os.O_RDWR|os.O_CREATE, 0644)
panicIfErr(err)
} else {
outFile = os.Stdout
}
fmt.Println(outFile)
// Open style
stylefn := cmd.Flag("style").Value.String()
if stylefn == "" {
stylefn = "data/styles/default.html"
}
style := etree.NewDocument()
style.ReadSettings.Entity = xml.HTMLEntity
err = style.ReadFromFile(stylefn)
panicIfErr(err)
fmt.Println(style)
// Open source
src := etree.NewDocument()
style.ReadSettings.Entity = xml.HTMLEntity
err = src.ReadFromFile(args[0])
panicIfErr(err)
fmt.Println(src)
fmt.Println(getInnerTxt(src.Root()))
processAbbrAndDfn(src)
src.WriteTo(outFile)
}
func getInnerTxt(cur *etree.Element) string {
buf := new(strings.Builder)
getInnerTxt_inner(cur, buf)
clean := reWhitespace.ReplaceAllString(buf.String(), " ")
return strings.TrimSpace(clean)
}
func getInnerTxt_inner(cur *etree.Element, buf *strings.Builder) {
for _, raw_child := range cur.Child {
switch child := raw_child.(type) {
case *etree.CharData:
buf.WriteString(child.Data)
case *etree.Element:
getInnerTxt_inner(child, buf)
}
}
}
func fixWAttr(elem *etree.Element) {
if elem.SelectAttr("w") != nil {
return
}
txt := getInnerTxt(elem)
elem.CreateAttr("w", txt)
}
func fixIdAttr(elem *etree.Element) {
id := elem.SelectAttrValue("id", "")
if id != "" {
return
}
w := elem.SelectAttrValue("w", "")
w = reInvalidId.ReplaceAllString(w, "")
elem.CreateAttr("id", elem.Tag+"-"+w)
}
func classifyAbbrDfn(elem *etree.Element) string {
if elem.Space == "" && (elem.Tag == "dfn" || elem.Tag == "abbr") {
if len(elem.Child) == 0 {
return "ref"
}
return "def"
}
return "neither"
}
func processAbbrAndDfn(src *etree.Document) {
m := make(map[string]*etree.Element)
processAbbrAndDfn_inner(src.Root(), m)
}
func addClass(elem *etree.Element, class string) {
val := elem.SelectAttrValue("class", "")
list := strings.Split(val, " ")
// Hack to deal with empty strings
if len(list) > 0 && len(list[0]) == 0 {
list = list[1:]
}
for _, item := range list {
if item == class {
return
}
}
list = append(list, class)
val = strings.Join(list, " ")
elem.CreateAttr("class", val)
}
func processAbbrAndDfn_inner(cur *etree.Element, m map[string]*etree.Element) {
for _, raw_child := range cur.Child {
switch child := raw_child.(type) {
case *etree.Element:
kind := classifyAbbrDfn(child)
if kind == "def" {
fixWAttr(child)
if child.Tag == "dfn" {
fixIdAttr(child)
}
w_attr := child.Tag + "-" + child.SelectAttrValue("w", "")
m[w_attr] = child
child.RemoveAttr("w")
}
if kind == "ref" {
fixWAttr(child)
w_attr := child.Tag + "-" + child.SelectAttrValue("w", "")
def := m[w_attr].Copy()
if child.Tag == "dfn" {
child.Tag = "a"
child.CreateAttr("href", "#"+def.SelectAttrValue("id", ""))
child.Child = def.Child
addClass(child, "dfn")
}
if child.Tag == "abbr" {
title := def.SelectAttrValue("title", "")
child.Child = def.Child
child.CreateAttr("title", title)
}
child.RemoveAttr("w")
}
processAbbrAndDfn_inner(child, m)
}
}
}