2017-06-10 21:38:09 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2017-06-15 17:30:44 +02:00
|
|
|
"fmt"
|
2017-06-10 21:38:09 +02:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
|
2017-06-15 17:30:44 +02:00
|
|
|
"github.com/acstech/liquid"
|
|
|
|
|
2017-06-10 21:38:09 +02:00
|
|
|
yaml "gopkg.in/yaml.v2"
|
|
|
|
)
|
|
|
|
|
2017-06-13 17:00:24 +02:00
|
|
|
// Site is a Jekyll site.
|
|
|
|
type Site struct {
|
2017-06-13 17:27:24 +02:00
|
|
|
ConfigFile *string
|
|
|
|
Source string
|
|
|
|
Dest string
|
|
|
|
|
|
|
|
Collections []*Collection
|
2017-06-14 23:41:15 +02:00
|
|
|
Variables VariableMap
|
2017-06-15 02:44:22 +02:00
|
|
|
Paths map[string]Page // URL path -> Page
|
2017-06-13 17:27:24 +02:00
|
|
|
|
|
|
|
config SiteConfig
|
2017-06-13 17:00:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// For now (and maybe always?), there's just one site.
|
2017-06-13 18:00:14 +02:00
|
|
|
var site = NewSite()
|
2017-06-13 17:00:24 +02:00
|
|
|
|
2017-06-10 21:38:09 +02:00
|
|
|
// SiteConfig is the Jekyll site configuration, typically read from _config.yml.
|
2017-06-12 23:12:40 +02:00
|
|
|
// See https://jekyllrb.com/docs/configuration/#default-configuration
|
2017-06-10 21:38:09 +02:00
|
|
|
type SiteConfig struct {
|
2017-06-13 14:54:35 +02:00
|
|
|
// Where things are:
|
2017-06-13 17:27:24 +02:00
|
|
|
Source string
|
|
|
|
Destination string
|
2017-06-14 23:41:15 +02:00
|
|
|
Collections map[string]VariableMap
|
2017-06-12 23:12:40 +02:00
|
|
|
|
2017-06-13 14:54:35 +02:00
|
|
|
// Handling Reading
|
|
|
|
Include []string
|
|
|
|
Exclude []string
|
|
|
|
MarkdownExt string `yaml:"markdown_ext"`
|
|
|
|
|
|
|
|
// Outputting
|
2017-06-12 23:12:40 +02:00
|
|
|
Permalink string
|
2017-06-10 21:38:09 +02:00
|
|
|
}
|
|
|
|
|
2017-06-12 23:12:40 +02:00
|
|
|
const siteConfigDefaults = `
|
|
|
|
# Where things are
|
|
|
|
source: .
|
|
|
|
destination: ./_site
|
|
|
|
include: [".htaccess"]
|
|
|
|
data_dir: _data
|
|
|
|
includes_dir: _includes
|
|
|
|
collections:
|
|
|
|
posts:
|
|
|
|
output: true
|
|
|
|
|
|
|
|
# Handling Reading
|
|
|
|
include: [".htaccess"]
|
|
|
|
exclude: ["Gemfile", "Gemfile.lock", "node_modules", "vendor/bundle/", "vendor/cache/", "vendor/gems/", "vendor/ruby/"]
|
|
|
|
keep_files: [".git", ".svn"]
|
|
|
|
encoding: "utf-8"
|
|
|
|
markdown_ext: "markdown,mkdown,mkdn,mkd,md"
|
|
|
|
strict_front_matter: false
|
|
|
|
|
|
|
|
# Outputting
|
|
|
|
permalink: date
|
|
|
|
paginate_path: /page:num
|
|
|
|
timezone: null
|
|
|
|
`
|
|
|
|
|
2017-06-13 17:00:24 +02:00
|
|
|
//TODO permalink: "/:categories/:year/:month/:day/:title.html",
|
2017-06-13 14:54:35 +02:00
|
|
|
|
2017-06-13 18:00:14 +02:00
|
|
|
// NewSite creates a new site.
|
|
|
|
func NewSite() *Site {
|
|
|
|
s := new(Site)
|
2017-06-14 19:20:52 +02:00
|
|
|
if err := s.readConfigBytes([]byte(siteConfigDefaults)); err != nil {
|
2017-06-13 14:54:35 +02:00
|
|
|
panic(err)
|
2017-06-12 23:12:40 +02:00
|
|
|
}
|
2017-06-13 18:00:14 +02:00
|
|
|
return s
|
2017-06-13 14:54:35 +02:00
|
|
|
}
|
|
|
|
|
2017-06-13 18:00:14 +02:00
|
|
|
// ReadConfiguration reads the configuration file, if it exists.
|
|
|
|
func (s *Site) ReadConfiguration(source, dest string) error {
|
2017-06-13 17:27:24 +02:00
|
|
|
configPath := filepath.Join(source, "_config.yml")
|
2017-06-13 18:00:14 +02:00
|
|
|
bytes, err := ioutil.ReadFile(configPath)
|
2017-06-13 17:27:24 +02:00
|
|
|
switch {
|
|
|
|
case err == nil:
|
2017-06-14 19:20:52 +02:00
|
|
|
if err = site.readConfigBytes(bytes); err != nil {
|
2017-06-13 17:27:24 +02:00
|
|
|
return err
|
|
|
|
}
|
2017-06-13 18:00:14 +02:00
|
|
|
s.Source = filepath.Join(source, s.config.Source)
|
|
|
|
s.Dest = filepath.Join(s.Source, s.config.Destination)
|
|
|
|
s.ConfigFile = &configPath
|
2017-06-13 17:27:24 +02:00
|
|
|
if dest != "" {
|
|
|
|
site.Dest = dest
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
case os.IsNotExist(err):
|
|
|
|
return nil
|
|
|
|
default:
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-14 19:20:52 +02:00
|
|
|
func (s *Site) readConfigBytes(bytes []byte) error {
|
2017-06-14 23:41:15 +02:00
|
|
|
configVariables := VariableMap{}
|
2017-06-13 18:00:14 +02:00
|
|
|
if err := yaml.Unmarshal(bytes, &s.config); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-06-14 19:20:52 +02:00
|
|
|
if err := yaml.Unmarshal(bytes, &configVariables); err != nil {
|
2017-06-12 23:12:40 +02:00
|
|
|
return err
|
2017-06-10 21:38:09 +02:00
|
|
|
}
|
2017-06-14 23:41:15 +02:00
|
|
|
s.Variables = mergeVariableMaps(s.Variables, configVariables)
|
2017-06-13 18:00:14 +02:00
|
|
|
return nil
|
2017-06-10 21:38:09 +02:00
|
|
|
}
|
|
|
|
|
2017-06-15 17:30:44 +02:00
|
|
|
// FindLayout returns a template for the named layout.
|
2017-06-15 17:51:40 +02:00
|
|
|
func (s *Site) FindLayout(name string, fm *VariableMap) (t *liquid.Template, err error) {
|
2017-06-15 17:30:44 +02:00
|
|
|
exts := []string{"", ".html"}
|
|
|
|
for _, ext := range strings.SplitN(s.config.MarkdownExt, `,`, -1) {
|
|
|
|
exts = append(exts, "."+ext)
|
|
|
|
}
|
|
|
|
var (
|
|
|
|
path string
|
|
|
|
content []byte
|
|
|
|
found bool
|
|
|
|
)
|
|
|
|
for _, ext := range exts {
|
|
|
|
// TODO respect layout config
|
|
|
|
path = filepath.Join(s.Source, "_layouts", name+ext)
|
|
|
|
content, err = ioutil.ReadFile(path)
|
|
|
|
if err == nil {
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if !os.IsNotExist(err) {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
panic(fmt.Errorf("no template for %s", name))
|
|
|
|
}
|
2017-06-15 17:51:40 +02:00
|
|
|
*fm, err = readFrontMatter(&content)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2017-06-15 17:30:44 +02:00
|
|
|
return liquid.Parse(content, nil)
|
|
|
|
}
|
|
|
|
|
2017-06-13 23:19:05 +02:00
|
|
|
// KeepFile returns a boolean indicating that clean should leave the file in the destination directory.
|
2017-06-13 18:00:14 +02:00
|
|
|
func (s *Site) KeepFile(path string) bool {
|
2017-06-13 17:00:24 +02:00
|
|
|
// TODO
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2017-06-15 17:30:44 +02:00
|
|
|
// MarkdownExtensions returns a set of markdown extension, without the final dots.
|
2017-06-13 17:00:24 +02:00
|
|
|
func (s *Site) MarkdownExtensions() map[string]bool {
|
2017-06-13 17:27:24 +02:00
|
|
|
extns := strings.SplitN(s.config.MarkdownExt, `,`, -1)
|
2017-06-13 14:54:35 +02:00
|
|
|
return stringArrayToMap(extns)
|
|
|
|
}
|
2017-06-13 14:55:15 +02:00
|
|
|
|
2017-06-13 17:00:24 +02:00
|
|
|
// GetFileURL returns the URL path given a file path, relative to the site source directory.
|
|
|
|
func (s *Site) GetFileURL(path string) (string, bool) {
|
2017-06-15 02:44:22 +02:00
|
|
|
for _, p := range s.Paths {
|
|
|
|
if p.Path() == path {
|
|
|
|
return p.Permalink(), true
|
2017-06-13 14:55:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return "", false
|
|
|
|
}
|
|
|
|
|
2017-06-13 23:19:05 +02:00
|
|
|
// Exclude returns a boolean indicating that the site excludes a file.
|
2017-06-13 17:00:24 +02:00
|
|
|
func (s *Site) Exclude(path string) bool {
|
|
|
|
// TODO exclude based on glob, not exact match
|
2017-06-13 18:38:06 +02:00
|
|
|
inclusionMap := stringArrayToMap(s.config.Include)
|
2017-06-13 17:27:24 +02:00
|
|
|
exclusionMap := stringArrayToMap(s.config.Exclude)
|
2017-06-13 17:00:24 +02:00
|
|
|
base := filepath.Base(path)
|
|
|
|
switch {
|
2017-06-13 18:38:06 +02:00
|
|
|
case inclusionMap[path]:
|
|
|
|
return false
|
2017-06-13 17:00:24 +02:00
|
|
|
case path == ".":
|
|
|
|
return false
|
|
|
|
case exclusionMap[path]:
|
|
|
|
return true
|
|
|
|
case strings.HasPrefix(base, "."), strings.HasPrefix(base, "_"):
|
|
|
|
return true
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
2017-06-10 21:38:09 +02:00
|
|
|
|
2017-06-13 17:00:24 +02:00
|
|
|
// ReadFiles scans the source directory and creates pages and collections.
|
|
|
|
func (s *Site) ReadFiles() error {
|
2017-06-15 02:44:22 +02:00
|
|
|
s.Paths = make(map[string]Page)
|
2017-06-14 23:41:15 +02:00
|
|
|
defaults := VariableMap{}
|
2017-06-12 23:12:40 +02:00
|
|
|
|
2017-06-10 21:38:09 +02:00
|
|
|
walkFn := func(path string, info os.FileInfo, err error) error {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-06-13 17:27:24 +02:00
|
|
|
rel, err := filepath.Rel(s.Source, path)
|
2017-06-10 21:38:09 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-06-13 17:00:24 +02:00
|
|
|
switch {
|
|
|
|
case info.IsDir() && s.Exclude(rel):
|
|
|
|
return filepath.SkipDir
|
|
|
|
case info.IsDir(), s.Exclude(rel):
|
2017-06-10 21:38:09 +02:00
|
|
|
return nil
|
|
|
|
}
|
2017-06-14 19:20:52 +02:00
|
|
|
p, err := ReadPage(rel, defaults)
|
2017-06-10 23:51:46 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2017-06-10 21:38:09 +02:00
|
|
|
}
|
2017-06-15 02:44:22 +02:00
|
|
|
if p.Published() {
|
|
|
|
s.Paths[p.Permalink()] = p
|
2017-06-11 01:32:39 +02:00
|
|
|
}
|
2017-06-10 21:38:09 +02:00
|
|
|
return nil
|
|
|
|
}
|
2017-06-11 01:32:39 +02:00
|
|
|
|
2017-06-13 17:27:24 +02:00
|
|
|
if err := filepath.Walk(s.Source, walkFn); err != nil {
|
2017-06-13 17:00:24 +02:00
|
|
|
return err
|
2017-06-13 14:55:15 +02:00
|
|
|
}
|
2017-06-14 19:20:52 +02:00
|
|
|
if err := s.readCollections(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-06-15 13:19:49 +02:00
|
|
|
s.initTemplateAttributes()
|
2017-06-14 19:20:52 +02:00
|
|
|
return nil
|
2017-06-13 14:55:15 +02:00
|
|
|
}
|
2017-06-10 23:51:46 +02:00
|
|
|
|
2017-06-14 19:20:52 +02:00
|
|
|
// readCollections scans the file system for collections. It adds each collection's
|
2017-06-13 14:55:15 +02:00
|
|
|
// pages to the site map, and creates a template site variable for each collection.
|
2017-06-14 19:20:52 +02:00
|
|
|
func (s *Site) readCollections() error {
|
2017-06-13 17:27:24 +02:00
|
|
|
for name, d := range s.config.Collections {
|
2017-06-14 23:41:15 +02:00
|
|
|
c := makeCollection(s, name, d)
|
2017-06-13 17:27:24 +02:00
|
|
|
s.Collections = append(s.Collections, c)
|
2017-06-13 17:00:24 +02:00
|
|
|
if c.Output { // TODO always read the pages; just don't build them / include them in routes
|
|
|
|
if err := c.ReadPages(); err != nil {
|
2017-06-13 14:55:15 +02:00
|
|
|
return err
|
2017-06-10 23:51:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-06-13 14:55:15 +02:00
|
|
|
return nil
|
|
|
|
}
|
2017-06-14 19:20:52 +02:00
|
|
|
|
2017-06-15 13:19:49 +02:00
|
|
|
func (s *Site) initTemplateAttributes() {
|
2017-06-14 19:20:52 +02:00
|
|
|
// TODO site: {time, pages, posts, related_posts, static_files, html_pages, html_files, collections, data, documents, categories.CATEGORY, tags.TAG}
|
|
|
|
for _, c := range s.Collections {
|
2017-06-15 13:19:49 +02:00
|
|
|
s.Variables[c.Name] = c.PageTemplateObjects()
|
2017-06-14 19:20:52 +02:00
|
|
|
}
|
|
|
|
}
|