From bfe4767eadeb1a29beb66b7d11f72ea099464bd4 Mon Sep 17 00:00:00 2001 From: PhatPhuckDave Date: Wed, 6 Nov 2024 00:19:00 +0100 Subject: [PATCH] Implement parsing functions partially --- function.go | 16 +++++++--- go.mod | 1 + go.sum | 2 ++ html-parser.go | 86 ++++++++++++++++++++++++++++++++++++++++++++++++-- main.go | 7 +++- 5 files changed, 103 insertions(+), 9 deletions(-) diff --git a/function.go b/function.go index 842cddb..d20fede 100644 --- a/function.go +++ b/function.go @@ -2,6 +2,7 @@ package main import ( _ "embed" + "fmt" "log" "os" "text/template" @@ -17,6 +18,7 @@ var fns = template.FuncMap{ var templatestr string var functionTemplate *template.Template + func init() { var err error functionTemplate, err = template.New("class").Funcs(fns).Parse(templatestr) @@ -30,10 +32,10 @@ func init() { type ( Function struct { Name string - Arguments []Argument - Returns []Argument + Arguments []Parameter + Returns []Parameter } - Argument struct { + Parameter struct { Name string Type string Description string @@ -44,15 +46,19 @@ func (f *Function) ResolveFileName() string { return f.Name + ".lua" } func (f *Function) WriteFile() error { + if f.Name == "" { + return fmt.Errorf("function name is empty of %+v", f) + } + file, err := os.Create(f.ResolveFileName()) if err != nil { return err } defer file.Close() - + err = functionTemplate.Execute(file, f) if err != nil { return err } return nil -} \ No newline at end of file +} diff --git a/go.mod b/go.mod index 55601eb..6e8e207 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,7 @@ go 1.23.2 require ( github.com/PuerkitoBio/goquery v1.10.0 + github.com/davecgh/go-spew v1.1.1 golang.org/x/time v0.7.0 ) diff --git a/go.sum b/go.sum index 278cc0d..7592a2a 100644 --- a/go.sum +++ b/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/andybalholm/cascadia v1.3.2 h1:3Xi6Dw5lHF15JtdcmAHD3i1+T8plmv7BQ/nsViSLyss= 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= 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= diff --git a/html-parser.go b/html-parser.go index 195fda0..960e634 100644 --- a/html-parser.go +++ b/html-parser.go @@ -2,9 +2,11 @@ package main import ( "fmt" + "log" "strings" "github.com/PuerkitoBio/goquery" + "github.com/davecgh/go-spew/spew" ) 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) } + log.Printf("Looking for links in %s", html) doc.Find("tr > td > a").Each(func(i int, s *goquery.Selection) { href, exists := s.Attr("href") if !exists { @@ -22,9 +25,86 @@ func ParseHTML(html string) ([]string, error) { } res = append(res, href) }) + log.Printf("Found %d links", len(res)) return res, nil } -func ParseDoc(html string) ([]string, error) { - return nil, nil -} \ No newline at end of file +func ParseDoc(html string) (Function, error) { + 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 +} diff --git a/main.go b/main.go index 02d1716..fd93df7 100644 --- a/main.go +++ b/main.go @@ -51,10 +51,15 @@ func main() { // return // } // log.Printf("%#v", foo) - foo, err := ParseHTML(doc) + foo, err := ParseDoc(doc) if err != nil { Error.Printf("Error parsing HTML: %v", err) return } log.Printf("%#v", foo) + err = foo.WriteFile() + if err != nil { + Error.Printf("Error writing file: %v", err) + return + } }