Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: update docs, add helper/ and note tests and refactor error handling #107

Merged
merged 3 commits into from
May 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
anna
!anna/
*.exe


**/.DS_Store
*.prof
ssg/
*.txt
dist/
.idea/

#Test directories
**/rendered/
test/**/static/
test/**/got_sitemap.xml
test/**/got_sitemap.xml
3 changes: 3 additions & 0 deletions cmd/anna/anna.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ func (cmd *Cmd) VanillaRender() {
// Copies the contents of the 'static/' directory to 'rendered/'
helper.CopyDirectoryContents(helpers.SiteDataPath+"static/", helpers.SiteDataPath+"rendered/static/")

// Copies the contents of the 'static/' directory to 'rendered/'
helper.CopyDirectoryContents(helpers.SiteDataPath+"public/", helpers.SiteDataPath+"rendered/")

e.GenerateSitemap(helpers.SiteDataPath + "rendered/sitemap.xml")
e.GenerateFeed()
e.GenerateJSONIndex(helpers.SiteDataPath)
Expand Down
5 changes: 4 additions & 1 deletion cmd/anna/livereload.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,10 @@ func eventsHandler(w http.ResponseWriter, r *http.Request) {
}

event := "event:\ndata:\n\n"
w.Write([]byte(event))
_, err := w.Write([]byte(event))
if err != nil {
log.Fatal(err)
}
w.(http.Flusher).Flush()

countRequests.Store(countRequests.Load() - 1)
Expand Down
7 changes: 6 additions & 1 deletion cmd/anna/validate_html.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,12 @@ func parseHTMLFile(path string) error {
if err != nil {
return err
}
defer file.Close()
defer func() {
err = file.Close()
if err != nil {
log.Fatal(err)
}
}()

// Load the HTML content into a GoQuery document
doc, err := goquery.NewDocumentFromReader(file)
Expand Down
10 changes: 8 additions & 2 deletions cmd/anna/wizard.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package anna
import (
"context"
"encoding/json"
"errors"
"fmt"
"log"
"net/http"
Expand Down Expand Up @@ -38,7 +39,7 @@ func (ws *WizardServer) Start() {
fs := http.FileServer(http.Dir("./site/static/wizard"))
http.Handle("/", fs)
fmt.Printf("Wizard is running at: http://localhost%s\n", ws.server.Addr)
if err := ws.server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
if err := ws.server.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
log.Fatalf("Could not start server: %v", err)
}
}
Expand Down Expand Up @@ -76,7 +77,12 @@ func writeConfigToFile(config Config) error {
if err != nil {
return err
}
defer file.Close()
defer func() {
err = file.Close()
if err != nil {
log.Fatal(err)
}
}()

// Encode the config into YAML format and write it to the file.
if err := yaml.NewEncoder(file).Encode(&config); err != nil {
Expand Down
8 changes: 0 additions & 8 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ go 1.22.2

require (
github.com/PuerkitoBio/goquery v1.9.1
github.com/cheggaaa/pb/v3 v3.1.5
github.com/mangoumbrella/goldmark-figure v1.0.0
github.com/spf13/cobra v1.8.0
github.com/yuin/goldmark v1.7.1
Expand All @@ -15,18 +14,11 @@ require (
)

require (
github.com/VividCortex/ewma v1.2.0 // indirect
github.com/andybalholm/cascadia v1.3.2 // indirect
github.com/fatih/color v1.16.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/kr/pretty v0.3.1 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
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.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
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
)
18 changes: 0 additions & 18 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
github.com/PuerkitoBio/goquery v1.9.1 h1:mTL6XjbJTZdpfL+Gwl5U2h1l9yEkJjhmlTeV9VPW7UI=
github.com/PuerkitoBio/goquery v1.9.1/go.mod h1:cW1n6TmIMDoORQU5IU/P1T3tGFunOeXEpGP2WHRwkbY=
github.com/VividCortex/ewma v1.2.0 h1:f58SaIzcDXrSy3kWaHNvuJgJ3Nmz59Zji6XoJR/q1ow=
github.com/VividCortex/ewma v1.2.0/go.mod h1:nz4BbCtbLyFDeC9SUHbtcT5644juEuWfUAUnGx7j5l4=
github.com/andybalholm/cascadia v1.3.2 h1:3Xi6Dw5lHF15JtdcmAHD3i1+T8plmv7BQ/nsViSLyss=
github.com/andybalholm/cascadia v1.3.2/go.mod h1:7gtRlve5FxPPgIgX36uWBX58OdBsSS6lUvCFb+h7KvU=
github.com/cheggaaa/pb/v3 v3.1.5 h1:QuuUzeM2WsAqG2gMqtzaWithDJv0i+i6UlnwSCI4QLk=
github.com/cheggaaa/pb/v3 v3.1.5/go.mod h1:CrxkeghYTXi1lQBEI7jSn+3svI3cuc19haAj6jM60XI=
github.com/chromedp/cdproto v0.0.0-20230220211738-2b1ec77315c9 h1:wMSvdj3BswqfQOXp2R1bJOAE7xIQLt2dlMQDMf836VY=
github.com/chromedp/cdproto v0.0.0-20230220211738-2b1ec77315c9/go.mod h1:GKljq0VrfU4D5yc+2qA6OVr8pmO/MBbPEWqWQ/oqGEs=
github.com/chromedp/chromedp v0.9.1 h1:CC7cC5p1BeLiiS2gfNNPwp3OaUxtRMBjfiw3E3k6dFA=
Expand All @@ -16,8 +12,6 @@ github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46t
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fatih/color v1.16.0 h1:zmkK9Ngbjj+K0yRhTVONQh1p/HknKYSlNT+vZCzyokM=
github.com/fatih/color v1.16.0/go.mod h1:fL2Sau1YI5c0pdGEVCbKQbLXB6edEj1ZgiY4NijnWvE=
github.com/gobwas/httphead v0.1.0 h1:exrUm0f4YX0L7EBwZHuCF4GDp8aJfVeBrlLQrs6NqWU=
github.com/gobwas/httphead v0.1.0/go.mod h1:O/RXo79gxV8G+RqlR/otEwx4Q36zl9rqC5u12GKvMCM=
github.com/gobwas/pool v0.2.1 h1:xfeeEhW7pwmX8nuLVlqbzVc7udMDrwetjEv+TZIz1og=
Expand All @@ -39,19 +33,9 @@ github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0
github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc=
github.com/mangoumbrella/goldmark-figure v1.0.0 h1:L+ebP73dET0a2n68PV7m5oFJmwwH5GzRIJLgszR3fwo=
github.com/mangoumbrella/goldmark-figure v1.0.0/go.mod h1:iIL+fhdmCQDpE0l/TKtGhokWzIbo5lo/Y2OIAcx6usI=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U=
github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
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.12.0 h1:exVL4IDcn6na9z1rAb56Vxr+CgyK3nn3O+epU5NdKM8=
github.com/rogpeppe/go-internal v1.12.0/go.mod h1:E+RYuTGaKKdloAfM02xzb0FW3Paa99yedzYV+kq4uf4=
Expand Down Expand Up @@ -90,9 +74,7 @@ golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.19.0 h1:q5f1RH2jigJ1MoAWp2KTp3gm5zAGFUTarQZ5U386+4o=
golang.org/x/sys v0.19.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
Expand Down
4 changes: 3 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ func main() {
server := anna.NewWizardServer(":8080")
go server.Start()
<-anna.FormSubmittedCh // wait for response
server.Stop() // stop the server
if err := server.Stop(); err != nil {
log.Println(err)
}
annaCmd.VanillaRender()
annaCmd.StartLiveReload()
}
Expand Down
35 changes: 29 additions & 6 deletions pkg/engine/anna_engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func (e *Engine) RenderTags(fileOutPath string, templ *template.Template) {
go func(tag template.URL, taggedTemplates []parser.TemplateData) {
defer wg.Done()

e.RenderPage(fileOutPath, template.URL(tag), templ, "tag-subpage")
e.RenderPage(fileOutPath, tag, templ, "tag-subpage")
}(tag, taggedTemplates)
}

Expand All @@ -105,7 +105,12 @@ func (e *Engine) GenerateNoteJSONIdex(outFilePath string) {
e.ErrorLogger.Fatal(err)
}

defer jsonFile.Close()
defer func() {
err = jsonFile.Close()
if err != nil {
e.ErrorLogger.Fatal(err)
}
}()

jsonMergedMarshaledData, err := json.Marshal(e.DeepDataMerge.Notes)
if err != nil {
Expand All @@ -123,11 +128,16 @@ func (e *Engine) GenerateJSONIndex(outFilePath string) {
// It extracts data from the e.Templates slice
// The index.json file is created during every VanillaRender()

jsonFile, err := os.Create(outFilePath + "/rendered/static/index.json")
jsonFile, err := os.Create(outFilePath + "rendered/static/index.json")
if err != nil {
e.ErrorLogger.Fatal(err)
}
defer jsonFile.Close()
defer func() {
err = jsonFile.Close()
if err != nil {
e.ErrorLogger.Fatal(err)
}
}()

// Copying contents from e.Templates to new JsonMerged struct
jsonIndexTemplate := make(map[template.URL]JSONIndexTemplate)
Expand Down Expand Up @@ -186,7 +196,14 @@ func (e *Engine) GenerateSitemap(outFilePath string) {
if err != nil {
e.ErrorLogger.Fatal(err)
}
defer outputFile.Close()

defer func() {
err = outputFile.Close()
if err != nil {
e.ErrorLogger.Fatal(err)
}
}()

_, err = outputFile.Write(buffer.Bytes())
if err != nil {
e.ErrorLogger.Fatal(err)
Expand Down Expand Up @@ -220,7 +237,13 @@ func (e *Engine) GenerateFeed() {
if err != nil {
e.ErrorLogger.Fatal(err)
}
defer outputFile.Close()
defer func() {
err = outputFile.Close()
if err != nil {
e.ErrorLogger.Fatal(err)
}
}()

_, err = outputFile.Write(buffer.Bytes())
if err != nil {
e.ErrorLogger.Fatal(err)
Expand Down
64 changes: 32 additions & 32 deletions pkg/engine/anna_engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,47 +67,47 @@ func TestRenderTags(t *testing.T) {
e.RenderTags(fileOutPath, templ)

t.Run("render tag.html", func(t *testing.T) {
got_tags_file, err := os.ReadFile(TestDirPath + "render_tags/rendered/tags.html")
gotTagsFile, err := os.ReadFile(TestDirPath + "render_tags/rendered/tags.html")
if err != nil {
t.Errorf("%v", err)
}

want_tags_file, err := os.ReadFile(TestDirPath + "render_tags/want_tags.html")
wantTagsFile, err := os.ReadFile(TestDirPath + "render_tags/want_tags.html")
if err != nil {
t.Errorf("%v", err)
}

if !slices.Equal(got_tags_file, want_tags_file) {
if !slices.Equal(gotTagsFile, wantTagsFile) {
t.Errorf("The expected and generated tags.html can be found in test/engine/render_tags/rendered/")
}
})

t.Run("render tag-subpage.html", func(t *testing.T) {
got_blogs_file, err := os.ReadFile(TestDirPath + "render_tags/rendered/tags/blogs.html")
gotBlogsFile, err := os.ReadFile(TestDirPath + "render_tags/rendered/tags/blogs.html")
if err != nil {
t.Errorf("%v", err)
}

want_blogs_file, err := os.ReadFile(TestDirPath + "render_tags/want_blogs_tags.html")
wantBlogsFile, err := os.ReadFile(TestDirPath + "render_tags/want_blogs_tags.html")
if err != nil {
t.Errorf("%v", err)
}

if !slices.Equal(got_blogs_file, want_blogs_file) {
if !slices.Equal(gotBlogsFile, wantBlogsFile) {
t.Errorf("The expected and generated blogs.html tag-subpage can be found in test/engine/render_tags/rendered/tags/")
}

got_tech_file, err := os.ReadFile(TestDirPath + "render_tags/rendered/tags/tech.html")
gotTechFile, err := os.ReadFile(TestDirPath + "render_tags/rendered/tags/tech.html")
if err != nil {
t.Errorf("%v", err)
}

want_tech_file, err := os.ReadFile(TestDirPath + "render_tags/want_tech_tags.html")
wantTechFile, err := os.ReadFile(TestDirPath + "render_tags/want_tech_tags.html")
if err != nil {
t.Errorf("%v", err)
}

if !slices.Equal(got_tech_file, want_tech_file) {
if !slices.Equal(gotTechFile, wantTechFile) {
t.Errorf("The expected and generated tech.html tag-subpage can be found in test/engine/render_tags/rendered/tags/")
}
})
Expand All @@ -132,34 +132,34 @@ func TestGenerateMergedJson(t *testing.T) {
},
}

e.GenerateJSONIndex(TestDirPath + "json_index_test")
e.GenerateJSONIndex(TestDirPath + "json_index_test/")

got_json, err := os.ReadFile(TestDirPath + "/json_index_test/rendered/static/index.json")
gotJson, err := os.ReadFile(TestDirPath + "/json_index_test/rendered/static/index.json")
if err != nil {
t.Errorf("%v", err)
}

want_json, err := os.ReadFile(TestDirPath + "/json_index_test/want_index.json")
wantJson, err := os.ReadFile(TestDirPath + "/json_index_test/want_index.json")
if err != nil {
t.Errorf("%v", err)
}

got_json = bytes.TrimSpace(got_json)
want_json = bytes.TrimSpace(want_json)
gotJson = bytes.TrimSpace(gotJson)
wantJson = bytes.TrimSpace(wantJson)

if !slices.Equal(got_json, want_json) {
if !slices.Equal(gotJson, wantJson) {
t.Errorf("The expected and generated json can be found in test/engine/json_index_test")
}
})
}

func TestGenerateSitemap(t *testing.T) {
t.Run("render sitemap.xml", func(t *testing.T) {
engine := engine.Engine{
testEngine := engine.Engine{
ErrorLogger: log.New(os.Stderr, "TEST ERROR\t", log.Ldate|log.Ltime|log.Lshortfile),
}
engine.DeepDataMerge.Templates = make(map[template.URL]parser.TemplateData)
engine.DeepDataMerge.TagsMap = make(map[template.URL][]parser.TemplateData)
testEngine.DeepDataMerge.Templates = make(map[template.URL]parser.TemplateData)
testEngine.DeepDataMerge.TagsMap = make(map[template.URL][]parser.TemplateData)

t1 := parser.TemplateData{
CompleteURL: "index.html",
Expand All @@ -182,35 +182,35 @@ func TestGenerateSitemap(t *testing.T) {
},
}

engine.DeepDataMerge.LayoutConfig.BaseURL = "example.org"
// setting up engine
engine.DeepDataMerge.Templates["index"] = t1
engine.DeepDataMerge.Templates["about"] = t2
engine.DeepDataMerge.Templates["research"] = t3
testEngine.DeepDataMerge.LayoutConfig.BaseURL = "example.org"
// setting up testEngine
testEngine.DeepDataMerge.Templates["index"] = t1
testEngine.DeepDataMerge.Templates["about"] = t2
testEngine.DeepDataMerge.Templates["research"] = t3

engine.GenerateSitemap(TestDirPath + "sitemap/got_sitemap.xml")
testEngine.GenerateSitemap(TestDirPath + "sitemap/got_sitemap.xml")

got_sitemap, err := os.ReadFile(TestDirPath + "sitemap/got_sitemap.xml")
gotSitemap, err := os.ReadFile(TestDirPath + "sitemap/got_sitemap.xml")
if err != nil {
t.Errorf("Error in reading the contents of got_sitemap.xml")
}

want_sitemap, err := os.ReadFile(TestDirPath + "sitemap/want_sitemap.xml")
wantSitemap, err := os.ReadFile(TestDirPath + "sitemap/want_sitemap.xml")
if err != nil {
t.Errorf("Error in reading the contents of _sitemap.xml")
}

got_sitemap_string := string(got_sitemap)
want_sitemap_string := string(want_sitemap)
got_sitemap_string = strings.TrimFunc(got_sitemap_string, func(r rune) bool {
gotSitemapString := string(gotSitemap)
wantSitemapString := string(wantSitemap)
gotSitemapString = strings.TrimFunc(gotSitemapString, func(r rune) bool {
return r == '\n' || r == '\t' || r == ' '
})
want_sitemap_string = strings.TrimFunc(want_sitemap_string, func(r rune) bool {
wantSitemapString = strings.TrimFunc(wantSitemapString, func(r rune) bool {
return r == '\n' || r == '\t' || r == ' '
})

if strings.Compare(got_sitemap_string, want_sitemap_string) == 0 {
t.Errorf("The expected and generated sitemap can be found in test/engine/sitemap/")
if strings.Compare(gotSitemapString, wantSitemapString) == 0 {
t.Errorf("The expected and generated sitemap can be found in test/testEngine/sitemap/")
}
})
}
Loading