2017-06-10 21:38:09 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2017-06-10 23:51:46 +02:00
|
|
|
"fmt"
|
2017-06-10 21:38:09 +02:00
|
|
|
"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-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
|
|
|
|
}
|
|
|
|
|
|
|
|
relPath, err := filepath.Rel(basePath, path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
base := filepath.Base(relPath)
|
|
|
|
// TODO exclude based on glob, not exact match
|
|
|
|
_, exclude := exclusionMap[relPath]
|
|
|
|
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-12 23:12:40 +02:00
|
|
|
p, err := readPage(relPath, 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
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, colVal := range siteConfig.Collections {
|
2017-06-11 01:32:39 +02:00
|
|
|
data, ok := colVal.(map[interface{}]interface{})
|
|
|
|
if !ok {
|
|
|
|
panic("expected collection value to be a map")
|
2017-06-10 23:51:46 +02:00
|
|
|
}
|
2017-06-11 01:32:39 +02:00
|
|
|
output := getBool(data, "output", false)
|
2017-06-10 23:51:46 +02:00
|
|
|
if output {
|
2017-06-11 01:32:39 +02:00
|
|
|
if err := addCollectionFiles(fileMap, name, data); err != nil {
|
2017-06-10 23:51:46 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-06-11 01:32:39 +02:00
|
|
|
|
|
|
|
return fileMap, nil
|
2017-06-10 21:38:09 +02:00
|
|
|
}
|
|
|
|
|
2017-06-12 23:12:40 +02:00
|
|
|
func addCollectionFiles(fileMap map[string]*Page, collectionName string, data map[interface{}]interface{}) error {
|
2017-06-10 23:51:46 +02:00
|
|
|
basePath := siteConfig.SourceDir
|
2017-06-12 00:36:31 +02:00
|
|
|
pages := []*Page{}
|
2017-06-12 23:12:40 +02:00
|
|
|
defaultPageData := map[interface{}]interface{}{
|
|
|
|
"site": siteData,
|
|
|
|
"collection": collectionName,
|
|
|
|
}
|
2017-06-10 23:51:46 +02:00
|
|
|
|
|
|
|
walkFn := func(path string, info os.FileInfo, err error) error {
|
|
|
|
if err != nil {
|
2017-06-11 01:32:39 +02:00
|
|
|
// if the issue is simply that the directory doesn't exist, ignore the error
|
2017-06-12 02:05:17 +02:00
|
|
|
if os.IsNotExist(err) {
|
2017-06-12 23:12:40 +02:00
|
|
|
if collectionName != "posts" {
|
|
|
|
fmt.Println("Missing directory for collection", collectionName)
|
|
|
|
}
|
2017-06-12 02:05:17 +02:00
|
|
|
return nil
|
2017-06-11 01:32:39 +02:00
|
|
|
}
|
2017-06-10 23:51:46 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
relPath, err := filepath.Rel(basePath, path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if info.IsDir() {
|
|
|
|
return nil
|
|
|
|
}
|
2017-06-12 23:12:40 +02:00
|
|
|
p, err := readPage(relPath, defaultPageData)
|
2017-06-10 23:51:46 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if p.Static {
|
|
|
|
fmt.Printf("skipping static file inside collection: %s\n", path)
|
2017-06-11 01:32:39 +02:00
|
|
|
} else if p.Published {
|
2017-06-10 23:51:46 +02:00
|
|
|
fileMap[p.Permalink] = p
|
2017-06-12 00:36:31 +02:00
|
|
|
pages = append(pages, p)
|
2017-06-10 23:51:46 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2017-06-12 23:12:40 +02:00
|
|
|
if err := filepath.Walk(filepath.Join(basePath, "_"+collectionName), walkFn); err != nil {
|
2017-06-12 00:36:31 +02:00
|
|
|
return err
|
|
|
|
}
|
2017-06-12 23:12:40 +02:00
|
|
|
collectionPageData := []interface{}{}
|
2017-06-12 00:36:31 +02:00
|
|
|
for _, p := range pages {
|
2017-06-12 23:12:40 +02:00
|
|
|
collectionPageData = append(collectionPageData, p.PageData())
|
2017-06-12 00:36:31 +02:00
|
|
|
}
|
2017-06-12 23:12:40 +02:00
|
|
|
siteData[collectionName] = collectionPageData
|
2017-06-12 00:36:31 +02:00
|
|
|
return nil
|
2017-06-10 23:51:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func getFileURL(path string) (string, bool) {
|
2017-06-10 21:38:09 +02:00
|
|
|
for _, v := range siteMap {
|
|
|
|
if v.Path == path {
|
|
|
|
return v.Permalink, true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "", false
|
|
|
|
}
|