From fd310a9b849a291310c58954ac548939cbd66b00 Mon Sep 17 00:00:00 2001 From: Kenneth Shaw Date: Fri, 1 Dec 2017 11:00:44 +0700 Subject: [PATCH] Centralizing comment / description cleanup in chromedp-gen --- cmd/chromedp-gen/internal/types.go | 5 ++--- cmd/chromedp-gen/internal/util.go | 31 ++++++++++++++++++++---------- cmd/chromedp-gen/templates/util.go | 11 +---------- 3 files changed, 24 insertions(+), 23 deletions(-) diff --git a/cmd/chromedp-gen/internal/types.go b/cmd/chromedp-gen/internal/types.go index 7872d8c..99cf16b 100644 --- a/cmd/chromedp-gen/internal/types.go +++ b/cmd/chromedp-gen/internal/types.go @@ -57,7 +57,7 @@ func (d *Domain) String() string { // GetDescription returns the formatted description of the domain. func (d *Domain) GetDescription() string { - return CodeRE.ReplaceAllString(d.Description, "") + return CleanDesc(d.Description) } // PackageName returns the Go package name to use for the domain. @@ -216,7 +216,6 @@ func (t Type) IDorName() string { func (t Type) String() string { desc := t.GetDescription() if desc != "" { - desc, _ = MisspellReplacer.Replace(desc) desc = " - " + desc } @@ -225,7 +224,7 @@ func (t Type) String() string { // GetDescription returns the cleaned description for the type. func (t *Type) GetDescription() string { - return CodeRE.ReplaceAllString(t.Description, "") + return CleanDesc(t.Description) } // EnumValues returns enum values for the type. diff --git a/cmd/chromedp-gen/internal/util.go b/cmd/chromedp-gen/internal/util.go index 11ba1bb..83ef345 100644 --- a/cmd/chromedp-gen/internal/util.go +++ b/cmd/chromedp-gen/internal/util.go @@ -19,12 +19,28 @@ const ( Base64EncodedDescriptionPrefix = "Base64-encoded" ) -// MisspellReplacer is the misspelling replacer -var MisspellReplacer *misspell.Replacer +// misspellReplacer is the misspelling replacer +var misspellReplacer *misspell.Replacer func init() { - MisspellReplacer = misspell.New() - MisspellReplacer.Compile() + misspellReplacer = misspell.New() + misspellReplacer.Compile() +} + +var badHTMLReplacer = strings.NewReplacer( + "<", "<", + ">", ">", + ">", ">", +) + +// codeRE is a regexp to match and tags. +var codeRE = regexp.MustCompile(`<\/?code>`) + +// CleanDesc cleans comments / descriptions of "" and "" strings +// and "`" characters, and fixes common misspellings. +func CleanDesc(s string) string { + s, _ = misspellReplacer.Replace(strings.Replace(codeRE.ReplaceAllString(s, ""), "`", "", -1)) + return badHTMLReplacer.Replace(s) } // ForceCamel forces camel case specific to go. @@ -49,9 +65,6 @@ func ForceCamelWithFirstLower(s string) string { return strings.ToLower(first) + s[len(first):] } -// CodeRE is a regexp to match and tags. -var CodeRE = regexp.MustCompile(`<\/?code>`) - // resolve finds the ref in the provided domains, relative to domain d when ref // is not namespaced. func resolve(ref string, d *Domain, domains []*Domain) (DomainType, *Type, string) { @@ -123,9 +136,7 @@ func structDef(types []*Type, d *Domain, domains []*Domain, noExposeOverride, om // add comment if v.Type != TypeObject && v.Description != "" { - comment := CodeRE.ReplaceAllString(v.Description, "") - comment, _ = MisspellReplacer.Replace(comment) - s += " // " + comment + s += " // " + CleanDesc(v.Description) } } if len(types) > 0 { diff --git a/cmd/chromedp-gen/templates/util.go b/cmd/chromedp-gen/templates/util.go index d3bc324..606d9e0 100644 --- a/cmd/chromedp-gen/templates/util.go +++ b/cmd/chromedp-gen/templates/util.go @@ -25,17 +25,10 @@ var keep = map[string]bool{ "JavaScript": true, } -var badHTMLReplacer = strings.NewReplacer( - "<", "<", - ">", ">", - ">", ">", -) - // formatComment formats a comment. func formatComment(s, chop, newstr string) string { s = strings.TrimPrefix(s, chop) - s = strings.TrimSpace(internal.CodeRE.ReplaceAllString(s, "")) - s = badHTMLReplacer.Replace(s) + s = strings.TrimSpace(internal.CleanDesc(s)) l := len(s) if newstr != "" && l > 0 { @@ -57,8 +50,6 @@ func formatComment(s, chop, newstr string) string { } s += "." - s, _ = internal.MisspellReplacer.Replace(s) - return wrap(s, commentWidth-len(commentPrefix), commentPrefix) }