-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathchm2docset.go
210 lines (186 loc) · 5.02 KB
/
chm2docset.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
package main
import (
"database/sql"
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"os/exec"
"path"
"path/filepath"
"regexp"
"strings"
_ "github.com/mattn/go-sqlite3"
)
var logFatal = log.Fatal
func usage() {
fmt.Fprintf(os.Stderr, "usage: %s [inputfile]\n", os.Args[0])
flag.PrintDefaults()
os.Exit(2)
}
func failOnError(err error) {
if err != nil {
logFatal(err)
}
}
// Options options
type Options struct {
Outdir string
Platform string
SourcePath string
}
var platform string
var outdir string
func initFlags() {
platform = "unknown"
outdir = "./"
}
func init() {
initFlags()
flag.Usage = usage
flag.StringVar(&platform, "platform", platform, "DocSet Platform Family")
flag.StringVar(&outdir, "out", outdir, "Output directory or file path")
}
// NewOptions returns new options
func NewOptions() *Options {
flag.Parse()
args := flag.Args()
if len(args) != 1 {
return nil
}
return &Options{
Outdir: outdir,
Platform: platform,
SourcePath: args[0],
}
}
// SourceFilename returns source file name
func (opts *Options) SourceFilename() string {
return filepath.Base(opts.SourcePath)
}
// Basename returns file basename
func (opts *Options) Basename() string {
fn := opts.SourceFilename()
ext := filepath.Ext(fn)
return fn[0 : len(fn)-len(ext)]
}
// DocsetPath returns path to docset bundle
func (opts *Options) DocsetPath() string {
if strings.HasSuffix(opts.Outdir, ".docset") {
return opts.Outdir
}
return path.Join(opts.Outdir, opts.Basename()+".docset")
}
// ContentPath returns path to docset resources
func (opts *Options) ContentPath() string {
return path.Join(opts.DocsetPath(), "Contents", "Resources", "Documents")
}
// DatabasePath returns path to SQLite3 database
func (opts *Options) DatabasePath() string {
return path.Join(opts.DocsetPath(), "Contents", "Resources", "docSet.dsidx")
}
// PlistPath returns path to Info.plist
func (opts *Options) PlistPath() string {
return path.Join(opts.DocsetPath(), "Contents", "Info.plist")
}
// BundleIdentifier returns bundle identifier of docset bundle
func (opts *Options) BundleIdentifier() string {
safeRE := regexp.MustCompile("[^^a-zA-Z\\d-_]")
return "io.ngs.documentation." + safeRE.ReplaceAllString(opts.Basename(), "")
}
// PlistContent returns plsit content
func (opts *Options) PlistContent() string {
// https://kapeli.com/resources/Info.plist
return `<?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>dashIndexFilePath</key>
<string>Welcome.htm</string>
<key>CFBundleIdentifier</key>
<string>` + opts.BundleIdentifier() + `</string>
<key>CFBundleName</key>
<string>` + opts.Basename() + `</string>
<key>DocSetPlatformFamily</key>
<string>` + opts.Platform + `</string>
<key>isDashDocset</key>
<true/>
</dict>
</plist>`
}
// WritePlist writes plist file
func (opts *Options) WritePlist() error {
return ioutil.WriteFile(opts.PlistPath(), []byte(opts.PlistContent()), 0644)
}
// Clean removes existing output
func (opts *Options) Clean() error {
return os.RemoveAll(opts.DocsetPath())
}
// CreateDirectory creates directory
func (opts *Options) CreateDirectory() error {
return os.MkdirAll(opts.ContentPath(), 0755)
}
// ExtractSource extracts source to destination
func (opts *Options) ExtractSource() error {
cmd := exec.Command("extract_chmLib", opts.SourcePath, opts.ContentPath())
return cmd.Run()
}
// CreateDatabase creates database
func (opts *Options) CreateDatabase() error {
os.Remove(opts.DatabasePath())
titleRE := regexp.MustCompile("<title>([^<]+)</title>")
spacesRE := regexp.MustCompile("[\\s\\t]+")
db, err := sql.Open("sqlite3", opts.DatabasePath())
if err != nil {
return err
}
defer db.Close()
sqlStmt := `
CREATE TABLE searchIndex(id INTEGER PRIMARY KEY, name TEXT, type TEXT, path TEXT);
CREATE UNIQUE INDEX anchor ON searchIndex (name, type, path);
`
if _, err = db.Exec(sqlStmt); err != nil {
return err
}
tx, err := db.Begin()
if err != nil {
return err
}
stmt, err := tx.Prepare("INSERT OR IGNORE INTO searchIndex(name, type, path) VALUES (?, ?, ?)")
if err != nil {
return err
}
defer stmt.Close()
if err = filepath.Walk(opts.ContentPath(), func(path string, info os.FileInfo, err error) error {
if strings.HasSuffix(path, ".htm") {
b, err := ioutil.ReadFile(path)
if err != nil {
return err
}
content := string(b)
res := titleRE.FindAllStringSubmatch(content, -1)
if len(res) >= 1 && len(res[0]) >= 2 {
ttl := strings.Replace(res[0][1], "\n", " ", -1)
ttl = spacesRE.ReplaceAllString(ttl, " ")
ttl = strings.TrimSpace(ttl)
_, err := stmt.Exec(ttl, "Guide", strings.TrimPrefix(path, opts.ContentPath()))
if err != nil {
return err
}
}
}
return nil
}); err != nil {
return err
}
return tx.Commit()
}
func main() {
opts := NewOptions()
opts.Clean()
failOnError(opts.CreateDirectory())
failOnError(opts.ExtractSource())
failOnError(opts.CreateDatabase())
failOnError(opts.WritePlist())
}