Debian Codegraph @

usage/report.go

raw symbols SCIP
1 package usage
2
3 import (
4 "bytes"
5 "encoding/json"
6 "errors"
7 "fmt"
8 "io"
9 "regexp"
10 "strings"
11
12 "golang.org/x/net/html"
13 )
14
15 // Section keeps track of individual sections
16 type Section struct {
17 Command string `json:"command"`
18 Name string `json:"name"`
19 Text string `json:"text"`
20 Words int `json:"words"`
21 Lines int `json:"lines"`
22 Sections []*Section `json:"sections"`
23 }
24
25 // Report holds together a report of sections
26 type Report struct {
27 Report []*Section `json:"report"`
28 }
29
30 // NewReport returns report based on raw
31 func NewReport(command string, top []byte) *Report {
32 report := Report{}
33 report.Process(command, top)
34
35 return &report
36 }
37
38 // Write serializes the report to json
39 func (report *Report) Write(w io.Writer) error {
40 j, err := json.MarshalIndent(report, "", " ")
41
42 if err != nil {
43 return err
44 }
45
46 w.Write(j)
47
48 return nil
49 }
50
51 // Process adds a html based help page to the report
52 func (report *Report) Process(command string, raw []byte) error {
53 r := bytes.NewBuffer(raw)
54 doc, err := html.Parse(r)
55
56 if err != nil {
57 return err
58 }
59
60 if doc.FirstChild.Type != html.ElementNode ||
61 doc.FirstChild.Data != "html" ||
62 doc.FirstChild.FirstChild.NextSibling.Data != "body" {
63 return errors.New("error parsing raw html")
64 }
65
66 body := doc.FirstChild.FirstChild.NextSibling
67
68 report.addSection(command, body.FirstChild, nil)
69
70 return nil
71 }
72
73 func (report *Report) addSection(command string, node *html.Node, section *Section) (*html.Node, *Section) {
74 if node == nil ||
75 node.Type != html.ElementNode ||
76 node.Data != "h2" {
77 return nil, nil
78 }
79
80 text, next := report.processNode(node)
81 words := strings.Fields(text)
82 lines := strings.Split(text, "\n")
83
84 s := Section{
85 Command: command,
86 Name: node.FirstChild.Data,
87 Text: text,
88 Words: len(words),
89 Lines: len(lines),
90 }
91
92 if section == nil {
93 report.Report = append(report.Report, &s)
94 return report.addSection(command, next, &s)
95 }
96
97 section.Sections = append(section.Sections, &s)
98 return report.addSection(command, next, section)
99 }
100
101 func (report *Report) processNode(node *html.Node) (string, *html.Node) {
102 text := ""
103 current := node.NextSibling
104
105 r, _ := regexp.Compile("<[^>]*>")
106
107 for current != nil {
108 var buf bytes.Buffer
109 w := io.Writer(&buf)
110 html.Render(w, current)
111
112 notags := r.ReplaceAllString(buf.String(), "")
113 clean := strings.TrimSpace(notags)
114
115 if len(text) > 0 && len(clean) > 0 {
116 text = fmt.Sprintf("%s %s", text, clean)
117 } else if len(clean) > 0 {
118 text = clean
119 }
120
121 current = current.NextSibling
122 if current == nil {
123 return text, nil
124 } else if current.Type == html.ElementNode &&
125 current.Data == "h2" {
126 node = current
127 current = nil
128 }
129 }
130
131 return text, node
132 }
133
134 // PerHeadline returns all sections across commands/pages with the same headline
135 func (report *Report) PerHeadline(headline string) []Section {
136 var results []Section
137 for _, top := range report.Report {
138 for _, section := range top.Sections {
139 if section.Name != headline {
140 continue
141 }
142
143 results = append(results, *section)
144 }
145 }
146
147 return results
148 }