From 2c055e3ca001ee6064677a0db16172148e5edcef Mon Sep 17 00:00:00 2001 From: Eddy Pedroni Date: Tue, 28 Jan 2020 22:06:16 +0100 Subject: Added tentative page rendering, outline main function --- config.toml | 2 +- entry.go | 7 ++++--- main.go | 39 +++++++++++++++++++++++++++++++++++++++ markdown.go | 17 +++++++++++++++++ page.go | 42 ++++++++++++++++++++++++++++++++++++++++++ template.html | 10 +++++----- utils.go | 12 ++++++------ 7 files changed, 114 insertions(+), 15 deletions(-) create mode 100644 markdown.go diff --git a/config.toml b/config.toml index bae7ca2..b26d4c3 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" +docsRoot = "/home/eddy/projects/logbook/guitar" # All pages are generated from this template templateFile = "/home/eddy/projects/godocs/template.html" diff --git a/entry.go b/entry.go index 1ee123a..d8f488f 100644 --- a/entry.go +++ b/entry.go @@ -1,6 +1,7 @@ package main import ( + "fmt" "os" "path/filepath" "regexp" @@ -22,12 +23,13 @@ type logEntry struct { year int } -func findEntries(rootPath string, cfg config) (entries []Renderer, err error) { +func findEntries(cfg config) (entries []Renderer, err error) { baseEntryRegex := regexp.MustCompile(".*\\.md") logEntryRegex := regexp.MustCompile("(20[0-9]{2})-([0-9]{2})-([0-9]{2}).*\\.md") - err = filepath.Walk(rootPath, func(p string, f os.FileInfo, e error) error { + err = filepath.Walk(cfg.DocsRoot, func(p string, f os.FileInfo, e error) error { if !f.IsDir() && baseEntryRegex.MatchString(f.Name()) { + fmt.Println(p) newBaseEntry := entry{name: f.Name(), fullPath: p} if logEntryRegex.MatchString(f.Name()) { @@ -50,4 +52,3 @@ func findEntries(rootPath string, cfg config) (entries []Renderer, err error) { }) return } - diff --git a/main.go b/main.go index 7905807..cab9fcc 100644 --- a/main.go +++ b/main.go @@ -1,5 +1,44 @@ package main +import ( + "fmt" + "os" +) + +type templateData struct { + SiteTitle string + Pages map[string]page + Index int +} + func main() { + // show usage if config file is missing + if len(os.Args) < 2 { + fmt.Println("Usage: godocs ") + os.Exit(1) + } + + // read config file specified in the command line + cfg, err := readConfig(os.Args[1]) + if err != nil { + panic(err) + } + + // collect all entries + entries, err := findEntries(cfg) + if err != nil { + panic(err) + } + + // render all entries + var pages = make(map[string]page) + for _, e := range entries { + fmt.Println("Processing page") + e.render(pages) + fmt.Println(pages) + fmt.Println("**************") + } + fmt.Println("----------------------------------------------------------------------------------------------") + fmt.Println(pages) } diff --git a/markdown.go b/markdown.go new file mode 100644 index 0000000..4e1d8a3 --- /dev/null +++ b/markdown.go @@ -0,0 +1,17 @@ +package main + +import ( + "gopkg.in/russross/blackfriday.v2" + "io/ioutil" +) + +// Takes the path to a markdown file and outputs the processed HTML in a string +func processMarkdown(path string) (md string, err error) { + data, err := ioutil.ReadFile(path) + if err != nil { + return + } + + md = string(blackfriday.Run(data)) + return +} diff --git a/page.go b/page.go index 207a80b..f9bb5b1 100644 --- a/page.go +++ b/page.go @@ -1,7 +1,49 @@ package main +//import "fmt" + /* * This file contains all the code related to turning entries into HTML pages */ +type page struct { + pageName string + content []string +} + +func (p *page) appendContent(c string) { + p.content = append(p.content, c) + //fmt.Println("---------------------------------------------------------------------------------------------------------") + //fmt.Println(p) +} + type Renderer interface { + render(pages map[string]page) (err error) +} + +func (e entry) render(pages map[string]page) (err error) { + md, err := processMarkdown(e.fullPath) + if err != nil { + return + } + + p := page{pageName: e.name, content: []string{md}} + + pages[e.name] = p + return +} + +func (le logEntry) render(pages map[string]page) (err error) { + md, err := processMarkdown(le.baseEntry.fullPath) + if err != nil { + return + } + + if val, ok := pages["log"]; ok { + val.appendContent(md) + pages["log"] = val + } else { + p := page{pageName: "Log", content: []string{md}} + pages["log"] = p + } + return } diff --git a/template.html b/template.html index 7dcbf5b..1e56d6f 100644 --- a/template.html +++ b/template.html @@ -1,7 +1,7 @@ - Docs + {{.SiteTitle}} - Docs @@ -10,17 +10,17 @@
{{.SiteTitle}}
- {{range .Entries}} + {{range .Content}}
- {{.Content}} + {{.}}
{{end}}
diff --git a/utils.go b/utils.go index 7157072..b5b3f02 100644 --- a/utils.go +++ b/utils.go @@ -6,15 +6,15 @@ import ( ) type config struct { - docsRoot string - templateFile string - targetDir string - cssFile string - baseUrl string + DocsRoot string + TemplateFile string + TargetDir string + CssFile string + BaseUrl string } func readConfig(file string) (cfg config, err error) { _, err = toml.DecodeFile(file, &cfg) - cfg.baseUrl = strings.TrimSuffix(cfg.baseUrl, "/") + cfg.BaseUrl = strings.TrimSuffix(cfg.BaseUrl, "/") return } -- cgit v1.2.3