diff options
author | Eddy Pedroni <eddy@0xf7.com> | 2020-01-28 22:06:16 +0100 |
---|---|---|
committer | Eddy Pedroni <eddy@0xf7.com> | 2020-01-28 22:06:16 +0100 |
commit | 2c055e3ca001ee6064677a0db16172148e5edcef (patch) | |
tree | 46073b7ea2df58520ffc560af72ae2bdabf8bdd6 | |
parent | ce81b97e80ada42a223a7fd45c5abaa5c9667d75 (diff) |
Added tentative page rendering, outline main function
-rw-r--r-- | config.toml | 2 | ||||
-rw-r--r-- | entry.go | 7 | ||||
-rw-r--r-- | main.go | 39 | ||||
-rw-r--r-- | markdown.go | 17 | ||||
-rw-r--r-- | page.go | 42 | ||||
-rw-r--r-- | template.html | 10 | ||||
-rw-r--r-- | utils.go | 12 |
7 files changed, 114 insertions, 15 deletions
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" @@ -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 } - @@ -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 <config>") + 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 +} @@ -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 @@ <!DOCTYPE html> <html> <head> - <title>Docs</title> + <title>{{.SiteTitle}} - Docs</title> <link rel="stylesheet" href="{{.StylesheetUrl}}" /> <meta charset="UTF-8"> <meta name="author" content="Eduardo Pedroni"> @@ -10,17 +10,17 @@ <header>{{.SiteTitle}}</header> <nav> <ul> - {{range .Pages}} + {{range .Nav}} <li> - <a href="{{.PageUrl}}">{{.PageName}}</a> + <a href="{{.AbsoluteUrl}}">{{.Text}}</a> </li> {{end}} </ul> </nav> <main> - {{range .Entries}} + {{range .Content}} <article> - {{.Content}} + {{.}} </article> {{end}} </main> @@ -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 } |