-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmodels.go
63 lines (53 loc) · 1.09 KB
/
models.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
package wikipedia
import (
"encoding/xml"
"io"
"regexp"
"strings"
)
type Parser struct {
x *xml.Decoder
}
func NewParser(r io.Reader) (*Parser, error) {
d := xml.NewDecoder(r)
_, err := d.Token()
if err != nil {
return nil, err
}
return &Parser{
x: d,
}, nil
}
func (p *Parser) Next() (*Page, error) {
rv := &Page{}
return rv, p.x.Decode(rv)
}
type Redirect struct {
Title string `xml:"title,attr"`
}
type Page struct {
ID uint64 `xml:"id"`
Title string `xml:"title"`
Redir Redirect `xml:"redirect"`
Text string `xml:"revision>text"`
}
func (p *Page) Categories(categoryRegexp *regexp.Regexp) string {
matches := categoryRegexp.FindAllStringSubmatch(p.Text, -1)
categories := []string{}
for _, c := range matches {
categories = append(categories, strings.ToLower(c[1]))
}
return strings.Join(categories, ",")
}
type MinifiedPage struct {
ID uint64 `xml:"id"`
Title string `xml:"title"`
Text string `xml:"revision>text"`
}
func NewMinifiedPage(p Page) MinifiedPage {
return MinifiedPage{
ID: p.ID,
Title: p.Title,
Text: FirstParagraph(p.Text),
}
}