-
Notifications
You must be signed in to change notification settings - Fork 0
/
template.go
281 lines (225 loc) · 5.09 KB
/
template.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
// Copyright 2022 Yevgeniy Chaban.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package template
import (
_ "embed"
"github.com/cyevgeniy/pldoc/ast"
"html/template"
"os"
"path/filepath"
"strings"
)
var (
//go:embed static/single.html
tmpl string
//go:embed static/main.css
css []byte
//go:embed static/pldoc.js
js []byte
)
func varHeader(vd *ast.Field) string {
switch vd.Kind {
case ast.VConst:
return "const"
case ast.VVar:
return "var"
case ast.VExc:
return "exception"
}
return ""
}
func funcHeader(fd *ast.FuncSpec) string {
if fd.Ftype == ast.FtFunc {
return "function"
}
return "procedure"
}
func funcListing(fd *ast.FuncSpec) string {
res := funcHeader(fd)
if fd.Name != nil && fd.Name.Name != "" {
res += " " + fd.Name.Name
}
res += fieldListListing(fd.Params)
if fd.Ftype == ast.FtFunc {
res += " return " + fd.T.Name
}
return res
}
func fieldListListing(fl *ast.FieldList) string {
if fl == nil {
return ""
}
var res string
if fl.List != nil {
res += "(\n"
for i := range fl.List {
last := i == len(fl.List)-1
res += " " + fl.List[i].String()
if !last {
res += ","
}
res += "\n"
}
res += ")"
}
return res
}
func typeHeader(td *ast.TypeDecl) string {
switch td.Kind {
case ast.TkTable:
return "table"
case ast.TkVarray:
return "varray"
case ast.TkRecord:
return "record"
case ast.TkRefCursor:
return "ref cursor"
}
return ""
}
func typeListing(td *ast.TypeDecl) string {
res := "type " + td.Name.Name + " is " + typeHeader(td) +
fieldListListing(td.Params)
if (td.Kind == ast.TkVarray || td.Kind == ast.TkTable) && td.T != nil {
res += " of " + td.T.Name
} else if td.Kind == ast.TkRefCursor && td.T != nil {
res += " return " + td.T.Name
}
return res
}
func cursorListing(cd *ast.CursorDecl) string {
return "cursor " + cd.Name.Name + fieldListListing(cd.Params) +
" is\n" + cd.SQL.Text
}
// Returns index of the first non-space
// character in a string. If string doesn't
// have any spaces at the beginning, returns 0
// If string doesn't have non-space characters,
// returns -1
func getFirstCharOffset(s string) int {
for i := range s {
if s[i] != ' ' {
return i
}
}
return -1
}
// Trims n spaces from the left side of a string
func trimNSpaces(s string, n int) string {
var i int
for i = 0; i <= n && i < len(s); i++ {
if s[i] != ' ' {
break
}
}
return s[i:]
}
func formatComment(cg *ast.CommentGroup) template.HTML {
if cg == nil {
return ""
}
lines := strings.Split(cg.Text(), "\n")
if len(lines) == 0 {
return ""
}
// Get the first non-space character's offset for the
// first line in the comment group.
offset := getFirstCharOffset(lines[0])
var res = make([]string, 0)
var buf = make([]string, 0)
fromPre := false
// Preformatted text offset related to ordinary comment text
var preOffs int
for i := range lines {
currOff := getFirstCharOffset(lines[i])
// Preformatted block starts.
// All previous text should be formatted as paragraph.
if currOff > offset {
// If we have not processing preformatted block
if !fromPre {
res = append(res, "<p>"+strings.Join(buf, "\n")+"</p>")
buf = make([]string, 0)
preOffs = currOff - offset
fromPre = true
}
} else if fromPre {
res = append(res, "<pre>"+strings.Join(buf, "\n")+"</pre>")
buf = make([]string, 0)
fromPre = false
}
var tmp string
if fromPre {
tmp = trimNSpaces(lines[i], preOffs)
} else {
tmp = lines[i]
}
buf = append(buf, tmp)
}
// Process unprocessed data
if len(buf) > 0 {
var str string
if fromPre {
str = "<pre>" + strings.Join(buf, "\n") + "</pre>"
} else {
str = "<p>" + strings.Join(buf, "\n") + "</p>"
}
res = append(res, str)
}
return template.HTML(strings.Join(res, "\n"))
}
type reportData struct {
Package *ast.Package
PackageList []*ast.Package
}
func Execute(dir string, f *ast.Files) error {
fm := template.FuncMap{
"varHeader": varHeader,
"funcHeader": funcHeader,
"funcListing": funcListing,
"typeHeader": typeHeader,
"typeListing": typeListing,
"cursorListing": cursorListing,
"formatComment": formatComment,
}
// Prepare directory
err := os.Mkdir(dir, 0750)
if err != nil && !os.IsExist(err) {
return err
}
// Create css and js files
err = os.WriteFile(filepath.Join(dir, "main.css"), css, 0666)
if err != nil {
return err
}
err = os.WriteFile(filepath.Join(dir, "pldoc.js"), js, 0666)
if err != nil {
return err
}
t, err := template.New("Documentation").Funcs(fm).Parse(tmpl)
if err != nil {
return err
}
for i := range f.Files {
for fn := range f.Files[i].Packages {
// Create file for each pl/sql package
fdoc, err := os.Create(filepath.Join(dir, f.Files[i].Packages[fn].Name.Name+".html"))
if err != nil {
return err
}
pckList := f.GetPackages()
err = t.Execute(fdoc,
reportData{
Package: f.Files[i].Packages[fn],
PackageList: pckList,
})
if err != nil {
return err
}
if err = fdoc.Close(); err != nil {
panic(err)
}
}
}
return nil
}