diff options
| author | Mitchell Riedstra <mitch@riedstra.dev> | 2021-07-15 01:10:41 -0400 |
|---|---|---|
| committer | Mitchell Riedstra <mitch@riedstra.dev> | 2021-07-15 01:10:41 -0400 |
| commit | 8b1d8bf26452f3ce8b38228a39755a6b8e18775a (patch) | |
| tree | f30f5cc86ce6758589d3d42607e829d0f537e70b | |
| parent | 904e37a88a6a2eab3919f7f2c40bbb2c07544a7c (diff) | |
| download | go-website-8b1d8bf26452f3ce8b38228a39755a6b8e18775a.tar.gz go-website-8b1d8bf26452f3ce8b38228a39755a6b8e18775a.tar.xz | |
Start on the example website. Remove some chunks of code. Fix up author handling in the feeds.
| -rw-r--r-- | cmd/server/app.go | 63 | ||||
| -rw-r--r-- | cmd/server/feed.go | 9 | ||||
| -rw-r--r-- | cmd/server/handlers.go | 30 | ||||
| -rw-r--r-- | cmd/server/main.go | 18 | ||||
| -rw-r--r-- | example-site/404.md | 10 | ||||
| -rw-r--r-- | example-site/blog/first-post.md | 19 | ||||
| -rw-r--r-- | example-site/blog/second-post.md | 14 | ||||
| -rw-r--r-- | example-site/conf.yml | 20 | ||||
| -rw-r--r-- | example-site/inc/base.html | 53 | ||||
| -rw-r--r-- | example-site/index.md | 47 | ||||
| -rw-r--r-- | example-site/multi-tag-page.md | 19 | ||||
| -rw-r--r-- | example-site/reIndex.md | 22 | ||||
| -rw-r--r-- | example-site/static/style.css | 91 | ||||
| -rw-r--r-- | example-site/untagged-page.md | 13 | ||||
| -rw-r--r-- | go.sum | 1 | ||||
| -rw-r--r-- | page/page.go | 26 |
16 files changed, 388 insertions, 67 deletions
diff --git a/cmd/server/app.go b/cmd/server/app.go new file mode 100644 index 0000000..743a389 --- /dev/null +++ b/cmd/server/app.go @@ -0,0 +1,63 @@ +package main + +import ( + "os" + + "gopkg.in/yaml.v3" + "riedstra.dev/mitch/go-website/page" +) + +var FeedPrefixDefault = ".feeds" + +type App struct { + ReIndexPath string + StaticDirectory string + BaseTemplate string + DocumentSplit string + Suffix string + + // Related to the Atom feed + Title string + Description string // aka, "subtitle" + Author Author + SiteURL string + FeedId string + Updated page.PageTime + FeedPrefix string +} + +func loadConf(fn string) (*App, error) { + fh, err := os.Open(fn) + if err != nil { + return nil, err + } + dec := yaml.NewDecoder(fh) + + app := &App{} + err = dec.Decode(app) + if err != nil { + return nil, err + } + + if app.StaticDirectory == "" { + app.StaticDirectory = "static" + } + if app.FeedPrefix == "" { + app.FeedPrefix = FeedPrefixDefault + } + if app.BaseTemplate != "" { + page.BaseTemplate = app.BaseTemplate + } + if app.DocumentSplit != "" { + page.DocumentSplit = app.DocumentSplit + } + if app.Suffix != "" { + page.Suffix = app.Suffix + } + + page.Global = map[string]interface{}{ + "App": app, + } + + return app, nil +} diff --git a/cmd/server/feed.go b/cmd/server/feed.go index 2d4a75b..073097b 100644 --- a/cmd/server/feed.go +++ b/cmd/server/feed.go @@ -189,6 +189,15 @@ func (a *App) FeedHandler(w http.ResponseWriter, r *http.Request) { Links: []Link{Link{Href: strings.Join([]string{a.SiteURL, p.Path()}, "/")}}, } + if p.AuthorName != "" { + entry.Author = &Author{ + Name: p.AuthorName, + } + if p.AuthorEmail != "" { + entry.Author.Email = p.AuthorEmail + } + } + if addContent { entry.Content = &Content{Type: "html", Data: content.String()} } diff --git a/cmd/server/handlers.go b/cmd/server/handlers.go index 5ea89cd..cb63774 100644 --- a/cmd/server/handlers.go +++ b/cmd/server/handlers.go @@ -9,20 +9,13 @@ import ( "riedstra.dev/mitch/go-website/page" ) -var FeedPrefixDefault = ".feeds" - -type App struct { - ReIndexPath string - StaticDirectory string - - // Related to the Atom feed - Title string - Description string // aka, "subtitle" - Author Author - SiteURL string - FeedId string - Updated page.PageTime - FeedPrefix string +func (a *App) ServeHTTP(w http.ResponseWriter, r *http.Request) { + rtr := mux.NewRouter() + rtr.HandleFunc(a.ReIndexPath, a.RebuildIndexHandler) + rtr.PathPrefix("/static/").Handler(a.StaticHandler()) + rtr.PathPrefix(fmt.Sprintf("/%s/{tag}", a.FeedPrefix)).HandlerFunc(a.FeedHandler) + rtr.PathPrefix("/").HandlerFunc(a.PageHandler) + rtr.ServeHTTP(w, r) } func (a *App) PageHandler(w http.ResponseWriter, r *http.Request) { @@ -55,12 +48,3 @@ func (a *App) RebuildIndexHandler(w http.ResponseWriter, r *http.Request) { func (a *App) StaticHandler() http.Handler { return http.StripPrefix("/static/", http.FileServer(http.Dir(a.StaticDirectory))) } - -func (a *App) ServeHTTP(w http.ResponseWriter, r *http.Request) { - rtr := mux.NewRouter() - rtr.HandleFunc(a.ReIndexPath, a.RebuildIndexHandler) - rtr.PathPrefix("/static/").Handler(a.StaticHandler()) - rtr.PathPrefix(fmt.Sprintf("/%s/{tag}", a.FeedPrefix)).HandlerFunc(a.FeedHandler) - rtr.PathPrefix("/").HandlerFunc(a.PageHandler) - rtr.ServeHTTP(w, r) -} diff --git a/cmd/server/main.go b/cmd/server/main.go index 4b2c1ad..00aecbf 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -19,18 +19,6 @@ func VersionPrint() { os.Exit(0) } -func loadConf(fn string) (*App, error) { - fh, err := os.Open(fn) - if err != nil { - return nil, err - } - dec := yaml.NewDecoder(fh) - - app := &App{} - err = dec.Decode(app) - return app, err -} - func main() { fl := flag.NewFlagSet("Website", flag.ExitOnError) listen := fl.String("l", "0.0.0.0:8001", "Listening address") @@ -61,12 +49,6 @@ func main() { if app.ReIndexPath == "" || *indexPath != defaultIndexPath { app.ReIndexPath = *indexPath } - if app.StaticDirectory == "" { - app.StaticDirectory = "static" - } - if app.FeedPrefix == "" { - app.FeedPrefix = FeedPrefixDefault - } if *verbose { b, _ := yaml.Marshal(app) diff --git a/example-site/404.md b/example-site/404.md new file mode 100644 index 0000000..162233c --- /dev/null +++ b/example-site/404.md @@ -0,0 +1,10 @@ +--- +title: 404 Not found | Example Website +description: 404 Not found +|--- + +# 404 Not fonud! + +Whatever it is you were looking for, we have no idea what it was. + +Perhaps you'd like to go back [home](/)? diff --git a/example-site/blog/first-post.md b/example-site/blog/first-post.md new file mode 100644 index 0000000..42ef931 --- /dev/null +++ b/example-site/blog/first-post.md @@ -0,0 +1,19 @@ +--- +title: 'Blog Post #1' +# head: "This is optional" +description: My example description + +date: 07.02.2021 15:00:00 EDT + +tags: + Blog: + +|--- + +`{{.Date.Time.Format "Monday January 2 2006"}}` + +# {{.Title}} + +This is an example first blog post. + +Not much here. diff --git a/example-site/blog/second-post.md b/example-site/blog/second-post.md new file mode 100644 index 0000000..f8abef2 --- /dev/null +++ b/example-site/blog/second-post.md @@ -0,0 +1,14 @@ +--- +title: 'Blog Post #2' +description: My example description for blog post #2 +date: 07.14.2021 15:00:00 EDT +tags: + Blog: +|--- + +`{{.Date.Time.Format "Monday January 2 2006"}}` + +# {{.Title}} + +This is an example second blog post. + diff --git a/example-site/conf.yml b/example-site/conf.yml new file mode 100644 index 0000000..b6f0202 --- /dev/null +++ b/example-site/conf.yml @@ -0,0 +1,20 @@ +# All these can be unset +reindexpath: /reIndex +staticdirectory: static +basetemplate: inc/base.html +documentsplit: "|---\n" +suffix: ".md" + +# Used by the feeds handler +title: "My Example Website!" +description: > + An example website powered by markdown, yaml and some clever templating +author: + name: "Example Author" + # uri: "https://example.com" # Not required + # email: "author@example.com" # Not required +# Change this to your actual website URI, e.g. https://example.com +siteurl: "http://localhost:8080" +feedid: "changeme" +updated: 07.13.2021 19:51:00 EDT +feedprefix: .feeds diff --git a/example-site/inc/base.html b/example-site/inc/base.html new file mode 100644 index 0000000..959c7d6 --- /dev/null +++ b/example-site/inc/base.html @@ -0,0 +1,53 @@ +<!DOCTYPE html> +<html lang="en"> +<head> + <meta charset="UTF-8"> + + <link id="maincss" rel="stylesheet" href="/static/style.css" defer> + + <meta name="viewport" content="width=device-width, initial-scale=1.0"> + <meta name="author" content="{{.Global.App.Author.Name}}"> + <meta name="description" content="{{.Global.App.Description}}"> + + <!-- + <link rel="apple-touch-icon" sizes="180x180" href="/static/apple-touch-icon.png"> + <link rel="icon" type="image/png" sizes="32x32" href="/static/favicon-32x32.png"> + <link rel="icon" type="image/png" sizes="16x16" href="/static/favicon-16x16.png"> + <link rel="manifest" href="/static/site.webmanifest"> + --> + +{{if .Title}} + <title>{{.Title}}</title> +{{else}} + <title>Please change me!</title> +{{end}} + +{{if .Description}} + <meta name="description" content="{{.Description}}"> +{{else}} + <meta name="description" content="Draft and or Preview page. Default Description."> +{{end}} + +</head> + +<body> + +{{if .Description}}{{else}} + <h1> + <span style="color: red;"> + This is a draft, preview or otherwise unfinished page. + </span> + </h1> +{{end}} + +<nav> + <a href="/">Home</a> + + <div style="display: block; float: right;"> + <a href="#">Git</a> + </div> +</nav> + +{{.RenderBody}} + +</body> diff --git a/example-site/index.md b/example-site/index.md new file mode 100644 index 0000000..6d292b5 --- /dev/null +++ b/example-site/index.md @@ -0,0 +1,47 @@ +--- +title: Example website home page +description: My example description + +|--- + +# {{.Title}} + +Example paragraph. This website utilizes Go's templates, a Blackfriday markdown +renderer and some yaml at the top of each file to provide some information about +it. + +`code style text` + +[reIndex page]({{.Global.App.ReIndexPath}}) + +## Recent Blog Entries: + +[Atom Feed](/{{.Global.App.FeedPrefix}}/Blog?limit=5&content) + +{{range $val := .Index.Blog.SortDate}} + * [{{$val.Date.Time.Format "2006-01-02"}} {{$val.Title}}]({{$val.Path}}){{end}} + +## Some internals + + +#### Global vars: + +``` +{{.Global}} + +"App" under Global vars: +{{.Global.App}} +``` + +#### Page Index + +{{range $key, $val := .Index}} +``` +### {{$key}}: + +{{range $v2 := $val}} +{{$v2.StringDetail}}{{end}} +``` + + +{{end}} diff --git a/example-site/multi-tag-page.md b/example-site/multi-tag-page.md new file mode 100644 index 0000000..a1a61d2 --- /dev/null +++ b/example-site/multi-tag-page.md @@ -0,0 +1,19 @@ +--- +title: Multi tag page +description: A page with several tags added to it +date: 07.02.2021 15:00:00 EDT +authorname: Someone Else +# authoremail: someoneelse@example.com +tags: + Blog: + Automotive: + MoarTags: +|--- + +`{{.Date.Time.Format "Monday January 2 2006"}}` + +# {{.Title}} + + +Just a sample page with several tags + diff --git a/example-site/reIndex.md b/example-site/reIndex.md new file mode 100644 index 0000000..4519dc5 --- /dev/null +++ b/example-site/reIndex.md @@ -0,0 +1,22 @@ +--- +title: reIndex page +description: > + This page is rendered after reindexing, with any encountered errors! +|--- + +# {{.Title}} + +this is the reindex page + +{{if .Vars.IndexError}} +<span style="color: red;"> +# Indexing Error: {{.Vars.IndexError}} +</span> +{{end}} + + +``` +This handler adds some information to the page vars: +{{.Vars}} +``` + diff --git a/example-site/static/style.css b/example-site/static/style.css new file mode 100644 index 0000000..158444b --- /dev/null +++ b/example-site/static/style.css @@ -0,0 +1,91 @@ +nav { + border-bottom: 3px solid #000; + padding-bottom: 10px; +} +a { + text-decoration: none; +} +a:hover { + text-decoration: underline; +} + +code { + background-color: #afafaf; + padding: 2px; + font-size: .8em; + font-family: "Roboto Mono", "Monaco", "Lucida Console", "DejaVu Sans Mono", "monospace"; +} + +pre code { + color: #000; + background-color: #fafafa; + display: block; + padding: 10px; + border: 1px solid; + line-height: 1.1; + overflow: auto; +} + + +blockquote { + border-left: 4px solid #aaa; + padding-left: 1em; +} + +/* + * The following was shamelessly ripped from: + * http://bettermotherfuckingwebsite.com/ + * And subsequently modified to suit my needs + */ + +body { + margin: 40px auto; + max-width: 80%; + line-height: 1.6; + font-size: 1em; + color: #444; + padding: 0 10px; + /* Added because some browsers don't default to white */ + background-color: #fff; +} + +img { + width: 100%; + height: auto; +} + +h1,h2,h3 { + line-height: 1.2 +} + +@media screen and (min-width: 960px) { + body { + max-width: 768px; + } +} + +@media print { + a, a:visited { + color: #000; + } + nav { + display: none; + } + body { + background-color: transparent; + line-height: 1; + font-size: 10pt; + /* margin: 5px auto; */ + margin: 5px 5px 5px 5px; + max-width: 100%; + } + + pre code { + color: #000; + background-color: inherit; + display: block; + padding: 10px; + border: none; + } + +} diff --git a/example-site/untagged-page.md b/example-site/untagged-page.md new file mode 100644 index 0000000..aa8400f --- /dev/null +++ b/example-site/untagged-page.md @@ -0,0 +1,13 @@ +--- +title: Untagged page +description: A page with no tags added to it +date: 07.02.2021 15:00:00 EDT +tags: +|--- + +`{{.Date.Time.Format "Monday January 2 2006"}}` + +# {{.Title}} + + +Just a sample page with no tags @@ -2,6 +2,7 @@ github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI= github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So= github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= +github.com/kr/pty v1.1.1 h1:VkoXIwSboBpnk99O/KFauAEILuNHv5DVFKZMBN/gUgw= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= diff --git a/page/page.go b/page/page.go index 8b84099..5604f1c 100644 --- a/page/page.go +++ b/page/page.go @@ -44,7 +44,6 @@ import ( type Page struct { path string Title string - Head string Description string AuthorName string AuthorEmail string @@ -112,34 +111,9 @@ func (p *Page) Render(wr io.Writer) error { return err } - // Automatically pull from the yml file if applicable - if p.Head != "" { - t, err = t.Parse(` - {{define "head"}} - {{.RenderHead}} - {{end}} - `) - if err != nil { - return err - } - } - return t.Execute(wr, p) } -func (p *Page) RenderHead() (string, error) { - buf := &bytes.Buffer{} - t, err := template.New("Head").Parse(p.Head) - if err != nil { - return "", err - } - err = t.Execute(buf, p) - if err != nil { - return "", err - } - return string(buf.Bytes()), nil -} - // Reads in the special markdown file format for the website off of the disk func (p *Page) Read() error { yamlBuf := bytes.NewBuffer(nil) |
