From 627ca81181984953c213ce7c0138885d5a600efa Mon Sep 17 00:00:00 2001 From: PhatPhuckDave Date: Sun, 15 Sep 2024 13:09:07 +0200 Subject: [PATCH] Implement field/property parsing --- class.go | 45 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git a/class.go b/class.go index 30a1624..73bf590 100644 --- a/class.go +++ b/class.go @@ -11,6 +11,7 @@ import ( "text/template" "github.com/PuerkitoBio/goquery" + "github.com/davecgh/go-spew/spew" ) //go:embed class.tmpl @@ -152,7 +153,14 @@ func ParseClass(file string) (*Class, error) { return } if id == "Properties" { - // TODO: Implement parsing properties + s.Children().Each(func(i int, s *goquery.Selection) { + field, err := parseField(s) + if err != nil { + Error.Printf("Error parsing field: %v", err) + return + } + res.Fields = append(res.Fields, field) + }) } }) @@ -266,6 +274,41 @@ func getConstructor(dataContainer *goquery.Selection) (Constructor, error) { return resConstructor, nil } +func parseField(s *goquery.Selection) (Field, error) { + res := Field{} + + id, ok := s.Attr("id") + if !ok { + return res, fmt.Errorf("no id found") + } + res.Name = id + + typeElement := s.Find("span.type") + if typeElement.Length() == 0 { + return res, fmt.Errorf("no type found") + } + res.Type = CleanUp(typeElement.Text()) + + comments := s.ChildrenFiltered("div") + if comments.Length() == 0 { + return res, fmt.Errorf("no comments found") + } + comments.Each(func(i int, s *goquery.Selection) { + text := CleanUp(s.Text()) + if text == "" { + return + } + if res.Comment != "" { + res.Comment += ". " + } + res.Comment += text + }) + res.Comment = strings.ReplaceAll(res.Comment, "\n--", ". ") + + spew.Dump(res) + return res, nil +} + func CleanUp(s string) string { s = strings.TrimSpace(s) s = strings.ReplaceAll(s, "\t", " ")