forked from technosophos/dashing
/
dashing.go
440 lines (388 loc) · 9.9 KB
/
dashing.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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
package main
import (
"bytes"
"database/sql"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/url"
"os"
"path/filepath"
"strings"
"text/template"
css "github.com/andybalholm/cascadia"
"github.com/codegangsta/cli"
"golang.org/x/net/html"
"golang.org/x/net/html/atom"
_ "github.com/mattn/go-sqlite3"
)
const plist = `<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleIdentifier</key>
<string>{{.Name}}</string>
<key>CFBundleName</key>
<string>{{.FancyName}}</string>
<key>DocSetPlatformFamily</key>
<string>{{.Name}}</string>
<key>isDashDocset</key>
<true/>
<key>DashDocSetFamily</key>
<string>dashtoc</string>
<key>dashIndexFilePath</key>
<string>{{.Index}}</string>
<key>isJavaScriptEnabled</key><{{.AllowJS}}/>
</dict>
</plist>
`
type Dashing struct {
// The human-oriented name of the package.
Name string `json:"name"`
// Computer-readable name. Recommendation is to use one word.
Package string `json:"package"`
// The location of the index.html file.
Index string `json:"index"`
// Selectors to match.
Selectors map[string]string `json:"selectors"`
// Entries that should be ignored.
Ignore []string `json:"ignore"`
// A 32x32 pixel PNG image.
Icon32x32 string `json:"icon32x32"`
AllowJS bool `json:"allowJS"`
}
var ignoreHash map[string]bool
func main() {
app := cli.NewApp()
app.Name = "dashing"
app.Usage = "Generate Dash documentation from HTML files"
app.Commands = commands()
app.Run(os.Args)
}
func commands() []cli.Command {
return []cli.Command{
{
Name: "build",
Usage: "build a doc set",
Action: build,
Flags: []cli.Flag{
cli.StringFlag{
Name: "source, s",
Usage: "The path to the HTML files this will ingest. (Default: ./ )",
},
cli.StringFlag{
Name: "config, f",
Usage: "The path to the JSON configuration file.",
},
},
},
{
Name: "init",
ShortName: "create",
Usage: "create a new template for building documentation",
Action: create,
Flags: []cli.Flag{
cli.StringFlag{
Name: "config, f",
Usage: "The path to the JSON configuration file.",
},
},
},
}
}
func create(c *cli.Context) {
f := c.String("config")
if len(f) == 0 {
f = "dashing.json"
}
conf := Dashing{
Name: "Dashing",
Package: "dashing",
Index: "index.html",
Selectors: map[string]string{
"title": "Package",
"dt a": "Command",
},
Ignore: []string{"ABOUT"},
}
j, err := json.MarshalIndent(conf, "", " ")
if err != nil {
panic("The programmer did something dumb.")
}
err = ioutil.WriteFile(f, j, 0755)
if err != nil {
fmt.Errorf("Could not initialize JSON file: %s", err)
os.Exit(1)
}
fmt.Printf("You may now edit %s", f)
}
func build(c *cli.Context) {
var dashing Dashing
//if !c.Args().Present() {
//fmt.Printf("Name is required: dashing build NAME\n")
//return
//}
//name := c.Args().First()
source := c.String("source")
if len(source) == 0 {
source = "."
}
cf := strings.TrimSpace(c.String("config"))
if len(cf) == 0 {
cf = "./dashing.json"
}
conf, err := ioutil.ReadFile(cf)
if err != nil {
fmt.Printf("Failed to open configuration file '%s': %s (Run `dashing init`?)\n", cf, err)
os.Exit(1)
}
if err := json.Unmarshal(conf, &dashing); err != nil {
fmt.Printf("Failed to parse JSON: %s", err)
os.Exit(1)
}
name := dashing.Package
fmt.Printf("Building %s from files in '%s'.\n", name, source)
os.MkdirAll(name+".docset/Contents/Resources/Documents/", 0755)
setIgnore(dashing.Ignore)
addPlist(name, &dashing)
if len(dashing.Icon32x32) > 0 {
addIcon(dashing.Icon32x32, name+".docset/icon.png")
}
db, err := createDB(name)
if err != nil {
fmt.Printf("Failed to create database: %s\n", err)
return
}
defer db.Close()
texasRanger(source, name, dashing, db)
}
func setIgnore(i []string) {
ignoreHash = make(map[string]bool, len(i))
for _, item := range i {
ignoreHash[item] = true
}
}
func addPlist(name string, config *Dashing) {
var file bytes.Buffer
t := template.Must(template.New("plist").Parse(plist))
fancyName := config.Name
if len(fancyName) == 0 {
fancyName = strings.ToTitle(name)
}
aj := "false"
if config.AllowJS {
aj = "true"
}
tvars := map[string]string{
"Name": name,
"FancyName": fancyName,
"Index": config.Index,
"AllowJS": aj,
}
err := t.Execute(&file, tvars)
if err != nil {
fmt.Printf("Failed: %s\n", err)
return
}
ioutil.WriteFile(name+".docset/Contents/Info.plist", file.Bytes(), 0755)
}
func createDB(name string) (*sql.DB, error) {
dbname := name + ".docset/Contents/Resources/docSet.dsidx"
os.Remove(dbname)
db, err := sql.Open("sqlite3", dbname)
if err != nil {
return db, err
}
if _, err := db.Exec(`CREATE TABLE searchIndex(id INTEGER PRIMARY KEY, name TEXT, type TEXT, path TEXT)`); err != nil {
return db, err
}
//if _, err := db.Exec(`CREATE UNIQUE INDEX anchor ON searchIndex (name, type, path)`); err != nil {
//return db, err
//}
return db, nil
}
// texasRanger is... wait for it... a WALKER!
func texasRanger(base, name string, dashing Dashing, db *sql.DB) error {
filepath.Walk(base, func(path string, info os.FileInfo, err error) error {
fmt.Printf("Reading %s\n", path)
if strings.HasPrefix(path, name+".docset") {
fmt.Printf("Ignoring directory %s\n", path)
return filepath.SkipDir
}
if info.IsDir() || ignore(path) {
return nil
}
dest := name + ".docset/Contents/Resources/Documents"
if htmlish(path) {
fmt.Printf("%s looks like HTML\n", path)
//if err := copyFile(path, name+".docset/Contents/Resources/Documents"); err != nil {
//fmt.Printf("Failed to copy file %s: %s\n", path, err)
//return err
//}
found, err := parseHTML(path, dest, dashing)
if err != nil {
fmt.Printf("Error parsing %s: %s\n", path, err)
return nil
}
for _, ref := range found {
fmt.Printf("Match: '%s' is type %s at %s\n", ref.name, ref.etype, ref.href)
db.Exec(`INSERT OR IGNORE INTO searchIndex(name, type, path) VALUES (?,?,?)`, ref.name, ref.etype, ref.href)
}
} else {
// Or we just copy the file.
err := copyFile(path, filepath.Join(dest, path))
if err != nil {
fmt.Printf("Skipping file %s. Error: %s\n", path, err)
}
return err
}
return nil
})
return nil
}
// ignore returns true if a file should be ignored by dashing.
func ignore(src string) bool {
// Skip our own config file.
if filepath.Base(src) == "dashing.json" {
return true
}
// Skip VCS dirs.
parts := strings.Split(src, "/")
for _, p := range parts {
switch p {
case ".git", ".svn":
return true
}
}
return false
}
func writeHTML(orig, dest string, root *html.Node) error {
dir := filepath.Dir(orig)
base := filepath.Base(orig)
os.MkdirAll(filepath.Join(dest, dir), 0755)
out, err := os.Create(filepath.Join(dest, dir, base))
if err != nil {
return err
}
defer out.Close()
return html.Render(out, root)
}
func htmlish(filename string) bool {
e := strings.ToLower(filepath.Ext(filename))
switch e {
case ".html", ".htm", ".xhtml", ".html5":
return true
}
return false
}
type reference struct {
name, etype, href string
}
func parseHTML(path, dest string, dashing Dashing) ([]*reference, error) {
refs := []*reference{}
r, err := os.Open(path)
if err != nil {
return refs, err
}
defer r.Close()
top, err := html.Parse(r)
for pattern, etype := range dashing.Selectors {
m := css.MustCompile(pattern)
found := m.MatchAll(top)
for _, n := range found {
name := text(n)
// Skip things explicitly ignored.
if ignored(name) {
fmt.Printf("Skipping entry for %s (Ignored by dashing JSON)\n", name)
continue
}
// References we want to track.
refs = append(refs, &reference{name, etype, path + "#" + anchor(n)})
// We need to modify the DOM with a special link to support TOC.
n.Parent.InsertBefore(newA(name, etype), n)
}
}
return refs, writeHTML(path, dest, top)
}
func ignored(n string) bool {
_, ok := ignoreHash[n]
return ok
}
func text(node *html.Node) string {
var b bytes.Buffer
for c := node.FirstChild; c != nil; c = c.NextSibling {
if c.Type == html.TextNode {
b.WriteString(c.Data)
} else if c.Type == html.ElementNode {
b.WriteString(text(c))
}
}
return b.String()
}
// tcounter is used to generate automatic anchors.
// NOTE: NOT THREADSAFE. If we switch to goroutines, swith to atom int.
var tcounter int
func anchor(node *html.Node) string {
if node.Type == html.ElementNode && node.Data == "a" {
for _, a := range node.Attr {
if a.Key == "name" {
return a.Val
}
}
}
tname := fmt.Sprintf("autolink-%d", tcounter)
link := autolink(tname)
node.Parent.InsertBefore(link, node)
tcounter++
return tname
}
//autolink creates an A tag for when one is not present in original docs.
func autolink(target string) *html.Node {
return &html.Node{
Type: html.ElementNode,
DataAtom: atom.A,
Data: atom.A.String(),
Attr: []html.Attribute{
html.Attribute{Key: "class", Val: "dashingAutolink"},
html.Attribute{Key: "name", Val: target},
},
}
}
// newA creates a TOC anchor.
func newA(name, etype string) *html.Node {
u := &url.URL{Path: name}
name = u.String()
target := fmt.Sprintf("//apple_ref/cpp/%s/%s", etype, name)
return &html.Node{
Type: html.ElementNode,
DataAtom: atom.A,
Data: atom.A.String(),
Attr: []html.Attribute{
html.Attribute{Key: "class", Val: "dashAnchor"},
html.Attribute{Key: "name", Val: target},
},
}
}
// addIcon adds an icon to the docset.
func addIcon(src, dest string) error {
return copyFile(src, dest)
}
// copyFile copies a source file to a new destination.
func copyFile(src, dest string) error {
if err := os.MkdirAll(filepath.Dir(dest), 0755); err != nil {
return err
}
in, err := os.Open(src)
if err != nil {
return err
}
defer in.Close()
out, err := os.Create(dest)
if err != nil {
return err
}
defer out.Close()
_, err = io.Copy(out, in)
return err
}