2017-06-10 21:38:09 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
yaml "gopkg.in/yaml.v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
// 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:
|
|
|
|
SourceDir string `yaml:"source"`
|
2017-06-12 23:12:40 +02:00
|
|
|
DestinationDir string `yaml:"destination"`
|
|
|
|
Collections map[string]interface{}
|
|
|
|
|
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
|
|
|
|
`
|
|
|
|
|
|
|
|
//permalink: "/:categories/:year/:month/:day/:title.html",
|
|
|
|
|
|
|
|
var siteConfig SiteConfig
|
2017-06-10 21:38:09 +02:00
|
|
|
|
|
|
|
// A map from URL path -> *Page
|
|
|
|
var siteMap map[string]*Page
|
|
|
|
|
2017-06-13 14:52:35 +02:00
|
|
|
var siteData = map[interface{}]interface{}{}
|
2017-06-13 14:54:35 +02:00
|
|
|
|
|
|
|
// For unit tests
|
|
|
|
func init() {
|
|
|
|
siteConfig.setDefaults()
|
2017-06-12 00:36:31 +02:00
|
|
|
}
|
2017-06-10 23:51:46 +02:00
|
|
|
|
2017-06-13 14:54:35 +02:00
|
|
|
func (c *SiteConfig) setDefaults() {
|
|
|
|
if err := yaml.Unmarshal([]byte(siteConfigDefaults), &siteData); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
if err := yaml.Unmarshal([]byte(siteConfigDefaults), &siteConfig); err != nil {
|
|
|
|
panic(err)
|
2017-06-12 23:12:40 +02:00
|
|
|
}
|
2017-06-13 14:54:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *SiteConfig) read(path string) error {
|
|
|
|
c.setDefaults()
|
2017-06-12 23:12:40 +02:00
|
|
|
switch configBytes, err := ioutil.ReadFile(path); {
|
|
|
|
case err != nil && !os.IsNotExist(err):
|
|
|
|
return nil
|
|
|
|
case err != nil:
|
|
|
|
return err
|
|
|
|
default:
|
2017-06-13 14:52:35 +02:00
|
|
|
if err := yaml.Unmarshal(configBytes, siteData); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-06-12 23:12:40 +02:00
|
|
|
return yaml.Unmarshal(configBytes, c)
|
2017-06-10 21:38:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-13 14:54:35 +02:00
|
|
|
// MarkdownExtensions returns a set of markdown extension.
|
|
|
|
func (c *SiteConfig) MarkdownExtensions() map[string]bool {
|
|
|
|
extns := strings.SplitN(siteConfig.MarkdownExt, `,`, -1)
|
|
|
|
return stringArrayToMap(extns)
|
|
|
|
}
|
2017-06-13 14:55:15 +02:00
|
|
|
|
|
|
|
func getFileURL(path string) (string, bool) {
|
|
|
|
for _, v := range siteMap {
|
|
|
|
if v.Path == path {
|
|
|
|
return v.Permalink, true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "", false
|
|
|
|
}
|
|
|
|
|
2017-06-10 23:51:46 +02:00
|
|
|
func buildSiteMap() (map[string]*Page, error) {
|
2017-06-10 21:38:09 +02:00
|
|
|
basePath := siteConfig.SourceDir
|
|
|
|
fileMap := map[string]*Page{}
|
|
|
|
exclusionMap := stringArrayToMap(siteConfig.Exclude)
|
|
|
|
|
2017-06-12 23:12:40 +02:00
|
|
|
defaultPageData := map[interface{}]interface{}{}
|
|
|
|
|
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-10 23:51:46 +02:00
|
|
|
if path == basePath {
|
2017-06-10 21:38:09 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-06-13 14:55:15 +02:00
|
|
|
rel, err := filepath.Rel(basePath, path)
|
2017-06-10 21:38:09 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-06-13 14:55:15 +02:00
|
|
|
base := filepath.Base(rel)
|
2017-06-10 21:38:09 +02:00
|
|
|
// TODO exclude based on glob, not exact match
|
2017-06-13 14:55:15 +02:00
|
|
|
_, exclude := exclusionMap[rel]
|
2017-06-10 21:38:09 +02:00
|
|
|
exclude = exclude || strings.HasPrefix(base, ".") || strings.HasPrefix(base, "_")
|
|
|
|
if exclude {
|
2017-06-10 23:51:46 +02:00
|
|
|
if info.IsDir() {
|
2017-06-10 21:38:09 +02:00
|
|
|
return filepath.SkipDir
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2017-06-10 23:51:46 +02:00
|
|
|
if info.IsDir() {
|
|
|
|
return nil
|
|
|
|
}
|
2017-06-13 14:55:15 +02:00
|
|
|
p, err := ReadPage(rel, defaultPageData)
|
2017-06-10 23:51:46 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2017-06-10 21:38:09 +02:00
|
|
|
}
|
2017-06-11 01:32:39 +02:00
|
|
|
if p.Published {
|
|
|
|
fileMap[p.Permalink] = p
|
|
|
|
}
|
2017-06-10 21:38:09 +02:00
|
|
|
return nil
|
|
|
|
}
|
2017-06-11 01:32:39 +02:00
|
|
|
|
|
|
|
if err := filepath.Walk(basePath, walkFn); err != nil {
|
2017-06-10 23:51:46 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
2017-06-13 14:55:15 +02:00
|
|
|
if err := ReadCollections(fileMap); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return fileMap, nil
|
|
|
|
}
|
2017-06-10 23:51:46 +02:00
|
|
|
|
2017-06-13 14:55:15 +02:00
|
|
|
// ReadCollections scans the file system for collections. It adds each collection's
|
|
|
|
// pages to the site map, and creates a template site variable for each collection.
|
|
|
|
func ReadCollections(fileMap map[string]*Page) error {
|
|
|
|
for s, d := range siteConfig.Collections {
|
|
|
|
data, ok := d.(map[interface{}]interface{})
|
2017-06-11 01:32:39 +02:00
|
|
|
if !ok {
|
|
|
|
panic("expected collection value to be a map")
|
2017-06-10 23:51:46 +02:00
|
|
|
}
|
2017-06-13 14:55:15 +02:00
|
|
|
c := makeCollection(s, data)
|
|
|
|
if c.Output { // TODO always read the pages; just don't build them
|
|
|
|
if err := c.ReadPages(fileMap); err != nil {
|
|
|
|
return err
|
2017-06-10 23:51:46 +02:00
|
|
|
}
|
|
|
|
}
|
2017-06-13 14:55:15 +02:00
|
|
|
siteData[c.Name] = c.PageData()
|
2017-06-10 23:51:46 +02:00
|
|
|
}
|
2017-06-13 14:55:15 +02:00
|
|
|
return nil
|
|
|
|
}
|