-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathoutput.go
51 lines (45 loc) · 1021 Bytes
/
output.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
package wikipedia
import (
"encoding/json"
"encoding/xml"
"fmt"
"log"
)
func PrintTitlesAndCategories(pages <-chan Page, categoryFilter CategoryFilter) {
for p := range pages {
fmt.Printf("%s \n\tCategories: %s\n", p.Title, p.Categories(categoryFilter.CategoryRegexp()))
}
}
func PrintTitles(pages <-chan Page) {
for p := range pages {
fmt.Println(p.Title)
}
}
func WritePagesToJSON(pages <-chan Page) {
for page := range pages {
output, err := json.Marshal(page)
if err == nil {
fmt.Println(string(output))
}
}
}
func WritePagesToXML(pages <-chan Page) {
for page := range pages {
output, err := xml.Marshal(page)
if err == nil {
fmt.Println(string(output))
} else {
log.Printf("Error while marshaling page `%s`", err)
}
}
}
func PrintAsText(pages <-chan Page, categoryFilter CategoryFilter) {
for p := range pages {
fmt.Printf(
"%s \n\tCategories: %s\n\tExcerpt: %s\n",
p.Title,
p.Categories(categoryFilter.CategoryRegexp()),
FirstParagraph(p.Text),
)
}
}