diff --git a/util/templates/documentation.go b/util/templates/documentation.go index a508a037c9..d329ffac21 100644 --- a/util/templates/documentation.go +++ b/util/templates/documentation.go @@ -16,7 +16,9 @@ var documentationTmpl string var documentationModbusTmpl string // RenderDocumentation renders the documentation template -func (t *Template) RenderDocumentation(product Product, values map[string]interface{}, lang string) ([]byte, error) { +func (t *Template) RenderDocumentation(product Product, lang string) ([]byte, error) { + values := t.Defaults(TemplateRenderModeDocs) + for index, p := range t.Params { for k, v := range values { if p.Name != k { @@ -39,47 +41,42 @@ func (t *Template) RenderDocumentation(product Product, values map[string]interf } } - usages := t.Usages() - modbusChoices := t.ModbusChoices() - modbusRender := "" - if len(modbusChoices) > 0 { + var modbusRender string + if modbusChoices := t.ModbusChoices(); len(modbusChoices) > 0 { if i, _ := t.ParamByName(ParamModbus); i > -1 { modbusTmpl, err := template.New("yaml").Funcs(template.FuncMap(sprig.FuncMap())).Parse(documentationModbusTmpl) if err != nil { panic(err) } - modbusData := map[string]interface{}{} + modbusData := make(map[string]interface{}) t.ModbusValues(TemplateRenderModeDocs, modbusData) - modbusOut := new(bytes.Buffer) - - err = modbusTmpl.Execute(modbusOut, modbusData) - if err != nil { + out := new(bytes.Buffer) + if err := modbusTmpl.Execute(out, modbusData); err != nil { panic(err) } - modbusRender = modbusOut.String() + modbusRender = out.String() } } + var hasAdvancedParams bool + // remove usage and deprecated from params and check if there are advanced params - var hasAdvancedParam bool - var newParams []Param + var filteredParams []Param for _, param := range t.Params { if param.IsDeprecated() || param.Name == ParamUsage { continue } if param.IsAdvanced() { - hasAdvancedParam = true + hasAdvancedParams = true } - newParams = append(newParams, param) + filteredParams = append(filteredParams, param) } - t.Params = newParams - out := new(bytes.Buffer) data := map[string]interface{}{ "Template": t.Template, "ProductBrand": product.Brand, @@ -88,12 +85,13 @@ func (t *Template) RenderDocumentation(product Product, values map[string]interf "Capabilities": t.Capabilities, "Requirements": t.Requirements.EVCC, "RequirementDescription": t.Requirements.Description.String(lang), - "Params": t.Params, - "AdvancedParams": hasAdvancedParam, - "Usages": usages, + "Params": filteredParams, + "AdvancedParams": hasAdvancedParams, + "Usages": t.Usages(), "Modbus": modbusRender, } + out := new(bytes.Buffer) tmpl, err := template.New("yaml").Funcs(template.FuncMap(sprig.FuncMap())).Parse(documentationTmpl) if err == nil { err = tmpl.Execute(out, data) diff --git a/util/templates/generate/generate.go b/util/templates/generate/generate.go index 93757f3db9..407e053c04 100644 --- a/util/templates/generate/generate.go +++ b/util/templates/generate/generate.go @@ -62,9 +62,7 @@ func generateClass(class templates.Class) error { } func writeTemplate(class templates.Class, index int, product templates.Product, tmpl templates.Template) error { - values := tmpl.Defaults(templates.TemplateRenderModeDocs) - - b, err := tmpl.RenderDocumentation(product, values, "de") + b, err := tmpl.RenderDocumentation(product, "de") if err == nil { filename := fmt.Sprintf("%s/%s/%s_%d.yaml", docsPath, strings.ToLower(class.String()), tmpl.Template, index) err = os.WriteFile(filename, b, 0o644)