Commit 2e8a74ec authored by Matthew Holt's avatar Matthew Holt

markdown: Prefix log messages, and slight refactor

Also change sha1 to md5 for the directory scans; slightly faster.
parent e94e90b0
...@@ -25,12 +25,12 @@ func Markdown(c *Controller) (middleware.Middleware, error) { ...@@ -25,12 +25,12 @@ func Markdown(c *Controller) (middleware.Middleware, error) {
IndexFiles: []string{"index.md"}, IndexFiles: []string{"index.md"},
} }
// For any configs that enabled static site gen, sweep the whole path at startup // Sweep the whole path at startup to at least generate link index, maybe generate static site
c.Startup = append(c.Startup, func() error { c.Startup = append(c.Startup, func() error {
for i := range mdconfigs { for i := range mdconfigs {
cfg := &mdconfigs[i] cfg := &mdconfigs[i]
// Generate static files. // Generate link index and static files (if enabled)
if err := markdown.GenerateStatic(md, cfg); err != nil { if err := markdown.GenerateStatic(md, cfg); err != nil {
return err return err
} }
......
package markdown package markdown
import ( import (
"crypto/sha1" "crypto/md5"
"encoding/hex" "encoding/hex"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
...@@ -13,7 +13,9 @@ import ( ...@@ -13,7 +13,9 @@ import (
"github.com/mholt/caddy/middleware" "github.com/mholt/caddy/middleware"
) )
// GenerateStatic generate static files from markdowns. // GenerateStatic generate static files and link index from markdowns.
// It only generates static files if it is enabled (cfg.StaticDir
// must be set).
func GenerateStatic(md Markdown, cfg *Config) error { func GenerateStatic(md Markdown, cfg *Config) error {
generated, err := generateLinks(md, cfg) generated, err := generateLinks(md, cfg)
if err != nil { if err != nil {
...@@ -130,6 +132,6 @@ func computeDirHash(md Markdown, c Config) (string, error) { ...@@ -130,6 +132,6 @@ func computeDirHash(md Markdown, c Config) (string, error) {
return "", err return "", err
} }
sum := sha1.Sum([]byte(hashString)) sum := md5.Sum([]byte(hashString))
return hex.EncodeToString(sum[:]), nil return hex.EncodeToString(sum[:]), nil
} }
...@@ -94,9 +94,8 @@ func (c Config) IsValidExt(ext string) bool { ...@@ -94,9 +94,8 @@ func (c Config) IsValidExt(ext string) bool {
// ServeHTTP implements the http.Handler interface. // ServeHTTP implements the http.Handler interface.
func (md Markdown) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) { func (md Markdown) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
for i := range md.Configs { for _, cfg := range md.Configs {
m := &md.Configs[i] if !middleware.Path(r.URL.Path).Matches(cfg.PathScope) {
if !middleware.Path(r.URL.Path).Matches(m.PathScope) {
continue continue
} }
...@@ -105,7 +104,7 @@ func (md Markdown) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error ...@@ -105,7 +104,7 @@ func (md Markdown) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error
fpath = idx fpath = idx
} }
for _, ext := range m.Extensions { for _, ext := range cfg.Extensions {
if strings.HasSuffix(fpath, ext) { if strings.HasSuffix(fpath, ext) {
f, err := md.FileSys.Open(fpath) f, err := md.FileSys.Open(fpath)
if err != nil { if err != nil {
...@@ -121,19 +120,18 @@ func (md Markdown) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error ...@@ -121,19 +120,18 @@ func (md Markdown) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error
} }
// if development is set, scan directory for file changes for links. // if development is set, scan directory for file changes for links.
if m.Development { if cfg.Development {
if err := GenerateStatic(md, m); err != nil { if err := GenerateStatic(md, &cfg); err != nil {
log.Println(err) log.Println("On-demand generation error (markdown):", err)
} }
} }
// if static site is generated, attempt to use it // if static site is generated, attempt to use it
if filepath, ok := m.StaticFiles[fpath]; ok { if filepath, ok := cfg.StaticFiles[fpath]; ok {
if fs1, err := os.Stat(filepath); err == nil { if fs1, err := os.Stat(filepath); err == nil {
// if markdown has not been modified // if markdown has not been modified since static page
// since static page generation, // generation, serve the static page
// serve the static page if fs.ModTime().Before(fs1.ModTime()) {
if fs.ModTime().UnixNano() < fs1.ModTime().UnixNano() {
if html, err := ioutil.ReadFile(filepath); err == nil { if html, err := ioutil.ReadFile(filepath); err == nil {
w.Write(html) w.Write(html)
return http.StatusOK, nil return http.StatusOK, nil
...@@ -156,7 +154,7 @@ func (md Markdown) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error ...@@ -156,7 +154,7 @@ func (md Markdown) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error
Req: r, Req: r,
URL: r.URL, URL: r.URL,
} }
html, err := md.Process(*m, fpath, body, ctx) html, err := md.Process(cfg, fpath, body, ctx)
if err != nil { if err != nil {
return http.StatusInternalServerError, err return http.StatusInternalServerError, err
} }
......
...@@ -92,7 +92,7 @@ func (l *linkGen) generateLinks(md Markdown, cfg *Config) bool { ...@@ -92,7 +92,7 @@ func (l *linkGen) generateLinks(md Markdown, cfg *Config) bool {
l.Unlock() l.Unlock()
return false return false
} else if err != nil { } else if err != nil {
log.Println("Error:", err) log.Println("Hash error (markdown):", err)
} }
cfg.Links = []PageLink{} cfg.Links = []PageLink{}
...@@ -100,53 +100,55 @@ func (l *linkGen) generateLinks(md Markdown, cfg *Config) bool { ...@@ -100,53 +100,55 @@ func (l *linkGen) generateLinks(md Markdown, cfg *Config) bool {
cfg.Lock() cfg.Lock()
l.lastErr = filepath.Walk(fp, func(path string, info os.FileInfo, err error) error { l.lastErr = filepath.Walk(fp, func(path string, info os.FileInfo, err error) error {
for _, ext := range cfg.Extensions { for _, ext := range cfg.Extensions {
if !info.IsDir() && strings.HasSuffix(info.Name(), ext) { if info.IsDir() || !strings.HasSuffix(info.Name(), ext) {
// Load the file continue
body, err := ioutil.ReadFile(path) }
if err != nil {
return err
}
// Get the relative path as if it were a HTTP request, // Load the file
// then prepend with "/" (like a real HTTP request) body, err := ioutil.ReadFile(path)
reqPath, err := filepath.Rel(md.Root, path) if err != nil {
if err != nil { return err
return err }
}
reqPath = "/" + reqPath
parser := findParser(body) // Get the relative path as if it were a HTTP request,
if parser == nil { // then prepend with "/" (like a real HTTP request)
// no metadata, ignore. reqPath, err := filepath.Rel(md.Root, path)
continue if err != nil {
} return err
summary, err := parser.Parse(body) }
if err != nil { reqPath = "/" + reqPath
return err
} parser := findParser(body)
if parser == nil {
// no metadata, ignore.
continue
}
summary, err := parser.Parse(body)
if err != nil {
return err
}
// truncate summary to maximum length // truncate summary to maximum length
if len(summary) > summaryLen { if len(summary) > summaryLen {
summary = summary[:summaryLen] summary = summary[:summaryLen]
// trim to nearest word // trim to nearest word
lastSpace := bytes.LastIndex(summary, []byte(" ")) lastSpace := bytes.LastIndex(summary, []byte(" "))
if lastSpace != -1 { if lastSpace != -1 {
summary = summary[:lastSpace] summary = summary[:lastSpace]
}
} }
}
metadata := parser.Metadata() metadata := parser.Metadata()
cfg.Links = append(cfg.Links, PageLink{ cfg.Links = append(cfg.Links, PageLink{
Title: metadata.Title, Title: metadata.Title,
URL: reqPath, URL: reqPath,
Date: metadata.Date, Date: metadata.Date,
Summary: string(blackfriday.Markdown(summary, SummaryRenderer{}, 0)), Summary: string(blackfriday.Markdown(summary, SummaryRenderer{}, 0)),
}) })
break // don't try other file extensions break // don't try other file extensions
}
} }
return nil return nil
......
...@@ -114,7 +114,7 @@ func (md Markdown) processTemplate(c Config, requestPath string, tmpl []byte, me ...@@ -114,7 +114,7 @@ func (md Markdown) processTemplate(c Config, requestPath string, tmpl []byte, me
// if static page generation fails, // if static page generation fails,
// nothing fatal, only log the error. // nothing fatal, only log the error.
// TODO: Report this non-fatal error, but don't log it here // TODO: Report this non-fatal error, but don't log it here
log.Println(err) log.Println("Rendering error (markdown):", err)
} }
return b.Bytes(), nil return b.Bytes(), nil
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment