-
-
Notifications
You must be signed in to change notification settings - Fork 374
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
149 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# DocGen | ||
|
||
This package provides documentation generator with JSON or Markdown output. | ||
|
||
## Usage | ||
|
||
Create a file and put next code into it. | ||
|
||
```go | ||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
|
||
"github.com/antonmedv/expr/docgen" | ||
) | ||
|
||
func main() { | ||
// TODO: Replace env with your own types. | ||
doc := docgen.CreateDoc(env) | ||
|
||
buf, err := json.MarshalIndent(doc, "", " ") | ||
if err != nil { | ||
panic(err) | ||
} | ||
fmt.Println(string(buf)) | ||
} | ||
``` | ||
|
||
Run `go run your_file.go`. Documentation will be printed in JSON format. | ||
|
||
## Markdown | ||
|
||
To generate markdown documentation: | ||
|
||
```go | ||
package main | ||
|
||
import "github.com/antonmedv/expr/docgen" | ||
|
||
func main() { | ||
// TODO: Replace env with your own types. | ||
doc := docgen.CreateDoc(env) | ||
|
||
print(doc.Markdown()) | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package docgen | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
func (c *Context) Markdown() string { | ||
out := `### Variables | ||
| Name | Type | | ||
|------|------| | ||
` | ||
for ident, v := range c.Variables { | ||
if v.Kind == "func" { | ||
continue | ||
} | ||
if v.Kind == "operator" { | ||
continue | ||
} | ||
out += fmt.Sprintf("| %v | %v |\n", ident, link(v)) | ||
} | ||
|
||
out += ` | ||
### Functions | ||
| Name | Return type | | ||
|------|-------------| | ||
` | ||
for ident, v := range c.Variables { | ||
if v.Kind == "func" { | ||
args := make([]string, len(v.Arguments)) | ||
for i, arg := range v.Arguments { | ||
args[i] = link(arg) | ||
} | ||
out += fmt.Sprintf("| %v(%v) | %v |\n", ident, strings.Join(args, ", "), link(v.Return)) | ||
} | ||
} | ||
|
||
out += "\n### Types\n" | ||
for name, t := range c.Types { | ||
out += fmt.Sprintf("#### %v\n", name) | ||
out += fields(t) | ||
out += "\n" | ||
} | ||
|
||
return out | ||
} | ||
|
||
func link(t *Type) string { | ||
if t == nil { | ||
return "nil" | ||
} | ||
if t.Name != "" { | ||
return fmt.Sprintf("[%v](#%v)", t.Name, t.Name) | ||
} | ||
if t.Kind == "array" { | ||
return fmt.Sprintf("array(%v)", link(t.Type)) | ||
} | ||
if t.Kind == "map" { | ||
return fmt.Sprintf("map(%v => %v)", link(t.Key), link(t.Type)) | ||
} | ||
return fmt.Sprintf("`%v`", t.Kind) | ||
} | ||
|
||
func fields(t *Type) string { | ||
out := "" | ||
foundFields := false | ||
for ident, v := range t.Fields { | ||
if v.Kind != "func" { | ||
if !foundFields { | ||
out += "| Field | Type |\n|---|---|\n" | ||
} | ||
foundFields = true | ||
|
||
out += fmt.Sprintf("| %v | %v |\n", ident, link(v)) | ||
} | ||
} | ||
foundMethod := false | ||
for ident, v := range t.Fields { | ||
if v.Kind == "func" { | ||
if !foundMethod { | ||
out += "\n| Method | Returns |\n|---|---|\n" | ||
} | ||
foundMethod = true | ||
|
||
args := make([]string, len(v.Arguments)) | ||
for i, arg := range v.Arguments { | ||
args[i] = link(arg) | ||
} | ||
out += fmt.Sprintf("| %v(%v) | %v |\n", ident, strings.Join(args, ", "), link(v.Return)) | ||
} | ||
} | ||
return out | ||
} |