Code generate the comments in the type and property GoDoc.

This commit is contained in:
Cory Slep 2019-01-13 23:04:24 +01:00
parent d3a4507a2a
commit e2eed869af
2 changed files with 56 additions and 4 deletions

View file

@ -8,17 +8,19 @@ const (
max_width = 80
tab_assumed_width = 8
replacement = "\n// "
httpsScheme = "https://"
httpScheme = "http://"
)
// FormatPackageDocumentation is used to format package-level comments.
func FormatPackageDocumentation(s string) string {
s = strings.Replace(s, "\n", replacement, -1)
return insertNewlines(s)
}
// insertNewlines is used to trade a space character for a newline character
// in order to keep a string's visual width under a certain amount.
func insertNewlines(s string) string {
s = strings.Replace(s, "\n", replacement, -1)
return insertNewlinesEvery(s, max_width)
}
@ -41,8 +43,15 @@ func insertNewlinesEvery(s string, n int) string {
if s[i] == ' ' && (since < n || found < 0) {
found = i
} else if s[i] == '\n' {
// Reset, found a newline
since = 0
found = -1
} else if i > len(httpScheme) && s[i-len(httpScheme)+1:i+1] == httpScheme {
// Reset, let the link just extend annoyingly.
found = -1
} else if i > len(httpsScheme) && s[i-len(httpsScheme)+1:i+1] == httpsScheme {
// Reset, let the link just extend annoyingly.
found = -1
}
if since >= n && found >= 0 {
// Replace character

View file

@ -1,6 +1,7 @@
package convert
import (
"encoding/json"
"fmt"
"github.com/cjslep/activity/tools/exp/codegen"
"github.com/cjslep/activity/tools/exp/gen"
@ -446,11 +447,19 @@ func (c Converter) convertType(t rdf.VocabularyType,
}
}
}
var examples []string
for _, ex := range t.Examples {
examples = append(examples, asComment(ex))
}
comment := t.Notes
if len(examples) > 0 {
comment = fmt.Sprintf("%s\n\n%s", comment, strings.Join(examples, "\n\n"))
}
tg, e = gen.NewTypeGenerator(
v.GetName(),
pm,
name,
t.Notes,
comment,
p,
wop,
rangeProps,
@ -475,11 +484,19 @@ func (c Converter) convertFunctionalProperty(p rdf.VocabularyProperty,
if e != nil {
return
}
var examples []string
for _, ex := range p.Examples {
examples = append(examples, asComment(ex))
}
comment := p.Notes
if len(examples) > 0 {
comment = fmt.Sprintf("%s\n\n%s", comment, strings.Join(examples, "\n\n"))
}
fp = gen.NewFunctionalPropertyGenerator(
v.GetName(),
pm,
toIdentifier(p),
p.Notes,
comment,
k,
p.NaturalLanguageMap)
return
@ -502,11 +519,19 @@ func (c Converter) convertNonFunctionalProperty(p rdf.VocabularyProperty,
if e != nil {
return
}
var examples []string
for _, ex := range p.Examples {
examples = append(examples, asComment(ex))
}
comment := p.Notes
if len(examples) > 0 {
comment = fmt.Sprintf("%s\n\n%s", comment, strings.Join(examples, "\n\n"))
}
nfp = gen.NewNonFunctionalPropertyGenerator(
v.GetName(),
pm,
toIdentifier(p),
p.Notes,
comment,
k,
p.NaturalLanguageMap)
return
@ -953,3 +978,21 @@ func funcsToFile(pkg gen.Package, fns []*codegen.Function, filename string) *Fil
Directory: pkg.WriteDir(),
}
}
// AsComment creates a Go-comment-compatible string out of an Example.
func asComment(v rdf.VocabularyExample) (s string) {
if len(v.Name) > 0 && v.URI != nil {
s = fmt.Sprintf("%s (%s):\n", v.Name, v.URI)
} else if len(v.Name) > 0 {
s = fmt.Sprintf("%s:\n", v.Name)
} else if v.URI != nil {
s = fmt.Sprintf("%s:\n", v.URI)
}
b, err := json.MarshalIndent(v.Example, "", " ")
if err != nil {
panic(err)
}
ex := string(b)
ex = strings.Replace(ex, "\n", "\n ", -1)
return fmt.Sprintf("%s %s", s, ex)
}