Skip to content

Commit

Permalink
Merge branch 'main' into improve-wizard
Browse files Browse the repository at this point in the history
  • Loading branch information
polarhive authored Mar 30, 2024
2 parents 65ad39f + 041e7d3 commit 05dad42
Show file tree
Hide file tree
Showing 24 changed files with 339 additions and 204 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ anna
*.exe
**/.DS_Store
*.pprof
ssg/
*.txt
3 changes: 3 additions & 0 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,6 @@ pkg
- [ ] tests for `main.go`
- [x] Add status checks for PRs and fix netlify deploy preview
- [ ] Minimum test coverage of 60% for all packages
- [ ] Minimum test coverage of 60% for `engine`
- [ ] Minimum test coverage of 60% for `helper`
- [x] Minimum test coverage of 60% for `parser`
30 changes: 0 additions & 30 deletions cmd/anna/profiler.go.temp → cmd/anna/profiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,43 +8,13 @@ import (
"time"
)

// StartProfiling()
// startTime := time.Now()
// if prof {
// go func() {
// for {
// time.Sleep(5 * time.Second) //change as per needed
// PrintStats(time.Since(startTime))
// }
// }()
// }
// generator.StartLiveReload(addr)
// }
//

// if validateHTML {
// anna.ValidateHTMLContent()
// }
// // if prof {

// generator.RenderSite("")

// // elapsedTimesince := time.Since(startTime) //this didn't work for some reason and was giving negitive deviation
// // // elapsedTime := time.Now().Sub(startTime)

// // PrintStats(elapsedTimesince)
// // // PrintStats(elapsedTime)
// // defer StopProfiling()
// // }

