Skip to content

Commit

Permalink
Use escape-core markdown generator on the EscapePlan struct
Browse files Browse the repository at this point in the history
  • Loading branch information
bspaans committed Nov 14, 2017
1 parent 3518e1a commit ee0ce4b
Show file tree
Hide file tree
Showing 25 changed files with 623 additions and 607 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ lock.json
manifest.json
deps/
docs/cmd/
docs/generated/
model/runners/build/testdata/*.json
model/runners/deploy/testdata/
model/runners/deploy/testdata/*.json
Expand Down
115 changes: 0 additions & 115 deletions docs/escape-plan.md

This file was deleted.

144 changes: 114 additions & 30 deletions docs/generate_cmd_docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@ package main

import (
"fmt"
"go/ast"
"go/parser"
"go/token"
"io/ioutil"
"log"
"os"
"path"
"path/filepath"
"strings"

"github.com/ankyra/escape/cmd"
"github.com/ankyra/escape/model/escape_plan"
"github.com/spf13/cobra/doc"
)

Expand Down Expand Up @@ -66,44 +69,125 @@ var typeLinks = map[string]string{
"Templates": "templates",
}

func GeneratePlanDocs() {
result := planHeader
for _, field := range escape_plan.Fields {
typ := ""
types, ok := typeMap[field]
if !ok {
typ = "`string`"
type Page struct {
Name string
Slug string
SrcFile string
StructName string
}

var Pages = map[string]Page{
"escape plan": Page{"The Escape Plan", "escape-plan", "model/escape_plan/escape_plan.go", "EscapePlan"},
}

const PageHeader = `---
date: 2017-11-11 00:00:00
title: "%s"
slug: %s
type: "docs"
toc: true
---
%s
Field | Type | Description
------|------|-------------
%s
`

func GetYamlFieldFromTag(tag string) string {
for _, s := range strings.Split(tag, " ") {
s = strings.Trim(s, "`")
if strings.HasPrefix(s, "yaml:\"") {
s = s[6 : len(s)-1]
parts := strings.Split(s, ",")
return parts[0]
}
for _, t := range types {
typLink, ok := typeLinks[t]
if ok {
typ += "[" + t + "](/docs/" + typLink + "/) "
} else {
typ += "`" + t + "` "
}
return ""
}

func ParseType(expr ast.Expr) string {
switch t := expr.(type) {
case *ast.Ident:
return t.Name
case *ast.SelectorExpr:
return ParseType(t.X) + "." + t.Sel.String() // probably wrong
case *ast.ArrayType:
return "[" + ParseType(t.Elt) + "]"
case *ast.StarExpr:
return ParseType(t.X)
case *ast.MapType:
return "{" + ParseType(t.Key) + ":" + ParseType(t.Value) + "}"
case *ast.InterfaceType:
return "any"
default:
fmt.Printf("%T\n", t)
panic("type not supported in documentation: ")
}
return ""
}

func StructTable(page Page, topLevelDoc string, s *ast.TypeSpec) string {
structType := s.Type.(*ast.StructType)
result := ""
for _, field := range structType.Fields.List {
tag := GetYamlFieldFromTag(field.Tag.Value)
typ := ParseType(field.Type)
result += "|" + tag + "|`" + typ + "`|"
doc := strings.TrimSpace(field.Doc.Text())
if doc != "" {
for _, line := range strings.Split(doc, "\n") {
if strings.HasPrefix(line, "#") {
line = line[1:]
}
line = strings.TrimSpace(line)
if line == "" {
result += "\n|||"
} else {
result += line + " "
}
}
}
desc := string(escape_plan.GetDoc(field))
desc = strings.TrimSpace(desc)
result += "|<a name='" + field + "'></a>" + field + "|" + typ + "|"
for _, line := range strings.Split(desc, "\n") {
if strings.HasPrefix(line, "#") {
line = line[1:]
}
line = strings.TrimSpace(line)
if line == "" {
result += "\n|||"
} else {
result += line + " "
result += "\n"
}
return fmt.Sprintf(PageHeader, page.Name, page.Slug, topLevelDoc, result)
}

func GenerateStructDocs(f *ast.File, page Page) string {
for _, decl := range f.Decls {
if gen, ok := decl.(*ast.GenDecl); ok && gen.Tok == token.TYPE {
for _, spec := range gen.Specs {
if s, ok := spec.(*ast.TypeSpec); ok {
switch s.Type.(type) {
case *ast.StructType:
if s.Name.String() == page.StructName {
return StructTable(page, gen.Doc.Text(), s)
}
}
}
}
}
result += "\n"
}
if err := ioutil.WriteFile("./docs/escape-plan.md", []byte(result), 0644); err != nil {
panic(err)
return ""
}

func GeneratePages() {
for _, page := range Pages {
fset := token.NewFileSet()
f, err := parser.ParseFile(fset, page.SrcFile, nil, parser.ParseComments)
if err != nil {
panic(err)
}
str := GenerateStructDocs(f, page)
filename := "docs/generated/" + page.Slug + ".md"
fmt.Println("Writing ", filename)
ioutil.WriteFile(filename, []byte(str), 0644)
}
}

func main() {
os.Mkdir("docs/generated/", 0755)
filePrepender := func(filename string) string {
name := filepath.Base(filename)
base := strings.TrimSuffix(name, path.Ext(name))
Expand All @@ -118,5 +202,5 @@ func main() {
if err != nil {
log.Fatal(err)
}
GeneratePlanDocs()
GeneratePages()
}
3 changes: 2 additions & 1 deletion scripts/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
set -euf -o pipefail

rm -rf docs/cmd/
rm -rf docs/generated/
rm -rf vendor/github.com/ankyra/escape-core
cp -r deps/_/escape-core/ vendor/github.com/ankyra/escape-core
rm -rf vendor/github.com/ankyra/escape-core/vendor/
Expand All @@ -19,5 +20,5 @@ docker run --rm \
golang:1.9.0 bash -c "go build -v -o escape && mkdir -p docs/cmd && go run docs/generate_cmd_docs.go"
docker cp src:/go/src/github.com/ankyra/escape/escape escape
docker cp src:/go/src/github.com/ankyra/escape/docs/cmd docs/cmd
docker cp src:/go/src/github.com/ankyra/escape/docs/escape-plan.md docs/escape-plan.md
docker cp src:/go/src/github.com/ankyra/escape/docs/generated docs/generated
docker rm src
7 changes: 4 additions & 3 deletions vendor/github.com/ankyra/escape-core/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion vendor/github.com/ankyra/escape-core/build.sh

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion vendor/github.com/ankyra/escape-core/core.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion vendor/github.com/ankyra/escape-core/dependency.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions vendor/github.com/ankyra/escape-core/dependency_config.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit ee0ce4b

Please sign in to comment.