Skip to content

Commit

Permalink
docs: initial blog
Browse files Browse the repository at this point in the history
  • Loading branch information
gernest committed Feb 13, 2024
1 parent c59822f commit f170d17
Show file tree
Hide file tree
Showing 6 changed files with 207 additions and 0 deletions.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ require (
github.com/google/cel-go v0.18.2 // indirect
github.com/google/flatbuffers v23.5.26+incompatible // indirect
github.com/google/pprof v0.0.0-20230207041349-798e818bf904 // indirect
github.com/gosimple/slug v1.13.1 // indirect
github.com/gosimple/unidecode v1.0.1 // indirect
github.com/klauspost/asmfmt v1.3.2 // indirect
github.com/klauspost/cpuid/v2 v2.2.5 // indirect
github.com/minio/asm2plan9s v0.0.0-20200509001527-cdd76441f9d8 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ github.com/google/uuid v1.4.0 h1:MtMxsa51/r9yyhkyLsVeVt0B+BGQZzpQiTQ4eHZ8bc4=
github.com/google/uuid v1.4.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/gorilla/websocket v1.5.1 h1:gmztn0JnHVt9JZquRuzLw3g4wouNVzKL15iLr/zn/QY=
github.com/gorilla/websocket v1.5.1/go.mod h1:x3kM2JMyaluk02fnUJpQuwD2dCS5NDG2ZHL0uE0tcaY=
github.com/gosimple/slug v1.13.1 h1:bQ+kpX9Qa6tHRaK+fZR0A0M2Kd7Pa5eHPPsb1JpHD+Q=
github.com/gosimple/slug v1.13.1/go.mod h1:UiRaFH+GEilHstLUmcBgWcI42viBN7mAb818JrYOeFQ=
github.com/gosimple/unidecode v1.0.1 h1:hZzFTMMqSswvf0LBJZCZgThIZrpDHFXux9KeGmn6T/o=
github.com/gosimple/unidecode v1.0.1/go.mod h1:CP0Cr1Y1kogOtx0bJblKzsVWrqYaqfNOnHzpgWw4Awc=
github.com/grpc-ecosystem/grpc-gateway/v2 v2.19.1 h1:/c3QmbOGMGTOumP2iT/rCwB7b0QDGLKzqOmktBjT+Is=
github.com/grpc-ecosystem/grpc-gateway/v2 v2.19.1/go.mod h1:5SN9VR2LTsRFsrEC6FHgRbTWrTHu6tqPeKxEQv15giM=
github.com/ianlancetaylor/demangle v0.0.0-20220319035150-800ac71e25c2/go.mod h1:aYm2/VgdVmcIU8iMfdMvDMsRAQjcfZSKFby6HOFvi/w=
Expand Down
113 changes: 113 additions & 0 deletions tools/docs/blog.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
package main

import (
"bytes"
_ "embed"
"html/template"
"log"
"strings"
"time"

"github.com/Depado/bfchroma/v2"
bhtml "github.com/alecthomas/chroma/v2/formatters/html"
"github.com/alecthomas/chroma/v2/styles"
"github.com/gosimple/slug"
"github.com/russross/blackfriday/v2"
)

//go:embed post.tmpl
var postData string

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

type Blog struct{}

type BlogSection struct {
URL string
Title string
Posts []*Post
}

type Post struct {
CreatedAt time.Time
UpdatedAt time.Time
Author string
Section string
Title string
URL string
Content string
}

const (
createdAt = "createdAt"
updatedAt = "updatedAt"
author = "author"
section = "section"
)

func renderPost(text []byte) (o Post) {
w := new(bytes.Buffer)
m := blackfriday.New(
blackfriday.WithExtensions(blackfriday.CommonExtensions),
)
r := &bfchroma.Renderer{
Base: blackfriday.NewHTMLRenderer(blackfriday.HTMLRendererParameters{
Flags: blackfriday.CommonHTMLFlags,
}),
Style: styles.SolarizedLight,
Autodetect: true,
}
r.Formatter = bhtml.New(r.ChromaOptions...)
ast := m.Parse(text)
var inHeading bool
var lastNode *blackfriday.Node
ast.Walk(func(node *blackfriday.Node, entering bool) blackfriday.WalkStatus {
if node.Type == blackfriday.Heading && !node.HeadingData.IsTitleblock {
inHeading = entering
if entering {
lastNode = node
}
return blackfriday.GoToNext
}
if inHeading {
switch lastNode.HeadingData.Level {
case 1:
o.Title = string(node.Literal)
o.URL = slug.Make(o.Title)
case 6:
txt := string(node.Literal)
key, value, _ := strings.Cut(txt, " ")
switch key {
case createdAt:
ts, err := time.Parse(time.DateOnly, value)
if err != nil {
fail(err)
}
o.CreatedAt = ts
case updatedAt:
ts, err := time.Parse(time.DateOnly, value)
if err != nil {
fail(err)
}
o.UpdatedAt = ts
case author:
o.Author = value
case section:
o.Author = section
}

}
}
return blackfriday.GoToNext
})

ast.Walk(func(node *blackfriday.Node, entering bool) blackfriday.WalkStatus {
return r.RenderNode(w, node, entering)
})
o.Content = w.String()
return
}

func fail(err error) {
log.Fatal(err)
}
13 changes: 13 additions & 0 deletions tools/docs/blog_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package main

import (
"os"
"testing"
)

func TestBlog(t *testing.T) {
b, _ := os.ReadFile("testdata/blog/post.md")
p := renderPost(b)

t.Errorf("%#v", p)
}
68 changes: 68 additions & 0 deletions tools/docs/post.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{{.Title}}</title>
<link rel="icon" {{.Icon}}>
<style>{{.CSS}}</style>
{{if .Domain}}
<script defer data-domain="{{.Domain}}" src="{{.Track}}"></script>
{{end}}
</head>

<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>

{{range .JS}}
<script>{{.}}</script>
{{end}}
</body>

</html>
7 changes: 7 additions & 0 deletions tools/docs/testdata/blog/post.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Initial post

###### createdAt 2024-01-13
###### updatedAt 2024-01-13
###### author Geofrey Ernest

This is the first post

0 comments on commit f170d17

Please sign in to comment.