Implement construcor parameter parsing

This commit is contained in:
2024-09-14 22:24:13 +02:00
parent acdfec2715
commit a766f09f87
2 changed files with 115 additions and 7 deletions

120
class.go
View File

@@ -132,7 +132,29 @@ func ParseClass(file string) (*Class, error) {
}
res.Description = classDescription
spew.Dump(res)
constructor, err := getConstructor(dataContainer)
if err != nil {
return nil, fmt.Errorf("error getting constructor: %w", err)
}
constructor = constructor
// Doing div+div specifically skips only the constructor which is usually the first div
// So the constructor would then be located at h1 + p + div OR h1 + div if there is no description (no p)
dataElements := dataContainer.ChildrenFiltered("div + div")
if dataElements.Length() == 0 {
return nil, fmt.Errorf("no data elements found")
}
dataElements.Each(func(i int, s *goquery.Selection) {
id, ok := s.Attr("id")
if !ok {
return
}
if id == "Properties" {
// TODO: Implement parsing properties
}
})
return &res, nil
}
@@ -152,14 +174,91 @@ func getClassDescription(dataContainer *goquery.Selection) (string, error) {
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--")
res := CleanUp(class.Text())
return res, nil
}
func getConstructor(dataContainer *goquery.Selection) (Constructor, error) {
resConstructor := Constructor{}
constructorBlock := dataContainer.Find("h1 + p + div")
if constructorBlock.Length() == 0 {
constructorBlock = dataContainer.Find("h1 + div")
if constructorBlock.Length() == 0 {
return resConstructor, fmt.Errorf("no constructor found")
}
}
types := constructorBlock.Find("div.function span.type")
if types.Length() == 0 {
return resConstructor, fmt.Errorf("no types found")
}
params := constructorBlock.Find("div.function span.parameter")
if params.Length() == 0 {
return resConstructor, fmt.Errorf("no params found")
}
types.Each(func(i int, s *goquery.Selection) {
resConstructor.Params = append(resConstructor.Params, Param{
Name: strings.TrimSpace(params.Eq(i).Text()),
Type: strings.TrimSpace(types.Eq(i).Text()),
Comment: "",
})
})
descriptor := constructorBlock.Find("div:not(.function)")
if descriptor.Length() == 0 {
return resConstructor, fmt.Errorf("no descriptor found")
}
// 0 nothing
// 1 parameters
// 2 returns
state := 0
children := descriptor.Children()
childrenLength := children.Length()
children.Each(func(i int, s *goquery.Selection) {
if i == childrenLength-1 {
// For some stupid reason the last child is always a mispalced </p> tag that does not close any open <p>
// I assume this is a bug with their documentation generator
// So we just ignore it
return
}
text := strings.TrimSpace(s.Text())
if text == "" {
return
}
if s.Is("p") {
switch text {
case "Parameters":
state = 1
return
case "Returns":
state = 2
return
}
} else {
switch state {
case 0:
return
case 1:
param := s.ChildrenFiltered("span.parameter").Text()
paramTrimmed := strings.TrimSpace(param)
for i := range resConstructor.Params {
cparam := &resConstructor.Params[i]
if paramTrimmed == cparam.Name {
cleanText := strings.TrimPrefix(text, param)
cparam.Comment = CleanUp(cleanText)
}
}
}
}
})
spew.Dump(resConstructor)
return resConstructor, nil
}
// TODO: Implement parsing comments for return values
// Something like "---returns (something) -> comment"
// Where "-> comment" is only shown if there's a comment
@@ -257,3 +356,12 @@ func getMethods(doc *goquery.Document) ([]Method, error) {
return res, nil
}
func CleanUp(s string) string {
s = strings.TrimSpace(s)
s = strings.ReplaceAll(s, "\t", " ")
s = strings.ReplaceAll(s, "\n", "")
s = manySpaceRe.ReplaceAllString(s, " ")
s = strings.ReplaceAll(s, ". ", "\n--")
return s
}