docs: support field docs for modules

docs-refactor
sammyette 2022-12-14 23:54:10 -04:00
parent a7767d9853
commit 0f97abef0f
Signed by: sammyette
GPG Key ID: 904FC49417B44DCD
3 changed files with 49 additions and 24 deletions

8
api.go
View File

@ -1,6 +1,14 @@
// the core Hilbish API // the core Hilbish API
// The Hilbish module includes the core API, containing // The Hilbish module includes the core API, containing
// interfaces and functions which directly relate to shell functionality. // interfaces and functions which directly relate to shell functionality.
// #field ver The version of Hilbish
// #field user Username of the user
// #field host Hostname of the machine
// #field dataDir Directory for Hilbish data files, including the docs and default modules
// #field interactive Is Hilbish in an interactive shell?
// #field login Is Hilbish the login shell?
// #field vimMode Current Vim input mode of Hilbish (will be nil if not in Vim input mode)
// #field exitCode xit code of the last executed command
package main package main
import ( import (

View File

@ -70,16 +70,11 @@ var prefix = map[string]string{
"terminal": "term", "terminal": "term",
} }
func setupDoc(mod string, fun *doc.Func) *docPiece { func getTagsAndDocs(docs string) (map[string][]tag, []string) {
docs := strings.TrimSpace(fun.Doc)
inInterface := strings.HasPrefix(docs, "#interface")
if (!strings.HasPrefix(fun.Name, prefix[mod]) && !inInterface) || (strings.ToLower(fun.Name) == "loader" && !inInterface) {
return nil
}
pts := strings.Split(docs, "\n") pts := strings.Split(docs, "\n")
parts := []string{} parts := []string{}
tags := make(map[string][]tag) tags := make(map[string][]tag)
for _, part := range pts { for _, part := range pts {
if strings.HasPrefix(part, "#") { if strings.HasPrefix(part, "#") {
tagParts := strings.Split(strings.TrimPrefix(part, "#"), " ") tagParts := strings.Split(strings.TrimPrefix(part, "#"), " ")
@ -109,6 +104,30 @@ func setupDoc(mod string, fun *doc.Func) *docPiece {
} }
} }
return tags, parts
}
func docPieceTag(tagName string, tags map[string][]tag) []docPiece {
dps := []docPiece{}
for _, tag := range tags[tagName] {
dps = append(dps, docPiece{
FuncName: tag.id,
Doc: tag.fields,
})
}
return dps
}
func setupDoc(mod string, fun *doc.Func) *docPiece {
docs := strings.TrimSpace(fun.Doc)
inInterface := strings.HasPrefix(docs, "#interface")
if (!strings.HasPrefix(fun.Name, prefix[mod]) && !inInterface) || (strings.ToLower(fun.Name) == "loader" && !inInterface) {
return nil
}
tags, parts := getTagsAndDocs(docs)
var interfaces string var interfaces string
funcsig := parts[0] funcsig := parts[0]
doc := parts[1:] doc := parts[1:]
@ -121,22 +140,8 @@ func setupDoc(mod string, fun *doc.Func) *docPiece {
} }
em := emmyPiece{FuncName: funcName} em := emmyPiece{FuncName: funcName}
// manage fields fields := docPieceTag("field", tags)
fields := []docPiece{} properties := docPieceTag("property", tags)
for _, tag := range tags["field"] {
fields = append(fields, docPiece{
FuncName: tag.id,
Doc: tag.fields,
})
}
properties := []docPiece{}
for _, tag := range tags["property"] {
properties = append(properties, docPiece{
FuncName: tag.id,
Doc: tag.fields,
})
}
for _, d := range doc { for _, d := range doc {
if strings.HasPrefix(d, "---") { if strings.HasPrefix(d, "---") {
@ -245,7 +250,7 @@ func main() {
} }
} }
descParts := strings.Split(strings.TrimSpace(p.Doc), "\n") tags, descParts := getTagsAndDocs(strings.TrimSpace(p.Doc))
shortDesc := descParts[0] shortDesc := descParts[0]
desc := descParts[1:] desc := descParts[1:]
filteredPieces := []docPiece{} filteredPieces := []docPiece{}
@ -279,6 +284,8 @@ func main() {
ShortDescription: shortDesc, ShortDescription: shortDesc,
Description: strings.Join(desc, "\n"), Description: strings.Join(desc, "\n"),
HasInterfaces: hasInterfaces, HasInterfaces: hasInterfaces,
Properties: docPieceTag("property", tags),
Fields: docPieceTag("field", tags),
} }
} }

View File

@ -11,6 +11,16 @@ menu:
The Hilbish module includes the core API, containing The Hilbish module includes the core API, containing
interfaces and functions which directly relate to shell functionality. interfaces and functions which directly relate to shell functionality.
## Interface fields
- `ver`: The version of Hilbish
- `user`: Username of the user
- `host`: Hostname of the machine
- `dataDir`: Directory for Hilbish data files, including the docs and default modules
- `interactive`: Is Hilbish in an interactive shell?
- `login`: Is Hilbish the login shell?
- `vimMode`: Current Vim input mode of Hilbish (will be nil if not in Vim input mode)
- `exitCode`: xit code of the last executed command
## Functions ## Functions
### alias(cmd, orig) ### alias(cmd, orig)
Sets an alias of `cmd` to `orig` Sets an alias of `cmd` to `orig`