package main import ( _ "embed" "fmt" "log" "os" "path/filepath" "regexp" "strings" "text/template" "github.com/PuerkitoBio/goquery" "github.com/davecgh/go-spew/spew" ) //go:embed class.tmpl var templatestr string var classTemplate *template.Template var fns = template.FuncMap{ "plus1": func(x int) int { return x + 1 }, } func init() { var err error classTemplate, err = template.New("class").Funcs(fns).Parse(templatestr) if err != nil { Error.Printf("Error parsing template: %v", err) return } } type ( Class struct { ClassName string Description string Fields []Field Methods []Method Constructors []Constructor } Field struct { Name string Type string Comment string } Method struct { Name string Params []Param Returns []Return Comment string } Param struct { Name string Type string Comment string } Return struct { Type string Comment string } Constructor struct { Params []Param Returns string Comment string } ) func (c *Class) GetOutFile(root string) (*os.File, error) { if c.ClassName == "" { return nil, fmt.Errorf("ClassName is empty") } filename := fmt.Sprintf("%s.lua", c.ClassName) filename = strings.ReplaceAll(filename, " ", "") filename = strings.ReplaceAll(filename, "-", "") filename = strings.ReplaceAll(filename, ",", "") filename = strings.ReplaceAll(filename, ":", "") filePath := filepath.Join(root, filename) filePath = filepath.Clean(filePath) f, err := os.Create(filePath) if err != nil { return nil, err } return f, nil } func (c *Class) Write(root string, tmpl *template.Template) error { outfile, err := c.GetOutFile(root) if err != nil { return fmt.Errorf("error creating output file %v: %v", c.ClassName, err) } spew.Dump(c) err = tmpl.Execute(outfile, c) if err != nil { return fmt.Errorf("error writing output file %v: %v", c.ClassName, err) } return nil } func ParseClass(file string) (*Class, error) { log.Printf("Parsing file: '%s'", file) res := Class{ Fields: []Field{}, Methods: []Method{}, Constructors: []Constructor{}, } filehandle, err := os.Open(file) if err != nil { return nil, fmt.Errorf("error opening file: %w", err) } defer filehandle.Close() doc, err := goquery.NewDocumentFromReader(filehandle) if err != nil { return nil, fmt.Errorf("error parsing file: %w", err) } dataContainer := doc.Find("div.floatright") if dataContainer.Length() == 0 { return nil, fmt.Errorf("no data container found") } className, err := getClassName(dataContainer) if err != nil { return nil, fmt.Errorf("error getting class name: %w", err) } res.ClassName = className classDescription, err := getClassDescription(dataContainer) if err == nil { // return nil, fmt.Errorf("error getting class description: %w", err) res.Description = classDescription } constructor, err := getConstructor(dataContainer) if err != nil { return nil, fmt.Errorf("error getting constructor: %w", err) } res.Constructors = append(res.Constructors, 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" { 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) }) } else { method, err := parseMethod(s) if err != nil { Error.Printf("Error parsing method: %v", err) return } res.Methods = append(res.Methods, method) } }) 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 := CleanUp(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 := 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: CleanUp(params.Eq(i).Text()), Type: MapType(CleanUp(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.Eq(0).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

tag that does not close any open

// I assume this is a bug with their documentation generator // So we just ignore it return } text := CleanUp(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 := CleanUp(param) for i := range resConstructor.Params { cparam := &resConstructor.Params[i] if paramTrimmed == cparam.Name { cleanText := strings.TrimPrefix(text, param) cparam.Comment = CleanUp(cleanText) } } case 2: cleaned := CleanUp(s.Text()) if resConstructor.Returns != "" { resConstructor.Returns += "\n" } resConstructor.Returns = cleaned } } }) // spew.Dump(resConstructor) 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 = MapType(CleanUp(typeElement.Text())) readonlyElement := s.Find("td[align='right']") if readonlyElement.Length() != 0 { // return res, fmt.Errorf("no readonly found") res.Comment = CleanUp(readonlyElement.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--", ". ") return res, nil } func parseMethod(s *goquery.Selection) (Method, error) { res := Method{} id, ok := s.Attr("id") if !ok { return res, fmt.Errorf("no id found") } res.Name = id signatureData := s.Children().Eq(0) returns := signatureData.Find("span.keyword") if returns.Length() != 0 { returnsText := CleanUp(returns.Text()) returnsText = strings.ReplaceAll(returnsText, "function", "") returnsText = CleanUp(returnsText) res.Returns = append(res.Returns, Return{ Type: MapType(returnsText), Comment: "", }) } types := signatureData.Find("span.type") parameters := signatureData.Find("span.parameter") types.Each(func(i int, s *goquery.Selection) { res.Params = append(res.Params, Param{ Name: MapName(CleanUp(parameters.Eq(i).Text())), Type: MapType(CleanUp(types.Eq(i).Text())), Comment: "", }) }) // 0 nothing // 1 parameters // 2 returns state := 0 signatureDetails := s.Children().Eq(1) signatureDetailsChildren := signatureDetails.Children() signatureDetailsChildrenLength := signatureDetailsChildren.Length() signatureDetailsChildren.Each(func(i int, s *goquery.Selection) { if s.Is("p") && i == 0 { methodDescription := CleanUp(s.Text()) methodDescription = strings.ReplaceAll(methodDescription, "\n--", "
\n\t---") res.Comment = methodDescription } if i == signatureDetailsChildrenLength-1 { // For some stupid reason the last child is always a mispalced

tag that does not close any open

// I assume this is a bug with their documentation generator // So we just ignore it return } text := CleanUp(s.Text()) if text == "" { return } if s.Is("p") { switch text { case "Parameters": state = 1 case "Returns": state = 2 return } } else { if s.Is("div.indented") { if state == 1 { parameter := "" html, err := s.Html() if err != nil { Error.Printf("Error parsing html: %v", err) return } html = strings.ReplaceAll(html, "\t", "") htmlLines := strings.Split(html, "\n") for _, line := range htmlLines { if strings.Contains(line, "") { line = strings.TrimSpace(line) line = strings.ReplaceAll(line, "", "") line = strings.ReplaceAll(line, "", "") parameter = line continue } if strings.Contains(line, "
") { comment := strings.TrimSpace(line) comment = strings.ReplaceAll(comment, "
", "") for i := range res.Params { if res.Params[i].Name == parameter { res.Params[i].Comment = comment break } } } } } else if state == 2 { text := CleanUp(s.Text()) if text == "" { return } // TODO: Figure out what to do when a function if res.Returns[0].Comment != "" { res.Returns[0].Comment += "\n" } res.Returns[0].Comment += text } } } }) 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 }