Skip to content

Commit

Permalink
chore: use anchorize in key creation (#38)
Browse files Browse the repository at this point in the history
* chore: use anchorize in key creation
* change: template now require # before key
* ci: added test workflow

Signed-off-by: roee88 <[email protected]>
  • Loading branch information
roee88 authored Apr 16, 2021
1 parent f30b0f9 commit 50da9e8
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 34 deletions.
25 changes: 25 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: test
on:
push:
tags:
- v*
branches:
- main
pull_request:
jobs:
test:
runs-on: ubuntu-latest
steps:
- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.16
- uses: actions/checkout@v2
- name: Run on example
run: |
go run main.go --resources example/crds --output example/output.md
- name: Check that there are no source code changes
run: |
go mod tidy
git checkout go.sum
git diff --exit-code
9 changes: 2 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,8 @@ func RootCmd() *cobra.Command {
return err
}

builder := pkg.ModelBuilder{
Model: model,
Strict: tocOptionValue != "",
TemplatesDirOrFile: templateOptionValue,
OutputFilepath: outputOptionValue,
BuiltinTemplates: builtinTemplates,
}
builder := pkg.NewModelBuilder(model, tocOptionValue != "",
templateOptionValue, outputOptionValue, builtinTemplates)

crds, err := pkg.LoadCRDs(resourcesOptionValue)
if err != nil {
Expand Down
50 changes: 25 additions & 25 deletions pkg/builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
package builder

import (
"embed"
"errors"
"fmt"
"io/fs"
"os"
"path/filepath"
"regexp"
"strings"
"text/template"

Expand All @@ -25,8 +25,22 @@ type ModelBuilder struct {
Strict bool
TemplatesDirOrFile string
OutputFilepath string
Links map[string]int
BuiltinTemplates fs.FS

keys map[string]int
builtinTemplates fs.FS
}

func NewModelBuilder(model *Model, strict bool, templatesDirOrFile string, outputFilepath string, builtinTemplates embed.FS) *ModelBuilder {

builder := &ModelBuilder{
Model: model,
Strict: strict,
TemplatesDirOrFile: templatesDirOrFile,
OutputFilepath: outputFilepath,
builtinTemplates: builtinTemplates,
}
builder.keys = make(map[string]int)
return builder
}

// Add adds a CustomResourceDefinition to the model
Expand Down Expand Up @@ -105,7 +119,7 @@ func (b *ModelBuilder) Output() error {
}()

// Values for embedded templates
templatesFs := b.BuiltinTemplates
templatesFs := b.builtinTemplates
pattern := "templates/**.tmpl"

dir, file := filepath.Split(b.TemplatesDirOrFile)
Expand All @@ -128,7 +142,7 @@ func (b *ModelBuilder) addTypeModels(groupModel *GroupModel, kindModel *KindMode
// Create an object type model
typeModel := &TypeModel{
Name: name,
Key: b.createLink(name),
Key: b.createKey(name),
Description: schema.Description,
IsTopLevel: isTopLevel,
}
Expand Down Expand Up @@ -174,29 +188,15 @@ func (b *ModelBuilder) addTypeModels(groupModel *GroupModel, kindModel *KindMode
return typeName, nil
}

func (b *ModelBuilder) createLink(name string) string {
link := fmt.Sprintf("#%s", headingID(name))
if b.Links == nil {
b.Links = make(map[string]int)
}
if value, exists := b.Links[link]; exists {
func (b *ModelBuilder) createKey(name string) string {
key := functions.Anchorize(name)
if value, exists := b.keys[key]; exists {
value++
link = fmt.Sprintf("%s-%d", link, value)
key = fmt.Sprintf("%s-%d", key, value)
} else {
b.Links[link] = 0
b.keys[key] = 0
}
return link
}

// headingID returns the ID built by hugo/github for a given header
func headingID(s string) string {
result := strings.ToLower(s)
result = strings.TrimSpace(result)
result = regexp.MustCompile(`([^\w\- ]+)`).ReplaceAllString(result, "")
result = regexp.MustCompile(`(\s)`).ReplaceAllString(result, "-")
result = regexp.MustCompile(`(\-+$)`).ReplaceAllString(result, "")

return result
return key
}

func getTypeName(props *apiextensions.JSONSchemaProps) string {
Expand Down
4 changes: 2 additions & 2 deletions templates/markdown.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Resource Types:

{{if not .IsTopLevel}}
### {{.Name}}
{{if .ParentKey}}<sup><sup>[↩ Parent]({{.ParentKey}})</sup></sup>{{end}}
{{if .ParentKey}}<sup><sup>[↩ Parent](#{{.ParentKey}})</sup></sup>{{end}}
{{end}}


Expand Down Expand Up @@ -62,7 +62,7 @@ Resource Types:
{{- end -}}
{{- range .Fields -}}
<tr>
<td><b>{{if .TypeKey}}<a href="{{.TypeKey}}">{{.Name}}</a>{{else}}{{.Name}}{{end}}</b></td>
<td><b>{{if .TypeKey}}<a href="#{{.TypeKey}}">{{.Name}}</a>{{else}}{{.Name}}{{end}}</b></td>
<td>{{.Type}}</td>
<td>{{.Description}}</td>
<td>{{.Required}}</td>
Expand Down

0 comments on commit 50da9e8

Please sign in to comment.