6
6
"strings"
7
7
8
8
"github.com/PuerkitoBio/goquery"
9
+
10
+ "github.com/gotd/getdoc/href"
9
11
)
10
12
11
13
// Doc represents full documentation description.
@@ -23,25 +25,27 @@ func docTitle(doc *goquery.Document) string {
23
25
}
24
26
25
27
// docDescription extracts description lines from document.
26
- func docDescription (doc * goquery.Document ) []string {
27
- var description []string
28
+ func docDescription (doc * goquery.Document ) (desc , links []string ) {
28
29
doc .Find ("#dev_page_content" ).Each (func (i int , s * goquery.Selection ) {
29
30
s .Children ().EachWithBreak (func (i int , selection * goquery.Selection ) bool {
30
31
if selection .Is ("p" ) && selection .Text () != "" {
31
- // Trimming space and handling newlines.
32
+ hrefs := href .Replace (selection )
33
+
32
34
text := strings .TrimSpace (selection .Text ())
33
35
for _ , part := range strings .Split (text , "\n " ) {
34
36
part = strings .TrimSpace (part )
35
37
if part == "" {
36
38
continue
37
39
}
38
- description = append (description , part )
40
+ desc = append (desc , part )
39
41
}
42
+
43
+ links = append (links , addHost (hrefs )... )
40
44
}
41
45
return ! selection .HasClass ("clearfix" )
42
46
})
43
47
})
44
- return description
48
+ return
45
49
}
46
50
47
51
// docTableAfter extracts table after selector "after".
@@ -69,19 +73,30 @@ func docTableAfter(doc *goquery.Document, after string) *goquery.Selection {
69
73
return table .First ().Find ("tbody > tr" )
70
74
}
71
75
76
+ type ParamDescription struct {
77
+ Description string `json:"description"`
78
+ Links []string `json:"links"`
79
+ }
80
+
72
81
// docParams extract parameters documentation from document.
73
82
//
74
- // Key is parameter name, value is documentation string.
75
- func docParams (doc * goquery.Document ) map [string ]string {
76
- fields := make (map [string ]string )
83
+ // Key is parameter name, value is documentation struct.
84
+ func docParams (doc * goquery.Document ) map [string ]ParamDescription {
85
+ fields := make (map [string ]ParamDescription )
86
+
77
87
docTableAfter (doc , "#parameters" ).
78
88
Each (func (i int , row * goquery.Selection ) {
79
89
var rowContents []string
90
+ var links []string
80
91
row .Find ("td" ).Each (func (i int , column * goquery.Selection ) {
81
- rowContents = append (rowContents , strings .TrimSpace (column .Text ()))
92
+ links = addHost (href .Replace (column ))
93
+ rowContents = append (rowContents , column .Text ())
82
94
})
83
95
if len (rowContents ) == 3 {
84
- fields [rowContents [0 ]] = rowContents [2 ]
96
+ fields [rowContents [0 ]] = ParamDescription {
97
+ Description : rowContents [2 ],
98
+ Links : links ,
99
+ }
85
100
}
86
101
})
87
102
return fields
0 commit comments