-
Notifications
You must be signed in to change notification settings - Fork 3
/
unicode_data.go
289 lines (264 loc) · 6.35 KB
/
unicode_data.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
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
// Copyright (c) 2021 Dean Jackson <deanishe@deanishe.net>
// MIT Licence applies http://opensource.org/licenses/MIT
// Created on 2021-02-05
package main
import (
"archive/zip"
"encoding/xml"
"fmt"
"io"
"log"
"net/http"
"os"
"strconv"
"strings"
"github.com/deanishe/awgo/util"
)
// list of all Unicode codepoints
const xmlURL = "https://www.unicode.org/Public/UCD/latest/ucdxml/ucd.nounihan.flat.zip"
// Codepoint is a Unicode codepoint.
type Codepoint struct {
Dec int32 `db:"dec"`
Hex string `db:"hex"`
Category string `db:"category"`
Name string `db:"name"`
Aliases string `db:"aliases"`
HTMLEntityName string `db:"entity"`
Rank float64 `db:"rank"`
}
// String implements Stringer.
func (cp Codepoint) String() string {
return fmt.Sprintf("U+%04X %q", cp.Dec, cp.Name)
}
// IsZero returns true if Codepoint is zero.
func (cp Codepoint) IsZero() bool { return cp.Hex == "" }
func setupRequired() bool { return !util.PathExists(dbPath) }
// ensure Unicode data and database exist.
func runSetup(force bool) {
var (
db *Repo
err error
)
if !util.PathExists(zipCachePath) || force {
log.Printf("[setup] downloading Unicode data ...")
checkErr(download(xmlURL, zipCachePath))
}
if newerThan(zipCachePath, dbPath) {
log.Printf("[setup] extracting Unicode data ...")
if err := unzip(xmlCacheName, xmlCachePath, zipCachePath); err != nil {
log.Printf("unzip failed: %v", err)
if strings.Contains(err.Error(), "not a valid zip file") {
_ = os.Remove(zipCachePath)
_ = os.Remove(xmlCachePath)
}
panic(err)
}
log.Printf("[setup] building database ...")
db, err = Open(dbPath)
checkErr(err)
defer db.Close()
var cps []Codepoint
cps, err = parseXML(xmlCachePath)
checkErr(err)
checkErr(db.Import(cps))
checkErr(os.Remove(xmlCachePath))
}
}
type xmlPayload struct {
Description string `xml:"description"`
Repetoire struct {
Chars []struct {
Name string `xml:"na,attr"`
Name1 string `xml:"na1,attr"`
Category string `xml:"gc,attr"`
Codepoint string `xml:"cp,attr"`
Aliases []struct {
Alias string `xml:"alias,attr"`
Type string `xml:"type,attr"`
} `xml:"name-alias"`
} `xml:"char"`
XMLName xml.Name `xml:"repertoire"`
}
XMLName xml.Name `xml:"ucd"`
}
func parseXML(path string) ([]Codepoint, error) {
log.Printf("[setup] loading %q ...", util.PrettyPath(path))
f, err := os.Open(path)
if err != nil {
return nil, fmt.Errorf("open XML file: %w", err)
}
defer f.Close()
var (
dec = xml.NewDecoder(f)
pl = xmlPayload{}
cps = []Codepoint{}
)
if err := dec.Decode(&pl); err != nil {
return nil, fmt.Errorf("parse XML: %w", err)
}
for _, v := range pl.Repetoire.Chars {
if v.Codepoint == "" || v.Name == "CJK UNIFIED IDEOGRAPH-#" {
continue
}
aliases := make([]string, len(v.Aliases))
for i, a := range v.Aliases {
aliases[i] = a.Alias
}
name := v.Name
if name == "" {
name = v.Name1
}
if name == "" {
continue
}
n, err := parseHex(v.Codepoint)
if err != nil {
return nil, err
}
cp := Codepoint{
Hex: fmt.Sprintf("%04X", n),
Dec: n,
Category: v.Category,
Name: name,
Aliases: strings.Join(aliases, ", "),
HTMLEntityName: NamedHTMLEntities[fmt.Sprintf("%05X", n)],
}
cps = append(cps, cp)
}
log.Printf("[setup] %d codepoints for %q from: %s",
len(cps), pl.Description, util.PrettyPath(path))
return cps, nil
}
func unzip(name, dest, zipfile string) error {
r, err := zip.OpenReader(zipfile)
if err != nil {
return fmt.Errorf("unzip: %w", err)
}
defer r.Close()
for _, f := range r.File {
log.Printf("[setup] %s: %s", zipfile, f.Name)
if f.Name == name {
var (
out *os.File
rc io.ReadCloser
)
if out, err = os.Create(dest); err != nil {
return fmt.Errorf("unzip: extract file: %w", err)
}
defer out.Close()
if rc, err = f.Open(); err != nil {
return fmt.Errorf("unzip: extract file: %w", err)
}
defer rc.Close()
if _, err = io.Copy(out, rc); err != nil {
return fmt.Errorf("unzip: write data: %w", err)
}
return nil
}
}
return fmt.Errorf("unzip: file %q not found in %q", name, zipfile)
}
func newerThan(name1, name2 string) bool {
var (
fi1, fi2 os.FileInfo
err error
)
if fi1, err = os.Stat(name1); err != nil {
return false
}
if fi2, err = os.Stat(name2); err != nil {
return true
}
return fi1.ModTime().After(fi2.ModTime())
}
// download saves file at URL to path.
func download(url, path string) error {
r, err := http.Get(url)
if err != nil {
return fmt.Errorf("retrieve URL %q: %w", url, err)
}
log.Printf("[http] [%d] %s", r.StatusCode, url)
defer r.Body.Close()
f, err := os.Create(path)
if err != nil {
return fmt.Errorf("create XML cache file: %w", err)
}
defer f.Close()
if _, err := io.Copy(f, r.Body); err != nil {
return fmt.Errorf("write XML cache file: %w", err)
}
log.Printf("[http] saved %q", util.PrettyPath(path))
return nil
}
func parseHex(hex string) (int32, error) {
n, err := strconv.ParseUint(hex, 16, 32)
if err != nil {
return 0, err
}
return int32(n), nil
}
// CategoryName returns the full name of Codepoint's category.
func (cp Codepoint) CategoryName() string {
switch cp.Category {
case "Cc":
return "Control"
case "Cf":
return "Format"
case "Co":
return "Private Use"
case "Cs":
return "Surrrogate"
case "Ll":
return "Lowercase Letter"
case "Lm":
return "Modifier Letter"
case "Lo":
return "Other Letter"
case "Lt":
return "Titlecase Letter"
case "Lu":
return "Uppercase Letter"
case "Mc":
return "Spacing Mark"
case "Me":
return "Enclosing Mark"
case "Mn":
return "Nonspacing Mark"
case "Nd":
return "Decimal Number"
case "Nl":
return "Letter Number"
case "No":
return "Other Number"
case "Pc":
return "Connector Punctuation"
case "Pd":
return "Dash Punctuation"
case "Pe":
return "Close Punctuation"
case "Pf":
return "Final Punctuation"
case "Pi":
return "Initial Punctuation"
case "Po":
return "Other Punctuation"
case "Ps":
return "Open Punctuation"
case "Sc":
return "Currency Symbol"
case "Sk":
return "Modifier Symbol"
case "Sm":
return "Math Symbol"
case "So":
return "Other Symbol"
case "Zl":
return "Line Separator"
case "Zp":
return "Paragraph Separator"
case "Zs":
return "Space Separator"
default:
return ""
}
}