mirror of
https://github.com/danog/gojekyll.git
synced 2024-11-30 10:49:00 +01:00
Use site config to recognize markdown files
This commit is contained in:
parent
ad70550a7c
commit
09acac0190
4
build.go
4
build.go
@ -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
33
page.go
@ -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 {
|
||||
|
33
site.go
33
site.go
@ -14,14 +14,17 @@ import (
|
||||
// See https://jekyllrb.com/docs/configuration/#default-configuration
|
||||
type SiteConfig struct {
|
||||
// Where things are:
|
||||
SourceDir string // `source`
|
||||
SourceDir string `yaml:"source"`
|
||||
DestinationDir string `yaml:"destination"`
|
||||
Collections map[string]interface{}
|
||||
|
||||
Permalink string
|
||||
Safe bool
|
||||
Exclude []string
|
||||
// Handling Reading
|
||||
Include []string
|
||||
Exclude []string
|
||||
MarkdownExt string `yaml:"markdown_ext"`
|
||||
|
||||
// Outputting
|
||||
Permalink 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{}
|
||||
|
Loading…
Reference in New Issue
Block a user