/
gortfm.go
159 lines (131 loc) · 3.42 KB
/
gortfm.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
package main
import (
"compress/gzip"
"./doce"
"template"
"strings"
"go/parser"
"go/token"
"go/ast"
"bufio"
"bytes"
"flag"
"path"
"fmt"
"os"
"io"
)
var (
// the directory where we should look for a package
optOutDir = flag.String("outdir", "html", "output directory")
optNiceName = flag.String("nicename", "", "use this instead of a package name in html")
optNoShared = flag.Bool("no-shared", false, "don't write shared dir")
optIndex = flag.String("index", "", "add index page shortcut to package page")
)
//-------------------------------------------------------------------------------
func writePackage(docs *doce.Package, pkgname string) {
os.MkdirAll(*optOutDir, 0755)
writePackageData(docs, pkgname)
writePackagePage(pkgname)
}
func writePackageData(docs *doce.Package, pkgname string) {
// TODO: I/O error checks
pkgfilename := strings.Replace(pkgname, "/", "_", -1)
dest := path.Join(*optOutDir, "gortfm-"+pkgfilename+"-data.js")
f, err := os.Create(dest)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
defer f.Close()
var b bytes.Buffer
writeJSPackageDoc(&b, docs, *optIndex)
f.Write([]byte("var gortfmData = "))
f.Write(b.Bytes())
}
func writePackagePage(pkgname string) {
// TODO: I/O error checks
pkgfilename := strings.Replace(pkgname, "/", "_", -1)
dest := path.Join(*optOutDir, pkgfilename+".html")
f, err := os.Create(dest)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
defer f.Close()
bf := bufio.NewWriter(f)
var packageTemplate = template.MustParse(packageTemplateStr, nil)
tplparams := map[string]string{
"pkgname": pkgname,
"datafile": "gortfm-" + pkgfilename + "-data.js",
}
packageTemplate.Execute(bf, tplparams)
bf.Flush()
}
func getOnlyPkg(pkgs map[string]*ast.Package) *ast.Package {
if len(pkgs) > 1 {
fmt.Fprintln(os.Stderr, "Too many packages in Go files")
os.Exit(1)
}
for _, v := range pkgs {
return v
}
return nil
}
func exitIf(err os.Error) {
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
func writeUnpackedFile(data []byte, dest string) {
// create "out"
outfile, err := os.Create(dest)
exitIf(err)
defer outfile.Close()
out := bufio.NewWriter(outfile)
// create "in"
ingzipped := bytes.NewBuffer(data)
in, err := gzip.NewReader(ingzipped)
exitIf(err)
defer in.Close()
// Copy!
_, err = io.Copy(out, in)
exitIf(err)
// Flush!
err = out.Flush()
exitIf(err)
}
func writeSharedDir() {
dest := path.Join(*optOutDir, "shared")
err := os.MkdirAll(dest, 0755)
exitIf(err)
writeUnpackedFile(jquery_js, path.Join(dest, "jquery-1.4.2.min.js"))
writeUnpackedFile(jquery_scrollTo_js, path.Join(dest, "jquery.scrollTo-1.4.2-min.js"))
writeUnpackedFile(gortfm_js, path.Join(dest, "gortfm.js"))
writeUnpackedFile(gortfm_fuzzy_js, path.Join(dest, "gortfm-fuzzy.js"))
writeUnpackedFile(gortfm_help_js, path.Join(dest, "gortfm-help.js"))
writeUnpackedFile(gortfm_index_js, path.Join(dest, "gortfm-index.js"))
writeUnpackedFile(gortfm_css, path.Join(dest, "gortfm.css"))
}
func main() {
flag.Parse()
if !*optNoShared {
writeSharedDir()
}
fset := token.NewFileSet()
pkgs, err := parser.ParseFiles(fset, flag.Args(), parser.ParseComments)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
pkg := getOnlyPkg(pkgs)
if pkg != nil {
if *optNiceName == "" {
*optNiceName = pkg.Name
}
ast.PackageExports(pkg)
docs := doce.NewPackage(pkg, fset)
writePackage(docs, *optNiceName)
}
}