Code generate the comments in the type and property GoDoc.

このコミットが含まれているのは:
Cory Slep 2019-01-13 23:04:24 +01:00
コミット e2eed869af
2個のファイルの変更56行の追加4行の削除

ファイルの表示

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

ファイルの表示

@ -1,6 +1,7 @@
package convert package convert
import ( import (
"encoding/json"
"fmt" "fmt"
"github.com/cjslep/activity/tools/exp/codegen" "github.com/cjslep/activity/tools/exp/codegen"
"github.com/cjslep/activity/tools/exp/gen" "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( tg, e = gen.NewTypeGenerator(
v.GetName(), v.GetName(),
pm, pm,
name, name,
t.Notes, comment,
p, p,
wop, wop,
rangeProps, rangeProps,
@ -475,11 +484,19 @@ func (c Converter) convertFunctionalProperty(p rdf.VocabularyProperty,
if e != nil { if e != nil {
return 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( fp = gen.NewFunctionalPropertyGenerator(
v.GetName(), v.GetName(),
pm, pm,
toIdentifier(p), toIdentifier(p),
p.Notes, comment,
k, k,
p.NaturalLanguageMap) p.NaturalLanguageMap)
return return
@ -502,11 +519,19 @@ func (c Converter) convertNonFunctionalProperty(p rdf.VocabularyProperty,
if e != nil { if e != nil {
return 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( nfp = gen.NewNonFunctionalPropertyGenerator(
v.GetName(), v.GetName(),
pm, pm,
toIdentifier(p), toIdentifier(p),
p.Notes, comment,
k, k,
p.NaturalLanguageMap) p.NaturalLanguageMap)
return return
@ -953,3 +978,21 @@ func funcsToFile(pkg gen.Package, fns []*codegen.Function, filename string) *Fil
Directory: pkg.WriteDir(), 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)
}