Skip to content

Commit

Permalink
Fixed bugs and added template commands
Browse files Browse the repository at this point in the history
  • Loading branch information
mariotoffia committed Oct 6, 2020
1 parent eb0b36c commit 9c813cb
Show file tree
Hide file tree
Showing 9 changed files with 187 additions and 41 deletions.
74 changes: 72 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ type HealthChecker struct {
```

```bash
goasciidoc v0.0.5
goasciidoc v0.0.6
Usage: goasciidoc [--out PATH] [--stdout] [--module PATH] [--internal] [--private] [--test] [--noindex] [--notoc] [--indexconfig JSON] [--overrides OVERRIDES] [PATH [PATH ...]]

Positional arguments:
Expand All @@ -72,7 +72,7 @@ Options:
--notoc Removes the table of contents if index document
--indexconfig JSON, -c JSON
JSON document to override the IndexConfig
--overrides OVERRIDES, -t OVERRIDES
--overrides OVERRIDES, -r OVERRIDES
name=template filepath to override default templates
--help, -h display this help and exit
--version display version and exit
Expand Down Expand Up @@ -103,6 +103,76 @@ You may now use the `goasciidoc` e.g. in the `goasciidoc` repo by `goasciidoc --
## Notes
This project consists of a parser to parse go-code and a producer to produce asciidoc files from the code & code documentation. It bases its rendering system heavily on templates (`asciidoc/template.go`) with some "sane" default so it may be rather easily overridden.
### List Default Templates
To list the default templates just do `goasciidoc --list-template`. Version 0.0.6 will list the following template names:
* interfaces
* interface
* consts
* typedeffunc
* package
* import
* typedefvars
* vars
* index
* function
* typedeffuncs
* functions
* structs
* struct
* typedefvar
* var
* const
### Get Default Templates
It is possible to retrieve the default templates (_use list to get the template names_) using a command switch `--out-template NAME`, for example:
```bash
goasciidoc --out-template struct
```
The above outputs (for v0.0.6):
```
"=== {{.Struct.Name}}
[source, go]
----
{{.Struct.Decl}} {
{{- range .Struct.Fields}}
{{if .Nested}}{{.Nested.Name}}{{"\t"}}struct{{else}}{{tabify .Decl}}{{end}}
{{- end}}
}
----
{{.Struct.Doc}}
{{range .Struct.Fields}}{{if not .Nested}}
==== {{.Decl}}
{{.Doc}}
{{- end}}
{{end}}
{{range .Struct.Fields}}{{if .Nested}}{{render $ .Nested}}{{end}}{{end}}
"
```
### Override Default Templates
If you're unhappy with one of the default templates, you may _override_ it (one or more) using the `-t FILEPATH` switch. It may be several `-t` on same command if multiple overrides. The filepath is either relative or fully qualified filepath to a template file.
For example, overriding the _package_ template can be done like this:
```bash
echo "== Override Package {{.File.FqPackage}}" > t.txt; go run main.go -r package=t.txt --stdout; rm t.txt
```
In the `stdout` you may observe, now, it has _Override Package_ instead of _Package_ as heading
```
== Override Package github.com/mariotoffia/goasciidoc/goparser
=== Imports
...
```
### Plugins
Since asciidoc supports plugins, thus is **very** versatile, myself is using [kroki](https://kroki.io) that may render many types of diagrams (can be done online or offline using docker-compose). Below there are just a few of many, many [diagrams](https://kroki.io/examples.html) that may be outputted just using kroki.
Expand Down
2 changes: 1 addition & 1 deletion asciidoc/producer.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func (p *Producer) Module(path string) *Producer {
panic(err)
}

path = d
path = filepath.Join(d, "go.mod")
}

if !strings.HasSuffix(path, "go.mod") {
Expand Down
8 changes: 7 additions & 1 deletion asciidoc/renderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,16 @@ import (
"github.com/mariotoffia/goasciidoc/goparser"
)

// CreateTemplateWithOverrides creates a new instance of _Template_
// and add the possible _Provider.overrides_ into it.
func (p *Producer) CreateTemplateWithOverrides() *Template {
return NewTemplateWithOverrides(p.overrides)
}

// Generate will execute the generation of the documentation
func (p *Producer) Generate() {

t := NewTemplateWithOverrides(p.overrides)
t := p.CreateTemplateWithOverrides()
w := tabwriter.NewWriter(p.createWriter(), 4, 4, 4, ' ', 0)

indexdone := !p.index
Expand Down
21 changes: 17 additions & 4 deletions asciidoc/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,21 @@ func (tt TemplateType) String() string {
return string(tt)
}

// TemplateAndText is a wrapper of _template.Template_
// but also includes the original text representation
// of the template and not just the parsed tree.
type TemplateAndText struct {
// Text is the actual template that got parsed by _template.Template_.
Text string
// Template is the instance of the parsed _Text_ including functions.
Template *template.Template
}

// Template is handling all templates and actions
// to perform.
type Template struct {
// Templates to use when rendering documentation
Templates map[string]*template.Template
Templates map[string]*TemplateAndText
}

// NewTemplate creates a new set of templates to be used
Expand All @@ -70,7 +80,7 @@ func NewTemplate() *Template {
func NewTemplateWithOverrides(overrides map[string]string) *Template {

return &Template{
Templates: map[string]*template.Template{
Templates: map[string]*TemplateAndText{
IndexTemplate.String(): createTemplate(IndexTemplate, templateIndex, overrides, template.FuncMap{
"cr": func() string { return "\n" },
}),
Expand Down Expand Up @@ -188,7 +198,7 @@ func (t *Template) NewContextWithConfig(f *goparser.GoFile, config *TemplateCont
//
// If name is found in override map it will use that string to parse the template
// instead of the provided str.
func createTemplate(name TemplateType, str string, overrides map[string]string, fm template.FuncMap) *template.Template {
func createTemplate(name TemplateType, str string, overrides map[string]string, fm template.FuncMap) *TemplateAndText {

if s, ok := overrides[name.String()]; ok {
str = s
Expand All @@ -198,6 +208,9 @@ func createTemplate(name TemplateType, str string, overrides map[string]string,
if err != nil {
panic(err)
}
return pt
return &TemplateAndText{
Text: str,
Template: pt,
}

}
34 changes: 17 additions & 17 deletions asciidoc/templatecontext.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ func (t *TemplateContext) Creator() *Template {
// RenderPackage will render the package defintion onto the provided writer.
func (t *TemplateContext) RenderPackage(wr io.Writer) *TemplateContext {

if err := t.creator.Templates[PackageTemplate.String()].Execute(wr, t.Clone(true /*clean*/)); nil != err {
if err := t.creator.Templates[PackageTemplate.String()].Template.Execute(wr, t.Clone(true /*clean*/)); nil != err {
panic(err)
}

Expand All @@ -158,7 +158,7 @@ func (t *TemplateContext) RenderPackage(wr io.Writer) *TemplateContext {
// RenderImports will render the imports section onto the provided writer.
func (t *TemplateContext) RenderImports(wr io.Writer) *TemplateContext {

if err := t.creator.Templates[ImportTemplate.String()].Execute(wr, t.Clone(true /*clean*/)); nil != err {
if err := t.creator.Templates[ImportTemplate.String()].Template.Execute(wr, t.Clone(true /*clean*/)); nil != err {
panic(err)
}

Expand All @@ -168,7 +168,7 @@ func (t *TemplateContext) RenderImports(wr io.Writer) *TemplateContext {
// RenderFunctions will render all functions for GoFile/GoPackage onto the provided writer.
func (t *TemplateContext) RenderFunctions(wr io.Writer) *TemplateContext {

if err := t.creator.Templates[FunctionsTemplate.String()].Execute(wr, t.Clone(true /*clean*/)); nil != err {
if err := t.creator.Templates[FunctionsTemplate.String()].Template.Execute(wr, t.Clone(true /*clean*/)); nil != err {
panic(err)
}

Expand All @@ -181,7 +181,7 @@ func (t *TemplateContext) RenderFunction(wr io.Writer, f *goparser.GoStructMetho
q := t.Clone(true /*clean*/)
q.Function = f

if err := t.creator.Templates[FunctionTemplate.String()].Execute(wr, q); nil != err {
if err := t.creator.Templates[FunctionTemplate.String()].Template.Execute(wr, q); nil != err {
panic(err)
}

Expand All @@ -191,7 +191,7 @@ func (t *TemplateContext) RenderFunction(wr io.Writer, f *goparser.GoStructMetho
// RenderInterfaces will render all interfaces for GoFile/GoPackage onto the provided writer.
func (t *TemplateContext) RenderInterfaces(wr io.Writer) *TemplateContext {

if err := t.creator.Templates[InterfacesTemplate.String()].Execute(wr, t.Clone(true /*clean*/)); nil != err {
if err := t.creator.Templates[InterfacesTemplate.String()].Template.Execute(wr, t.Clone(true /*clean*/)); nil != err {
panic(err)
}

Expand All @@ -204,7 +204,7 @@ func (t *TemplateContext) RenderInterface(wr io.Writer, i *goparser.GoInterface)
q := t.Clone(true /*clean*/)
q.Interface = i

if err := t.creator.Templates[InterfaceTemplate.String()].Execute(wr, q); nil != err {
if err := t.creator.Templates[InterfaceTemplate.String()].Template.Execute(wr, q); nil != err {
panic(err)
}

Expand All @@ -214,7 +214,7 @@ func (t *TemplateContext) RenderInterface(wr io.Writer, i *goparser.GoInterface)
// RenderStructs will render all structs for GoFile/GoPackage onto the provided writer.
func (t *TemplateContext) RenderStructs(wr io.Writer) *TemplateContext {

if err := t.creator.Templates[StructsTemplate.String()].Execute(wr, t.Clone(true /*clean*/)); nil != err {
if err := t.creator.Templates[StructsTemplate.String()].Template.Execute(wr, t.Clone(true /*clean*/)); nil != err {
panic(err)
}

Expand All @@ -227,7 +227,7 @@ func (t *TemplateContext) RenderStruct(wr io.Writer, s *goparser.GoStruct) *Temp
q := t.Clone(true /*clean*/)
q.Struct = s

if err := t.creator.Templates[StructTemplate.String()].Execute(wr, q); nil != err {
if err := t.creator.Templates[StructTemplate.String()].Template.Execute(wr, q); nil != err {
panic(err)
}

Expand All @@ -237,7 +237,7 @@ func (t *TemplateContext) RenderStruct(wr io.Writer, s *goparser.GoStruct) *Temp
// RenderVarTypeDefs will render all variable type definitions for GoFile/GoPackage onto the provided writer.
func (t *TemplateContext) RenderVarTypeDefs(wr io.Writer) *TemplateContext {

if err := t.creator.Templates[CustomVarTypeDefsTemplate.String()].Execute(wr, t.Clone(true /*clean*/)); nil != err {
if err := t.creator.Templates[CustomVarTypeDefsTemplate.String()].Template.Execute(wr, t.Clone(true /*clean*/)); nil != err {
panic(err)
}

Expand All @@ -250,7 +250,7 @@ func (t *TemplateContext) RenderVarTypeDef(wr io.Writer, td *goparser.GoCustomTy
q := t.Clone(true /*clean*/)
q.TypeDefVar = td

if err := t.creator.Templates[CustomVarTypeDefTemplate.String()].Execute(wr, q); nil != err {
if err := t.creator.Templates[CustomVarTypeDefTemplate.String()].Template.Execute(wr, q); nil != err {
panic(err)
}

Expand All @@ -260,7 +260,7 @@ func (t *TemplateContext) RenderVarTypeDef(wr io.Writer, td *goparser.GoCustomTy
// RenderVarDeclarations will render all variable declarations for GoFile/GoPackage onto the provided writer.
func (t *TemplateContext) RenderVarDeclarations(wr io.Writer) *TemplateContext {

if err := t.creator.Templates[VarDeclarationsTemplate.String()].Execute(wr, t.Clone(true /*clean*/)); nil != err {
if err := t.creator.Templates[VarDeclarationsTemplate.String()].Template.Execute(wr, t.Clone(true /*clean*/)); nil != err {
panic(err)
}

Expand All @@ -273,7 +273,7 @@ func (t *TemplateContext) RenderVarDeclaration(wr io.Writer, a *goparser.GoAssig
q := t.Clone(true /*clean*/)
q.VarAssignment = a

if err := t.creator.Templates[VarDeclarationTemplate.String()].Execute(wr, q); nil != err {
if err := t.creator.Templates[VarDeclarationTemplate.String()].Template.Execute(wr, q); nil != err {
panic(err)
}

Expand All @@ -283,7 +283,7 @@ func (t *TemplateContext) RenderVarDeclaration(wr io.Writer, a *goparser.GoAssig
// RenderConstDeclarations will render all const declarations for GoFile/GoPackage onto the provided writer.
func (t *TemplateContext) RenderConstDeclarations(wr io.Writer) *TemplateContext {

if err := t.creator.Templates[ConstDeclarationsTemplate.String()].Execute(wr, t.Clone(true /*clean*/)); nil != err {
if err := t.creator.Templates[ConstDeclarationsTemplate.String()].Template.Execute(wr, t.Clone(true /*clean*/)); nil != err {
panic(err)
}

Expand All @@ -296,7 +296,7 @@ func (t *TemplateContext) RenderConstDeclaration(wr io.Writer, a *goparser.GoAss
q := t.Clone(true /*clean*/)
q.ConstAssignment = a

if err := t.creator.Templates[ConstDeclarationTemplate.String()].Execute(wr, q); nil != err {
if err := t.creator.Templates[ConstDeclarationTemplate.String()].Template.Execute(wr, q); nil != err {
panic(err)
}

Expand All @@ -306,7 +306,7 @@ func (t *TemplateContext) RenderConstDeclaration(wr io.Writer, a *goparser.GoAss
// RenderTypeDefFuncs will render all type definitions for GoFile/GoPackage onto the provided writer.
func (t *TemplateContext) RenderTypeDefFuncs(wr io.Writer) *TemplateContext {

if err := t.creator.Templates[CustomFuncTypeDefsTemplate.String()].Execute(wr, t.Clone(true /*clean*/)); nil != err {
if err := t.creator.Templates[CustomFuncTypeDefsTemplate.String()].Template.Execute(wr, t.Clone(true /*clean*/)); nil != err {
panic(err)
}

Expand All @@ -319,7 +319,7 @@ func (t *TemplateContext) RenderTypeDefFunc(wr io.Writer, td *goparser.GoMethod)
q := t.Clone(true /*clean*/)
q.TypeDefFunc = td

if err := t.creator.Templates[CustomFuncTypeDefTemplate.String()].Execute(wr, q); nil != err {
if err := t.creator.Templates[CustomFuncTypeDefTemplate.String()].Template.Execute(wr, q); nil != err {
panic(err)
}

Expand All @@ -338,7 +338,7 @@ func (t *TemplateContext) RenderIndex(wr io.Writer, ic *IndexConfig) *TemplateCo
q := t.Clone(true /*clean*/)
q.Index = ic

if err := t.creator.Templates[IndexTemplate.String()].Execute(wr, q); nil != err {
if err := t.creator.Templates[IndexTemplate.String()].Template.Execute(wr, q); nil != err {
panic(err)
}

Expand Down
4 changes: 4 additions & 0 deletions goparser/gomodule.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ func (gm *GoModule) ResolvePackage(path string) string {
rpkg = rpkg[1:]
}

if rpkg == "" {
return gm.Name // root package
}

return fmt.Sprintf("%s/%s", gm.Name, rpkg)

}
Expand Down
9 changes: 5 additions & 4 deletions goparser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,10 +190,11 @@ func ParseSinglePackageWalker(config ParseConfig, process ParseSinglePackageWalk

pkg := &GoPackage{
GoFile: GoFile{
Module: config.Module,
Package: goFiles[0].Package,
FilePath: k,
Decl: goFiles[0].Decl,
Module: config.Module,
Package: goFiles[0].Package,
FqPackage: goFiles[0].FqPackage,
FilePath: k,
Decl: goFiles[0].Decl,
},
Files: goFiles,
}
Expand Down
Loading

0 comments on commit 9c813cb

Please sign in to comment.