aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEddy Pedroni <eddy@0xf7.com>2020-01-28 22:06:16 +0100
committerEddy Pedroni <eddy@0xf7.com>2020-01-28 22:06:16 +0100
commit2c055e3ca001ee6064677a0db16172148e5edcef (patch)
tree46073b7ea2df58520ffc560af72ae2bdabf8bdd6
parentce81b97e80ada42a223a7fd45c5abaa5c9667d75 (diff)
Added tentative page rendering, outline main function
-rw-r--r--config.toml2
-rw-r--r--entry.go7
-rw-r--r--main.go39
-rw-r--r--markdown.go17
-rw-r--r--page.go42
-rw-r--r--template.html10
-rw-r--r--utils.go12
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"
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 <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
+}
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 @@
<!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>
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
}