From e556e9d23f77c6745ff36d60156afce853d11698 Mon Sep 17 00:00:00 2001 From: PhatPhuckDave Date: Sun, 15 Sep 2024 14:08:51 +0200 Subject: [PATCH] Implement method description parser --- class.go | 69 +++++++++++++++++++++++++++++++++++++++++++++++------- class.tmpl | 3 ++- 2 files changed, 62 insertions(+), 10 deletions(-) diff --git a/class.go b/class.go index fd148d9..db4442c 100644 --- a/class.go +++ b/class.go @@ -330,7 +330,8 @@ func parseMethod(s *goquery.Selection) (Method, error) { } res.Name = id - returns := s.Find("span.keyword") + signatureData := s.Children().Eq(0) + returns := signatureData.Find("span.keyword") if returns.Length() != 0 { returnsText := CleanUp(returns.Text()) returnsText = strings.ReplaceAll(returnsText, "function", "") @@ -342,8 +343,8 @@ func parseMethod(s *goquery.Selection) (Method, error) { }) } - types := s.Find("span.type") - parameters := s.Find("span.parameter") + 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())), @@ -352,18 +353,68 @@ func parseMethod(s *goquery.Selection) (Method, error) { }) }) - //
- //
+ // 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 { + log.Printf("%#v", state) + } + }) + + //

+ //
//

- // function var add(CargoBay other)
+ // function unsigned int addCargo(TradingGood + // good, int amount)
//

//
- //
+ //
+ //

+ // Adds cargo to the entity. If the amount specified exceeds the maximum capacity of the cargo + // bay, as + // much cargo as still fits in will be added. + //

+ //

Parameters

+ //
+ // good + // TradingGood that is to be added.
+ // amount + // The amount of cargo that should be added.
+ //
//

Returns

//
//

- // nothing + // How much was actually added (can be less than amount when cargo bay is full) //

//
//

diff --git a/class.tmpl b/class.tmpl index 47b52b8..2c69178 100644 --- a/class.tmpl +++ b/class.tmpl @@ -13,7 +13,8 @@ {{- end}} {{- range $ret := $method.Returns}} ---@return {{.Type}}{{if ne .Comment ""}} #{{.Comment}}{{end}} - {{- end}} + {{- end}}{{if ne .Comment ""}} + ---{{.Comment}}{{end}} {{.Name}} = function(self{{if gt (len .Params) 0}}, {{range $index, $param := .Params}}{{if $index}}, {{end}}{{$param.Name}}{{end}}{{end}}) end, {{- if ne (plus1 $index) $n}} {{end}}