diff --git a/cmd/docgen/docgen.go b/cmd/docgen/docgen.go index 5b452d7..6e295be 100644 --- a/cmd/docgen/docgen.go +++ b/cmd/docgen/docgen.go @@ -64,11 +64,13 @@ type docPiece struct { Fields []docPiece Properties []docPiece Params []param + Tags map[string][]tag } type tag struct { id string fields []string + startIdx int } var docs = make(map[string]module) @@ -89,7 +91,7 @@ func getTagsAndDocs(docs string) (map[string][]tag, []string) { parts := []string{} tags := make(map[string][]tag) - for _, part := range pts { + for idx, part := range pts { if strings.HasPrefix(part, "#") { tagParts := strings.Split(strings.TrimPrefix(part, "#"), " ") if tags[tagParts[0]] == nil { @@ -98,12 +100,21 @@ func getTagsAndDocs(docs string) (map[string][]tag, []string) { id = tagParts[1] } tags[tagParts[0]] = []tag{ - {id: id}, + {id: id, startIdx: idx}, } if len(tagParts) >= 2 { tags[tagParts[0]][0].fields = tagParts[2:] } } else { + if tagParts[0] == "example" { + exampleIdx := tags["example"][0].startIdx + exampleCode := pts[exampleIdx+1:idx] + + tags["example"][0].fields = exampleCode + parts = strings.Split(strings.Replace(strings.Join(parts, "\n"), strings.TrimPrefix(strings.Join(exampleCode, "\n"), "#example\n"), "", -1), "\n") + continue + } + fleds := []string{} if len(tagParts) >= 2 { fleds = tagParts[2:] @@ -188,6 +199,7 @@ func setupDocType(mod string, typ *doc.Type) *docPiece { ParentModule: parentMod, Fields: fields, Properties: properties, + Tags: tags, } typeTable[strings.ToLower(typeName)] = []string{parentMod, interfaces} @@ -273,6 +285,7 @@ start: Fields: fields, Properties: properties, Params: params, + Tags: tags, } if strings.HasSuffix(dps.GoFuncName, strings.ToLower("loader")) { dps.Doc = parts @@ -565,6 +578,10 @@ func main() { f.WriteString(strings.Join(p.Doc, " ")) f.WriteString("\n\n") } + if codeExample := dps.Tags["example"]; codeExample != nil { + f.WriteString("#### Example\n") + f.WriteString(fmt.Sprintf("```lua\n%s\n````\n", strings.Join(codeExample[0].fields, "\n"))) + } f.WriteString("") f.WriteString("\n\n") } diff --git a/docs/api/bait.md b/docs/api/bait.md index 91cfb31..cc0eb1a 100644 --- a/docs/api/bait.md +++ b/docs/api/bait.md @@ -35,7 +35,7 @@ this function will set the user prompt. ## Functions ||| |----|----| -|catch(name, cb)|Catches a hook with `name`. Runs the `cb` when it is thrown| +|catch(name, cb)|Catches a hook. This function is used to act on hooks/events.| |catchOnce(name, cb)|Same as catch, but only runs the `cb` once and then removes the hook| |hooks(name) -> table|Returns a table with hooks (callback functions) on the event with `name`.| |release(name, catcher)|Removes the `catcher` for the event with `name`.| @@ -49,14 +49,22 @@ bait.catch(name, cb) -Catches a hook with `name`. Runs the `cb` when it is thrown +Catches a hook. This function is used to act on hooks/events. + + #### Parameters `string` **`name`** -ummm +The name of the hook. `function` **`cb`** -? +The function that will be called when the hook is thrown. +#### Example +```lua +bait.catch('hilbish.exit', function() + print 'Goodbye Hilbish!' +end) +````