-
Notifications
You must be signed in to change notification settings - Fork 1
/
doc.go
225 lines (200 loc) · 5.86 KB
/
doc.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
// Copyright 2013 The GoGL2 Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE.mkd file.
package main
import (
"encoding/xml"
"fmt"
"io"
"os"
"path/filepath"
"sort"
"strconv"
"strings"
)
type docRefEntry struct {
XMLName xml.Name `xml:"refentry"`
Id string `xml:"id,attr"`
RefName string `xml:"refnamediv>refname"`
RefPurpose string `xml:"refnamediv>refpurpose"`
}
type docSvnIndex struct {
XMLName xml.Name `xml:"svn"`
FileRef []docFileRef `xml:"index>file"`
}
type docFileRef struct {
Ref string `xml:"href,attr"`
}
type docFile struct {
BaseName string
FileName string
}
type CommandDoc struct {
BaseName string
Purpose string
// TODO: more?
}
type CommandDocs struct {
MajorVersion int
Commands []*CommandDoc
}
type Documentation struct {
CommandDocs []CommandDocs
}
func writeKhronosDocCopyright(w io.Writer) {
fmt.Fprintln(w, "// Copyright (c) 2010 Khronos Group.")
fmt.Fprintln(w, "// This material may be distributed subject to the terms and conditions")
fmt.Fprintln(w, "// set forth in the Open Publication License, v 1.0, 8 June 1999.")
fmt.Fprintln(w, "// http://opencontent.org/openpub/.")
fmt.Fprintln(w, "// ")
}
func writeSgiDocCopyright(w io.Writer) {
fmt.Fprintln(w, "// Copyright (c) 1991-2006 Silicon Graphics, Inc.")
fmt.Fprintln(w, "// This document is licensed under the SGI Free Software B License.")
fmt.Fprintln(w, "// For details, see http://oss.sgi.com/projects/FreeB.")
fmt.Fprintln(w, "//")
}
func makeCmdDocUrl(cmdName string, majorVersion int) string {
manVer := "2"
if majorVersion >= 3 {
manVer = strconv.Itoa(majorVersion)
}
return fmt.Sprintf("https://www.opengl.org/sdk/docs/man%s/xhtml/gl%s.xml", manVer, cmdName)
}
func makeGLDocUrl(majorVersion int) string {
manVer := "2"
if majorVersion >= 3 {
manVer = strconv.Itoa(majorVersion)
}
return fmt.Sprintf("https://www.opengl.org/sdk/docs/man%s", manVer)
}
func makeExtenionSpecDocUrl(vendor, extension string) string {
return fmt.Sprintf("https://www.opengl.org/registry/specs/%s/%s.txt", vendor, extension)
}
func (cd CommandDocs) Len() int {
return len(cd.Commands)
}
func (cd CommandDocs) Swap(i, j int) {
cd.Commands[i], cd.Commands[j] = cd.Commands[j], cd.Commands[i]
}
func (cd CommandDocs) Less(i, j int) bool {
return cd.Commands[i].BaseName < cd.Commands[j].BaseName
}
func (d *Documentation) findCmd(majorVersion int, cmdName string) (*CommandDoc, error) {
if majorVersion == 1 {
majorVersion = 2
}
for _, cd := range d.CommandDocs {
if cd.MajorVersion == majorVersion {
index := sort.Search(len(cd.Commands), func(i int) bool {
return cd.Commands[i].BaseName >= cmdName
})
if index == len(cd.Commands) {
return nil, fmt.Errorf("Command doc not found: %s, version %d", cmdName, majorVersion)
}
if strings.HasPrefix(cmdName, cd.Commands[index].BaseName) {
return cd.Commands[index], nil
}
if index == 0 {
return nil, fmt.Errorf("Command doc not found: %s, version %d", cmdName, majorVersion)
}
if strings.HasPrefix(cmdName, cd.Commands[index-1].BaseName) {
return cd.Commands[index-1], nil
}
return nil, fmt.Errorf("Command doc not found: %s, version %d", cmdName, majorVersion)
}
}
return nil, fmt.Errorf("Version not found %d", majorVersion)
}
func (d *Documentation) WriteGoCmdDoc(w io.Writer, cmdName string, majorVersion int) error {
cd, err := d.findCmd(majorVersion, cmdName)
if err != nil {
return err
}
fmt.Fprintf(w, "// %s (%s)\n", cd.Purpose, makeCmdDocUrl(cd.BaseName, majorVersion))
return nil
}
func readXmlFileNonStrict(fileName string, data interface{}) error {
reader, err := os.Open(fileName)
if err != nil {
return err
}
defer reader.Close()
decoder := xml.NewDecoder(reader)
decoder.Strict = false
return decoder.Decode(data)
}
func parseDocIndex(fileName string) ([]docFile, error) {
var di docSvnIndex
if err := readXmlFileNonStrict(fileName, &di); err != nil {
return nil, err
}
files := make([]docFile, 0, 256)
for _, fr := range di.FileRef {
if strings.HasPrefix(fr.Ref, "glu") { // ignore
continue
}
if strings.HasPrefix(fr.Ref, "glX") { // ignore
continue
}
if strings.HasPrefix(fr.Ref, "gl") {
fn := strings.TrimPrefix(strings.TrimSuffix(fr.Ref, ".xml"), "gl")
files = append(files, docFile{BaseName: fn, FileName: fr.Ref})
}
}
return files, nil
}
func DownloadDocs(url, docCat, outDir string) error {
complOutDir := filepath.Join(outDir, docCat)
err := downloadFile(url, docCat, complOutDir, "index.xml")
if err != nil {
return err
}
file, err := parseDocIndex(filepath.Join(complOutDir, "index.xml"))
if err != nil {
return err
}
for _, file := range file {
err = downloadFile(fmt.Sprintf("%s/%s", url, docCat), file.FileName, complOutDir, file.FileName)
if err != nil {
return err
}
}
return nil
}
func parseDocFile(fileName string) (*CommandDoc, error) {
var d docRefEntry
if err := readXmlFileNonStrict(fileName, &d); err != nil {
return nil, err
}
return &CommandDoc{Purpose: d.RefPurpose}, nil
}
func parseDocs(docCat, dir string) ([]*CommandDoc, error) {
complOutDir := filepath.Join(dir, docCat)
file, err := parseDocIndex(filepath.Join(complOutDir, "index.xml"))
if err != nil {
return nil, err
}
commandDocs := make([]*CommandDoc, 0, 256)
for _, file := range file {
cd, err := parseDocFile(filepath.Join(complOutDir, file.FileName))
if err != nil {
return nil, err
}
cd.BaseName = file.BaseName
commandDocs = append(commandDocs, cd)
}
return commandDocs, nil
}
func ParseAllDocs(dir string) (*Documentation, error) {
cdocs := make([]CommandDocs, 0, 4)
for ver := 2; ver <= 4; ver++ {
cds, err := parseDocs(fmt.Sprintf("man%d", ver), dir)
if err != nil {
return nil, err
}
cd := CommandDocs{MajorVersion: ver, Commands: cds}
cdocs = append(cdocs, cd)
}
return &Documentation{CommandDocs: cdocs}, nil
}