2017-06-10 15:38:09 -04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
2017-06-11 20:51:01 -04:00
|
|
|
"io"
|
2017-06-10 15:38:09 -04:00
|
|
|
"io/ioutil"
|
2017-06-11 11:51:25 -04:00
|
|
|
"os"
|
2017-06-10 15:38:09 -04:00
|
|
|
"path/filepath"
|
2017-06-14 20:44:22 -04:00
|
|
|
"reflect"
|
2017-06-10 15:38:09 -04:00
|
|
|
"regexp"
|
2017-06-13 08:54:35 -04:00
|
|
|
"strings"
|
2017-06-10 15:38:09 -04:00
|
|
|
|
2017-06-15 09:07:06 -04:00
|
|
|
yaml "gopkg.in/yaml.v2"
|
|
|
|
|
2017-06-10 15:38:09 -04:00
|
|
|
"github.com/acstech/liquid"
|
|
|
|
"github.com/russross/blackfriday"
|
|
|
|
)
|
|
|
|
|
2017-06-11 11:51:25 -04:00
|
|
|
var (
|
|
|
|
frontmatterMatcher = regexp.MustCompile(`(?s)^---\n(.+?\n)---\n`)
|
2017-06-13 08:51:58 -04:00
|
|
|
templateVariableMatcher = regexp.MustCompile(`:\w+\b`)
|
2017-06-11 11:51:25 -04:00
|
|
|
nonAlphanumericSequenceMatcher = regexp.MustCompile(`[^[:alnum:]]+`)
|
|
|
|
)
|
|
|
|
|
2017-06-15 07:19:49 -04:00
|
|
|
// Page is a Jekyll page.
|
2017-06-14 20:44:22 -04:00
|
|
|
type Page interface {
|
|
|
|
Path() string
|
|
|
|
Source() string
|
|
|
|
Static() bool
|
|
|
|
Published() bool
|
|
|
|
Permalink() string
|
2017-06-15 07:19:49 -04:00
|
|
|
TemplateObject() VariableMap
|
2017-06-14 20:44:22 -04:00
|
|
|
Write(io.Writer) error
|
|
|
|
DebugVariables() VariableMap
|
2017-06-15 09:31:04 -04:00
|
|
|
|
|
|
|
setPermalink(string)
|
2017-06-14 20:44:22 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
type pageFields struct {
|
|
|
|
path string // this is the relative path
|
|
|
|
permalink string
|
2017-06-15 07:31:52 -04:00
|
|
|
frontMatter VariableMap
|
2017-06-10 15:38:09 -04:00
|
|
|
}
|
|
|
|
|
2017-06-14 20:44:22 -04:00
|
|
|
func (p *pageFields) String() string {
|
|
|
|
return fmt.Sprintf("%s{Path=%v, Permalink=%v}",
|
|
|
|
reflect.TypeOf(p).Name(), p.path, p.permalink)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *pageFields) Path() string { return p.path }
|
|
|
|
func (p *pageFields) Permalink() string { return p.permalink }
|
2017-06-15 09:01:31 -04:00
|
|
|
|
|
|
|
func (p *pageFields) Published() bool {
|
|
|
|
return p.frontMatter.Bool("published", true)
|
|
|
|
}
|
2017-06-14 20:44:22 -04:00
|
|
|
|
2017-06-15 09:31:04 -04:00
|
|
|
func (p *pageFields) setPermalink(permalink string) {
|
|
|
|
p.permalink = permalink
|
|
|
|
}
|
|
|
|
|
2017-06-15 09:07:06 -04:00
|
|
|
// ReadPage reads a Page from a file, using defaults as the default front matter.
|
|
|
|
func ReadPage(path string, defaults VariableMap) (p Page, err error) {
|
2017-06-15 09:31:04 -04:00
|
|
|
// TODO only read the first four bytes unless it's dynamic
|
2017-06-15 09:07:06 -04:00
|
|
|
source, err := ioutil.ReadFile(filepath.Join(site.Source, path))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-06-15 09:31:04 -04:00
|
|
|
data := pageFields{
|
|
|
|
path: path,
|
|
|
|
frontMatter: defaults,
|
|
|
|
}
|
|
|
|
if string(source[:4]) == "---\n" {
|
|
|
|
p, err = makeDynamicPage(&data, source)
|
2017-06-15 09:07:06 -04:00
|
|
|
} else {
|
2017-06-15 09:31:04 -04:00
|
|
|
p = &StaticPage{data}
|
|
|
|
}
|
|
|
|
if p != nil {
|
|
|
|
// Compute this after creating the page, to pick up the the front matter.
|
|
|
|
// The permalink is computed once instead of on demand, so that subsequent
|
|
|
|
// access needn't check for an error.
|
|
|
|
pattern := data.frontMatter.String("permalink", ":path")
|
|
|
|
permalink, err := expandPermalinkPattern(pattern, data.path, data.frontMatter)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
p.setPermalink(permalink)
|
2017-06-15 09:07:06 -04:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *StaticPage) Write(w io.Writer) error {
|
|
|
|
source, err := ioutil.ReadFile(p.Source())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err = w.Write(source)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// TemplateObject returns the attributes of the template page object.
|
|
|
|
// See https://jekyllrb.com/docs/variables/#page-variables
|
|
|
|
func (p *pageFields) TemplateObject() VariableMap {
|
|
|
|
var (
|
|
|
|
path = "/" + p.path
|
|
|
|
base = filepath.Base(path)
|
|
|
|
ext = filepath.Ext(path)
|
|
|
|
)
|
|
|
|
|
|
|
|
return VariableMap{
|
|
|
|
"path": path,
|
|
|
|
"modified_time": 0, // TODO
|
|
|
|
"name": base,
|
|
|
|
"basename": base[:len(base)-len(ext)],
|
|
|
|
"extname": ext,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// DebugVariables returns a map that's useful to present during diagnostics.
|
|
|
|
// For a static page, this is just the page's template object attributes.
|
|
|
|
func (p *pageFields) DebugVariables() VariableMap {
|
|
|
|
return p.TemplateObject()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Source returns the file path of the page source.
|
|
|
|
func (p *pageFields) Source() string {
|
|
|
|
return filepath.Join(site.Source, p.path)
|
|
|
|
}
|
|
|
|
|
2017-06-15 07:19:49 -04:00
|
|
|
// StaticPage is a static page.
|
2017-06-14 20:44:22 -04:00
|
|
|
type StaticPage struct {
|
|
|
|
pageFields
|
2017-06-10 15:38:09 -04:00
|
|
|
}
|
|
|
|
|
2017-06-15 09:07:06 -04:00
|
|
|
// Static returns a bool indicating that the page is a static page.
|
|
|
|
func (p *StaticPage) Static() bool { return true }
|
|
|
|
|
|
|
|
// TemplateObject returns metadata for use in the representation of the page as a collection item
|
|
|
|
func (p *StaticPage) TemplateObject() VariableMap {
|
|
|
|
return mergeVariableMaps(p.frontMatter, p.pageFields.TemplateObject())
|
|
|
|
}
|
|
|
|
|
2017-06-15 07:19:49 -04:00
|
|
|
// DynamicPage is a static page, that includes frontmatter.
|
2017-06-14 20:44:22 -04:00
|
|
|
type DynamicPage struct {
|
|
|
|
pageFields
|
|
|
|
Content []byte
|
|
|
|
}
|
|
|
|
|
2017-06-15 07:19:49 -04:00
|
|
|
// Static returns a bool indicatingthat the page is a not static page.
|
2017-06-14 20:44:22 -04:00
|
|
|
func (p *DynamicPage) Static() bool { return false }
|
|
|
|
|
2017-06-15 09:31:04 -04:00
|
|
|
func makeDynamicPage(data *pageFields, source []byte) (*DynamicPage, error) {
|
|
|
|
match := frontmatterMatcher.FindSubmatchIndex(source)
|
|
|
|
// TODO recognize files that begin with "---\n---\n"
|
|
|
|
|
|
|
|
// This fixes the line numbers for template errors
|
|
|
|
// TODO find a less hacky solution
|
2017-06-15 09:07:06 -04:00
|
|
|
body := append(
|
|
|
|
regexp.MustCompile(`[^\n\r]+`).ReplaceAllLiteral(source[:match[1]], []byte{}),
|
|
|
|
source[match[1]:]...)
|
2017-06-15 07:19:49 -04:00
|
|
|
|
2017-06-15 09:07:06 -04:00
|
|
|
frontMatter := VariableMap{}
|
|
|
|
if err := yaml.Unmarshal(source[match[2]:match[3]], &frontMatter); err != nil {
|
2017-06-15 09:31:04 -04:00
|
|
|
err := &os.PathError{Op: "read frontmatter", Path: data.path, Err: err}
|
2017-06-15 09:07:06 -04:00
|
|
|
return nil, err
|
|
|
|
}
|
2017-06-15 09:31:04 -04:00
|
|
|
data.frontMatter = mergeVariableMaps(data.frontMatter, frontMatter)
|
2017-06-15 09:07:06 -04:00
|
|
|
|
|
|
|
p := &DynamicPage{
|
2017-06-15 09:31:04 -04:00
|
|
|
pageFields: *data,
|
|
|
|
Content: body,
|
2017-06-15 09:07:06 -04:00
|
|
|
}
|
|
|
|
return p, nil
|
2017-06-14 20:44:22 -04:00
|
|
|
}
|
|
|
|
|
2017-06-15 07:19:49 -04:00
|
|
|
// TemplateObject returns the attributes of the template page object.
|
|
|
|
func (p *DynamicPage) TemplateObject() VariableMap {
|
2017-06-15 09:01:42 -04:00
|
|
|
var (
|
|
|
|
path = p.path
|
|
|
|
ext = filepath.Ext(path)
|
|
|
|
root = p.path[:len(path)-len(ext)]
|
|
|
|
base = filepath.Base(root)
|
|
|
|
)
|
|
|
|
|
2017-06-14 17:41:15 -04:00
|
|
|
data := VariableMap{
|
2017-06-15 09:01:42 -04:00
|
|
|
"path": p.path,
|
2017-06-14 20:44:22 -04:00
|
|
|
"url": p.Permalink(),
|
2017-06-15 09:01:42 -04:00
|
|
|
// TODO content output
|
|
|
|
|
|
|
|
// not documented, but present in both collection and non-collection pages
|
|
|
|
"permalink": p.Permalink(),
|
|
|
|
|
|
|
|
// TODO only in non-collection pages:
|
|
|
|
// TODO dir
|
|
|
|
// TODO name
|
|
|
|
// TODO next previous
|
|
|
|
|
|
|
|
// TODO Documented as present in all pages, but de facto only defined for collection pages
|
|
|
|
"id": base,
|
|
|
|
"title": base, // TODO capitalize
|
|
|
|
// TODO date (of the collection?) 2017-06-15 07:44:21 -0400
|
|
|
|
// TODO excerpt category? categories tags
|
|
|
|
// TODO slug
|
|
|
|
|
|
|
|
// TODO Only present in collection pages https://jekyllrb.com/docs/collections/#documents
|
2017-06-14 20:44:22 -04:00
|
|
|
"relative_path": p.Path(),
|
2017-06-15 09:01:42 -04:00
|
|
|
// TODO collection(name)
|
|
|
|
|
|
|
|
// TODO undocumented; only present in collection pages:
|
|
|
|
"ext": ext,
|
2017-06-11 18:36:31 -04:00
|
|
|
}
|
2017-06-15 07:31:52 -04:00
|
|
|
for k, v := range p.frontMatter {
|
2017-06-12 17:12:40 -04:00
|
|
|
switch k {
|
2017-06-15 09:01:42 -04:00
|
|
|
// doc implies these aren't present, but they appear to be present in a collection page:
|
|
|
|
// case "layout", "published":
|
|
|
|
case "permalink":
|
|
|
|
// omit this, in order to use the value above
|
2017-06-12 17:12:40 -04:00
|
|
|
default:
|
|
|
|
data[k] = v
|
|
|
|
}
|
2017-06-11 18:36:31 -04:00
|
|
|
}
|
|
|
|
return data
|
|
|
|
}
|
|
|
|
|
2017-06-15 07:19:49 -04:00
|
|
|
// TemplateVariables returns the local variables for template evaluation
|
|
|
|
func (p *DynamicPage) TemplateVariables() VariableMap {
|
2017-06-14 17:41:15 -04:00
|
|
|
return VariableMap{
|
2017-06-15 07:19:49 -04:00
|
|
|
"page": p.TemplateObject(),
|
2017-06-14 13:20:52 -04:00
|
|
|
"site": site.Variables,
|
2017-06-12 17:12:40 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-15 07:19:49 -04:00
|
|
|
// DebugVariables returns a map that's useful to present during diagnostics.
|
|
|
|
// For a dynamic page, this is the local variable map that is used for template evaluation.
|
2017-06-14 20:44:22 -04:00
|
|
|
func (p *DynamicPage) DebugVariables() VariableMap {
|
2017-06-15 07:19:49 -04:00
|
|
|
return p.TemplateVariables()
|
2017-06-14 20:44:22 -04:00
|
|
|
}
|
2017-06-10 15:38:09 -04:00
|
|
|
|
2017-06-14 20:44:22 -04:00
|
|
|
// Write applies Liquid and Markdown, as appropriate.
|
|
|
|
func (p *DynamicPage) Write(w io.Writer) error {
|
2017-06-13 11:00:24 -04:00
|
|
|
parsingLiquid := true
|
|
|
|
defer func() {
|
|
|
|
if parsingLiquid {
|
|
|
|
fmt.Println("While processing", p.Source())
|
|
|
|
}
|
|
|
|
}()
|
2017-06-11 20:30:25 -04:00
|
|
|
template, err := liquid.Parse(p.Content, nil)
|
2017-06-13 11:00:24 -04:00
|
|
|
parsingLiquid = false
|
2017-06-11 20:30:25 -04:00
|
|
|
if err != nil {
|
2017-06-13 08:54:35 -04:00
|
|
|
err := &os.PathError{Op: "Liquid Error", Path: p.Source(), Err: err}
|
2017-06-11 20:51:01 -04:00
|
|
|
return err
|
2017-06-11 20:30:25 -04:00
|
|
|
}
|
|
|
|
writer := new(bytes.Buffer)
|
2017-06-15 07:19:49 -04:00
|
|
|
template.Render(writer, p.TemplateVariables())
|
2017-06-11 20:30:25 -04:00
|
|
|
body := writer.Bytes()
|
|
|
|
|
2017-06-14 20:44:22 -04:00
|
|
|
if isMarkdown(p.path) {
|
2017-06-11 20:51:01 -04:00
|
|
|
body = blackfriday.MarkdownCommon(body)
|
2017-06-11 20:30:25 -04:00
|
|
|
}
|
|
|
|
|
2017-06-11 20:51:01 -04:00
|
|
|
_, err = w.Write(body)
|
|
|
|
return err
|
2017-06-11 20:30:25 -04:00
|
|
|
}
|
|
|
|
|
2017-06-13 08:54:35 -04:00
|
|
|
func isMarkdown(path string) bool {
|
|
|
|
ext := filepath.Ext(path)
|
2017-06-13 11:00:24 -04:00
|
|
|
return site.MarkdownExtensions()[strings.TrimLeft(ext, ".")]
|
2017-06-13 08:54:35 -04:00
|
|
|
}
|