diff --git a/class.go b/class.go index ee23314..8881e8e 100644 --- a/class.go +++ b/class.go @@ -6,6 +6,7 @@ import ( "log" "os" "path/filepath" + "regexp" "strings" "text/template" @@ -35,6 +36,7 @@ func init() { type ( Class struct { ClassName string + Description string Fields []Field Methods []Method Constructors []Constructor @@ -113,31 +115,51 @@ func ParseClass(file string) (*Class, error) { return nil, fmt.Errorf("error parsing file: %w", err) } - class := doc.Find("div.floatright > h1") - if class.Length() == 0 { - return nil, fmt.Errorf("no class found") + dataContainer := doc.Find("div.floatright") + if dataContainer.Length() == 0 { + 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 { - 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 { - 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) - if err != nil { - return nil, fmt.Errorf("error getting methods: %w", err) - } - - // spew.Dump(res) + spew.Dump(res) 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 // Something like "---returns (something) -> 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) }) - spew.Dump(resConstructor) return append(res, resConstructor), nil } diff --git a/class.tmpl b/class.tmpl index 951584e..dc234ff 100644 --- a/class.tmpl +++ b/class.tmpl @@ -1,5 +1,6 @@ ---@diagnostic disable: missing-return, lowercase-global ---@class {{.ClassName}} +---{{.Description}} {{- range .Fields}} ---@field {{.Name}} {{.Type}}{{if ne .Comment ""}} {{.Comment}}{{end}} {{- end}}