2017-10-08 04:48:58 +00:00
|
|
|
package core
|
|
|
|
|
|
|
|
import (
|
2017-12-02 18:47:21 +00:00
|
|
|
"html/template"
|
|
|
|
"io/ioutil"
|
2017-10-08 04:48:58 +00:00
|
|
|
"net/http"
|
|
|
|
"strings"
|
2018-02-10 03:01:56 +00:00
|
|
|
|
|
|
|
"github.com/kirsle/blog/core/internal/markdown"
|
2018-02-10 18:08:45 +00:00
|
|
|
"github.com/kirsle/blog/core/internal/render"
|
2018-02-10 19:20:27 +00:00
|
|
|
"github.com/kirsle/blog/core/internal/responses"
|
2017-10-08 04:48:58 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// PageHandler is the catch-all route handler, for serving static web pages.
|
|
|
|
func (b *Blog) PageHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
path := r.URL.Path
|
2017-12-23 02:34:58 +00:00
|
|
|
// log.Debug("Catch-all page handler invoked for request URI: %s", path)
|
2017-10-08 04:48:58 +00:00
|
|
|
|
|
|
|
// Remove trailing slashes by redirecting them away.
|
|
|
|
if len(path) > 1 && path[len(path)-1] == '/' {
|
2018-02-10 19:20:27 +00:00
|
|
|
responses.Redirect(w, strings.TrimRight(path, "/"))
|
2017-10-31 16:42:15 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Restrict special paths.
|
|
|
|
if strings.HasPrefix(strings.ToLower(path), "/.") {
|
2018-02-10 19:14:42 +00:00
|
|
|
b.Forbidden(w, r, "Forbidden")
|
2017-10-08 04:48:58 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Search for a file that matches their URL.
|
2018-02-10 18:08:45 +00:00
|
|
|
filepath, err := render.ResolvePath(path)
|
2017-10-31 16:42:15 +00:00
|
|
|
if err != nil {
|
2017-11-24 19:56:32 +00:00
|
|
|
// See if it resolves as a blog entry.
|
2017-11-27 03:05:31 +00:00
|
|
|
err = b.viewPost(w, r, strings.TrimLeft(path, "/"))
|
2017-11-24 19:56:32 +00:00
|
|
|
if err != nil {
|
|
|
|
b.NotFound(w, r, "The page you were looking for was not found.")
|
|
|
|
}
|
2017-10-31 16:42:15 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Is it a template file?
|
2017-12-23 02:34:58 +00:00
|
|
|
if strings.HasSuffix(filepath.URI, ".gohtml") {
|
2018-02-10 22:05:41 +00:00
|
|
|
render.Template(w, r, filepath.URI, nil)
|
2017-10-31 16:42:15 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-12-02 18:47:21 +00:00
|
|
|
// Is it a Markdown file?
|
|
|
|
if strings.HasSuffix(filepath.URI, ".md") || strings.HasSuffix(filepath.URI, ".markdown") {
|
|
|
|
source, err := ioutil.ReadFile(filepath.Absolute)
|
|
|
|
if err != nil {
|
|
|
|
b.Error(w, r, "Couldn't read Markdown source!")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Render it to HTML and find out its title.
|
|
|
|
body := string(source)
|
2018-02-10 03:01:56 +00:00
|
|
|
html := markdown.RenderTrustedMarkdown(body)
|
|
|
|
title, _ := markdown.TitleFromMarkdown(body)
|
2017-12-02 18:47:21 +00:00
|
|
|
|
2018-02-10 22:05:41 +00:00
|
|
|
render.Template(w, r, ".markdown", map[string]interface{}{
|
2017-12-23 23:29:38 +00:00
|
|
|
"Title": title,
|
|
|
|
"HTML": template.HTML(html),
|
2018-02-10 22:05:41 +00:00
|
|
|
"MarkdownPath": filepath.URI,
|
|
|
|
})
|
2017-12-02 18:47:21 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-10-31 16:42:15 +00:00
|
|
|
http.ServeFile(w, r, filepath.Absolute)
|
|
|
|
}
|