Rework class name and description parsing

This commit is contained in:
2024-09-14 20:19:39 +02:00
parent 62ae7ab59c
commit acdfec2715
2 changed files with 37 additions and 15 deletions

View File

@@ -6,6 +6,7 @@ import (
"log" "log"
"os" "os"
"path/filepath" "path/filepath"
"regexp"
"strings" "strings"
"text/template" "text/template"
@@ -35,6 +36,7 @@ func init() {
type ( type (
Class struct { Class struct {
ClassName string ClassName string
Description string
Fields []Field Fields []Field
Methods []Method Methods []Method
Constructors []Constructor Constructors []Constructor
@@ -113,31 +115,51 @@ func ParseClass(file string) (*Class, error) {
return nil, fmt.Errorf("error parsing file: %w", err) return nil, fmt.Errorf("error parsing file: %w", err)
} }
class := doc.Find("div.floatright > h1") dataContainer := doc.Find("div.floatright")
if class.Length() == 0 { if dataContainer.Length() == 0 {
return nil, fmt.Errorf("no class found") return nil, fmt.Errorf("no data container found")
} }
res.ClassName = strings.TrimSpace(class.Text())
res.Constructors, err = getConstructors(doc) className, err := getClassName(dataContainer)
if err != nil { if err != nil {
return nil, fmt.Errorf("error getting constructors: %w", err) return nil, fmt.Errorf("error getting class name: %w", err)
} }
res.ClassName = className
res.Fields, err = getFields(doc) classDescription, err := getClassDescription(dataContainer)
if err != nil { if err != nil {
return nil, fmt.Errorf("error getting fields: %w", err) return nil, fmt.Errorf("error getting class description: %w", err)
} }
res.Description = classDescription
res.Methods, err = getMethods(doc) spew.Dump(res)
if err != nil {
return nil, fmt.Errorf("error getting methods: %w", err)
}
// spew.Dump(res)
return &res, nil return &res, nil
} }
func getClassName(dataContainer *goquery.Selection) (string, error) {
class := dataContainer.ChildrenFiltered("h1")
if class.Length() == 0 {
return "", fmt.Errorf("no class found")
}
res := strings.TrimSpace(class.Text())
return res, nil
}
var manySpaceRe = regexp.MustCompile(`\s{2,}`)
func getClassDescription(dataContainer *goquery.Selection) (string, error) {
class := dataContainer.ChildrenFiltered("h1 + p")
if class.Length() == 0 {
return "", fmt.Errorf("no class description found")
}
res := strings.TrimSpace(class.Text())
res = strings.ReplaceAll(res, "\t", " ")
res = strings.ReplaceAll(res, "\n", "")
res = manySpaceRe.ReplaceAllString(res, " ")
res = strings.ReplaceAll(res, ". ", "\n--")
return res, nil
}
// TODO: Implement parsing comments for return values // TODO: Implement parsing comments for return values
// Something like "---returns (something) -> comment" // Something like "---returns (something) -> comment"
// Where "-> comment" is only shown if there's a comment // Where "-> comment" is only shown if there's a comment
@@ -190,7 +212,6 @@ func getConstructors(doc *goquery.Document) ([]Constructor, error) {
resConstructor.Comment = strings.TrimSpace(resConstructor.Comment) resConstructor.Comment = strings.TrimSpace(resConstructor.Comment)
}) })
spew.Dump(resConstructor)
return append(res, resConstructor), nil return append(res, resConstructor), nil
} }

View File

@@ -1,5 +1,6 @@
---@diagnostic disable: missing-return, lowercase-global ---@diagnostic disable: missing-return, lowercase-global
---@class {{.ClassName}} ---@class {{.ClassName}}
---{{.Description}}
{{- range .Fields}} {{- range .Fields}}
---@field {{.Name}} {{.Type}}{{if ne .Comment ""}} {{.Comment}}{{end}} ---@field {{.Name}} {{.Type}}{{if ne .Comment ""}} {{.Comment}}{{end}}
{{- end}} {{- end}}