Implement parsing functions partially
This commit is contained in:
12
function.go
12
function.go
@@ -2,6 +2,7 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
_ "embed"
|
_ "embed"
|
||||||
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"text/template"
|
"text/template"
|
||||||
@@ -17,6 +18,7 @@ var fns = template.FuncMap{
|
|||||||
var templatestr string
|
var templatestr string
|
||||||
|
|
||||||
var functionTemplate *template.Template
|
var functionTemplate *template.Template
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
var err error
|
var err error
|
||||||
functionTemplate, err = template.New("class").Funcs(fns).Parse(templatestr)
|
functionTemplate, err = template.New("class").Funcs(fns).Parse(templatestr)
|
||||||
@@ -30,10 +32,10 @@ func init() {
|
|||||||
type (
|
type (
|
||||||
Function struct {
|
Function struct {
|
||||||
Name string
|
Name string
|
||||||
Arguments []Argument
|
Arguments []Parameter
|
||||||
Returns []Argument
|
Returns []Parameter
|
||||||
}
|
}
|
||||||
Argument struct {
|
Parameter struct {
|
||||||
Name string
|
Name string
|
||||||
Type string
|
Type string
|
||||||
Description string
|
Description string
|
||||||
@@ -44,6 +46,10 @@ func (f *Function) ResolveFileName() string {
|
|||||||
return f.Name + ".lua"
|
return f.Name + ".lua"
|
||||||
}
|
}
|
||||||
func (f *Function) WriteFile() error {
|
func (f *Function) WriteFile() error {
|
||||||
|
if f.Name == "" {
|
||||||
|
return fmt.Errorf("function name is empty of %+v", f)
|
||||||
|
}
|
||||||
|
|
||||||
file, err := os.Create(f.ResolveFileName())
|
file, err := os.Create(f.ResolveFileName())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
1
go.mod
1
go.mod
@@ -4,6 +4,7 @@ go 1.23.2
|
|||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/PuerkitoBio/goquery v1.10.0
|
github.com/PuerkitoBio/goquery v1.10.0
|
||||||
|
github.com/davecgh/go-spew v1.1.1
|
||||||
golang.org/x/time v0.7.0
|
golang.org/x/time v0.7.0
|
||||||
)
|
)
|
||||||
|
|
||||||
|
2
go.sum
2
go.sum
@@ -2,6 +2,8 @@ github.com/PuerkitoBio/goquery v1.10.0 h1:6fiXdLuUvYs2OJSvNRqlNPoBm6YABE226xrbav
|
|||||||
github.com/PuerkitoBio/goquery v1.10.0/go.mod h1:TjZZl68Q3eGHNBA8CWaxAN7rOU1EbDz3CWuolcO5Yu4=
|
github.com/PuerkitoBio/goquery v1.10.0/go.mod h1:TjZZl68Q3eGHNBA8CWaxAN7rOU1EbDz3CWuolcO5Yu4=
|
||||||
github.com/andybalholm/cascadia v1.3.2 h1:3Xi6Dw5lHF15JtdcmAHD3i1+T8plmv7BQ/nsViSLyss=
|
github.com/andybalholm/cascadia v1.3.2 h1:3Xi6Dw5lHF15JtdcmAHD3i1+T8plmv7BQ/nsViSLyss=
|
||||||
github.com/andybalholm/cascadia v1.3.2/go.mod h1:7gtRlve5FxPPgIgX36uWBX58OdBsSS6lUvCFb+h7KvU=
|
github.com/andybalholm/cascadia v1.3.2/go.mod h1:7gtRlve5FxPPgIgX36uWBX58OdBsSS6lUvCFb+h7KvU=
|
||||||
|
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||||
|
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
|
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
|
||||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||||
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
|
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
|
||||||
|
@@ -2,9 +2,11 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"log"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/PuerkitoBio/goquery"
|
"github.com/PuerkitoBio/goquery"
|
||||||
|
"github.com/davecgh/go-spew/spew"
|
||||||
)
|
)
|
||||||
|
|
||||||
func ParseHTML(html string) ([]string, error) {
|
func ParseHTML(html string) ([]string, error) {
|
||||||
@@ -14,6 +16,7 @@ func ParseHTML(html string) ([]string, error) {
|
|||||||
return res, fmt.Errorf("failed parsing html: %v", err)
|
return res, fmt.Errorf("failed parsing html: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
log.Printf("Looking for links in %s", html)
|
||||||
doc.Find("tr > td > a").Each(func(i int, s *goquery.Selection) {
|
doc.Find("tr > td > a").Each(func(i int, s *goquery.Selection) {
|
||||||
href, exists := s.Attr("href")
|
href, exists := s.Attr("href")
|
||||||
if !exists {
|
if !exists {
|
||||||
@@ -22,9 +25,86 @@ func ParseHTML(html string) ([]string, error) {
|
|||||||
}
|
}
|
||||||
res = append(res, href)
|
res = append(res, href)
|
||||||
})
|
})
|
||||||
|
log.Printf("Found %d links", len(res))
|
||||||
return res, nil
|
return res, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func ParseDoc(html string) ([]string, error) {
|
func ParseDoc(html string) (Function, error) {
|
||||||
return nil, nil
|
res := Function{}
|
||||||
|
log.Printf("Parsing doc %s", html)
|
||||||
|
|
||||||
|
doc, err := goquery.NewDocumentFromReader(strings.NewReader(html))
|
||||||
|
if err != nil {
|
||||||
|
return res, fmt.Errorf("failed parsing html: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
isArgs := false
|
||||||
|
isReturn := false
|
||||||
|
doc.Find("div.api-listing > p, div.api-listing > ul").Each(func(i int, s *goquery.Selection) {
|
||||||
|
if s.Is("p") {
|
||||||
|
switch s.Text() {
|
||||||
|
case "Arguments:":
|
||||||
|
isArgs = true
|
||||||
|
isReturn = false
|
||||||
|
return
|
||||||
|
case "Returns:":
|
||||||
|
isReturn = true
|
||||||
|
isArgs = false
|
||||||
|
return
|
||||||
|
default:
|
||||||
|
Warning.Printf("Unknown p tag: %s", s.Text())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if s.Is("ul") {
|
||||||
|
params, err := parseUl(s)
|
||||||
|
if err != nil {
|
||||||
|
Error.Printf("Error parsing ul %s: %v", s.Text(), err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if isArgs {
|
||||||
|
res.Arguments = params
|
||||||
|
} else if isReturn {
|
||||||
|
res.Returns = params
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
spew.Dump(res)
|
||||||
|
return res, nil
|
||||||
|
}
|
||||||
|
func parseUl(ul *goquery.Selection) ([]Parameter, error) {
|
||||||
|
res := []Parameter{}
|
||||||
|
ul.Find("li").Each(func(i int, s *goquery.Selection) {
|
||||||
|
log.Printf("Parsing li %s", s.Text())
|
||||||
|
param := Parameter{}
|
||||||
|
|
||||||
|
codes := s.Find("code")
|
||||||
|
if codes.Length() == 0 {
|
||||||
|
Warning.Printf("No code found for %s", s.Text())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
code := codes.First()
|
||||||
|
name := code.Text()
|
||||||
|
if name == "" {
|
||||||
|
Warning.Printf("No name found for %s", s.Text())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
param.Name = name
|
||||||
|
|
||||||
|
if codes.Length() > 1 {
|
||||||
|
code := codes.Last()
|
||||||
|
typ := code.Text()
|
||||||
|
if typ == "" {
|
||||||
|
Warning.Printf("No type found for %s", s.Text())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
param.Type = typ
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Printf("Found param %+v", param)
|
||||||
|
res = append(res, param)
|
||||||
|
})
|
||||||
|
return res, nil
|
||||||
}
|
}
|
7
main.go
7
main.go
@@ -51,10 +51,15 @@ func main() {
|
|||||||
// return
|
// return
|
||||||
// }
|
// }
|
||||||
// log.Printf("%#v", foo)
|
// log.Printf("%#v", foo)
|
||||||
foo, err := ParseHTML(doc)
|
foo, err := ParseDoc(doc)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
Error.Printf("Error parsing HTML: %v", err)
|
Error.Printf("Error parsing HTML: %v", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
log.Printf("%#v", foo)
|
log.Printf("%#v", foo)
|
||||||
|
err = foo.WriteFile()
|
||||||
|
if err != nil {
|
||||||
|
Error.Printf("Error writing file: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user