diff options
| author | Mitchell Riedstra <mitch@riedstra.dev> | 2021-07-18 16:27:24 -0400 |
|---|---|---|
| committer | Mitchell Riedstra <mitch@riedstra.dev> | 2021-07-18 16:27:24 -0400 |
| commit | bcf6d391f17a193c81ad6643f8d006de6c6abad8 (patch) | |
| tree | b8eaec45b02d79f8d51d6726ffbbe34ce5de2ed7 | |
| parent | 904e37a88a6a2eab3919f7f2c40bbb2c07544a7c (diff) | |
| download | go-website-0.0.13.tar.gz go-website-0.0.13.tar.xz | |
Adjust the program a bit, remove clunky "head" templates. Add an example site among other improvementsv0.0.13
| -rw-r--r-- | cmd/server/app.go | 63 | ||||
| -rw-r--r-- | cmd/server/feed.go | 13 | ||||
| -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 | 16 | ||||
| -rw-r--r-- | example-site/blog/second-post.md | 15 | ||||
| -rw-r--r-- | example-site/blog/unpublished-post.md | 22 | ||||
| -rw-r--r-- | example-site/checkup.md | 36 | ||||
| -rw-r--r-- | example-site/conf.yml | 20 | ||||
| -rw-r--r-- | example-site/inc/base.html | 51 | ||||
| -rw-r--r-- | example-site/index.md | 46 | ||||
| -rw-r--r-- | example-site/multi-tag-page.md | 20 | ||||
| -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 | 14 | ||||
| -rw-r--r-- | go.sum | 1 | ||||
| -rw-r--r-- | page/checkup.go | 85 | ||||
| -rw-r--r-- | page/misc.go | 31 | ||||
| -rw-r--r-- | page/page.go | 33 |
20 files changed, 563 insertions, 74 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..c478365 100644 --- a/cmd/server/feed.go +++ b/cmd/server/feed.go @@ -175,6 +175,10 @@ func (a *App) FeedHandler(w http.ResponseWriter, r *http.Request) { break } + if !p.Published { + continue + } + content := &bytes.Buffer{} err := p.Render(content) if err != nil { @@ -189,6 +193,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..f9c5ea2 --- /dev/null +++ b/example-site/blog/first-post.md @@ -0,0 +1,16 @@ +--- +title: 'Blog Post #1' +description: My example description +date: 07.02.2021 15:00:00 EDT +published: true +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..34fe318 --- /dev/null +++ b/example-site/blog/second-post.md @@ -0,0 +1,15 @@ +--- +title: 'Blog Post #2' +description: My example description for blog post #2 +date: 07.14.2021 15:00:00 EDT +published: true +tags: + Blog: +|--- + +`{{.Date.Time.Format "Monday January 2 2006"}}` + +# {{.Title}} + +This is an example second blog post. + diff --git a/example-site/blog/unpublished-post.md b/example-site/blog/unpublished-post.md new file mode 100644 index 0000000..3088250 --- /dev/null +++ b/example-site/blog/unpublished-post.md @@ -0,0 +1,22 @@ +--- +title: 'Unpublished Blog Post' +description: My example description for an unpublished blog post +date: 07.15.2021 15:00:00 EDT + +# Defaults to false +# published: false + +tags: + Blog: +|--- + +`{{.Date.Time.Format "Monday January 2 2006"}}` + +# {{.Title}} + +This is an example unpublished blog post. + +Description: + +> {{.Description}} + diff --git a/example-site/checkup.md b/example-site/checkup.md new file mode 100644 index 0000000..622d836 --- /dev/null +++ b/example-site/checkup.md @@ -0,0 +1,36 @@ +--- +title: Example checkup page +description: Showing the output of one of the internal checkup mechanisms + +|--- + +# {{.Title}} + +# Status + +Current statuses: +``` +{{range $key, $val := .Checkup}}{{$key}} +{{end}} +``` + +{{range $key, $val := .Checkup}} +## {{$key}}: +{{range $page := $val}} +``` +{{$page}} +{{$.EncodeYaml $page}} +``` +{{end}} +{{end}} + + +<!-- + +## Raw + +``` +{{.EncodeYaml .Checkup}} +``` + +--> 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..2f93ec7 --- /dev/null +++ b/example-site/inc/base.html @@ -0,0 +1,51 @@ +<!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"> + + {{if .AuthorName }} + <meta name="author" content="{{.AuthorName}}"> + {{else if .Global.App.Author.Name }} + <meta name="author" content="{{.Global.App.Author.Name}}"> + {{else}} + {{end}} + +{{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..f9f8fa8 --- /dev/null +++ b/example-site/index.md @@ -0,0 +1,46 @@ +--- +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}}) + * [404 Page](/some/path/that/does/not/exist/at/all) + * [Example checkup page](/checkup) + + +## 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}} + +## Published Blog Entries: + +{{range $val := .Index.Blog.SortDate}}{{if $val.Published}} + * [{{$val.Date.Time.Format "2006-01-02"}} * {{$val.Title}}]({{$val.Path}}){{end}}{{end}} + +## Some internals + + +#### Global vars: + +``` +{{.EncodeYaml .Global}} +``` + +#### Page Index ( details ) + +``` +{{.EncodeYaml .Index}} +``` + diff --git a/example-site/multi-tag-page.md b/example-site/multi-tag-page.md new file mode 100644 index 0000000..6e3b4f6 --- /dev/null +++ b/example-site/multi-tag-page.md @@ -0,0 +1,20 @@ +--- +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 +published: true +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..c55274e --- /dev/null +++ b/example-site/untagged-page.md @@ -0,0 +1,14 @@ +--- +title: Untagged page +description: A page with no tags added to it +date: 07.02.2021 15:00:00 EDT +published: true +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/checkup.go b/page/checkup.go new file mode 100644 index 0000000..c4501c2 --- /dev/null +++ b/page/checkup.go @@ -0,0 +1,85 @@ +package page + +import ( + "fmt" + "os" + "path/filepath" + "reflect" + "strings" +) + +// Checkup will return a map[string]PageList of all the pages broken down by +// the status of their fields. For instance, whehter or not they have a date +func (p *Page) Checkup() (map[string]PageList, error) { + Logger.Println("Checking up on all files...") + + out := make(map[string]PageList) + + filepath.Walk(filepath.Dir("."), + func(path string, info os.FileInfo, err error) error { + if err != nil { + return err + } + + if !info.IsDir() && strings.HasSuffix(info.Name(), Suffix) { + + p2 := NewPage(strings.ReplaceAll(path, Suffix, "")) + err = p2.Read() + + if err != nil { + Logger.Println("Error encountered: ", err) + return err + } + + r := reflect.ValueOf(*p2) + + for i := 0; i < r.NumField(); i++ { + key := "" + f := r.Field(i) + fi := r.Type().Field(i) + + if fi.PkgPath != "" { + continue + } + + name := fi.Name + + switch f.Interface().(type) { + case string: + if f.String() == "" { + key = fmt.Sprintf("Empty \"%s\"", name) + } else { + key = fmt.Sprintf("Not Empty \"%s\"", name) + } + case bool: + if !f.Bool() { + key = fmt.Sprintf("\"%s\" is false", name) + } else { + key = fmt.Sprintf("\"%s\" is true", name) + } + case map[string]interface{}: + case *PageTime: + if f.IsNil() { + key = fmt.Sprintf("\"%s\" is empty", name) + } + } + + if key == "" { + continue + } + + if _, ok := out[key]; !ok { + out[key] = []*Page{p2} + } else { + out[key] = append(out[key], p2) + } + + } + + } + + return nil + }) + + return out, nil +} diff --git a/page/misc.go b/page/misc.go new file mode 100644 index 0000000..0dbd059 --- /dev/null +++ b/page/misc.go @@ -0,0 +1,31 @@ +package page + +import ( + "encoding/json" + + "gopkg.in/yaml.v3" +) + +func (p Page) EncodeYaml(data interface{}) string { + if data == nil { + data = p + } + + b, err := yaml.Marshal(data) + if err != nil { + Logger.Println("Encountered error in EncodeYaml: ", err) + } + return string(b) +} + +func (p Page) EncodeJson(data interface{}) string { + if data == nil { + data = p + } + + b, err := json.MarshalIndent(data, "", " ") + if err != nil { + Logger.Println("Encountered error in EncodeJson: ", err) + } + return string(b) +} diff --git a/page/page.go b/page/page.go index 8b84099..e984383 100644 --- a/page/page.go +++ b/page/page.go @@ -25,7 +25,6 @@ package page import ( "bufio" "bytes" - "encoding/json" "fmt" "io" "log" @@ -44,7 +43,6 @@ import ( type Page struct { path string Title string - Head string Description string AuthorName string AuthorEmail string @@ -112,34 +110,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) @@ -203,9 +176,3 @@ func (p *Page) RenderBody() (string, error) { func (p Page) String() string { return fmt.Sprintf("Page: %s", p.path) } - -// StringDetail prints a detailed string of the page -func (p Page) StringDetail() string { - b, _ := json.MarshalIndent(p, "", " ") - return fmt.Sprintf("Page: %s\n%s\n", p.path, b) -} |
