Skip to content

Commit

Permalink
Merge branch 'main' into zettel
Browse files Browse the repository at this point in the history
  • Loading branch information
bwaklog committed Apr 11, 2024
2 parents 6257e32 + e159c6a commit fe8df9c
Show file tree
Hide file tree
Showing 67 changed files with 1,159 additions and 663 deletions.
45 changes: 45 additions & 0 deletions .github/workflows/bench.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Benchmark anna v/s other SSGs
name: Benchmark

on:
workflow_dispatch:

jobs:
benchmark:
runs-on: ubuntu-latest

steps:
- name: Checkout Repository
uses: actions/checkout@v4

- name: Install Hyperfine
run: |
sudo apt-get update
sudo apt-get install -y hyperfine
- name: Install Go
uses: actions/setup-go@v4
with:
go-version-file: "go.mod"

- name: Install Rust
uses: actions-rs/[email protected]
with:
profile: minimal
toolchain: stable

- name: Install Dependencies
run: |
sudo apt-get install -y curl git
curl https://sh.rustup.rs -sSf | sh -s -- -y
export PATH="$HOME/.cargo/bin:$PATH"
rustup default stable
rustup update
rustc --version
cargo --version
go version
- name: Run Benchmark
run: |
chmod +x test/bench.sh
./test/bench.sh
19 changes: 4 additions & 15 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
# Sample workflow for building and deploying an anna site to GitHub pages
name: Build
name: Build and Deploy

on:
push:
branches: [main]
workflow_dispatch:

# Sets permissions of the GITHUB_TOKEN to allow deployment to GitHub Pages
Expand All @@ -25,6 +23,7 @@ defaults:
jobs:
build:
runs-on: ubuntu-latest
needs: test-and-coverage
environment:
name: github-pages
url: ${{steps.deployment.outputs.page_url}}
Expand All @@ -37,19 +36,8 @@ jobs:
with:
go-version-file: "go.mod"

- name: test anna
run: go test ./...

- name: build anna
run: go build

- name: Insert commit hash in footer
run: |
sed -i "s/COMMIT_HASH/${{ github.sha }}/g" site/layout/partials/footer.layout
- name: build site with anna
run: |
./anna
run: go run .

- name: upload /rendered
uses: actions/upload-artifact@master
Expand All @@ -64,6 +52,7 @@ jobs:
environment:
name: github-pages
url: ${{steps.deployment.outputs.page_url}}

steps:
- name: download /rendered
uses: actions/download-artifact@master
Expand Down
30 changes: 30 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: Release

on:
push:
tags:
- '*'

permissions:
contents: write

jobs:
goreleaser:
runs-on: ubuntu-latest
steps:
- name: checkout code
uses: actions/checkout@v4
with:
fetch-depth: 0

- name: setup go
uses: actions/setup-go@v4

- name: run goreleaser
uses: goreleaser/goreleaser-action@v5
with:
distribution: goreleaser
version: latest
args: release --clean
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
31 changes: 13 additions & 18 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -1,32 +1,27 @@
# Sample workflow for building and deploying an anna site to GitHub pages
name: Tests

on: [push, pull_request]

# Sets permissions of the GITHUB_TOKEN to allow deployment to GitHub Pages
permissions:
contents: read
pages: write
id-token: write

# Allow only one concurrent deployment, skipping runs queued between the run in-progress and latest queued.
# However, do NOT cancel in-progress runs as we want to allow these production deployments to complete.
concurrency:
group: "pages"
cancel-in-progress: false
name: Go Test and Coverage

on:
push:
pull_request:
workflow_dispatch:

defaults:
run:
shell: bash

jobs:
check:
test-and-coverage:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Setup Go
uses: actions/setup-go@v4
with:
go-version-file: "go.mod"

- run: go test ./...
- name: Run Tests
run: go test ./...

- name: Generate Test Coverage
run: go test -cover ./...
7 changes: 5 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
!rendered/
site/rendered/
anna
!anna/
*.exe
Expand All @@ -8,3 +6,8 @@ anna
ssg/
*.txt
dist/

#Test directories
**/rendered/
test/**/static/
test/**/got_sitemap.xml
25 changes: 11 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,31 +17,28 @@ A static site generator in go

