Skip to content

Commit

Permalink
feat: render directory structure instead of flat file names (#20)
Browse files Browse the repository at this point in the history
* feat: render directory structure instead of flat file names

* add gitignore
  • Loading branch information
Duologic authored Jun 4, 2021
1 parent 1f8d4c6 commit 5776ff8
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 9 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
docsonnet
7 changes: 6 additions & 1 deletion pkg/render/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@ func To(pkg docsonnet.Package, dir string, opts Opts) (int, error) {

n := 0
for k, v := range data {
if err := ioutil.WriteFile(filepath.Join(dir, k), []byte(v), 0644); err != nil {
fullpath := filepath.Join(dir, k)
dir := filepath.Dir(fullpath)
if err := os.MkdirAll(dir, os.ModePerm); err != nil {
return n, err
}
if err := ioutil.WriteFile(fullpath, []byte(v), 0644); err != nil {
return n, err
}
n++
Expand Down
17 changes: 9 additions & 8 deletions pkg/render/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,14 @@ func render(pkg docsonnet.Package, parents []string, root bool, urlPrefix string
md.Frontmatter(map[string]interface{}{
"permalink": link,
}),
md.Headline(1, "package "+pkg.Name),
md.Headline(1, strings.Join(append(parents, pkg.Name), ".")),
}
if pkg.Import != "" {
elems = append(elems, md.CodeBlock("jsonnet", fmt.Sprintf(`local %s = import "%s"`, pkg.Name, pkg.Import)))
}
elems = append(elems, md.Text(pkg.Help))

if len(pkg.Sub) > 0 {
elems = append(elems, md.Headline(2, "Subpackages"))

keys := make([]string, 0, len(pkg.Sub))
for _, s := range pkg.Sub {
keys = append(keys, s.Name)
Expand All @@ -55,11 +53,11 @@ func render(pkg docsonnet.Package, parents []string, root bool, urlPrefix string
for _, k := range keys {
s := pkg.Sub[k]

link := strings.Join(append(parents, pkg.Name, s.Name), "-")
if root {
link = strings.Join(append(parents, s.Name), "-")
link := s.Name + ".md"
if len(s.Sub) > 0 {
link = s.Name + "/index.md"
}
items = append(items, md.Link(md.Text(s.Name), link+".md"))
items = append(items, md.Link(md.Text(s.Name), link))
}

elems = append(elems, md.List(items...))
Expand All @@ -79,7 +77,10 @@ func render(pkg docsonnet.Package, parents []string, root bool, urlPrefix string
}

content := md.Doc(elems...).String()
key := strings.Join(append(parents, pkg.Name+".md"), "-")
key := strings.Join(append(parents, pkg.Name+".md"), "/")
if len(pkg.Sub) > 0 {
key = strings.Join(append(parents, pkg.Name+"/index.md"), "/")
}
if root {
key = "README.md"
}
Expand Down

0 comments on commit 5776ff8

Please sign in to comment.