Verified Commit 0e577272 authored by (Holloway), Chew Kean Ho's avatar (Holloway), Chew Kean Ho 🤘🏼
Browse files

App.go: recreated go doc command rendering



The goal to build our own go doc renderer requires us to reproduce
the go doc output for our code. Hence, we need to re-create the
exact output.

This patch recreates the go doc output in App.go.
Signed-off-by: (Holloway), Chew Kean Ho's avatar(Holloway) Chew, Kean Ho <kean.ho.chew@zoralab.com>
parent 89903bd0
Pipeline #110415648 passed with stage
in 1 minute and 3 seconds
......@@ -206,9 +206,10 @@ func (c *App) buildDocsData() {
canva := renderer.New()
for _, v := range c.targets {
c.printStatus(debugTag, "---NEW FILE---")
canva.Process(v)
canva.Render()
canva.Clear()
c.printStatus(debugTag, "new file.")
c.printStatus(debugTag, "---END FILE---")
}
}
......@@ -20,13 +20,14 @@ type DocData struct {
IsCMD bool
Package *doc.Package
BuildPackage *build.Package
FileSet *token.FileSet
}
// Generate is to create the Go doc data from a given Go package.
func (d *DocData) Generate() {
fset := token.NewFileSet()
d.FileSet = token.NewFileSet()
pkgSet, err := parser.ParseDir(fset,
pkgSet, err := parser.ParseDir(d.FileSet,
d.Path,
func(file os.FileInfo) bool {
name := file.Name()
......
......@@ -3,6 +3,9 @@ package renderer
import (
"bytes"
"fmt"
"go/doc"
"go/printer"
"go/token"
"gitlab.com/zoralab/godocgen/internal/generator"
)
......@@ -34,9 +37,88 @@ func (c *Canva) Clear() {
// Process is to process the given input data
func (c *Canva) Process(d *generator.DocData) {
c.renderHeader(d)
c.renderHeader(d.Name)
c.renderSynopsis(d.Package.Doc)
c.renderConstants(d.Package.Consts, d.FileSet)
c.renderVariables(d.Package.Vars, d.FileSet)
c.renderFunctions(d.Package.Funcs, d.FileSet)
c.renderTypes(d.Package.Types, d.FileSet)
}
func (c *Canva) renderHeader(d *generator.DocData) {
fmt.Fprintf(c.buffer, "# %s\n", d.Name)
func (c *Canva) renderHeader(name string) {
fmt.Fprintf(c.buffer, "# %s\n", name)
}
func (c *Canva) renderSynopsis(content string) {
fmt.Fprintf(c.buffer, "%s\n", content)
}
func (c *Canva) renderConstants(list []*doc.Value, fileSet *token.FileSet) {
for _, entry := range list {
var decl bytes.Buffer
mode := printer.TabIndent | printer.UseSpaces
cfg := &printer.Config{Mode: mode, Tabwidth: 8}
_ = cfg.Fprint(&decl, fileSet, entry.Decl)
fmt.Fprintf(c.buffer, "%s\n%s\n", decl.String(), entry.Doc)
}
}
func (c *Canva) renderVariables(list []*doc.Value, fileSet *token.FileSet) {
for _, entry := range list {
var decl bytes.Buffer
mode := printer.TabIndent | printer.UseSpaces
cfg := &printer.Config{Mode: mode, Tabwidth: 8}
_ = cfg.Fprint(&decl, fileSet, entry.Decl)
fmt.Fprintf(c.buffer, "%s\n%s\n", decl.String(), entry.Doc)
}
}
func (c *Canva) renderFunctions(list []*doc.Func, fileSet *token.FileSet) {
for _, entry := range list {
var decl bytes.Buffer
// process decl
mode := printer.TabIndent | printer.UseSpaces
cfg := &printer.Config{Mode: mode, Tabwidth: 8}
_ = cfg.Fprint(&decl, fileSet, entry.Decl)
// process receiver
receiver := " "
if entry.Recv != "" {
receiver = fmt.Sprintf("(%s) ", entry.Recv)
}
fmt.Fprintf(c.buffer,
"func %s%s\n\n%s\n%s\n",
receiver,
entry.Name,
decl.String(),
entry.Doc)
}
}
func (c *Canva) renderTypes(list []*doc.Type, fileSet *token.FileSet) {
for _, entry := range list {
var decl bytes.Buffer
// process decl
mode := printer.TabIndent | printer.UseSpaces
cfg := &printer.Config{Mode: mode, Tabwidth: 8}
_ = cfg.Fprint(&decl, fileSet, entry.Decl)
fmt.Fprintf(c.buffer,
"type %s\n\n%s\n\n%s\n",
entry.Name,
decl.String(),
entry.Doc)
c.renderConstants(entry.Consts, fileSet)
c.renderVariables(entry.Vars, fileSet)
c.renderFunctions(entry.Funcs, fileSet)
c.renderFunctions(entry.Methods, fileSet)
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment