-
Notifications
You must be signed in to change notification settings - Fork 24
/
docs.go
61 lines (51 loc) · 1.22 KB
/
docs.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
package main
import (
"encoding/xml"
"os"
"strings"
)
type xmlDoc struct {
Purpose string `xml:"refnamediv>refpurpose"`
Names []string `xml:"refnamediv>refname"`
}
// Documentation is a map from function name to documentation string.
type Documentation map[string]string
func readDocFile(file string) (*xmlDoc, error) {
var xmlDoc xmlDoc
f, err := os.Open(file)
if err != nil {
return nil, err
}
defer f.Close()
decoder := xml.NewDecoder(f)
decoder.Strict = false
err = decoder.Decode(&xmlDoc)
if err != nil {
return nil, err
}
return &xmlDoc, nil
}
// AddDocs adds function documentation to the specified package.
func (d Documentation) AddDocs(pkg *Package) {
for _, fn := range pkg.Functions {
doc, ok := d[fn.Name]
if ok {
// Let the template handle line-wrapping if it chooses
fn.Doc = strings.Replace(doc, "\n", " ", -1)
}
}
}
// NewDocumentation parses XML documentation files.
func NewDocumentation(files []string) (Documentation, error) {
documentation := make(Documentation)
for _, file := range files {
xmlDoc, err := readDocFile(file)
if err != nil {
return nil, err
}
for _, name := range xmlDoc.Names {
documentation[name] = xmlDoc.Purpose
}
}
return documentation, nil
}