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 = `---
|
2022-12-15 01:05:11 +00:00
|
|
|
title: %s %s
|
2022-12-02 19:08:03 +00:00
|
|
|
description: %s
|
2022-12-15 01:05:11 +00:00
|
|
|
layout: doc
|
|
|
|
menu:
|
|
|
|
docs:
|
|
|
|
parent: "API"
|
2022-12-02 19:08:03 +00:00
|
|
|
---
|
|
|
|
|
|
|
|
`
|
|
|
|
|
2022-12-02 20:00:55 +00:00
|
|
|
type emmyPiece struct {
|
2022-12-04 02:50:15 +00:00
|
|
|
DocPiece *docPiece
|
|
|
|
Annotations []string
|
2022-02-25 22:00:18 +00:00
|
|
|
Params []string // we only need to know param name to put in function
|
2022-12-04 02:50:15 +00:00
|
|
|
FuncName string
|
2022-02-25 22:00:18 +00:00
|
|
|
}
|
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-14 00:12:16 +00:00
|
|
|
Fields []docPiece
|
2022-12-05 22:57:59 +00:00
|
|
|
Properties []docPiece
|
2022-12-02 19:08:03 +00:00
|
|
|
ShortDescription string
|
|
|
|
Description string
|
2022-12-04 02:50:15 +00:00
|
|
|
ParentModule string
|
|
|
|
HasInterfaces bool
|
2022-12-02 19:08:03 +00:00
|
|
|
}
|
2022-12-04 02:50:15 +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-04 02:50:15 +00:00
|
|
|
Interfacing string
|
|
|
|
ParentModule string
|
|
|
|
GoFuncName string
|
|
|
|
IsInterface bool
|
|
|
|
IsMember bool
|
2022-12-14 00:12:16 +00:00
|
|
|
Fields []docPiece
|
2022-12-05 22:57:59 +00:00
|
|
|
Properties []docPiece
|
|
|
|
}
|
|
|
|
|
|
|
|
type tag struct {
|
|
|
|
id string
|
|
|
|
fields []string
|
2022-02-25 22:00:18 +00:00
|
|
|
}
|
|
|
|
|
2022-12-03 15:15:25 +00:00
|
|
|
var docs = make(map[string]module)
|
2022-12-04 02:50:15 +00:00
|
|
|
var interfaceDocs = make(map[string]module)
|
2022-12-03 15:15:25 +00:00
|
|
|
var emmyDocs = make(map[string][]emmyPiece)
|
|
|
|
var prefix = map[string]string{
|
|
|
|
"main": "hl",
|
|
|
|
"hilbish": "hl",
|
|
|
|
"fs": "f",
|
|
|
|
"commander": "c",
|
|
|
|
"bait": "b",
|
|
|
|
"terminal": "term",
|
|
|
|
}
|
|
|
|
|
2022-12-15 03:54:10 +00:00
|
|
|
func getTagsAndDocs(docs string) (map[string][]tag, []string) {
|
2022-12-04 02:50:15 +00:00
|
|
|
pts := strings.Split(docs, "\n")
|
|
|
|
parts := []string{}
|
2022-12-05 22:57:59 +00:00
|
|
|
tags := make(map[string][]tag)
|
2022-12-15 03:54:10 +00:00
|
|
|
|
2022-12-04 02:50:15 +00:00
|
|
|
for _, part := range pts {
|
|
|
|
if strings.HasPrefix(part, "#") {
|
|
|
|
tagParts := strings.Split(strings.TrimPrefix(part, "#"), " ")
|
2022-12-05 22:57:59 +00:00
|
|
|
if tags[tagParts[0]] == nil {
|
|
|
|
var id string
|
|
|
|
if len(tagParts) > 1 {
|
|
|
|
id = tagParts[1]
|
|
|
|
}
|
|
|
|
tags[tagParts[0]] = []tag{
|
|
|
|
{id: id},
|
|
|
|
}
|
|
|
|
if len(tagParts) >= 2 {
|
|
|
|
tags[tagParts[0]][0].fields = tagParts[2:]
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
fleds := []string{}
|
|
|
|
if len(tagParts) >= 2 {
|
|
|
|
fleds = tagParts[2:]
|
|
|
|
}
|
|
|
|
tags[tagParts[0]] = append(tags[tagParts[0]], tag{
|
|
|
|
id: tagParts[1],
|
|
|
|
fields: fleds,
|
|
|
|
})
|
|
|
|
}
|
2022-12-04 02:50:15 +00:00
|
|
|
} else {
|
|
|
|
parts = append(parts, part)
|
|
|
|
}
|
2022-12-03 15:15:25 +00:00
|
|
|
}
|
2022-12-04 02:50:15 +00:00
|
|
|
|
2022-12-15 03:54:10 +00:00
|
|
|
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)
|
|
|
|
|
2022-12-04 02:50:15 +00:00
|
|
|
var interfaces string
|
2022-12-03 15:15:25 +00:00
|
|
|
funcsig := parts[0]
|
|
|
|
doc := parts[1:]
|
2022-12-04 02:50:15 +00:00
|
|
|
funcName := strings.TrimPrefix(fun.Name, prefix[mod])
|
2022-12-03 15:15:25 +00:00
|
|
|
funcdoc := []string{}
|
2022-12-04 02:50:15 +00:00
|
|
|
|
|
|
|
if inInterface {
|
2022-12-05 22:57:59 +00:00
|
|
|
interfaces = tags["interface"][0].id
|
2022-12-04 02:50:15 +00:00
|
|
|
funcName = interfaces + "." + strings.Split(funcsig, "(")[0]
|
|
|
|
}
|
|
|
|
em := emmyPiece{FuncName: funcName}
|
|
|
|
|
2022-12-15 03:54:10 +00:00
|
|
|
fields := docPieceTag("field", tags)
|
|
|
|
properties := docPieceTag("property", tags)
|
2022-12-05 22:57:59 +00:00
|
|
|
|
2022-12-03 15:15:25 +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])
|
|
|
|
}
|
|
|
|
if emmyType == "@vararg" {
|
|
|
|
em.Params = append(em.Params, "...") // add vararg
|
|
|
|
}
|
2022-12-04 02:50:15 +00:00
|
|
|
em.Annotations = append(em.Annotations, d)
|
2022-12-03 15:15:25 +00:00
|
|
|
} else {
|
|
|
|
funcdoc = append(funcdoc, d)
|
|
|
|
}
|
|
|
|
}
|
2022-12-04 02:50:15 +00:00
|
|
|
|
|
|
|
var isMember bool
|
|
|
|
if tags["member"] != nil {
|
|
|
|
isMember = true
|
|
|
|
}
|
|
|
|
var parentMod string
|
|
|
|
if inInterface {
|
|
|
|
parentMod = mod
|
|
|
|
}
|
|
|
|
dps := &docPiece{
|
2022-12-03 15:15:25 +00:00
|
|
|
Doc: funcdoc,
|
|
|
|
FuncSig: funcsig,
|
2022-12-04 02:50:15 +00:00
|
|
|
FuncName: funcName,
|
|
|
|
Interfacing: interfaces,
|
|
|
|
GoFuncName: strings.ToLower(fun.Name),
|
|
|
|
IsInterface: inInterface,
|
|
|
|
IsMember: isMember,
|
|
|
|
ParentModule: parentMod,
|
2022-12-14 00:12:16 +00:00
|
|
|
Fields: fields,
|
2022-12-05 22:57:59 +00:00
|
|
|
Properties: properties,
|
2022-12-04 02:50:15 +00:00
|
|
|
}
|
|
|
|
if strings.HasSuffix(dps.GoFuncName, strings.ToLower("loader")) {
|
|
|
|
dps.Doc = parts
|
2022-12-03 15:15:25 +00:00
|
|
|
}
|
2022-12-04 02:50:15 +00:00
|
|
|
em.DocPiece = dps
|
2022-12-03 15:15:25 +00:00
|
|
|
|
|
|
|
emmyDocs[mod] = append(emmyDocs[mod], em)
|
2022-12-04 02:50:15 +00:00
|
|
|
return dps
|
2022-12-03 15:15:25 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-12-04 02:50:15 +00:00
|
|
|
interfaceModules := make(map[string]*module)
|
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
|
2022-12-04 02:50:15 +00:00
|
|
|
if mod == "main" {
|
|
|
|
mod = "hilbish"
|
|
|
|
}
|
|
|
|
var hasInterfaces bool
|
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)
|
2022-12-04 02:50:15 +00:00
|
|
|
if piece == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
pieces = append(pieces, *piece)
|
|
|
|
if piece.IsInterface {
|
|
|
|
hasInterfaces = true
|
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)
|
2022-12-04 02:50:15 +00:00
|
|
|
if piece == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
pieces = append(pieces, *piece)
|
|
|
|
if piece.IsInterface {
|
|
|
|
hasInterfaces = true
|
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
|
|
|
|
2022-12-15 03:54:10 +00:00
|
|
|
tags, descParts := getTagsAndDocs(strings.TrimSpace(p.Doc))
|
2022-12-02 19:08:03 +00:00
|
|
|
shortDesc := descParts[0]
|
|
|
|
desc := descParts[1:]
|
2022-12-04 02:50:15 +00:00
|
|
|
filteredPieces := []docPiece{}
|
|
|
|
for _, piece := range pieces {
|
|
|
|
if !piece.IsInterface {
|
|
|
|
filteredPieces = append(filteredPieces, piece)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
modname := piece.ParentModule + "." + piece.Interfacing
|
|
|
|
if interfaceModules[modname] == nil {
|
|
|
|
interfaceModules[modname] = &module{
|
|
|
|
ParentModule: piece.ParentModule,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.HasSuffix(piece.GoFuncName, strings.ToLower("loader")) {
|
|
|
|
shortDesc := piece.Doc[0]
|
|
|
|
desc := piece.Doc[1:]
|
|
|
|
interfaceModules[modname].ShortDescription = shortDesc
|
|
|
|
interfaceModules[modname].Description = strings.Join(desc, "\n")
|
2022-12-14 00:12:16 +00:00
|
|
|
interfaceModules[modname].Fields = piece.Fields
|
2022-12-05 22:57:59 +00:00
|
|
|
interfaceModules[modname].Properties = piece.Properties
|
2022-12-04 02:50:15 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
interfaceModules[modname].Docs = append(interfaceModules[modname].Docs, piece)
|
|
|
|
}
|
|
|
|
|
2022-12-02 20:57:38 +00:00
|
|
|
docs[mod] = module{
|
2022-12-04 02:50:15 +00:00
|
|
|
Docs: filteredPieces,
|
2022-12-02 19:08:03 +00:00
|
|
|
ShortDescription: shortDesc,
|
|
|
|
Description: strings.Join(desc, "\n"),
|
2022-12-04 02:50:15 +00:00
|
|
|
HasInterfaces: hasInterfaces,
|
2022-12-15 03:54:10 +00:00
|
|
|
Properties: docPieceTag("property", tags),
|
|
|
|
Fields: docPieceTag("field", tags),
|
2022-12-02 19:08:03 +00:00
|
|
|
}
|
2021-10-16 03:58:56 +00:00
|
|
|
}
|
2021-10-16 16:38:46 +00:00
|
|
|
|
2022-12-04 02:50:15 +00:00
|
|
|
for key, mod := range interfaceModules {
|
|
|
|
docs[key] = *mod
|
|
|
|
}
|
|
|
|
|
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-04 02:50:15 +00:00
|
|
|
docPath := "docs/api/" + mod + ".md"
|
|
|
|
if v.HasInterfaces {
|
|
|
|
os.Mkdir("docs/api/" + mod, 0777)
|
|
|
|
os.Remove(docPath) // remove old doc path if it exists
|
|
|
|
docPath = "docs/api/" + mod + "/_index.md"
|
|
|
|
}
|
|
|
|
if v.ParentModule != "" {
|
|
|
|
docPath = "docs/api/" + v.ParentModule + "/" + mod + ".md"
|
2022-12-03 15:15:25 +00:00
|
|
|
}
|
2022-12-03 15:33:18 +00:00
|
|
|
|
2022-12-04 02:50:15 +00:00
|
|
|
go func(modname, docPath string, modu module) {
|
2022-12-03 15:33:18 +00:00
|
|
|
defer wg.Done()
|
2022-12-04 02:50:15 +00:00
|
|
|
modOrIface := "Module"
|
|
|
|
if modu.ParentModule != "" {
|
|
|
|
modOrIface = "Interface"
|
|
|
|
}
|
2022-12-03 15:33:18 +00:00
|
|
|
|
2022-12-04 02:50:15 +00:00
|
|
|
f, _ := os.Create(docPath)
|
|
|
|
f.WriteString(fmt.Sprintf(header, modOrIface, modname, modu.ShortDescription))
|
2022-12-05 22:57:59 +00:00
|
|
|
f.WriteString(fmt.Sprintf("## Introduction\n%s\n\n", modu.Description))
|
2022-12-14 00:12:16 +00:00
|
|
|
if len(modu.Fields) != 0 {
|
|
|
|
f.WriteString("## Interface fields\n")
|
|
|
|
for _, dps := range modu.Fields {
|
|
|
|
f.WriteString(fmt.Sprintf("- `%s`: ", dps.FuncName))
|
|
|
|
f.WriteString(strings.Join(dps.Doc, " "))
|
|
|
|
f.WriteString("\n")
|
|
|
|
}
|
|
|
|
f.WriteString("\n")
|
|
|
|
}
|
2022-12-05 22:57:59 +00:00
|
|
|
if len(modu.Properties) != 0 {
|
2022-12-14 00:12:16 +00:00
|
|
|
f.WriteString("## Object properties\n")
|
2022-12-05 22:57:59 +00:00
|
|
|
for _, dps := range modu.Properties {
|
|
|
|
f.WriteString(fmt.Sprintf("- `%s`: ", dps.FuncName))
|
|
|
|
f.WriteString(strings.Join(dps.Doc, " "))
|
|
|
|
f.WriteString("\n")
|
|
|
|
}
|
2022-12-14 00:12:16 +00:00
|
|
|
f.WriteString("\n")
|
2022-12-05 22:57:59 +00:00
|
|
|
}
|
|
|
|
if len(modu.Docs) != 0 {
|
|
|
|
f.WriteString("## Functions\n")
|
|
|
|
}
|
2022-12-03 20:52:52 +00:00
|
|
|
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-04 02:50:15 +00:00
|
|
|
}(mod, docPath, v)
|
2022-12-03 15:15:25 +00:00
|
|
|
|
2022-12-04 02:50:15 +00:00
|
|
|
go func(md, modname string, modu module) {
|
2022-12-03 15:33:18 +00:00
|
|
|
defer wg.Done()
|
|
|
|
|
2022-12-04 02:50:15 +00:00
|
|
|
if modu.ParentModule != "" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ff, _ := os.Create("emmyLuaDocs/" + modname + ".lua")
|
|
|
|
ff.WriteString("--- @meta\n\nlocal " + modname + " = {}\n\n")
|
|
|
|
for _, em := range emmyDocs[modname] {
|
|
|
|
if strings.HasSuffix(em.DocPiece.GoFuncName, strings.ToLower("loader")) {
|
|
|
|
continue
|
2022-12-03 15:30:41 +00:00
|
|
|
}
|
2022-12-04 02:50:15 +00:00
|
|
|
|
|
|
|
dps := em.DocPiece
|
|
|
|
funcdocs := dps.Doc
|
2022-12-03 15:33:18 +00:00
|
|
|
ff.WriteString("--- " + strings.Join(funcdocs, "\n--- ") + "\n")
|
2022-12-04 02:50:15 +00:00
|
|
|
if len(em.Annotations) != 0 {
|
|
|
|
ff.WriteString(strings.Join(em.Annotations, "\n") + "\n")
|
|
|
|
}
|
|
|
|
accessor := "."
|
|
|
|
if dps.IsMember {
|
|
|
|
accessor = ":"
|
|
|
|
}
|
|
|
|
signature := strings.Split(dps.FuncSig, " ->")[0]
|
|
|
|
var intrface string
|
|
|
|
if dps.IsInterface {
|
|
|
|
intrface = "." + dps.Interfacing
|
2022-12-03 15:33:18 +00:00
|
|
|
}
|
2022-12-04 02:50:15 +00:00
|
|
|
ff.WriteString("function " + modname + intrface + accessor + signature + " end\n\n")
|
2022-12-03 15:30:41 +00:00
|
|
|
}
|
2022-12-04 02:50:15 +00:00
|
|
|
ff.WriteString("return " + modname + "\n")
|
|
|
|
}(mod, mod, v)
|
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
|
|
|
}
|