2021-10-16 03:58:56 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2021-10-16 14:21:05 +00:00
|
|
|
"path/filepath"
|
|
|
|
"go/ast"
|
2021-10-16 03:58:56 +00:00
|
|
|
"go/doc"
|
|
|
|
"go/parser"
|
|
|
|
"go/token"
|
2021-10-16 14:21:05 +00:00
|
|
|
"strings"
|
|
|
|
"os"
|
2022-12-03 15:33:18 +00:00
|
|
|
"sync"
|
2021-10-16 03:58:56 +00:00
|
|
|
)
|
|
|
|
|
2022-12-02 19:08:03 +00:00
|
|
|
var header = `---
|
|
|
|
name: Module %s
|
|
|
|
description: %s
|
|
|
|
layout: apidoc
|
|
|
|
---
|
|
|
|
|
|
|
|
`
|
|
|
|
|
2022-12-02 20:00:55 +00:00
|
|
|
type emmyPiece struct {
|
2022-02-25 22:00:18 +00:00
|
|
|
FuncName string
|
|
|
|
Docs []string
|
|
|
|
Params []string // we only need to know param name to put in function
|
|
|
|
}
|
2022-12-02 19:08:03 +00:00
|
|
|
|
2022-12-02 20:00:55 +00:00
|
|
|
type module struct {
|
2022-12-03 15:30:41 +00:00
|
|
|
Docs []docPiece
|
2022-12-02 19:08:03 +00:00
|
|
|
ShortDescription string
|
|
|
|
Description string
|
2022-12-03 15:15:25 +00:00
|
|
|
Interface bool
|
2022-12-02 19:08:03 +00:00
|
|
|
}
|
2022-12-02 20:00:55 +00:00
|
|
|
type docPiece struct {
|
2022-02-25 22:00:18 +00:00
|
|
|
Doc []string
|
|
|
|
FuncSig string
|
|
|
|
FuncName string
|
|
|
|
}
|
|
|
|
|
2022-12-03 15:15:25 +00:00
|
|
|
var docs = make(map[string]module)
|
|
|
|
var emmyDocs = make(map[string][]emmyPiece)
|
|
|
|
var prefix = map[string]string{
|
|
|
|
"main": "hl",
|
|
|
|
"hilbish": "hl",
|
|
|
|
"fs": "f",
|
|
|
|
"commander": "c",
|
|
|
|
"bait": "b",
|
|
|
|
"terminal": "term",
|
|
|
|
}
|
|
|
|
|
|
|
|
func setupDoc(mod string, fun *doc.Func) *docPiece {
|
|
|
|
if !strings.HasPrefix(fun.Name, "hl") && mod == "main" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if !strings.HasPrefix(fun.Name, prefix[mod]) || fun.Name == "Loader" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
parts := strings.Split(strings.TrimSpace(fun.Doc), "\n")
|
|
|
|
funcsig := parts[0]
|
|
|
|
doc := parts[1:]
|
|
|
|
funcdoc := []string{}
|
|
|
|
em := emmyPiece{FuncName: strings.TrimPrefix(fun.Name, prefix[mod])}
|
|
|
|
for _, d := range doc {
|
|
|
|
if strings.HasPrefix(d, "---") {
|
|
|
|
emmyLine := strings.TrimSpace(strings.TrimPrefix(d, "---"))
|
|
|
|
emmyLinePieces := strings.Split(emmyLine, " ")
|
|
|
|
emmyType := emmyLinePieces[0]
|
|
|
|
if emmyType == "@param" {
|
|
|
|
em.Params = append(em.Params, emmyLinePieces[1])
|
|
|
|
}
|
|
|
|
if emmyType == "@vararg" {
|
|
|
|
em.Params = append(em.Params, "...") // add vararg
|
|
|
|
}
|
|
|
|
em.Docs = append(em.Docs, d)
|
|
|
|
} else {
|
|
|
|
funcdoc = append(funcdoc, d)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
dps := docPiece{
|
|
|
|
Doc: funcdoc,
|
|
|
|
FuncSig: funcsig,
|
|
|
|
FuncName: strings.TrimPrefix(fun.Name, prefix[mod]),
|
|
|
|
}
|
|
|
|
|
|
|
|
emmyDocs[mod] = append(emmyDocs[mod], em)
|
|
|
|
return &dps
|
|
|
|
}
|
|
|
|
|
2021-10-16 16:38:46 +00:00
|
|
|
// feel free to clean this up
|
|
|
|
// it works, dont really care about the code
|
2021-10-16 03:58:56 +00:00
|
|
|
func main() {
|
|
|
|
fset := token.NewFileSet()
|
2022-02-25 22:00:18 +00:00
|
|
|
os.Mkdir("docs", 0777)
|
2022-12-02 19:08:03 +00:00
|
|
|
os.Mkdir("docs/api", 0777)
|
2022-02-25 22:00:18 +00:00
|
|
|
os.Mkdir("emmyLuaDocs", 0777)
|
2021-10-16 03:58:56 +00:00
|
|
|
|
2021-10-16 14:21:05 +00:00
|
|
|
dirs := []string{"./"}
|
|
|
|
filepath.Walk("golibs/", func (path string, info os.FileInfo, err error) error {
|
|
|
|
if !info.IsDir() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
dirs = append(dirs, "./" + path)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
pkgs := make(map[string]*ast.Package)
|
|
|
|
for _, path := range dirs {
|
|
|
|
d, err := parser.ParseDir(fset, path, nil, parser.ParseComments)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for k, v := range d {
|
|
|
|
pkgs[k] = v
|
|
|
|
}
|
2021-10-16 03:58:56 +00:00
|
|
|
}
|
|
|
|
|
2021-10-16 14:21:05 +00:00
|
|
|
for l, f := range pkgs {
|
|
|
|
p := doc.New(f, "./", doc.AllDecls)
|
2022-12-03 15:30:41 +00:00
|
|
|
pieces := []docPiece{}
|
2022-12-02 20:57:38 +00:00
|
|
|
mod := l
|
2021-10-16 03:58:56 +00:00
|
|
|
for _, t := range p.Funcs {
|
2022-12-03 15:15:25 +00:00
|
|
|
piece := setupDoc(mod, t)
|
|
|
|
if piece != nil {
|
2022-12-03 15:30:41 +00:00
|
|
|
pieces = append(pieces, *piece)
|
2022-02-25 22:00:18 +00:00
|
|
|
}
|
2021-10-16 14:21:05 +00:00
|
|
|
}
|
|
|
|
for _, t := range p.Types {
|
|
|
|
for _, m := range t.Methods {
|
2022-12-03 15:15:25 +00:00
|
|
|
piece := setupDoc(mod, m)
|
|
|
|
if piece != nil {
|
2022-12-03 15:30:41 +00:00
|
|
|
pieces = append(pieces, *piece)
|
2022-02-25 22:00:18 +00:00
|
|
|
}
|
2021-10-16 14:21:05 +00:00
|
|
|
}
|
2021-10-16 03:58:56 +00:00
|
|
|
}
|
2022-12-02 19:08:03 +00:00
|
|
|
|
|
|
|
descParts := strings.Split(strings.TrimSpace(p.Doc), "\n")
|
|
|
|
shortDesc := descParts[0]
|
|
|
|
desc := descParts[1:]
|
2022-12-02 20:57:38 +00:00
|
|
|
docs[mod] = module{
|
2022-12-02 19:08:03 +00:00
|
|
|
Docs: pieces,
|
|
|
|
ShortDescription: shortDesc,
|
|
|
|
Description: strings.Join(desc, "\n"),
|
|
|
|
}
|
2021-10-16 03:58:56 +00:00
|
|
|
}
|
2021-10-16 16:38:46 +00:00
|
|
|
|
2022-12-03 15:33:18 +00:00
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(len(docs) * 2)
|
|
|
|
|
2021-10-16 16:38:46 +00:00
|
|
|
for mod, v := range docs {
|
2022-12-03 15:15:25 +00:00
|
|
|
modN := mod
|
|
|
|
if mod == "main" {
|
|
|
|
modN = "hilbish"
|
|
|
|
}
|
2022-12-03 15:33:18 +00:00
|
|
|
|
2022-12-03 20:52:52 +00:00
|
|
|
go func(modName string, modu module) {
|
2022-12-03 15:33:18 +00:00
|
|
|
defer wg.Done()
|
|
|
|
|
2022-12-03 20:52:52 +00:00
|
|
|
f, _ := os.Create("docs/api/" + modName + ".md")
|
|
|
|
f.WriteString(fmt.Sprintf(header, modName, modu.ShortDescription))
|
|
|
|
f.WriteString(fmt.Sprintf("## Introduction\n%s\n\n## Functions\n", modu.Description))
|
|
|
|
for _, dps := range modu.Docs {
|
2022-12-03 15:33:18 +00:00
|
|
|
f.WriteString(fmt.Sprintf("### %s\n", dps.FuncSig))
|
|
|
|
for _, doc := range dps.Doc {
|
|
|
|
if !strings.HasPrefix(doc, "---") {
|
|
|
|
f.WriteString(doc + "\n")
|
|
|
|
}
|
2022-02-25 22:00:18 +00:00
|
|
|
}
|
2022-12-03 15:33:18 +00:00
|
|
|
f.WriteString("\n")
|
2022-02-25 22:00:18 +00:00
|
|
|
}
|
2022-12-03 20:52:52 +00:00
|
|
|
}(modN, v)
|
2022-12-03 15:15:25 +00:00
|
|
|
|
2022-12-03 15:33:18 +00:00
|
|
|
go func(md, modName string) {
|
|
|
|
defer wg.Done()
|
|
|
|
|
2022-12-03 20:52:52 +00:00
|
|
|
ff, _ := os.Create("emmyLuaDocs/" + modName + ".lua")
|
|
|
|
ff.WriteString("--- @meta\n\nlocal " + modName + " = {}\n\n")
|
2022-12-03 15:33:18 +00:00
|
|
|
for _, em := range emmyDocs[md] {
|
|
|
|
funcdocs := []string{}
|
2022-12-03 20:52:52 +00:00
|
|
|
for _, dps := range docs[md].Docs {
|
2022-12-03 15:33:18 +00:00
|
|
|
if dps.FuncName == em.FuncName {
|
|
|
|
funcdocs = dps.Doc
|
|
|
|
}
|
2022-12-03 15:30:41 +00:00
|
|
|
}
|
2022-12-03 15:33:18 +00:00
|
|
|
ff.WriteString("--- " + strings.Join(funcdocs, "\n--- ") + "\n")
|
|
|
|
if len(em.Docs) != 0 {
|
|
|
|
ff.WriteString(strings.Join(em.Docs, "\n") + "\n")
|
|
|
|
}
|
2022-12-03 20:52:52 +00:00
|
|
|
ff.WriteString("function " + modName + "." + em.FuncName + "(" + strings.Join(em.Params, ", ") + ") end\n\n")
|
2022-12-03 15:30:41 +00:00
|
|
|
}
|
2022-12-03 20:52:52 +00:00
|
|
|
ff.WriteString("return " + modName + "\n")
|
2022-12-03 15:33:18 +00:00
|
|
|
}(mod, modN)
|
2021-10-16 16:38:46 +00:00
|
|
|
}
|
2022-12-03 15:33:18 +00:00
|
|
|
wg.Wait()
|
2021-10-16 03:58:56 +00:00
|
|
|
}
|