From 50f608846f1021db4f2335744cc023231fb74652 Mon Sep 17 00:00:00 2001 From: Adam Mathes Date: Thu, 9 Feb 2017 20:14:46 -0800 Subject: fix some templates, add verbose-logger --- main.go | 18 ++++++++++-------- post/post.go | 23 +++++++++++++---------- site/init.go | 9 ++++++--- vlog/vlog.go | 14 ++++++++++++++ 4 files changed, 43 insertions(+), 21 deletions(-) create mode 100644 vlog/vlog.go diff --git a/main.go b/main.go index 221a514..267ce40 100644 --- a/main.go +++ b/main.go @@ -9,8 +9,8 @@ import ( "snkt/render" "snkt/site" "snkt/web" + "snkt/vlog" "fmt" - "log" ) func main() { @@ -18,13 +18,13 @@ func main() { var configFile, init_dir string var build, preview, version, verbose, help bool - flag.StringVar(&configFile, "c", "config.yml", "configuration file") + flag.StringVar(&configFile, "c", "config.yml", "`configuration` file") flag.StringVar(&init_dir, "init", "", "initialize new site at `directory`") - flag.BoolVar(&build, "b", false, "build the site") - flag.BoolVar(&preview, "p", false, "start local HTTP server for preview") + flag.BoolVar(&build, "b", false, "build site") + flag.BoolVar(&preview, "p", false, "preview site with local HTTP server") flag.BoolVar(&version, "v", false, "print version number") flag.BoolVar(&help, "h", false, "help") - flag.BoolVar(&verbose, "verbose", false, "log more actions while building") + flag.BoolVar(&verbose, "verbose", false, "log actions during build") flag.Parse() if !help && !build && !preview && !version && init_dir=="" { @@ -32,7 +32,7 @@ func main() { return } if(init_dir != "") { - fmt.Printf("Initializing new site in %s\n", init_dir) + fmt.Printf("initializing new site in %s\n", init_dir) site.Init(init_dir) return } @@ -51,14 +51,16 @@ func main() { render.Init() if build { - log.Printf("Building site...\n") + vlog.Printf("building site...\n") var s site.Site + vlog.Printf("reading posts...\n") s.Read() + vlog.Printf("writing posts and archives...\n") s.Write() } if preview { - log.Printf("Spawning preview at [%s] of [%s]\n", + fmt.Printf("spawning preview at [%s] of [%s]\n", config.Config.PreviewDir, config.Config.PreviewServer) web.Serve(config.Config.PreviewServer, config.Config.PreviewDir) } diff --git a/post/post.go b/post/post.go index 19a8e73..ab1ccb6 100644 --- a/post/post.go +++ b/post/post.go @@ -8,6 +8,7 @@ import ( "snkt/config" "snkt/render" "snkt/text" + "snkt/vlog" "github.com/russross/blackfriday" "io/ioutil" "log" @@ -130,18 +131,20 @@ func (p *Post) parse() { if len(ds) > 0 { date_str = ds[0] } - var err error - p.Time, err = time.Parse("2006-1-2", date_str) - if err != nil { - - if config.Config.Verbose { - log.Println(err) - } - // fallback is to use file modtime - // should use create time but that doesn't seem to be in stdlib - // TODO: figure out how to use file birth time + if date_str == "" { p.Time = p.FileInfo.ModTime() + vlog.Printf("no date field in post %s, using file modification time\n", p.SourceFile) + } else { + var err error + p.Time, err = time.Parse("2006-1-2", date_str) + if err != nil { + // fallback is to use file modtime + // should use create time but that doesn't seem to be in stdlib + // TODO: figure out how to use file birth time + vlog.Printf("no valid date parsed for post %s, using file modification time\n", p.SourceFile) + p.Time = p.FileInfo.ModTime() + } } p.Year, p.Month, p.Day = p.Time.Date() diff --git a/site/init.go b/site/init.go index dc17cfa..02a7586 100644 --- a/site/init.go +++ b/site/init.go @@ -27,7 +27,6 @@ var skeletons = []skeleton { {{template "title" .}} -

{{template "title"}}

{{template "content" .}}

Home

@@ -55,9 +54,10 @@ var skeletons = []skeleton { Dir: "tmpl", Filename: "post", Content: []byte( -`{{define "title"}}{{ .Post.Title }}{{end}} +`{{define "title"}}{{.Post.Title}} - {{.Site.Title}}{{end}} {{define "content"}} -

{{ .Post.Title }}

+

{{.Site.Title}}

+

{{.Post.Title}}

{{.Post.Content}} {{if .Post.Next.Title}}

{{.Post.Next.Title}}

@@ -74,6 +74,7 @@ var skeletons = []skeleton { `{{define "title"}}{{.Site.Title}} Archives{{end}} {{define "content"}}

{{ .Site.Title }}

+

Archives

{{range .Posts}} {{if .}}

{{.Title}}

{{end}} {{end}} @@ -124,6 +125,8 @@ func Init(directory string) { } var cfg = config.Settings{ + SiteTitle: "snkt", + SiteURL: "", TxtDir: path.Join(init_dir, "txt"), HtmlDir: path.Join(init_dir, "html"), TmplDir: path.Join(init_dir, "tmpl"), diff --git a/vlog/vlog.go b/vlog/vlog.go new file mode 100644 index 0000000..9ef771c --- /dev/null +++ b/vlog/vlog.go @@ -0,0 +1,14 @@ +// package vlog wraps log actions and only performs them if "verbose" config is set +package vlog + +import ( + "snkt/config" + "log" +) + +func Printf(format string, v ...interface{}) { + if config.Config.Verbose { + log.Printf(format, v...) + } +} + -- cgit v1.2.3