Inspired by [Hugo](https://gohugo.io) and [Saaru](https://github.com/anirudhRowjee/saaru), this static site generator aims to take performance to the next level with parallel rendering, live reload and so much more, all in Go.

> Pronounced: `/ɐnːɐ/` which means rice in Kannada 🍚
Pronounced: `/ɐnːɐ/` which means rice 🍚 in Kannada

This project is a part of the ACM PESU-ECC's yearly [AIEP](https://acmpesuecc.github.io/aiep) program, and is maintained by [Adhesh Athrey](https://github.com/DedLad), [Nathan Paul](https://github.com/polarhive), [Anirudh Sudhir](https://github.com/anirudhsudhir), and [Aditya Hegde](https://github.com/bwaklog)
> This project is a part of the ACM PESU-ECC's yearly [AIEP](https://acmpesuecc.github.io/aiep) program, and is maintained by [Adhesh Athrey](https://github.com/DedLad), [Nathan Paul](https://github.com/polarhive), [Anirudh Sudhir](https://github.com/anirudhsudhir), and [Aditya Hegde](https://github.com/bwaklog)
---

## Install
## Get Started

```sh
go run github.com/acmpesuecc/[email protected]
```
> If you don't have a site dir with the pre-requisite layout template; anna proceeds to fetch the default site dir from our GitHub repository
Alternatively, clone our repository to build the latest version of anna:

```sh
git clone github.com/acmpesuecc/anna --depth=1; cd anna
go run .
```

### Detailed documentation of our SSG can be found [here](https://anna-docs.netlify.app/)
## Contributing to Anna

---
Detailed documentation for our SSG can be found: [here](https://anna-docs.netlify.app/)

## Flags
If you have git installed, clone our repository and build against the latest commit

```sh
git clone github.com/acmpesuecc/anna; cd anna
go build
```
```text
Usage:
anna [flags]
Expand Down
25 changes: 14 additions & 11 deletions cmd/anna/anna.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,24 @@ import (
type Cmd struct {
RenderDrafts bool
Addr string
LiveReload bool
}

func (cmd *Cmd) VanillaRender() {
// Defining Engine and Parser Structures
p := parser.Parser{
Templates: make(map[template.URL]parser.TemplateData),
TagsMap: make(map[string][]parser.TemplateData),
TagsMap: make(map[template.URL][]parser.TemplateData),
ErrorLogger: log.New(os.Stderr, "ERROR\t", log.Ldate|log.Ltime|log.Lshortfile),
RenderDrafts: cmd.RenderDrafts,
LiveReload: cmd.LiveReload,
}

e := engine.Engine{
Templates: make(map[template.URL]parser.TemplateData),
TagsMap: make(map[string][]parser.TemplateData),
ErrorLogger: log.New(os.Stderr, "ERROR\t", log.Ldate|log.Ltime|log.Lshortfile),
}
e.DeepDataMerge.Templates = make(map[template.URL]parser.TemplateData)
e.DeepDataMerge.TagsMap = make(map[template.URL][]parser.TemplateData)

helper := helpers.Helper{
ErrorLogger: e.ErrorLogger,
Expand All @@ -47,26 +50,26 @@ func (cmd *Cmd) VanillaRender() {
p.ParseRobots(helpers.SiteDataPath+"layout/robots.txt", helpers.SiteDataPath+"rendered/robots.txt")
p.ParseLayoutFiles()

e.Templates = p.Templates
e.TagsMap = p.TagsMap
e.LayoutConfig = p.LayoutConfig
e.Posts = p.Posts
e.DeepDataMerge.Templates = p.Templates
e.DeepDataMerge.TagsMap = p.TagsMap
e.DeepDataMerge.LayoutConfig = p.LayoutConfig
e.DeepDataMerge.Posts = p.Posts

e.GenerateSitemap(helpers.SiteDataPath + "rendered/sitemap.xml")
e.GenerateFeed()
e.GenerateJSONIndex(helpers.SiteDataPath)
helper.CopyDirectoryContents(helpers.SiteDataPath+"static/", helpers.SiteDataPath+"rendered/static/")

sort.Slice(e.Posts, func(i, j int) bool {
return e.Posts[i].Frontmatter.Date > e.Posts[j].Frontmatter.Date
sort.Slice(e.DeepDataMerge.Posts, func(i, j int) bool {
return e.DeepDataMerge.Posts[i].Frontmatter.Date > e.DeepDataMerge.Posts[j].Frontmatter.Date
})

templ, err := template.ParseGlob(helpers.SiteDataPath + "layout/*.layout")
templ, err := template.ParseGlob(helpers.SiteDataPath + "layout/*.html")
if err != nil {
e.ErrorLogger.Fatalf("%v", err)
}

templ, err = templ.ParseGlob(helpers.SiteDataPath + "layout/partials/*.layout")
templ, err = templ.ParseGlob(helpers.SiteDataPath + "layout/partials/*.html")
if err != nil {
e.ErrorLogger.Fatalf("%v", err)
}
Expand Down
35 changes: 34 additions & 1 deletion cmd/anna/livereload.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,16 @@ import (
"net/http"
"os"
"path/filepath"
"sync/atomic"
"time"

"github.com/acmpesuecc/anna/pkg/helpers"
)

var reloadPage = make(chan struct{})

var countRequests atomic.Int32

type liveReload struct {
errorLogger *log.Logger
fileTimes map[string]time.Time
Expand Down Expand Up @@ -43,6 +48,7 @@ func (cmd *Cmd) StartLiveReload() {
for _, rootDir := range lr.rootDirs {
if lr.traverseDirectory(rootDir) {
cmd.VanillaRender()
reloadPage <- struct{}{}
}
}
if !lr.serverRunning {
Expand Down Expand Up @@ -97,8 +103,35 @@ func (lr *liveReload) checkFile(path string, modTime time.Time) bool {

func (lr *liveReload) startServer(addr string) {
fmt.Print("Serving content at: http://localhost:", addr, "\n")
err := http.ListenAndServe(":"+addr, http.FileServer(http.Dir(helpers.SiteDataPath+"./rendered")))
http.Handle("/", http.FileServer(http.Dir(helpers.SiteDataPath+"./rendered")))
http.HandleFunc("/events", eventsHandler)
err := http.ListenAndServe(":"+addr, nil)
if err != nil {
lr.errorLogger.Fatal(err)
}
}

func eventsHandler(w http.ResponseWriter, r *http.Request) {
countRequests.Add(1)

// Set CORS headers to allow all origins.
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Expose-Headers", "Content-Type")

w.Header().Set("Content-Type", "text/event-stream")
w.Header().Set("Cache-Control", "no-cache")
w.Header().Set("Connection", "keep-alive")

if countRequests.Load() == 1 {
<-reloadPage
} else {
countRequests.Store(countRequests.Load() - 1)
return
}

event := "event:\ndata:\n\n"
w.Write([]byte(event))
w.(http.Flusher).Flush()

countRequests.Store(countRequests.Load() - 1)
}
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/acmpesuecc/anna

go 1.22.0
go 1.22.2

require (
github.com/PuerkitoBio/goquery v1.9.1
Expand All @@ -20,7 +20,7 @@ require (
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-runewidth v0.0.15 // indirect
github.com/rivo/uniseg v0.4.7 // indirect
github.com/rogpeppe/go-internal v1.11.0 // indirect
github.com/rogpeppe/go-internal v1.12.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/net v0.24.0 // indirect
golang.org/x/sys v0.19.0 // indirect
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJ
github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M=
github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA=
github.com/rogpeppe/go-internal v1.12.0 h1:exVL4IDcn6na9z1rAb56Vxr+CgyK3nn3O+epU5NdKM8=
github.com/rogpeppe/go-internal v1.12.0/go.mod h1:E+RYuTGaKKdloAfM02xzb0FW3Paa99yedzYV+kq4uf4=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/spf13/cobra v1.8.0 h1:7aJaZx1B85qltLMc546zn58BxxfZdR/W22ej9CFoEf0=
github.com/spf13/cobra v1.8.0/go.mod h1:WXLWApfZ71AjXPya3WOlMsY9yMs7YeiHhFVlvLyhcho=
Expand Down
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ func main() {
}

if serve {
annaCmd.LiveReload = true
annaCmd.StartLiveReload()
}

Expand Down
Loading

0 comments on commit fe8df9c

Please sign in to comment.