Skip to content

Commit

Permalink
docs: generate blog tree
Browse files Browse the repository at this point in the history
  • Loading branch information
gernest committed Feb 13, 2024
1 parent 6111695 commit efab3b5
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 52 deletions.
67 changes: 62 additions & 5 deletions tools/docs/blog.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,20 @@ var postData string

var post = template.Must(template.New("main").Parse(postData))

type Blog struct {
Title string
Domain string
Track string
CSS template.CSS
Logo template.HTMLAttr
Icon template.HTMLAttr
JS []template.JS

Post *Post
Sections Sections
Section *Section
}

type Section struct {
URL string
Title string
Expand All @@ -37,6 +51,15 @@ func (s *Section) Update() {
}
}

func writeBlogFile(path string, ctx Blog) error {
b := new(bytes.Buffer)
err := post.Execute(b, ctx)
if err != nil {
return err
}
return os.WriteFile(filepath.Join(path, "index.html"), b.Bytes(), 0600)
}

func (s *Section) Write(base string) error {
for _, p := range s.Posts {
err := os.MkdirAll(filepath.Join(base, p.URL), 0755)
Expand All @@ -48,7 +71,17 @@ func (s *Section) Write(base string) error {
return err
}
}
return nil
return writeBlogFile(filepath.Join(base, s.URL), Blog{
Title: s.Title,
Section: s,

Domain: domain,
Track: track,
CSS: style,
JS: script,
Logo: LOGO,
Icon: Icon,
})
}

func writeBlog(src, out string) error {
Expand Down Expand Up @@ -102,7 +135,17 @@ func (ls Sections) Write(base string) error {
return err
}
}
return nil
return writeBlogFile(base, Blog{
Title: "vince- Blog",
Sections: ls,

Domain: domain,
Track: track,
CSS: style,
JS: script,
Logo: LOGO,
Icon: Icon,
})
}

func (ls Sections) Len() int {
Expand All @@ -124,11 +167,25 @@ type Post struct {
Section string
Title string
URL string
Content string
Content template.HTML
}

func (p *Post) Date() string {
return time.UnixMilli(p.Timestamp()).Format(time.DateOnly)
}

func (p *Post) Write(base string) error {
return os.WriteFile(filepath.Join(base, p.URL, "index.html"), []byte(p.Content), 0600)
return writeBlogFile(filepath.Join(base, p.URL), Blog{
Title: p.Title,
Post: p,

Domain: domain,
Track: track,
CSS: style,
JS: script,
Logo: LOGO,
Icon: Icon,
})
}

func (p *Post) Timestamp() int64 {
Expand Down Expand Up @@ -246,7 +303,7 @@ func renderPost(text []byte) (o Post) {
ast.Walk(func(node *blackfriday.Node, entering bool) blackfriday.WalkStatus {
return r.RenderNode(w, node, entering)
})
o.Content = w.String()
o.Content = template.HTML(w.String())
return
}

Expand Down
2 changes: 1 addition & 1 deletion tools/docs/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func main() {
}
defer os.RemoveAll(out)
println("serving from", out)
err = writeDocs(src, out)
err = build(src, out)
if err != nil {
fail(err)
}
Expand Down
3 changes: 3 additions & 0 deletions tools/docs/page.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
<div id="menu">
<a href="/" class="logo">vince </a>
<ul>
<li>
<a href="/blog">Blog</a>
</li>
{{range .Menus}}
<li>
<a href="#{{.ID}}">{{.Text}}</a>
Expand Down
66 changes: 20 additions & 46 deletions tools/docs/post.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -14,52 +14,26 @@

<body>

<div id="menubar"><a id="menutoggle" href="javascript:void 0" aria-label="Toggle the menu"><svg width="50" height="50" xmlns="http://www.w3.org/2000/svg"><rect x="15" y="18" width="20" height="2" stroke-width="0"></rect><rect x="15" y="24" width="20" height="2" stroke-width="0"></rect><rect x="15" y="30" width="20" height="2" stroke-width="0"></rect></svg></a></div>
<nav>
<div id="shadow"></div>
<div id="menu">
<a href="/" class="logo">vince </a>
<ul>
{{range .Menus}}
<li>
<a href="#{{.ID}}">{{.Text}}</a>
<ul class="h2">
{{range .Items}}
<li>
<a href="{{.URL}}">{{.Text}}</a>
</li>
{{end}}
</ul>
</li>
{{end}}
</ul>
<div id="icons">
<a href="https://github.com/vinceanalytics/vince" aria-label="View this project on GitHub">
<svg xmlns="http://www.w3.org/2000/svg" width="25" height="25">
<path fill-rule="evenodd" stroke-width="0" d="M13 5a8 8 0 00-2.53 15.59c.4.07.55-.17.55
-.38l-.01-1.49c-2.01.37-2.53-.49-2.69-.94-.09-.23-.48-.94-.82-1.13-.28-.15-.68-.52
-.01-.53.63-.01 1.08.58 1.23.82.72 1.21 1.87.87 2.33.66.07-.52.28-.87.51-1.07-1.78
-.2-3.64-.89-3.64-3.95 0-.87.31-1.59.82-2.15-.08-.2-.36-1.02.08-2.12 0 0 .67-.21 2.2
.82a7.42 7.42 0 014 0c1.53-1.04 2.2-.82 2.2-.82.44 1.1.16 1.92.08 2.12.51.56.82 1.27
.82 2.15 0 3.07-1.87 3.75-3.65 3.95.29.25.54.73.54 1.48l-.01 2.2c0 .21.15.46.55.38A
8.01 8.01 0 0021 13a8 8 0 00-8-8z"></path>
</svg>
</a>
</div>
</div>
</nav>
<main>
<div class="index">
<h1>vince</h1>
<blockquote>API first high performance self hosted and cost effective privacy friendly web analytics server for organizations of any size</blockquote>
</div>
{{range .Pages}}
<div>{{.}}</div>
<hr/>
<br/>
{{end}}
</main>

<main>
{{with .Post}}
{{.Content}}
{{end}}
{{range .Sections}}
<h2><a href="/blog/{{.URL}}">{{.Title}}</a> </h2>
<ul>
{{range .Posts}}
<li><a href="/blog/{{.URL}}">{{.Title}} - <i>{{.Date}}</i></a> </li>
{{end}}
</ul>
{{end}}
{{with .Section}}
<h2><a href="/blog/{{.URL}}">{{.Title}}</a> </h2>
<ul>
{{range .Posts}}
<li><a href="/blog/{{.URL}}">{{.Title}} - <i>{{.Date}}</i></a> </li>
{{end}}
{{end}}
</main>
{{range .JS}}
<script>{{.}}</script>
{{end}}
Expand Down

0 comments on commit efab3b5

Please sign in to comment.