func PrintStats(elapsedTime time.Duration) {
memStats := new(runtime.MemStats)
runtime.ReadMemStats(memStats)
log.Printf("Memory Usage: %d bytes", memStats.Alloc)
log.Printf("Time Elapsed: %s", elapsedTime)
cpuUsage := runtime.NumCPU()
threads := runtime.GOMAXPROCS(0)
// memStats := new(runtime.MemStats)
runtime.ReadMemStats(memStats)

log.Printf("Threads: %d", threads)
Expand Down
9 changes: 8 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"log"
"time"

"github.com/acmpesuecc/anna/cmd/anna"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -29,7 +30,13 @@ func main() {
}

if prof {
cmd.Println("TODO: To be filled later")
startTime := time.Now()
go anna.StartProfiling()

annaCmd.VanillaRender()
elapsedTime := time.Now().Sub(startTime)
go anna.PrintStats(elapsedTime)
defer anna.StopProfiling()
}

if validateHTML {
Expand Down
27 changes: 22 additions & 5 deletions pkg/engine/anna_engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ package engine
import (
"bytes"
"cmp"
"fmt"
"html/template"
"os"
"slices"
"sort"
"strings"
"time"

Expand Down Expand Up @@ -66,13 +68,28 @@ func (e *Engine) GenerateSitemap(outFilePath string) {
buffer.WriteString("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
buffer.WriteString("<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">\n")

// iterate over parsed markdown files
// Sorting templates by key
keys := make([]string, 0, len(e.Templates))
for k := range e.Templates {
keys = append(keys, string(k))
}
sort.Strings(keys)

tempTemplates := make(map[template.URL]parser.TemplateData)
for _, templateURL := range keys {
tempTemplates[template.URL(templateURL)] = e.Templates[template.URL(templateURL)]
}

e.Templates = tempTemplates

fmt.Println(e.LayoutConfig.BaseURL)
// Iterate over parsed markdown files
for _, templateData := range e.Templates {
url := e.LayoutConfig.BaseURL + "/" + templateData.FilenameWithoutExtension + ".html"
buffer.WriteString(" <url>\n")
buffer.WriteString(" <loc>" + url + "</loc>\n")
buffer.WriteString(" <lastmod>" + templateData.Frontmatter.Date + "</lastmod>\n")
buffer.WriteString(" </url>\n")
buffer.WriteString("\t<url>\n")
buffer.WriteString("\t\t<loc>" + url + "</loc>\n")
buffer.WriteString("\t\t<lastmod>" + templateData.Frontmatter.Date + "</lastmod>\n")
buffer.WriteString("\t</url>\n")
}
buffer.WriteString("</urlset>\n")
// helpers.SiteDataPath is the DirPath
Expand Down
154 changes: 76 additions & 78 deletions pkg/engine/anna_engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,20 +55,20 @@ func TestRenderTags(t *testing.T) {
},
}

templ, err := template.ParseFiles(TestDirPath+"engine/render_tags/tags_template.html", TestDirPath+"engine/render_tags/tags_subpage_template.html")
templ, err := template.ParseFiles(TestDirPath+"render_tags/tags_template.html", TestDirPath+"render_tags/tags_subpage_template.html")
if err != nil {
t.Errorf("%v", err)
}
e.RenderTags(fileOutPath, templ)

t.Run("render tag.html", func(t *testing.T) {

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

want_tags_file, err := os.ReadFile(TestDirPath + "engine/render_tags/want_tags.html")
want_tags_file, err := os.ReadFile(TestDirPath + "render_tags/want_tags.html")
if err != nil {
t.Errorf("%v", err)
}
Expand All @@ -80,12 +80,12 @@ func TestRenderTags(t *testing.T) {

t.Run("render tag-subpage.html", func(t *testing.T) {

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

want_blogs_file, err := os.ReadFile(TestDirPath + "engine/render_tags/want_blogs_tags.html")
want_blogs_file, err := os.ReadFile(TestDirPath + "render_tags/want_blogs_tags.html")
if err != nil {
t.Errorf("%v", err)
}
Expand All @@ -94,12 +94,12 @@ func TestRenderTags(t *testing.T) {
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 + "engine/render_tags/rendered/tags/tech.html")
got_tech_file, err := os.ReadFile(TestDirPath + "render_tags/rendered/tags/tech.html")
if err != nil {
t.Errorf("%v", err)
}

want_tech_file, err := os.ReadFile(TestDirPath + "engine/render_tags/want_tech_tags.html")
want_tech_file, err := os.ReadFile(TestDirPath + "render_tags/want_tech_tags.html")
if err != nil {
t.Errorf("%v", err)
}
Expand All @@ -110,74 +110,72 @@ func TestRenderTags(t *testing.T) {
})
}

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

// t1 := parser.TemplateData{
// FilenameWithoutExtension: "index",
// Frontmatter: parser.Frontmatter{
// Date: "2024-02-23",
// },
// }

// t2 := parser.TemplateData{
// FilenameWithoutExtension: "about",
// Frontmatter: parser.Frontmatter{
// Date: "2023-01-02",
// },
// }

// t3 := parser.TemplateData{
// FilenameWithoutExtension: "research",
// Frontmatter: parser.Frontmatter{
// Date: "2024-01-01",
// },
// }

// engine.LayoutConfig.BaseURL = "https://ssg-test-org.github.io"
// // setting up engine
// engine.Templates["index"] = t1
// engine.Templates["about"] = t2
// engine.Templates["research"] = t3

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

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

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

// // remove spaces and whitespace characters
// /*
// got_sitemap = []byte(strings.ReplaceAll(string(got_sitemap), " ", ""))
// want_sitemap = []byte(strings.ReplaceAll(string(want_sitemap), " ", ""))
// // replace all tabs
// got_sitemap = []byte(strings.ReplaceAll(string(got_sitemap), "\t", ""))
// want_sitemap = []byte(strings.ReplaceAll(string(want_sitemap), "\t", ""))

// got_sitemap = []byte(strings.ReplaceAll(string(got_sitemap), "\n", ""))
// want_sitemap = []byte(strings.ReplaceAll(string(want_sitemap), "\n", ""))

// fmt.Println(string(got_sitemap))
// fmt.Println(string(want_sitemap))

// fmt.Println(len(got_sitemap))
// fmt.Println(len(want_sitemap))

// if !reflect.DeepEqual(got_sitemap, want_sitemap) {
// t.Errorf("The expected and generated sitemap can be found in test/layout/sitemap/")
// }
// })
// }
// */
/*
func TestGenerateSitemap(t *testing.T) {
t.Run("render sitemap.xml", func(t *testing.T) {
engine := engine.Engine{
Templates: make(map[template.URL]parser.TemplateData),
TagsMap: make(map[string][]parser.TemplateData),
ErrorLogger: log.New(os.Stderr, "TEST ERROR\t", log.Ldate|log.Ltime|log.Lshortfile),
}
t1 := parser.TemplateData{
FilenameWithoutExtension: "index",
Frontmatter: parser.Frontmatter{
Date: "2024-02-23",
},
}
t2 := parser.TemplateData{
FilenameWithoutExtension: "about",
Frontmatter: parser.Frontmatter{
Date: "2024-02-23",
},
}
t3 := parser.TemplateData{
FilenameWithoutExtension: "research",
Frontmatter: parser.Frontmatter{
Date: "2024-02-23",
},
}
engine.LayoutConfig.BaseURL = "example.org"
// setting up engine
engine.Templates["index"] = t1
engine.Templates["about"] = t2
engine.Templates["research"] = t3
engine.GenerateSitemap(TestDirPath + "sitemap/got_sitemap.xml")
got_sitemap, 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")
if err != nil {
t.Errorf("Error in reading the contents of _sitemap.xml")
}
// Remove spaces and whitespace characters
// got_sitemap = []byte(strings.ReplaceAll(string(got_sitemap), " ", ""))
// want_sitemap = []byte(strings.ReplaceAll(string(want_sitemap), " ", ""))
// // Replace all tabs
// got_sitemap = []byte(strings.ReplaceAll(string(got_sitemap), "\t", ""))
// want_sitemap = []byte(strings.ReplaceAll(string(want_sitemap), "\t", ""))
// got_sitemap = []byte(strings.ReplaceAll(string(got_sitemap), "\n", ""))
// want_sitemap = []byte(strings.ReplaceAll(string(want_sitemap), "\n", ""))
// got_sitemap_string := string(got_sitemap)
// strings.TrimFunc(got_sitemap_string, func(r rune) bool {
// })
if strings.Compare(string(got_sitemap), string(want_sitemap)) != 0 {
t.Errorf("The expected and generated sitemap can be found in test/layout/sitemap/")
}
})
}
*/
12 changes: 6 additions & 6 deletions pkg/engine/engine_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,18 @@ func TestRenderUserDefinedPages(t *testing.T) {

t.Run("render a set of user defined pages", func(t *testing.T) {

templ, err := template.ParseFiles(TestDirPath + "engine/render_user_defined/template_input.html")
templ, err := template.ParseFiles(TestDirPath + "render_user_defined/template_input.html")
if err != nil {
t.Errorf("%v", err)
}
engine.RenderUserDefinedPages(TestDirPath+"engine/render_user_defined/", templ)
engine.RenderUserDefinedPages(TestDirPath+"render_user_defined/", templ)

want_index_file, err := os.ReadFile(TestDirPath + "engine/render_user_defined/want_index.html")
want_index_file, err := os.ReadFile(TestDirPath + "render_user_defined/want_index.html")
if err != nil {
t.Errorf("%v", err)
}

got_index_file, err := os.ReadFile(TestDirPath + "engine/render_user_defined/rendered/index.html")
got_index_file, err := os.ReadFile(TestDirPath + "render_user_defined/rendered/index.html")
if err != nil {
t.Errorf("%v", err)
}
Expand All @@ -51,12 +51,12 @@ func TestRenderUserDefinedPages(t *testing.T) {
t.Errorf("The expected and generated index.html can be found in test/engine/render_user_defined/rendered/")
}

want_post_hello, err := os.ReadFile(TestDirPath + "engine/render_user_defined/want_post_hello.html")
want_post_hello, err := os.ReadFile(TestDirPath + "render_user_defined/want_post_hello.html")
if err != nil {
t.Errorf("%v", err)
}

got_post_hello, err := os.ReadFile(TestDirPath + "engine/render_user_defined/rendered/posts/hello.html")
got_post_hello, err := os.ReadFile(TestDirPath + "render_user_defined/rendered/posts/hello.html")
if err != nil {
t.Errorf("%v", err)
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/engine/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/acmpesuecc/anna/pkg/parser"
)

const TestDirPath = "../../test/"
const TestDirPath = "../../test/engine/"

func TestRenderPage(t *testing.T) {
t.Run("render a single page while creating a new directory", func(t *testing.T) {
Expand Down Expand Up @@ -41,19 +41,19 @@ func TestRenderPage(t *testing.T) {
},
}

templ, err := template.ParseFiles(TestDirPath + "engine/render_page/template_input.html")
templ, err := template.ParseFiles(TestDirPath + "render_page/template_input.html")
if err != nil {
t.Errorf("%v", err)
}

engine.RenderPage(TestDirPath+"engine/render_page/", "posts/got.md", page, templ, "page")

got_file, err := os.ReadFile(TestDirPath + "engine/render_page/rendered/posts/got.html")
got_file, err := os.ReadFile(TestDirPath + "render_page/rendered/posts/got.html")
if err != nil {
t.Errorf("%v", err)
}

want_file, err := os.ReadFile(TestDirPath + "engine/render_page/want.html")
want_file, err := os.ReadFile(TestDirPath + "render_page/want.html")
if err != nil {
t.Errorf("%v", err)
}
Expand Down
Loading

0 comments on commit 05dad42

Please sign in to comment.