Implement method description parser

This commit is contained in:
2024-09-15 14:08:51 +02:00
parent 6836b35004
commit e556e9d23f
2 changed files with 62 additions and 10 deletions

View File

@@ -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) {
})
})
// <div id="add" class="codecontainer">
// <div id="add" class="function">
// 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--", "<br>\n\t---")
res.Comment = methodDescription
}
if i == signatureDetailsChildrenLength-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 := 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)
}
})
// <div id="addCargo" class="codecontainer">
// <div id="addCargo" class="function">
// <p>
// <span class="keyword">function var</span> add(<span class="type">CargoBay</span> <span
// class="parameter">other</span>) <br />
// <span class="keyword">function unsigned int</span> addCargo(<span
// class="type">TradingGood</span>
// <span class="parameter">good</span>, <span class="type">int</span> <span
// class="parameter">amount</span>) <br />
// </p>
// </div>
// <div id="add" class="">
// <div id="addCargo" class="">
// <p>
// 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.
// </p>
// <p><span class="docheader">Parameters</span></p>
// <div class="indented">
// <span class="parameter">good</span>
// TradingGood that is to be added. <br />
// <span class="parameter">amount</span>
// The amount of cargo that should be added. <br />
// </div>
// <p><span class="docheader">Returns</span></p>
// <div class="indented">
// <p>
// nothing
// How much was actually added (can be less than amount when cargo bay is full)
// </p>
// </div>
// </p>

View File

@@ -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}}