website(docs): add example

pull/260/head
sammyette 2023-11-30 18:30:54 -04:00
parent e8ea79b5ec
commit 2363e6ff2f
Signed by: sammyette
GPG Key ID: 904FC49417B44DCD
5 changed files with 46 additions and 23 deletions

View File

@ -64,11 +64,13 @@ type docPiece struct {
Fields []docPiece Fields []docPiece
Properties []docPiece Properties []docPiece
Params []param Params []param
Tags map[string][]tag
} }
type tag struct { type tag struct {
id string id string
fields []string fields []string
startIdx int
} }
var docs = make(map[string]module) var docs = make(map[string]module)
@ -89,7 +91,7 @@ func getTagsAndDocs(docs string) (map[string][]tag, []string) {
parts := []string{} parts := []string{}
tags := make(map[string][]tag) tags := make(map[string][]tag)
for _, part := range pts { for idx, part := range pts {
if strings.HasPrefix(part, "#") { if strings.HasPrefix(part, "#") {
tagParts := strings.Split(strings.TrimPrefix(part, "#"), " ") tagParts := strings.Split(strings.TrimPrefix(part, "#"), " ")
if tags[tagParts[0]] == nil { if tags[tagParts[0]] == nil {
@ -98,12 +100,21 @@ func getTagsAndDocs(docs string) (map[string][]tag, []string) {
id = tagParts[1] id = tagParts[1]
} }
tags[tagParts[0]] = []tag{ tags[tagParts[0]] = []tag{
{id: id}, {id: id, startIdx: idx},
} }
if len(tagParts) >= 2 { if len(tagParts) >= 2 {
tags[tagParts[0]][0].fields = tagParts[2:] tags[tagParts[0]][0].fields = tagParts[2:]
} }
} else { } 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{} fleds := []string{}
if len(tagParts) >= 2 { if len(tagParts) >= 2 {
fleds = tagParts[2:] fleds = tagParts[2:]
@ -188,6 +199,7 @@ func setupDocType(mod string, typ *doc.Type) *docPiece {
ParentModule: parentMod, ParentModule: parentMod,
Fields: fields, Fields: fields,
Properties: properties, Properties: properties,
Tags: tags,
} }
typeTable[strings.ToLower(typeName)] = []string{parentMod, interfaces} typeTable[strings.ToLower(typeName)] = []string{parentMod, interfaces}
@ -273,6 +285,7 @@ start:
Fields: fields, Fields: fields,
Properties: properties, Properties: properties,
Params: params, Params: params,
Tags: tags,
} }
if strings.HasSuffix(dps.GoFuncName, strings.ToLower("loader")) { if strings.HasSuffix(dps.GoFuncName, strings.ToLower("loader")) {
dps.Doc = parts dps.Doc = parts
@ -565,6 +578,10 @@ func main() {
f.WriteString(strings.Join(p.Doc, " ")) f.WriteString(strings.Join(p.Doc, " "))
f.WriteString("\n\n") 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("</div>") f.WriteString("</div>")
f.WriteString("\n\n") f.WriteString("\n\n")
} }

View File

@ -35,7 +35,7 @@ this function will set the user prompt.
## Functions ## Functions
||| |||
|----|----| |----|----|
|<a href="#catch">catch(name, cb)</a>|Catches a hook with `name`. Runs the `cb` when it is thrown| |<a href="#catch">catch(name, cb)</a>|Catches a hook. This function is used to act on hooks/events.|
|<a href="#catchOnce">catchOnce(name, cb)</a>|Same as catch, but only runs the `cb` once and then removes the hook| |<a href="#catchOnce">catchOnce(name, cb)</a>|Same as catch, but only runs the `cb` once and then removes the hook|
|<a href="#hooks">hooks(name) -> table</a>|Returns a table with hooks (callback functions) on the event with `name`.| |<a href="#hooks">hooks(name) -> table</a>|Returns a table with hooks (callback functions) on the event with `name`.|
|<a href="#release">release(name, catcher)</a>|Removes the `catcher` for the event with `name`.| |<a href="#release">release(name, catcher)</a>|Removes the `catcher` for the event with `name`.|
@ -49,14 +49,22 @@ bait.catch(name, cb)
</a> </a>
</h4> </h4>
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 #### Parameters
`string` **`name`** `string` **`name`**
ummm The name of the hook.
`function` **`cb`** `function` **`cb`**
? The function that will be called when the hook is thrown.
#### Example
```lua
bait.catch('hilbish.exit', function()
print 'Goodbye Hilbish!'
end)
````
</div> </div>
<hr><div id='catchOnce'> <hr><div id='catchOnce'>

View File

@ -7,16 +7,5 @@ menu:
docs docs
--- ---
Here is a list of bait hooks that are thrown by Hilbish. If a hook is related Hooks are Hilbish's versions of events, which are used via the [Bait](../api/bait) module.
to a command, it will have the `command` scope, as example. For more detail on how to act on these hooks, you may check the Bait page.
Here is the format for a doc for a hook:
+ {hook name} -> args > description
`{args}` just means the arguments of the hook. If a hook doc has the format
of `arg...`, it means the hook can take/recieve any number of `arg`.
+ error -> eventName, handler, err > Emitted when there is an error in
an event handler. The `eventName` is the name of the event the handler
is for, the `handler` is the callback function, and `err` is the error
message.

View File

@ -2,7 +2,9 @@
local bait = {} local bait = {}
--- Catches a hook with `name`. Runs the `cb` when it is thrown --- Catches a hook. This function is used to act on hooks/events.
---
---
function bait.catch(name, cb) end function bait.catch(name, cb) end
--- Same as catch, but only runs the `cb` once and then removes the hook --- Same as catch, but only runs the `cb` once and then removes the hook

View File

@ -271,9 +271,16 @@ func (b *Bait) bthrow(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
} }
// catch(name, cb) // 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.
// #param name string ummm // #param name string The name of the hook.
// #param cb function ? // #param cb function The function that will be called when the hook is thrown.
/*
#example
bait.catch('hilbish.exit', function()
print 'Goodbye Hilbish!'
end)
#example
*/
func (b *Bait) bcatch(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) { func (b *Bait) bcatch(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
name, catcher, err := util.HandleStrCallback(t, c) name, catcher, err := util.HandleStrCallback(t, c)
if err != nil { if err != nil {