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"
|
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 {
|
|
|
|
Docs []docPiece
|
2022-12-02 19:08:03 +00:00
|
|
|
ShortDescription string
|
|
|
|
Description string
|
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
|
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
|
|
|
prefix := map[string]string{
|
2021-10-16 16:38:46 +00:00
|
|
|
"hilbish": "hl",
|
2021-10-16 14:21:05 +00:00
|
|
|
"fs": "f",
|
|
|
|
"commander": "c",
|
|
|
|
"bait": "b",
|
2021-10-17 20:10:57 +00:00
|
|
|
"terminal": "term",
|
2021-10-16 14:21:05 +00:00
|
|
|
}
|
2022-12-02 20:00:55 +00:00
|
|
|
docs := make(map[string]module)
|
|
|
|
emmyDocs := make(map[string][]emmyPiece)
|
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-02 20:00:55 +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 {
|
2021-10-16 16:38:46 +00:00
|
|
|
if strings.HasPrefix(t.Name, "hl") { mod = "hilbish" }
|
2022-12-02 20:57:38 +00:00
|
|
|
if !strings.HasPrefix(t.Name, "hl") && l == "main" { continue }
|
2021-10-16 16:38:46 +00:00
|
|
|
if !strings.HasPrefix(t.Name, prefix[mod]) || t.Name == "Loader" { continue }
|
2022-02-25 00:42:54 +00:00
|
|
|
parts := strings.Split(strings.TrimSpace(t.Doc), "\n")
|
2021-10-16 14:21:05 +00:00
|
|
|
funcsig := parts[0]
|
2021-10-16 16:38:46 +00:00
|
|
|
doc := parts[1:]
|
2022-02-25 22:00:18 +00:00
|
|
|
funcdoc := []string{}
|
2022-12-02 20:00:55 +00:00
|
|
|
em := emmyPiece{FuncName: strings.TrimPrefix(t.Name, prefix[mod])}
|
2022-02-25 22:00:18 +00:00
|
|
|
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])
|
|
|
|
}
|
2022-04-05 01:34:46 +00:00
|
|
|
if emmyType == "@vararg" {
|
|
|
|
em.Params = append(em.Params, "...") // add vararg
|
|
|
|
}
|
2022-02-25 22:00:18 +00:00
|
|
|
em.Docs = append(em.Docs, d)
|
|
|
|
} else {
|
|
|
|
funcdoc = append(funcdoc, d)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-02 20:00:55 +00:00
|
|
|
dps := docPiece{
|
2022-02-25 22:00:18 +00:00
|
|
|
Doc: funcdoc,
|
|
|
|
FuncSig: funcsig,
|
|
|
|
FuncName: strings.TrimPrefix(t.Name, prefix[mod]),
|
|
|
|
}
|
|
|
|
|
2022-12-02 19:08:03 +00:00
|
|
|
pieces = append(pieces, dps)
|
2022-02-25 22:00:18 +00:00
|
|
|
emmyDocs[mod] = append(emmyDocs[mod], em)
|
2021-10-16 14:21:05 +00:00
|
|
|
}
|
|
|
|
for _, t := range p.Types {
|
|
|
|
for _, m := range t.Methods {
|
|
|
|
if !strings.HasPrefix(m.Name, prefix[l]) || m.Name == "Loader" { continue }
|
2022-02-25 00:42:54 +00:00
|
|
|
parts := strings.Split(strings.TrimSpace(m.Doc), "\n")
|
2021-10-16 14:21:05 +00:00
|
|
|
funcsig := parts[0]
|
2021-10-16 16:38:46 +00:00
|
|
|
doc := parts[1:]
|
2022-02-25 22:00:18 +00:00
|
|
|
funcdoc := []string{}
|
2022-12-02 20:00:55 +00:00
|
|
|
em := emmyPiece{FuncName: strings.TrimPrefix(m.Name, prefix[l])}
|
2022-02-25 22:00:18 +00:00
|
|
|
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])
|
|
|
|
}
|
2022-04-05 01:34:46 +00:00
|
|
|
if emmyType == "@vararg" {
|
|
|
|
em.Params = append(em.Params, "...") // add vararg
|
|
|
|
}
|
2022-02-25 22:00:18 +00:00
|
|
|
em.Docs = append(em.Docs, d)
|
|
|
|
} else {
|
|
|
|
funcdoc = append(funcdoc, d)
|
|
|
|
}
|
|
|
|
}
|
2022-12-02 20:00:55 +00:00
|
|
|
dps := docPiece{
|
2022-02-25 22:00:18 +00:00
|
|
|
Doc: funcdoc,
|
|
|
|
FuncSig: funcsig,
|
|
|
|
FuncName: strings.TrimPrefix(m.Name, prefix[l]),
|
|
|
|
}
|
2021-10-16 14:21:05 +00:00
|
|
|
|
2022-12-02 19:08:03 +00:00
|
|
|
pieces = append(pieces, dps)
|
2022-02-25 22:00:18 +00:00
|
|
|
emmyDocs[l] = append(emmyDocs[l], em)
|
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
|
|
|
|
|
|
|
for mod, v := range docs {
|
2022-02-25 22:00:18 +00:00
|
|
|
if mod == "main" { continue }
|
2022-12-02 19:08:03 +00:00
|
|
|
f, _ := os.Create("docs/api/" + mod + ".md")
|
|
|
|
f.WriteString(fmt.Sprintf(header, mod, v.ShortDescription))
|
|
|
|
f.WriteString(fmt.Sprintf("## Introduction\n%s\n\n## Functions\n", v.Description))
|
|
|
|
for _, dps := range v.Docs {
|
|
|
|
f.WriteString(fmt.Sprintf("### %s\n", dps.FuncSig))
|
2022-02-25 22:00:18 +00:00
|
|
|
for _, doc := range dps.Doc {
|
|
|
|
if !strings.HasPrefix(doc, "---") {
|
|
|
|
f.WriteString(doc + "\n")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
f.WriteString("\n")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for mod, v := range emmyDocs {
|
|
|
|
if mod == "main" { continue }
|
|
|
|
f, _ := os.Create("emmyLuaDocs/" + mod + ".lua")
|
|
|
|
f.WriteString("--- @meta\n\nlocal " + mod + " = {}\n\n")
|
|
|
|
for _, em := range v {
|
|
|
|
var funcdocs []string
|
2022-12-02 19:08:03 +00:00
|
|
|
for _, dps := range docs[mod].Docs {
|
2022-02-25 22:00:18 +00:00
|
|
|
if dps.FuncName == em.FuncName {
|
|
|
|
funcdocs = dps.Doc
|
|
|
|
}
|
|
|
|
}
|
|
|
|
f.WriteString("--- " + strings.Join(funcdocs, "\n--- ") + "\n")
|
|
|
|
if len(em.Docs) != 0 {
|
|
|
|
f.WriteString(strings.Join(em.Docs, "\n") + "\n")
|
|
|
|
}
|
|
|
|
f.WriteString("function " + mod + "." + em.FuncName + "(" + strings.Join(em.Params, ", ") + ") end\n\n")
|
|
|
|
}
|
|
|
|
f.WriteString("return " + mod + "\n")
|
2021-10-16 16:38:46 +00:00
|
|
|
}
|
2021-10-16 03:58:56 +00:00
|
|
|
}
|