1
0
mirror of https://github.com/danog/gojekyll.git synced 2024-11-30 10:59:05 +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 ( import (
"os" "os"
"path/filepath" "path/filepath"
"strings"
) )
func cleanDirectory() error { func cleanDirectory() error {
@ -32,8 +31,7 @@ func build() error {
return err return err
} }
for path, page := range siteMap { for path, page := range siteMap {
// TODO don't do this for js, css, etc. pages if !page.Static && filepath.Ext(path) == "" {
if !page.Static && !strings.HasSuffix(path, ".html") {
path = filepath.Join(path, "/index.html") path = filepath.Join(path, "/index.html")
} }
src := filepath.Join(siteConfig.SourceDir, page.Path) src := filepath.Join(siteConfig.SourceDir, page.Path)

33
page.go
View File

@ -9,6 +9,7 @@ import (
"os" "os"
"path/filepath" "path/filepath"
"regexp" "regexp"
"strings"
"github.com/acstech/liquid" "github.com/acstech/liquid"
"github.com/russross/blackfriday" "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 ( var (
frontMatter map[interface{}]interface{} frontMatter map[interface{}]interface{}
static = true static = true
@ -128,10 +130,15 @@ func readPage(path string, defaults map[interface{}]interface{}) (p *Page, err e
return p, nil 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. // 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 { if p.Static {
source, err := ioutil.ReadFile(filepath.Join(siteConfig.SourceDir, p.Path)) source, err := ioutil.ReadFile(p.Source())
if err != nil { if err != nil {
return err return err
} }
@ -139,21 +146,17 @@ func (p Page) Render(w io.Writer) error {
return err return err
} }
var ( defer fmt.Println("While processing", p.Source())
path = p.Path
ext = filepath.Ext(path)
)
template, err := liquid.Parse(p.Content, nil) template, err := liquid.Parse(p.Content, nil)
if err != 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 return err
} }
writer := new(bytes.Buffer) writer := new(bytes.Buffer)
template.Render(writer, stringMap(p.Data())) template.Render(writer, stringMap(p.Data()))
body := writer.Bytes() body := writer.Bytes()
if ext == ".md" { if isMarkdown(p.Path) {
body = blackfriday.MarkdownCommon(body) body = blackfriday.MarkdownCommon(body)
} }
@ -161,6 +164,11 @@ func (p Page) Render(w io.Writer) error {
return err 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 { func expandPermalinkPattern(pattern string, data map[interface{}]interface{}, path string) string {
if p, found := permalinkStyles[pattern]; found { if p, found := permalinkStyles[pattern]; found {
pattern = p pattern = p
@ -176,9 +184,8 @@ func expandPermalinkPattern(pattern string, data map[interface{}]interface{}, pa
title = getString(data, "title", name) title = getString(data, "title", name)
) )
if ext == ".md" { if isMarkdown(path) {
outputExt = "" outputExt = ".html"
localPath = localPath[:len(localPath)-len(ext)]
} }
if val, found := data["collection"]; found { 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. // SiteConfig is the Jekyll site configuration, typically read from _config.yml.
// See https://jekyllrb.com/docs/configuration/#default-configuration // See https://jekyllrb.com/docs/configuration/#default-configuration
type SiteConfig struct { type SiteConfig struct {
//Where things are: // Where things are:
SourceDir string // `source` SourceDir string `yaml:"source"`
DestinationDir string `yaml:"destination"` DestinationDir string `yaml:"destination"`
Collections map[string]interface{} Collections map[string]interface{}
// Handling Reading
Include []string
Exclude []string
MarkdownExt string `yaml:"markdown_ext"`
// Outputting
Permalink string Permalink string
Safe bool
Exclude []string
Include []string
} }
const siteConfigDefaults = ` const siteConfigDefaults = `
@ -57,12 +60,23 @@ var siteConfig SiteConfig
var siteMap map[string]*Page var siteMap map[string]*Page
var siteData = map[interface{}]interface{}{} 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 { func (c *SiteConfig) read(path string) error {
if err := yaml.Unmarshal([]byte(siteConfigDefaults), c); err != nil { c.setDefaults()
return err
}
switch configBytes, err := ioutil.ReadFile(path); { switch configBytes, err := ioutil.ReadFile(path); {
case err != nil && !os.IsNotExist(err): case err != nil && !os.IsNotExist(err):
return nil 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) { func buildSiteMap() (map[string]*Page, error) {
basePath := siteConfig.SourceDir basePath := siteConfig.SourceDir
fileMap := map[string]*Page{} fileMap := map[string]*Page{}