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
|
package usage
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"regexp"
"strings"
"golang.org/x/net/html"
)
// Section keeps track of individual sections
type Section struct {
Command string `json:"command"`
Name string `json:"name"`
Text string `json:"text"`
Words int `json:"words"`
Lines int `json:"lines"`
Sections []*Section `json:"sections"`
}
// Report holds together a report of sections
type Report struct {
Report []*Section `json:"report"`
}
// NewReport returns report based on raw
func NewReport(command string, top []byte) *Report {
report := Report{}
report.Process(command, top)
return &report
}
// Write serializes the report to json
func (report *Report) Write(w io.Writer) error {
j, err := json.MarshalIndent(report, "", " ")
if err != nil {
return err
}
w.Write(j)
return nil
}
// Process adds a html based help page to the report
func (report *Report) Process(command string, raw []byte) error {
r := bytes.NewBuffer(raw)
doc, err := html.Parse(r)
if err != nil {
return err
}
if doc.FirstChild.Type != html.ElementNode ||
doc.FirstChild.Data != "html" ||
doc.FirstChild.FirstChild.NextSibling.Data != "body" {
return errors.New("error parsing raw html")
}
body := doc.FirstChild.FirstChild.NextSibling
report.addSection(command, body.FirstChild, nil)
return nil
}
func (report *Report) addSection(command string, node *html.Node, section *Section) (*html.Node, *Section) {
if node == nil ||
node.Type != html.ElementNode ||
node.Data != "h2" {
return nil, nil
}
text, next := report.processNode(node)
words := strings.Fields(text)
lines := strings.Split(text, "\n")
s := Section{
Command: command,
Name: node.FirstChild.Data,
Text: text,
Words: len(words),
Lines: len(lines),
}
if section == nil {
report.Report = append(report.Report, &s)
return report.addSection(command, next, &s)
}
section.Sections = append(section.Sections, &s)
return report.addSection(command, next, section)
}
func (report *Report) processNode(node *html.Node) (string, *html.Node) {
text := ""
current := node.NextSibling
r, _ := regexp.Compile("<[^>]*>")
for current != nil {
var buf bytes.Buffer
w := io.Writer(&buf)
html.Render(w, current)
notags := r.ReplaceAllString(buf.String(), "")
clean := strings.TrimSpace(notags)
if len(text) > 0 && len(clean) > 0 {
text = fmt.Sprintf("%s %s", text, clean)
} else if len(clean) > 0 {
text = clean
}
current = current.NextSibling
if current == nil {
return text, nil
} else if current.Type == html.ElementNode &&
current.Data == "h2" {
node = current
current = nil
}
}
return text, node
}
// PerHeadline returns all sections across commands/pages with the same headline
func (report *Report) PerHeadline(headline string) []Section {
var results []Section
for _, top := range report.Report {
for _, section := range top.Sections {
if section.Name != headline {
continue
}
results = append(results, *section)
}
}
return results
}
|