2013-07-04 11:32:55 -04:00
|
|
|
// Copyright © 2013 Steve Francia <spf@spf13.com>.
|
|
|
|
//
|
|
|
|
// Licensed under the Simple Public License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
// http://opensource.org/licenses/Simple-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package hugolib
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2013-08-05 10:53:58 -04:00
|
|
|
"errors"
|
2013-07-04 11:32:55 -04:00
|
|
|
"fmt"
|
2013-07-08 22:23:54 -04:00
|
|
|
"github.com/BurntSushi/toml"
|
2013-12-05 09:29:41 -05:00
|
|
|
"github.com/spf13/hugo/helpers"
|
2013-09-18 12:15:46 -04:00
|
|
|
"github.com/spf13/hugo/parser"
|
2013-09-03 18:38:20 -04:00
|
|
|
"github.com/spf13/hugo/template/bundle"
|
2013-07-04 11:32:55 -04:00
|
|
|
"github.com/theplant/blackfriday"
|
2013-09-03 15:41:13 -04:00
|
|
|
"html/template"
|
2013-09-03 18:38:20 -04:00
|
|
|
"io"
|
2013-07-08 17:57:01 -04:00
|
|
|
"launchpad.net/goyaml"
|
2013-10-01 22:45:24 -04:00
|
|
|
json "launchpad.net/rjson"
|
|
|
|
"net/url"
|
2013-09-12 13:48:59 -04:00
|
|
|
"path"
|
2013-07-04 11:32:55 -04:00
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Page struct {
|
2013-09-13 17:46:34 -04:00
|
|
|
Status string
|
|
|
|
Images []string
|
2013-12-06 23:32:00 -05:00
|
|
|
RawContent []byte
|
2013-09-13 17:46:34 -04:00
|
|
|
Content template.HTML
|
|
|
|
Summary template.HTML
|
2013-11-10 15:04:51 -05:00
|
|
|
Truncated bool
|
2013-10-15 09:15:52 -04:00
|
|
|
plain string // TODO should be []byte
|
2013-09-13 17:46:34 -04:00
|
|
|
Params map[string]interface{}
|
|
|
|
contentType string
|
|
|
|
Draft bool
|
|
|
|
Aliases []string
|
|
|
|
Tmpl bundle.Template
|
|
|
|
Markup string
|
2013-09-18 13:17:43 -04:00
|
|
|
renderable bool
|
2013-10-08 12:33:57 -04:00
|
|
|
layout string
|
2013-10-25 18:37:53 -04:00
|
|
|
linkTitle string
|
2013-07-04 11:32:55 -04:00
|
|
|
PageMeta
|
|
|
|
File
|
|
|
|
Position
|
|
|
|
Node
|
|
|
|
}
|
|
|
|
|
|
|
|
type File struct {
|
2013-09-20 20:24:25 -04:00
|
|
|
FileName, Extension, Dir string
|
2013-07-04 11:32:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
type PageMeta struct {
|
|
|
|
WordCount int
|
|
|
|
FuzzyWordCount int
|
2013-10-15 09:32:21 -04:00
|
|
|
MinRead int
|
2013-10-18 11:01:31 -04:00
|
|
|
Weight int
|
2013-07-04 11:32:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
type Position struct {
|
|
|
|
Prev *Page
|
|
|
|
Next *Page
|
|
|
|
}
|
|
|
|
|
|
|
|
type Pages []*Page
|
|
|
|
|
2013-10-18 11:01:31 -04:00
|
|
|
func (p Pages) Len() int { return len(p) }
|
|
|
|
func (p Pages) Less(i, j int) bool {
|
|
|
|
if p[i].Weight == p[j].Weight {
|
|
|
|
return p[i].Date.Unix() > p[j].Date.Unix()
|
|
|
|
} else {
|
|
|
|
return p[i].Weight > p[j].Weight
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p Pages) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
|
2013-07-04 11:32:55 -04:00
|
|
|
|
|
|
|
// TODO eliminate unnecessary things
|
2013-07-08 17:57:01 -04:00
|
|
|
func (p Pages) Sort() { sort.Sort(p) }
|
2013-07-04 11:32:55 -04:00
|
|
|
func (p Pages) Limit(n int) Pages { return p[0:n] }
|
|
|
|
|
2013-10-15 09:15:52 -04:00
|
|
|
func (p Page) Plain() string {
|
|
|
|
if len(p.plain) == 0 {
|
|
|
|
p.plain = StripHTML(StripShortcodes(string(p.Content)))
|
|
|
|
}
|
|
|
|
return p.plain
|
|
|
|
}
|
|
|
|
|
2013-11-10 15:04:51 -05:00
|
|
|
// nb: this is only called for recognised types; so while .html might work for
|
|
|
|
// creating posts, it results in missing summaries.
|
|
|
|
func getSummaryString(content []byte, pagefmt string) (summary []byte, truncates bool) {
|
2013-09-03 18:38:20 -04:00
|
|
|
if bytes.Contains(content, summaryDivider) {
|
2013-10-04 12:28:28 -04:00
|
|
|
// If user defines split:
|
|
|
|
// Split then render
|
2013-11-10 15:04:51 -05:00
|
|
|
truncates = true // by definition
|
|
|
|
summary = renderBytes(bytes.Split(content, summaryDivider)[0], pagefmt)
|
2013-09-03 18:38:20 -04:00
|
|
|
} else {
|
2013-10-04 12:28:28 -04:00
|
|
|
// If hugo defines split:
|
|
|
|
// render, strip html, then split
|
2013-11-10 15:04:51 -05:00
|
|
|
plain := strings.TrimSpace(StripHTML(StripShortcodes(string(renderBytes(content, pagefmt)))))
|
|
|
|
summary = []byte(TruncateWordsToWholeSentence(plain, summaryLength))
|
|
|
|
truncates = len(summary) != len(plain)
|
2013-10-04 12:28:28 -04:00
|
|
|
}
|
2013-11-10 15:04:51 -05:00
|
|
|
return
|
2013-10-04 12:28:28 -04:00
|
|
|
}
|
|
|
|
|
2013-11-10 15:04:51 -05:00
|
|
|
func renderBytes(content []byte, pagefmt string) []byte {
|
|
|
|
switch pagefmt {
|
2013-10-04 12:28:28 -04:00
|
|
|
default:
|
|
|
|
return blackfriday.MarkdownCommon(content)
|
|
|
|
case "markdown":
|
|
|
|
return blackfriday.MarkdownCommon(content)
|
|
|
|
case "rst":
|
|
|
|
return []byte(getRstContent(content))
|
2013-09-03 18:38:20 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-13 19:39:24 -04:00
|
|
|
// TODO abstract further to support loading from more
|
|
|
|
// than just files on disk. Should load reader (file, []byte)
|
2013-09-04 19:57:17 -04:00
|
|
|
func newPage(filename string) *Page {
|
2013-08-13 19:39:24 -04:00
|
|
|
page := Page{contentType: "",
|
2013-08-04 22:02:15 -04:00
|
|
|
File: File{FileName: filename, Extension: "html"},
|
|
|
|
Node: Node{Keywords: make([]string, 10, 30)},
|
2013-09-18 17:21:27 -04:00
|
|
|
Params: make(map[string]interface{})}
|
2013-07-04 11:32:55 -04:00
|
|
|
page.Date, _ = time.Parse("20060102", "20080101")
|
2013-08-30 20:14:36 -04:00
|
|
|
page.guessSection()
|
2013-08-13 19:39:24 -04:00
|
|
|
return &page
|
|
|
|
}
|
2013-07-04 11:32:55 -04:00
|
|
|
|
2013-09-03 18:38:20 -04:00
|
|
|
func StripHTML(s string) string {
|
|
|
|
output := ""
|
|
|
|
|
|
|
|
// Shortcut strings with no tags in them
|
|
|
|
if !strings.ContainsAny(s, "<>") {
|
|
|
|
output = s
|
|
|
|
} else {
|
|
|
|
s = strings.Replace(s, "\n", " ", -1)
|
|
|
|
s = strings.Replace(s, "</p>", " \n", -1)
|
|
|
|
s = strings.Replace(s, "<br>", " \n", -1)
|
|
|
|
s = strings.Replace(s, "</br>", " \n", -1)
|
|
|
|
|
|
|
|
// Walk through the string removing all tags
|
|
|
|
b := new(bytes.Buffer)
|
|
|
|
inTag := false
|
|
|
|
for _, r := range s {
|
|
|
|
switch r {
|
|
|
|
case '<':
|
|
|
|
inTag = true
|
|
|
|
case '>':
|
|
|
|
inTag = false
|
|
|
|
default:
|
|
|
|
if !inTag {
|
|
|
|
b.WriteRune(r)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
output = b.String()
|
|
|
|
}
|
|
|
|
return output
|
|
|
|
}
|
|
|
|
|
2013-09-18 13:17:43 -04:00
|
|
|
func (p *Page) IsRenderable() bool {
|
|
|
|
return p.renderable
|
|
|
|
}
|
|
|
|
|
2013-08-14 08:57:14 -04:00
|
|
|
func (p *Page) guessSection() {
|
|
|
|
if p.Section == "" {
|
2013-09-12 13:48:59 -04:00
|
|
|
x := strings.Split(p.FileName, "/")
|
2013-10-07 00:57:45 -04:00
|
|
|
x = x[:len(x)-1]
|
|
|
|
if len(x) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if x[0] == "content" {
|
|
|
|
x = x[1:]
|
2013-08-13 19:39:24 -04:00
|
|
|
}
|
2013-10-07 00:57:45 -04:00
|
|
|
p.Section = path.Join(x...)
|
2013-07-04 11:32:55 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (page *Page) Type() string {
|
|
|
|
if page.contentType != "" {
|
|
|
|
return page.contentType
|
|
|
|
}
|
2013-08-14 08:57:14 -04:00
|
|
|
page.guessSection()
|
2013-08-14 18:22:57 -04:00
|
|
|
if x := page.Section; x != "" {
|
2013-07-04 11:32:55 -04:00
|
|
|
return x
|
|
|
|
}
|
|
|
|
|
|
|
|
return "page"
|
|
|
|
}
|
|
|
|
|
2013-10-07 00:57:45 -04:00
|
|
|
func (page *Page) Layout(l ...string) []string {
|
|
|
|
if page.layout != "" {
|
|
|
|
return layouts(page.Type(), page.layout)
|
|
|
|
}
|
|
|
|
|
2013-07-04 11:32:55 -04:00
|
|
|
layout := ""
|
|
|
|
if len(l) == 0 {
|
|
|
|
layout = "single"
|
|
|
|
} else {
|
|
|
|
layout = l[0]
|
|
|
|
}
|
|
|
|
|
2013-10-07 00:57:45 -04:00
|
|
|
return layouts(page.Type(), layout)
|
|
|
|
}
|
2013-07-04 11:32:55 -04:00
|
|
|
|
2013-10-07 00:57:45 -04:00
|
|
|
func layouts(types string, layout string) (layouts []string) {
|
|
|
|
t := strings.Split(types, "/")
|
2013-10-08 12:33:57 -04:00
|
|
|
for i := range t {
|
2013-10-07 00:57:45 -04:00
|
|
|
search := t[:len(t)-i]
|
|
|
|
layouts = append(layouts, fmt.Sprintf("%s/%s.html", strings.ToLower(path.Join(search...)), layout))
|
|
|
|
}
|
|
|
|
layouts = append(layouts, fmt.Sprintf("%s.html", layout))
|
|
|
|
return
|
2013-07-04 11:32:55 -04:00
|
|
|
}
|
|
|
|
|
2013-08-05 10:53:58 -04:00
|
|
|
func ReadFrom(buf io.Reader, name string) (page *Page, err error) {
|
|
|
|
if len(name) == 0 {
|
|
|
|
return nil, errors.New("Zero length page name")
|
|
|
|
}
|
|
|
|
|
2013-09-04 19:57:17 -04:00
|
|
|
p := newPage(name)
|
2013-08-05 10:53:58 -04:00
|
|
|
|
|
|
|
if err = p.parse(buf); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
p.analyzePage()
|
|
|
|
|
2013-08-13 19:39:24 -04:00
|
|
|
return p, nil
|
2013-07-04 11:32:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Page) analyzePage() {
|
2013-10-15 09:15:52 -04:00
|
|
|
p.WordCount = TotalWords(p.Plain())
|
2013-07-04 11:32:55 -04:00
|
|
|
p.FuzzyWordCount = int((p.WordCount+100)/100) * 100
|
2013-10-15 09:32:21 -04:00
|
|
|
p.MinRead = int((p.WordCount + 212) / 213)
|
2013-07-04 11:32:55 -04:00
|
|
|
}
|
|
|
|
|
2013-10-02 19:33:51 -04:00
|
|
|
func (p *Page) permalink() (*url.URL, error) {
|
2013-08-28 12:33:10 -04:00
|
|
|
baseUrl := string(p.Site.BaseUrl)
|
2013-10-08 12:33:57 -04:00
|
|
|
dir := strings.TrimSpace(p.Dir)
|
2013-08-28 12:33:10 -04:00
|
|
|
pSlug := strings.TrimSpace(p.Slug)
|
|
|
|
pUrl := strings.TrimSpace(p.Url)
|
2013-09-12 13:48:59 -04:00
|
|
|
var permalink string
|
2013-11-18 04:35:56 -05:00
|
|
|
var err error
|
|
|
|
|
|
|
|
if override, ok := p.Site.Permalinks[p.Section]; ok {
|
|
|
|
permalink, err = override.Expand(p)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2013-07-26 09:51:07 -04:00
|
|
|
}
|
2013-11-18 04:35:56 -05:00
|
|
|
//fmt.Printf("have an override for %q in section %s → %s\n", p.Title, p.Section, permalink)
|
2013-07-04 11:32:55 -04:00
|
|
|
} else {
|
2013-11-18 04:35:56 -05:00
|
|
|
|
|
|
|
if len(pSlug) > 0 {
|
|
|
|
if p.Site.Config != nil && p.Site.Config.UglyUrls {
|
|
|
|
permalink = path.Join(dir, p.Slug, p.Extension)
|
|
|
|
} else {
|
2013-11-19 08:10:03 -05:00
|
|
|
permalink = path.Join(dir, p.Slug) + "/"
|
2013-11-18 04:35:56 -05:00
|
|
|
}
|
|
|
|
} else if len(pUrl) > 2 {
|
|
|
|
permalink = pUrl
|
2013-07-26 09:51:07 -04:00
|
|
|
} else {
|
2013-11-18 04:35:56 -05:00
|
|
|
_, t := path.Split(p.FileName)
|
|
|
|
if p.Site.Config != nil && p.Site.Config.UglyUrls {
|
|
|
|
x := replaceExtension(strings.TrimSpace(t), p.Extension)
|
|
|
|
permalink = path.Join(dir, x)
|
|
|
|
} else {
|
|
|
|
file, _ := fileExt(strings.TrimSpace(t))
|
|
|
|
permalink = path.Join(dir, file)
|
|
|
|
}
|
2013-07-26 09:51:07 -04:00
|
|
|
}
|
2013-11-18 04:35:56 -05:00
|
|
|
|
2013-07-04 11:32:55 -04:00
|
|
|
}
|
2013-09-25 00:24:49 -04:00
|
|
|
|
|
|
|
base, err := url.Parse(baseUrl)
|
|
|
|
if err != nil {
|
2013-10-02 19:33:51 -04:00
|
|
|
return nil, err
|
2013-09-25 00:24:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
path, err := url.Parse(permalink)
|
|
|
|
if err != nil {
|
2013-10-02 19:33:51 -04:00
|
|
|
return nil, err
|
2013-09-25 00:24:49 -04:00
|
|
|
}
|
|
|
|
|
2013-10-02 19:33:51 -04:00
|
|
|
return MakePermalink(base, path), nil
|
|
|
|
}
|
|
|
|
|
2013-10-25 18:37:53 -04:00
|
|
|
func (p *Page) LinkTitle() string {
|
|
|
|
if len(p.linkTitle) > 0 {
|
|
|
|
return p.linkTitle
|
|
|
|
} else {
|
|
|
|
return p.Title
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-02 19:33:51 -04:00
|
|
|
func (p *Page) Permalink() (string, error) {
|
|
|
|
link, err := p.permalink()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return link.String(), nil
|
2013-07-04 11:32:55 -04:00
|
|
|
}
|
|
|
|
|
2013-10-02 20:00:21 -04:00
|
|
|
func (p *Page) RelPermalink() (string, error) {
|
|
|
|
link, err := p.permalink()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
link.Scheme = ""
|
|
|
|
link.Host = ""
|
|
|
|
link.User = nil
|
|
|
|
link.Opaque = ""
|
|
|
|
return link.String(), nil
|
|
|
|
}
|
|
|
|
|
2013-07-09 18:53:08 -04:00
|
|
|
func (page *Page) handleTomlMetaData(datum []byte) (interface{}, error) {
|
2013-07-08 22:23:54 -04:00
|
|
|
m := map[string]interface{}{}
|
2013-08-25 00:27:41 -04:00
|
|
|
datum = removeTomlIdentifier(datum)
|
2013-07-08 22:23:54 -04:00
|
|
|
if _, err := toml.Decode(string(datum), &m); err != nil {
|
2013-07-09 18:53:08 -04:00
|
|
|
return m, fmt.Errorf("Invalid TOML in %s \nError parsing page meta data: %s", page.FileName, err)
|
2013-07-08 22:23:54 -04:00
|
|
|
}
|
2013-07-09 18:53:08 -04:00
|
|
|
return m, nil
|
2013-07-08 22:23:54 -04:00
|
|
|
}
|
|
|
|
|
2013-08-25 00:27:41 -04:00
|
|
|
func removeTomlIdentifier(datum []byte) []byte {
|
|
|
|
return bytes.Replace(datum, []byte("+++"), []byte(""), -1)
|
|
|
|
}
|
|
|
|
|
2013-07-09 18:53:08 -04:00
|
|
|
func (page *Page) handleYamlMetaData(datum []byte) (interface{}, error) {
|
2013-07-06 22:58:08 -04:00
|
|
|
m := map[string]interface{}{}
|
|
|
|
if err := goyaml.Unmarshal(datum, &m); err != nil {
|
2013-07-09 18:53:08 -04:00
|
|
|
return m, fmt.Errorf("Invalid YAML in %s \nError parsing page meta data: %s", page.FileName, err)
|
2013-07-04 11:32:55 -04:00
|
|
|
}
|
2013-07-09 18:53:08 -04:00
|
|
|
return m, nil
|
2013-07-07 00:49:57 -04:00
|
|
|
}
|
|
|
|
|
2013-07-19 03:10:42 -04:00
|
|
|
func (page *Page) handleJsonMetaData(datum []byte) (interface{}, error) {
|
2013-07-07 00:49:57 -04:00
|
|
|
var f interface{}
|
|
|
|
if err := json.Unmarshal(datum, &f); err != nil {
|
2013-07-09 18:53:08 -04:00
|
|
|
return f, fmt.Errorf("Invalid JSON in %v \nError parsing page meta data: %s", page.FileName, err)
|
2013-07-07 00:49:57 -04:00
|
|
|
}
|
2013-07-09 18:53:08 -04:00
|
|
|
return f, nil
|
2013-07-07 00:49:57 -04:00
|
|
|
}
|
|
|
|
|
2013-08-05 10:53:58 -04:00
|
|
|
func (page *Page) update(f interface{}) error {
|
2013-07-07 00:49:57 -04:00
|
|
|
m := f.(map[string]interface{})
|
2013-07-04 11:32:55 -04:00
|
|
|
|
|
|
|
for k, v := range m {
|
2013-10-24 17:31:04 -04:00
|
|
|
loki := strings.ToLower(k)
|
|
|
|
switch loki {
|
2013-07-04 11:32:55 -04:00
|
|
|
case "title":
|
|
|
|
page.Title = interfaceToString(v)
|
2013-10-25 18:37:53 -04:00
|
|
|
case "linktitle":
|
|
|
|
page.linkTitle = interfaceToString(v)
|
2013-07-04 11:32:55 -04:00
|
|
|
case "description":
|
|
|
|
page.Description = interfaceToString(v)
|
|
|
|
case "slug":
|
2013-12-05 09:29:41 -05:00
|
|
|
page.Slug = helpers.Urlize(interfaceToString(v))
|
2013-07-04 11:32:55 -04:00
|
|
|
case "url":
|
|
|
|
if url := interfaceToString(v); strings.HasPrefix(url, "http://") || strings.HasPrefix(url, "https://") {
|
|
|
|
return fmt.Errorf("Only relative urls are supported, %v provided", url)
|
|
|
|
}
|
2013-12-05 09:29:41 -05:00
|
|
|
page.Url = helpers.Urlize(interfaceToString(v))
|
2013-07-04 11:32:55 -04:00
|
|
|
case "type":
|
|
|
|
page.contentType = interfaceToString(v)
|
|
|
|
case "keywords":
|
|
|
|
page.Keywords = interfaceArrayToStringArray(v)
|
|
|
|
case "date", "pubdate":
|
2013-10-24 18:18:57 -04:00
|
|
|
page.Date = interfaceToTime(v)
|
2013-07-04 11:32:55 -04:00
|
|
|
case "draft":
|
|
|
|
page.Draft = interfaceToBool(v)
|
|
|
|
case "layout":
|
|
|
|
page.layout = interfaceToString(v)
|
2013-07-06 22:31:43 -04:00
|
|
|
case "markup":
|
|
|
|
page.Markup = interfaceToString(v)
|
2013-10-18 11:01:31 -04:00
|
|
|
case "weight":
|
|
|
|
page.Weight = interfaceToInt(v)
|
2013-08-10 09:08:38 -04:00
|
|
|
case "aliases":
|
|
|
|
page.Aliases = interfaceArrayToStringArray(v)
|
|
|
|
for _, alias := range page.Aliases {
|
|
|
|
if strings.HasPrefix(alias, "http://") || strings.HasPrefix(alias, "https://") {
|
|
|
|
return fmt.Errorf("Only relative aliases are supported, %v provided", alias)
|
|
|
|
}
|
|
|
|
}
|
2013-07-04 11:32:55 -04:00
|
|
|
case "status":
|
|
|
|
page.Status = interfaceToString(v)
|
|
|
|
default:
|
|
|
|
// If not one of the explicit values, store in Params
|
|
|
|
switch vv := v.(type) {
|
2013-10-24 17:31:04 -04:00
|
|
|
case string:
|
|
|
|
page.Params[loki] = vv
|
|
|
|
case int64, int32, int16, int8, int:
|
|
|
|
page.Params[loki] = vv
|
|
|
|
case float64, float32:
|
|
|
|
page.Params[loki] = vv
|
|
|
|
case time.Time:
|
|
|
|
page.Params[loki] = vv
|
2013-07-04 11:32:55 -04:00
|
|
|
default: // handle array of strings as well
|
|
|
|
switch vvv := vv.(type) {
|
|
|
|
case []interface{}:
|
|
|
|
var a = make([]string, len(vvv))
|
|
|
|
for i, u := range vvv {
|
|
|
|
a[i] = interfaceToString(u)
|
|
|
|
}
|
2013-10-24 18:18:57 -04:00
|
|
|
page.Params[loki] = a
|
2013-07-04 11:32:55 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
2013-07-07 00:49:57 -04:00
|
|
|
|
2013-07-04 11:32:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (page *Page) GetParam(key string) interface{} {
|
|
|
|
v := page.Params[strings.ToLower(key)]
|
|
|
|
|
|
|
|
if v == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
switch v.(type) {
|
|
|
|
case string:
|
|
|
|
return interfaceToString(v)
|
2013-10-24 17:31:04 -04:00
|
|
|
case int64, int32, int16, int8, int:
|
|
|
|
return interfaceToInt(v)
|
|
|
|
case float64, float32:
|
|
|
|
return interfaceToFloat64(v)
|
|
|
|
case time.Time:
|
|
|
|
return interfaceToTime(v)
|
2013-07-04 11:32:55 -04:00
|
|
|
case []string:
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-08-05 10:53:58 -04:00
|
|
|
type frontmatterType struct {
|
|
|
|
markstart, markend []byte
|
|
|
|
parse func([]byte) (interface{}, error)
|
2013-08-07 02:04:49 -04:00
|
|
|
includeMark bool
|
2013-08-05 10:53:58 -04:00
|
|
|
}
|
|
|
|
|
2013-09-18 13:17:43 -04:00
|
|
|
const YAML_DELIM = "---"
|
|
|
|
const TOML_DELIM = "+++"
|
|
|
|
|
2013-08-05 10:53:58 -04:00
|
|
|
func (page *Page) detectFrontMatter(mark rune) (f *frontmatterType) {
|
|
|
|
switch mark {
|
|
|
|
case '-':
|
2013-09-18 13:17:43 -04:00
|
|
|
return &frontmatterType{[]byte(YAML_DELIM), []byte(YAML_DELIM), page.handleYamlMetaData, false}
|
2013-08-05 10:53:58 -04:00
|
|
|
case '+':
|
2013-09-18 13:17:43 -04:00
|
|
|
return &frontmatterType{[]byte(TOML_DELIM), []byte(TOML_DELIM), page.handleTomlMetaData, false}
|
2013-08-05 10:53:58 -04:00
|
|
|
case '{':
|
2013-08-07 02:04:49 -04:00
|
|
|
return &frontmatterType{[]byte{'{'}, []byte{'}'}, page.handleJsonMetaData, true}
|
2013-08-05 10:53:58 -04:00
|
|
|
default:
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-03 15:41:13 -04:00
|
|
|
func (p *Page) Render(layout ...string) template.HTML {
|
2013-07-04 11:32:55 -04:00
|
|
|
curLayout := ""
|
|
|
|
|
|
|
|
if len(layout) > 0 {
|
|
|
|
curLayout = layout[0]
|
|
|
|
}
|
|
|
|
|
2013-09-03 15:41:13 -04:00
|
|
|
return template.HTML(string(p.ExecuteTemplate(curLayout).Bytes()))
|
2013-07-04 11:32:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Page) ExecuteTemplate(layout string) *bytes.Buffer {
|
|
|
|
l := p.Layout(layout)
|
|
|
|
buffer := new(bytes.Buffer)
|
2013-10-07 00:57:45 -04:00
|
|
|
for _, layout := range l {
|
|
|
|
if p.Tmpl.Lookup(layout) != nil {
|
|
|
|
p.Tmpl.ExecuteTemplate(buffer, layout, p)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2013-07-04 11:32:55 -04:00
|
|
|
return buffer
|
|
|
|
}
|
|
|
|
|
2013-09-18 17:21:27 -04:00
|
|
|
func (page *Page) guessMarkupType() string {
|
|
|
|
if page.Markup != "" {
|
|
|
|
return page.Markup
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.HasSuffix(page.FileName, ".md") {
|
|
|
|
return "md"
|
|
|
|
}
|
|
|
|
|
|
|
|
return "unknown"
|
|
|
|
}
|
|
|
|
|
2013-08-05 10:53:58 -04:00
|
|
|
func (page *Page) parse(reader io.Reader) error {
|
2013-08-25 00:27:41 -04:00
|
|
|
p, err := parser.ReadFrom(reader)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-07-04 11:32:55 -04:00
|
|
|
|
2013-09-18 13:17:43 -04:00
|
|
|
page.renderable = p.IsRenderable()
|
|
|
|
|
2013-08-25 00:27:41 -04:00
|
|
|
front := p.FrontMatter()
|
2013-07-04 11:32:55 -04:00
|
|
|
|
2013-09-18 13:17:43 -04:00
|
|
|
if len(front) != 0 {
|
|
|
|
fm := page.detectFrontMatter(rune(front[0]))
|
|
|
|
meta, err := fm.parse(front)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = page.update(meta); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-12-06 23:32:00 -05:00
|
|
|
|
2013-08-25 00:27:41 -04:00
|
|
|
}
|
2013-12-06 23:32:00 -05:00
|
|
|
page.Content = template.HTML(p.Content())
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2013-08-25 00:27:41 -04:00
|
|
|
|
2013-12-06 23:32:00 -05:00
|
|
|
func (page *Page) Convert() error {
|
2013-09-18 17:21:27 -04:00
|
|
|
switch page.guessMarkupType() {
|
|
|
|
case "md", "markdown", "mdown":
|
2013-12-06 23:32:00 -05:00
|
|
|
page.convertMarkdown(bytes.NewReader([]byte(page.Content)))
|
2013-07-06 22:31:43 -04:00
|
|
|
case "rst":
|
2013-12-06 23:32:00 -05:00
|
|
|
page.convertRestructuredText(bytes.NewReader([]byte(page.Content)))
|
2013-09-18 17:21:27 -04:00
|
|
|
case "html":
|
|
|
|
fallthrough
|
|
|
|
default:
|
2013-12-06 23:32:00 -05:00
|
|
|
page.Content = template.HTML(page.Content)
|
2013-07-06 22:31:43 -04:00
|
|
|
}
|
2013-07-04 11:32:55 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-08-05 10:53:58 -04:00
|
|
|
func (page *Page) convertMarkdown(lines io.Reader) {
|
|
|
|
b := new(bytes.Buffer)
|
|
|
|
b.ReadFrom(lines)
|
2013-08-21 05:37:14 -04:00
|
|
|
content := b.Bytes()
|
2013-09-03 15:41:13 -04:00
|
|
|
page.Content = template.HTML(string(blackfriday.MarkdownCommon(RemoveSummaryDivider(content))))
|
2013-11-10 15:04:51 -05:00
|
|
|
summary, truncated := getSummaryString(content, "markdown")
|
2013-10-04 12:28:28 -04:00
|
|
|
page.Summary = template.HTML(string(summary))
|
2013-11-10 15:04:51 -05:00
|
|
|
page.Truncated = truncated
|
2013-07-04 11:32:55 -04:00
|
|
|
}
|
2013-07-06 22:31:43 -04:00
|
|
|
|
2013-08-05 10:53:58 -04:00
|
|
|
func (page *Page) convertRestructuredText(lines io.Reader) {
|
2013-08-21 05:37:14 -04:00
|
|
|
b := new(bytes.Buffer)
|
|
|
|
b.ReadFrom(lines)
|
|
|
|
content := b.Bytes()
|
2013-09-03 15:41:13 -04:00
|
|
|
page.Content = template.HTML(getRstContent(content))
|
2013-11-10 15:04:51 -05:00
|
|
|
summary, truncated := getSummaryString(content, "rst")
|
2013-10-04 12:28:28 -04:00
|
|
|
page.Summary = template.HTML(string(summary))
|
2013-11-10 15:04:51 -05:00
|
|
|
page.Truncated = truncated
|
2013-07-06 22:31:43 -04:00
|
|
|
}
|
2013-09-20 20:24:25 -04:00
|
|
|
|
|
|
|
func (p *Page) TargetPath() (outfile string) {
|
|
|
|
|
|
|
|
// Always use Url if it's specified
|
|
|
|
if len(strings.TrimSpace(p.Url)) > 2 {
|
|
|
|
outfile = strings.TrimSpace(p.Url)
|
|
|
|
|
|
|
|
if strings.HasSuffix(outfile, "/") {
|
|
|
|
outfile = outfile + "index.html"
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2013-11-18 04:35:56 -05:00
|
|
|
// If there's a Permalink specification, we use that
|
|
|
|
if override, ok := p.Site.Permalinks[p.Section]; ok {
|
|
|
|
var err error
|
|
|
|
outfile, err = override.Expand(p)
|
|
|
|
if err == nil {
|
|
|
|
if strings.HasSuffix(outfile, "/") {
|
|
|
|
outfile += "index.html"
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-20 20:24:25 -04:00
|
|
|
if len(strings.TrimSpace(p.Slug)) > 0 {
|
|
|
|
outfile = strings.TrimSpace(p.Slug) + "." + p.Extension
|
|
|
|
} else {
|
|
|
|
// Fall back to filename
|
|
|
|
_, t := path.Split(p.FileName)
|
|
|
|
outfile = replaceExtension(strings.TrimSpace(t), p.Extension)
|
|
|
|
}
|
|
|
|
|
|
|
|
return path.Join(p.Dir, strings.TrimSpace(outfile))
|
|
|
|
}
|