-
Notifications
You must be signed in to change notification settings - Fork 3
/
parser.go
126 lines (103 loc) · 3.31 KB
/
parser.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
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
package main
import (
"fmt"
"net/http"
"strings"
"github.com/PuerkitoBio/goquery"
)
type Section struct {
Name string
Description []string
Fields []Field
InterfaceOf []string
IsInterface bool
}
type Field struct {
Name string
Type string
Description string
IsOptional bool
}
func Fetch() (*goquery.Document, error) {
resp, err := http.Get(TelegramDocURL)
if err != nil {
return nil, fmt.Errorf("failed to fetch telegram doc > %s", err.Error())
}
defer resp.Body.Close()
doc, err := goquery.NewDocumentFromReader(resp.Body)
if err != nil {
return nil, err
}
return doc, nil
}
func Parse(doc *goquery.Document) (data TemplateData) {
data.Implementers = make(map[string]string)
// looping through each group
doc.Find("#dev_page_content h4").Each(func(i int, s *goquery.Selection) {
// it doesn't seems to be a group, so we'll ignore it
if strings.Contains(s.Text(), " ") {
return
}
var section Section
section.Name = s.Text()
// We implement this ourselves.
if section.Name == "InputFile" {
return
}
s.NextUntil("h3,h4,hr").Each(func(i int, s *goquery.Selection) {
switch {
case s.Is("p"), s.Is("blockquote"):
section.Description = append(section.Description, strings.Split(s.Text(), "\n")...)
case s.Is("ul"):
supportedTypes := s.Find("li").Map(func(i int, s *goquery.Selection) string { return s.Text() })
section.Description = append(section.Description, strings.Join(supportedTypes, ", "))
for _, t := range supportedTypes {
data.Implementers[t] = section.Name
}
if supportedTypes != nil {
section.InterfaceOf = supportedTypes
section.IsInterface = true
}
case s.Is("table"):
s.Find("tbody tr").Each(func(i int, s *goquery.Selection) {
field := Field{}
fields := s.Find("td").Map(func(i int, s *goquery.Selection) string { return s.Text() })
field.Name = fields[0]
field.Type = fields[1]
switch len(fields) {
case 3:
field.IsOptional = strings.HasPrefix(fields[2], "Optional.")
field.Description = strings.ReplaceAll(fields[2], "\n", " ")
case 4:
field.IsOptional = fields[2] == "Optional"
field.Description = strings.ReplaceAll(fields[3], "\n", " ")
}
section.Fields = append(section.Fields, field)
})
}
})
// Normalize the description
if len(section.Description) != 0 {
section.Description[0] = strings.Replace(section.Description[0], "This object", section.Name, 1)
section.Description[0] = strings.Replace(section.Description[0], "Use this method to", section.Name+" is used to", 1)
}
data.Sections = append(data.Sections, section)
})
// extended sections
data.Sections = append(data.Sections, Section{
Name: "ReplyMarkup",
Description: []string{"ReplyMarkup is an interface for InlineKeyboardMarkup, ReplyKeyboardMarkup, ReplyKeyboardRemove, ForceReply"},
IsInterface: true,
})
data.Sections = append(data.Sections, Section{
Name: "ChatID",
Description: []string{"ChatID is an interface for usernames and chatIDs"},
IsInterface: true,
})
// extended implementers
data.Implementers["InlineKeyboardMarkup"] = "ReplyMarkup"
data.Implementers["ReplyKeyboardMarkup"] = "ReplyMarkup"
data.Implementers["ReplyKeyboardRemove"] = "ReplyMarkup"
data.Implementers["ForceReply"] = "ReplyMarkup"
return data
}