From ed306e653641769ffb8d084ea7717f18a5a9239c Mon Sep 17 00:00:00 2001 From: Eddy Pedroni Date: Sat, 1 Feb 2020 15:55:15 +0100 Subject: Refactored, all sites are generated now --- config.toml | 2 +- entry.go | 10 ++++++++-- main.go | 26 ++++++++++++-------------- nav.go | 9 ++++----- page.go | 6 +++--- site.go | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ template.go | 38 ++++++++++++-------------------------- 7 files changed, 91 insertions(+), 51 deletions(-) create mode 100644 site.go diff --git a/config.toml b/config.toml index b26d4c3..bae7ca2 100644 --- a/config.toml +++ b/config.toml @@ -2,7 +2,7 @@ # These values are intended to generate a temporary, test version of the website that is navigable with a web browser # This is where the logbook entries are actually stored -docsRoot = "/home/eddy/projects/logbook/guitar" +docsRoot = "/home/eddy/projects/logbook" # All pages are generated from this template templateFile = "/home/eddy/projects/godocs/template.html" diff --git a/entry.go b/entry.go index ad4cca3..189014f 100644 --- a/entry.go +++ b/entry.go @@ -5,6 +5,7 @@ import ( "path/filepath" "regexp" "strconv" + "fmt" ) /* @@ -23,11 +24,12 @@ type logEntry struct { year int } -func findEntries(cfg config) (entries []Renderer, err error) { +func findEntries(root string) (entries []Renderer) { baseEntryRegex := regexp.MustCompile("([A-z\\-]+)\\.md") logEntryRegex := regexp.MustCompile("(20[0-9]{2})-([0-9]{2})-([0-9]{2})-([A-z\\-]+)\\.md") - err = filepath.Walk(cfg.DocsRoot, func(p string, f os.FileInfo, e error) error { + fmt.Println(root) + err := filepath.Walk(root, func(p string, f os.FileInfo, e error) error { if !f.IsDir() && baseEntryRegex.MatchString(f.Name()) { baseCaptures := baseEntryRegex.FindStringSubmatch(f.Name()) newBaseEntry := entry{name: nameify(baseCaptures[1]), @@ -52,5 +54,9 @@ func findEntries(cfg config) (entries []Renderer, err error) { } return nil }) + if err != nil { + panic(err) + } + return } diff --git a/main.go b/main.go index 7ed9a77..075caa4 100644 --- a/main.go +++ b/main.go @@ -18,22 +18,20 @@ func main() { panic(err) } - // collect all entries - entries, err := findEntries(cfg) - if err != nil { - panic(err) - } + // gather all sites in docsRoot + sites := getSites(cfg) - // render all entries - pages := createPageMap(entries) - fmt.Println("Pages:") - fmt.Println(pages) + for _, site := range sites { + // collect all entries + entries := findEntries(site.sourcePath) - // create navigation item slice - navItems := createNavSlice(pages, cfg) - fmt.Println("Navigation:") - fmt.Println(navItems) + // render all entries + createPageMap(&site, entries) - generatePages(pages, navItems, cfg) + // create navigation item slice + createNavSlice(&site, cfg) + // output the site + generateSite(site, cfg) + } } diff --git a/nav.go b/nav.go index e7c9579..39b5b64 100644 --- a/nav.go +++ b/nav.go @@ -7,10 +7,9 @@ type navItem struct { Text string } -func createNavSlice(pages map[string]page, cfg config) (nav []navItem) { - for k, v := range pages { - newNavItem := navItem{AbsoluteUrl: cfg.BaseUrl + "/" + k, Text: v.pageName} - nav = append(nav, newNavItem) +func createNavSlice(site *siteData, cfg config) { + for k, v := range site.pages { + newNavItem := navItem{AbsoluteUrl: site.baseUrl + "/" + k, Text: v.pageName} + site.nav = append(site.nav, newNavItem) } - return } diff --git a/page.go b/page.go index 0d0b1dd..5226df8 100644 --- a/page.go +++ b/page.go @@ -46,8 +46,8 @@ func (le logEntry) render(pages map[string]page) (err error) { return } -func createPageMap(entries []Renderer) (pages map[string]page) { - pages = make(map[string]page) +func createPageMap(site *siteData, entries []Renderer) { + pages := make(map[string]page) for _, e := range entries { err := e.render(pages) if err != nil { @@ -55,5 +55,5 @@ func createPageMap(entries []Renderer) (pages map[string]page) { } } - return + site.pages = pages } diff --git a/site.go b/site.go new file mode 100644 index 0000000..8a9f08d --- /dev/null +++ b/site.go @@ -0,0 +1,51 @@ +package main + +import ( + "io/ioutil" + "path/filepath" +) + +type siteData struct { + name string + rawName string + baseUrl string + outputPath string + sourcePath string + pages map[string]page + nav []navItem +} + +func getSites(cfg config) (sites []siteData) { + files, err := ioutil.ReadDir(cfg.DocsRoot) + if err != nil { + panic(err) + } + + for _, f := range files { + if f.IsDir() && f.Name() != ".git" { + newSite := siteData{ + name: nameify(f.Name()), + rawName: f.Name(), + baseUrl: filepath.Join(cfg.BaseUrl, f.Name()), + outputPath: filepath.Join(cfg.TargetDir, f.Name()), + sourcePath: filepath.Join(cfg.DocsRoot, f.Name())} + sites = append(sites, newSite) + } + } + + return +} + +func generateSite(site siteData, cfg config) { + template := loadTemplate(cfg) + templData := templateData{SiteTitle: site.name, + StylesheetUrl: cfg.CssFile, + Nav: site.nav} + for k, v := range site.pages { + templData.Content = v.content + err := applyTemplate(filepath.Join(site.outputPath, k), templData, template) + if err != nil { + panic(err) + } + } +} diff --git a/template.go b/template.go index ed38d2c..413c15f 100644 --- a/template.go +++ b/template.go @@ -15,6 +15,18 @@ type templateData struct { Content []string } +func loadTemplate(cfg config) (templ *template.Template) { + tmpl_raw, err := ioutil.ReadFile(cfg.TemplateFile) + if err != nil { + panic(err) + } + templ, err = template.New("template").Parse(string(tmpl_raw)) + if err != nil { + panic(err) + } + return +} + func applyTemplate(dir string, data templateData, tmpl *template.Template) error { // ensure directory exists os.MkdirAll(strings.ToLower(dir), 0755) @@ -35,29 +47,3 @@ func applyTemplate(dir string, data templateData, tmpl *template.Template) error return nil } - -func loadTemplate(cfg config) (templ *template.Template) { - tmpl_raw, err := ioutil.ReadFile(cfg.TemplateFile) - if err != nil { - panic(err) - } - templ, err = template.New("template").Parse(string(tmpl_raw)) - if err != nil { - panic(err) - } - return -} - -func generatePages(pages map[string]page, nav []navItem, cfg config) { - template := loadTemplate(cfg) - data := templateData{SiteTitle: "TestTitle", - StylesheetUrl: cfg.CssFile, - Nav: nav} - for k, v := range pages { - data.Content = v.content - err := applyTemplate("/tmp/tempgodocs/"+k, data, template) - if err != nil { - panic(err) - } - } -} -- cgit v1.2.3