1
0
mirror of https://github.com/danog/gojekyll.git synced 2024-11-27 04:24:56 +01:00

Use site config to recognize markdown files

This commit is contained in:
Oliver Steele 2017-06-13 08:54:35 -04:00
parent ad70550a7c
commit 09acac0190
3 changed files with 48 additions and 24 deletions

View File

@ -3,7 +3,6 @@ package main
import (
"os"
"path/filepath"
"strings"
)
func cleanDirectory() error {
@ -32,8 +31,7 @@ func build() error {
return err
}
for path, page := range siteMap {
// TODO don't do this for js, css, etc. pages
if !page.Static && !strings.HasSuffix(path, ".html") {
if !page.Static && filepath.Ext(path) == "" {
path = filepath.Join(path, "/index.html")
}
src := filepath.Join(siteConfig.SourceDir, page.Path)

33
page.go
View File

@ -9,6 +9,7 @@ import (
"os"
"path/filepath"
"regexp"
"strings"
"github.com/acstech/liquid"
"github.com/russross/blackfriday"
@ -72,7 +73,8 @@ func (p Page) Data() map[interface{}]interface{} {
}
}
func readPage(path string, defaults map[interface{}]interface{}) (p *Page, err error) {
// ReadPage reads a Page from a file, using defaults as the default front matter.
func ReadPage(path string, defaults map[interface{}]interface{}) (p *Page, err error) {
var (
frontMatter map[interface{}]interface{}
static = true
@ -128,10 +130,15 @@ func readPage(path string, defaults map[interface{}]interface{}) (p *Page, err e
return p, nil
}
// Source returns the file path of the page source.
func (p *Page) Source() string {
return filepath.Join(siteConfig.SourceDir, p.Path)
}
// Render applies Liquid and Markdown, as appropriate.
func (p Page) Render(w io.Writer) error {
func (p *Page) Render(w io.Writer) error {
if p.Static {
source, err := ioutil.ReadFile(filepath.Join(siteConfig.SourceDir, p.Path))
source, err := ioutil.ReadFile(p.Source())
if err != nil {
return err
}
@ -139,21 +146,17 @@ func (p Page) Render(w io.Writer) error {
return err
}
var (
path = p.Path
ext = filepath.Ext(path)
)
defer fmt.Println("While processing", p.Source())
template, err := liquid.Parse(p.Content, nil)
if err != nil {
err := &os.PathError{Op: "Liquid Error", Path: path, Err: err}
err := &os.PathError{Op: "Liquid Error", Path: p.Source(), Err: err}
return err
}
writer := new(bytes.Buffer)
template.Render(writer, stringMap(p.Data()))
body := writer.Bytes()
if ext == ".md" {
if isMarkdown(p.Path) {
body = blackfriday.MarkdownCommon(body)
}
@ -161,6 +164,11 @@ func (p Page) Render(w io.Writer) error {
return err
}
func isMarkdown(path string) bool {
ext := filepath.Ext(path)
return siteConfig.MarkdownExtensions()[strings.TrimLeft(ext, ".")]
}
func expandPermalinkPattern(pattern string, data map[interface{}]interface{}, path string) string {
if p, found := permalinkStyles[pattern]; found {
pattern = p
@ -176,9 +184,8 @@ func expandPermalinkPattern(pattern string, data map[interface{}]interface{}, pa
title = getString(data, "title", name)
)
if ext == ".md" {
outputExt = ""
localPath = localPath[:len(localPath)-len(ext)]
if isMarkdown(path) {
outputExt = ".html"
}
if val, found := data["collection"]; found {

35
site.go
View File

@ -13,15 +13,18 @@ import (
// SiteConfig is the Jekyll site configuration, typically read from _config.yml.
// See https://jekyllrb.com/docs/configuration/#default-configuration
type SiteConfig struct {
//Where things are:
SourceDir string // `source`
// Where things are:
SourceDir string `yaml:"source"`
DestinationDir string `yaml:"destination"`
Collections map[string]interface{}
// Handling Reading
Include []string
Exclude []string
MarkdownExt string `yaml:"markdown_ext"`
// Outputting
Permalink string
Safe bool
Exclude []string
Include []string
}
const siteConfigDefaults = `
@ -57,12 +60,23 @@ var siteConfig SiteConfig
var siteMap map[string]*Page
var siteData = map[interface{}]interface{}{}
// For unit tests
func init() {
siteConfig.setDefaults()
}
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)
}
}
func (c *SiteConfig) read(path string) error {
if err := yaml.Unmarshal([]byte(siteConfigDefaults), c); err != nil {
return err
}
c.setDefaults()
switch configBytes, err := ioutil.ReadFile(path); {
case err != nil && !os.IsNotExist(err):
return nil
@ -76,6 +90,11 @@ func (c *SiteConfig) read(path string) error {
}
}
// MarkdownExtensions returns a set of markdown extension.
func (c *SiteConfig) MarkdownExtensions() map[string]bool {
extns := strings.SplitN(siteConfig.MarkdownExt, `,`, -1)
return stringArrayToMap(extns)
}
func buildSiteMap() (map[string]*Page, error) {
basePath := siteConfig.SourceDir
fileMap := map[string]*Page{}