-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
207 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |