1
0
mirror of https://github.com/danog/gojekyll.git synced 2024-11-26 19:34:52 +01:00

Move helpers -> utils

This commit is contained in:
Oliver Steele 2017-07-09 16:17:20 -04:00
parent 4f664d2998
commit 4dc06356e9
31 changed files with 68 additions and 68 deletions

View File

@ -12,10 +12,10 @@ import (
yaml "gopkg.in/yaml.v2"
"github.com/montanaflynn/stats"
"github.com/osteele/gojekyll/helpers"
"github.com/osteele/gojekyll/pages"
"github.com/osteele/gojekyll/server"
"github.com/osteele/gojekyll/site"
"github.com/osteele/gojekyll/utils"
"github.com/osteele/liquid"
)
@ -105,13 +105,13 @@ func pageFromPathOrRoute(s *site.Site, path string) (pages.Document, error) {
case strings.HasPrefix(path, "/"):
page, found := s.URLPage(path)
if !found {
return nil, helpers.NewPathError("render", path, "the site does not include a file with this URL path")
return nil, utils.NewPathError("render", path, "the site does not include a file with this URL path")
}
return page, nil
default:
page, found := s.FilePathPage(path)
if !found {
return nil, helpers.NewPathError("render", path, "no such file")
return nil, utils.NewPathError("render", path, "no such file")
}
return page, nil
}

View File

@ -3,7 +3,7 @@ package main
import (
"fmt"
"github.com/osteele/gojekyll/helpers"
"github.com/osteele/gojekyll/utils"
)
type bannerLogger struct{ labelWidth int }
@ -19,10 +19,10 @@ func (l *bannerLogger) label(label string, msg string, a ...interface{}) {
l.labelWidth = len(label)
}
if !quiet {
fmt.Printf("%s %s\n", helpers.LeftPad(label, l.labelWidth), fmt.Sprintf(msg, a...))
fmt.Printf("%s %s\n", utils.LeftPad(label, l.labelWidth), fmt.Sprintf(msg, a...))
}
}
func (l *bannerLogger) path(label string, filename string) {
l.label(label, helpers.MustAbs(filename))
l.label(label, utils.MustAbs(filename))
}

View File

@ -7,9 +7,9 @@ import (
"sort"
"strings"
"github.com/osteele/gojekyll/helpers"
"github.com/osteele/gojekyll/pages"
"github.com/osteele/gojekyll/templates"
"github.com/osteele/gojekyll/utils"
)
const draftsPath = "_drafts"
@ -32,7 +32,7 @@ func (c *Collection) ScanDirectory(dirname string) error {
}
return err
}
relname := helpers.MustRel(sitePath, filename)
relname := utils.MustRel(sitePath, filename)
switch {
case strings.HasPrefix(filepath.Base(relname), "."):
return nil

View File

@ -3,7 +3,7 @@ package collection
import (
"time"
"github.com/osteele/gojekyll/helpers"
"github.com/osteele/gojekyll/utils"
)
// A collectionStrategy encapsulates behavior differences between the _post
@ -31,18 +31,18 @@ func (s defaultStrategy) future(filename string) bool { return fa
type postsStrategy struct{}
func (s postsStrategy) addDate(filename string, fm map[string]interface{}) {
if t, found := helpers.FilenameDate(filename); found {
if t, found := utils.FilenameDate(filename); found {
fm["date"] = t
}
}
func (s postsStrategy) collectible(filename string) bool {
_, ok := helpers.FilenameDate(filename)
_, ok := utils.FilenameDate(filename)
return ok
}
func (s postsStrategy) future(filename string) bool {
t, ok := helpers.FilenameDate(filename)
t, ok := utils.FilenameDate(filename)
return ok && t.After(time.Now())
}

View File

@ -3,8 +3,8 @@ package config
import (
"strings"
"github.com/osteele/gojekyll/helpers"
"github.com/osteele/gojekyll/templates"
"github.com/osteele/gojekyll/utils"
yaml "gopkg.in/yaml.v2"
)
@ -70,7 +70,7 @@ type collectionsMap struct {
// SourceDir returns the source directory as an absolute path.
func (c *Config) SourceDir() string {
return helpers.MustAbs(c.Source)
return utils.MustAbs(c.Source)
}
// GetFrontMatterDefaults implements https://jekyllrb.com/docs/configuration/#front-matter-defaults

View File

@ -4,7 +4,7 @@ import (
"path/filepath"
"strings"
"github.com/osteele/gojekyll/helpers"
"github.com/osteele/gojekyll/utils"
)
// IsMarkdown returns a boolean indicating whether the file is a Markdown file, according to the current project.
@ -21,7 +21,7 @@ func (c *Config) IsSASSPath(name string) bool {
// markdownExtensions returns a set of markdown extensions, without the initial dots.
func (c *Config) markdownExtensions() map[string]bool {
exts := strings.SplitN(c.MarkdownExt, `,`, -1)
return helpers.StringArrayToMap(exts)
return utils.StringArrayToMap(exts)
}
// MarkdownExtensions returns a list of markdown extensions, with dotsa.

View File

@ -5,7 +5,7 @@ import (
"sort"
"strings"
"github.com/osteele/gojekyll/helpers"
"github.com/osteele/gojekyll/utils"
"github.com/osteele/liquid/evaluator"
)
@ -18,7 +18,7 @@ const fmMagic = "---\n"
// FileHasFrontMatter returns a bool indicating whether the
// file looks like it has frontmatter.
func FileHasFrontMatter(filename string) (bool, error) {
magic, err := helpers.ReadFileMagic(filename)
magic, err := utils.ReadFileMagic(filename)
if err != nil {
return false, err
}

View File

@ -6,8 +6,8 @@ import (
"path"
"path/filepath"
"github.com/osteele/gojekyll/helpers"
"github.com/osteele/gojekyll/templates"
"github.com/osteele/gojekyll/utils"
)
// ToLiquid is part of the liquid.Drop interface.
@ -30,7 +30,7 @@ func (f *file) ToLiquid() interface{} {
"path": relpath,
"modified_time": f.fileModTime,
"name": base,
"basename": helpers.TrimExt(base),
"basename": utils.TrimExt(base),
"extname": ext,
})
}
@ -40,7 +40,7 @@ func (p *page) ToLiquid() interface{} {
var (
relpath = p.relpath
ext = filepath.Ext(relpath)
root = helpers.TrimExt(p.relpath)
root = utils.TrimExt(p.relpath)
base = filepath.Base(root)
content = p.raw
excerpt string

View File

@ -7,8 +7,8 @@ import (
"strconv"
"strings"
"github.com/osteele/gojekyll/helpers"
"github.com/osteele/gojekyll/templates"
"github.com/osteele/gojekyll/utils"
)
// DefaultPermalinkPattern is the default permalink pattern for pages that aren't in a collection
@ -42,16 +42,16 @@ var templateVariableMatcher = regexp.MustCompile(`:\w+\b`)
func (f *file) permalinkVariables() map[string]string {
var (
relpath = strings.TrimPrefix(f.relpath, f.container.PathPrefix())
root = helpers.TrimExt(relpath)
root = utils.TrimExt(relpath)
name = filepath.Base(root)
fm = f.frontMatter
bindings = templates.VariableMap(fm)
slug = bindings.String("slug", helpers.Slugify(name))
slug = bindings.String("slug", utils.Slugify(name))
)
vars := map[string]string{
"categories": strings.Join(f.Categories(), "/"),
"collection": bindings.String("collection", ""),
"name": helpers.Slugify(name),
"name": utils.Slugify(name),
"path": "/" + root, // TODO are we removing and then adding this?
"slug": slug,
"title": slug,
@ -71,7 +71,7 @@ func (f *file) computePermalink(vars map[string]string) (src string, err error)
pattern = p
}
templateVariables := f.permalinkVariables()
s, err := helpers.SafeReplaceAllStringFunc(templateVariableMatcher, pattern, func(m string) (string, error) {
s, err := utils.SafeReplaceAllStringFunc(templateVariableMatcher, pattern, func(m string) (string, error) {
varname := m[1:]
value, found := templateVariables[varname]
if !found {
@ -82,7 +82,7 @@ func (f *file) computePermalink(vars map[string]string) (src string, err error)
if err != nil {
return "", err
}
return helpers.URLPathClean("/" + s), nil
return utils.URLPathClean("/" + s), nil
}
func (f *file) setPermalink() (err error) {

View File

@ -6,9 +6,9 @@ import (
"github.com/osteele/gojekyll/config"
"github.com/osteele/gojekyll/filters"
"github.com/osteele/gojekyll/helpers"
"github.com/osteele/gojekyll/tags"
"github.com/osteele/gojekyll/templates"
"github.com/osteele/gojekyll/utils"
"github.com/osteele/liquid"
"github.com/russross/blackfriday"
)
@ -81,12 +81,12 @@ func (p *Pipeline) Render(w io.Writer, b []byte, filename string, lineNo int, e
func (p *Pipeline) renderTemplate(src []byte, b map[string]interface{}, filename string, lineNo int) ([]byte, error) {
tpl, err := p.liquidEngine.ParseTemplate(src)
if err != nil {
return nil, helpers.PathError(err, "Liquid Error", filename)
return nil, utils.PathError(err, "Liquid Error", filename)
}
tpl.SetSourceLocation(filename, lineNo)
out, err := tpl.Render(b)
if err != nil {
return nil, helpers.PathError(err, "Liquid Error", filename)
return nil, utils.PathError(err, "Liquid Error", filename)
}
return out, err
}
@ -99,13 +99,13 @@ func (p *Pipeline) ApplyLayout(name string, data []byte, e map[string]interface{
if err != nil {
return nil, err
}
b := helpers.MergeStringMaps(e, map[string]interface{}{
b := utils.MergeStringMaps(e, map[string]interface{}{
"content": string(data),
"layout": lfm,
})
data, err = tpl.Render(b)
if err != nil {
return nil, helpers.PathError(err, "render template", name)
return nil, utils.PathError(err, "render template", name)
}
name = templates.VariableMap(lfm).String("layout", "")
}

View File

@ -10,7 +10,7 @@ import (
"strings"
"github.com/dchest/cssmin"
"github.com/osteele/gojekyll/helpers"
"github.com/osteele/gojekyll/utils"
libsass "github.com/wellington/go-libsass"
)
@ -34,9 +34,9 @@ func (p *Pipeline) CopySassFileIncludes() error {
if err != nil || info.IsDir() {
return err
}
rel := helpers.MustRel(src, from)
rel := utils.MustRel(src, from)
to := filepath.Join(dst, strings.TrimPrefix(rel, "_"))
return helpers.CopyFileContents(to, from, 0644)
return utils.CopyFileContents(to, from, 0644)
})
if os.IsNotExist(err) {
return nil

View File

@ -11,8 +11,8 @@ import (
"github.com/kyokomi/emoji"
"github.com/osteele/gojekyll/config"
"github.com/osteele/gojekyll/helpers"
"github.com/osteele/gojekyll/pages"
"github.com/osteele/gojekyll/utils"
"github.com/osteele/liquid"
"github.com/osteele/liquid/render"
)
@ -84,7 +84,7 @@ func init() {
type jekyllJemojiPlugin struct{ plugin }
func (p jekyllJemojiPlugin) PostRender(b []byte) []byte {
return helpers.ApplyToHTMLText(b, func(s string) string {
return utils.ApplyToHTMLText(b, func(s string) string {
s = emoji.Sprint(s)
return s
})
@ -97,7 +97,7 @@ type jekyllMentionsPlugin struct{ plugin }
var mentionPattern = regexp.MustCompile(`@(\w+)`)
func (p jekyllMentionsPlugin) PostRender(b []byte) []byte {
return helpers.ApplyToHTMLText(b, func(s string) string {
return utils.ApplyToHTMLText(b, func(s string) string {
return mentionPattern.ReplaceAllString(s, `<a href="https://github.com/$1" class="user-mention">@$1</a>`)
})
}

View File

@ -5,7 +5,7 @@ import (
"time"
"github.com/fsnotify/fsnotify"
"github.com/osteele/gojekyll/helpers"
"github.com/osteele/gojekyll/utils"
)
func (s *Server) watchAndReload() error {
@ -54,7 +54,7 @@ func (s *Server) watchFiles(fn func([]string)) error {
filenames := <-debounced
relpaths := make([]string, 0, len(filenames))
for _, filename := range filenames {
relpath := helpers.MustRel(sourceDir, filename)
relpath := utils.MustRel(sourceDir, filename)
if relpath == "_config.yml" || !site.Exclude(relpath) {
relpaths = append(relpaths, relpath)
}

View File

@ -5,7 +5,7 @@ import (
"os"
"path/filepath"
"github.com/osteele/gojekyll/helpers"
"github.com/osteele/gojekyll/utils"
)
// BuildOptions holds options for Build and Clean
@ -29,7 +29,7 @@ func (s *Site) Clean(options BuildOptions) error {
return err
case info.IsDir():
return nil
case s.KeepFile(helpers.MustRel(s.DestDir(), filename)):
case s.KeepFile(utils.MustRel(s.DestDir(), filename)):
return nil
case options.DryRun:
return nil
@ -40,7 +40,7 @@ func (s *Site) Clean(options BuildOptions) error {
if err := filepath.Walk(s.DestDir(), removeFiles); err != nil {
return err
}
return helpers.RemoveEmptyDirectories(s.DestDir())
return utils.RemoveEmptyDirectories(s.DestDir())
}
// Build cleans the destination and create files in it.

View File

@ -6,7 +6,7 @@ import (
"os"
"path/filepath"
"github.com/osteele/gojekyll/helpers"
"github.com/osteele/gojekyll/utils"
)
func (s *Site) readDataFiles() error {
@ -33,11 +33,11 @@ func (s *Site) readDataFiles() error {
return err
}
var d interface{} // map or slice
err = helpers.UnmarshalYAMLInterface(b, &d)
err = utils.UnmarshalYAMLInterface(b, &d)
if err != nil {
return helpers.PathError(err, "read YAML", filename)
return utils.PathError(err, "read YAML", filename)
}
basename := helpers.TrimExt(filepath.Base(f.Name()))
basename := utils.TrimExt(filepath.Base(f.Name()))
s.data[basename] = d
}
}

View File

@ -7,9 +7,9 @@ import (
"github.com/osteele/gojekyll/collection"
"github.com/osteele/gojekyll/config"
"github.com/osteele/gojekyll/helpers"
"github.com/osteele/gojekyll/pages"
"github.com/osteele/gojekyll/plugins"
"github.com/osteele/gojekyll/utils"
)
// FromDirectory reads the configuration file, if it exists.
@ -66,7 +66,7 @@ func (s *Site) readFiles() error {
return err
}
relname := helpers.MustRel(s.SourceDir(), filename)
relname := utils.MustRel(s.SourceDir(), filename)
switch {
case info.IsDir() && s.Exclude(relname):
return filepath.SkipDir
@ -76,7 +76,7 @@ func (s *Site) readFiles() error {
defaultFrontmatter := s.config.GetFrontMatterDefaults("", relname)
p, err := pages.NewFile(filename, s, filepath.ToSlash(relname), defaultFrontmatter)
if err != nil {
return helpers.PathError(err, "read", filename)
return utils.PathError(err, "read", filename)
}
s.AddDocument(p, true)
return nil

View File

@ -8,10 +8,10 @@ import (
"github.com/osteele/gojekyll/collection"
"github.com/osteele/gojekyll/config"
"github.com/osteele/gojekyll/helpers"
"github.com/osteele/gojekyll/pages"
"github.com/osteele/gojekyll/pipelines"
"github.com/osteele/gojekyll/plugins"
"github.com/osteele/gojekyll/utils"
)
// Site is a Jekyll site.
@ -122,7 +122,7 @@ func (s *Site) FilenameURLs() map[string]string {
// KeepFile returns a boolean indicating that clean should leave the file in the destination directory.
func (s *Site) KeepFile(filename string) bool {
return helpers.SearchStrings(s.config.KeepFiles, filename)
return utils.SearchStrings(s.config.KeepFiles, filename)
}
// FilePathPage returns a Page, give a file path relative to site source directory.
@ -186,8 +186,8 @@ func (s *Site) URLPage(urlpath string) (p pages.Document, found bool) {
// Exclude returns a boolean indicating that the site excludes a file.
func (s *Site) Exclude(path string) bool {
// TODO exclude based on glob, not exact match
inclusionMap := helpers.StringArrayToMap(s.config.Include)
exclusionMap := helpers.StringArrayToMap(s.config.Exclude)
inclusionMap := utils.StringArrayToMap(s.config.Include)
exclusionMap := utils.StringArrayToMap(s.config.Exclude)
base := filepath.Base(path)
switch {
case inclusionMap[path]:

View File

@ -7,9 +7,9 @@ import (
"os"
"path/filepath"
"github.com/osteele/gojekyll/helpers"
"github.com/osteele/gojekyll/pages"
"github.com/osteele/gojekyll/plugins"
"github.com/osteele/gojekyll/utils"
)
func (s *Site) prepareRendering() error {
@ -89,7 +89,7 @@ func (s *Site) SavePage(p pages.Document, options BuildOptions) error {
case p.Static() && options.UseHardLinks:
return os.Link(from, to)
case p.Static():
return helpers.CopyFileContents(to, from, 0644)
return utils.CopyFileContents(to, from, 0644)
default:
return s.SaveDocumentToFile(p, to)
}
@ -97,7 +97,7 @@ func (s *Site) SavePage(p pages.Document, options BuildOptions) error {
// SaveDocumentToFile writes a page to filename.
func (s *Site) SaveDocumentToFile(d pages.Document, filename string) error {
return helpers.VisitCreatedFile(filename, func(w io.Writer) error {
return utils.VisitCreatedFile(filename, func(w io.Writer) error {
return s.WriteDocument(w, d)
})
}

View File

@ -1,4 +1,4 @@
package helpers
package utils
// SearchStrings returns a bool indicating whether array contains the string.
// Unlike sort.SearchStrings, it does not require a sorted array.

View File

@ -1,4 +1,4 @@
package helpers
package utils
import (
"bytes"

View File

@ -1,4 +1,4 @@
package helpers
package utils
import (
"errors"

View File

@ -1,4 +1,4 @@
package helpers
package utils
import (
"io"

View File

@ -1,4 +1,4 @@
package helpers
package utils
// MergeStringMaps creates a new variable map that merges its arguments,
// from first to last.

View File

@ -1,4 +1,4 @@
package helpers
package utils
import (
"path"

View File

@ -1,4 +1,4 @@
package helpers
package utils
import (
"strings"

View File

@ -1,4 +1,4 @@
package helpers
package utils
import (
"regexp"

View File

@ -1,4 +1,4 @@
package helpers
package utils
import (
"fmt"

View File

@ -1,4 +1,4 @@
package helpers
package utils
import yaml "gopkg.in/yaml.v2"

View File

@ -1,4 +1,4 @@
package helpers
package utils
import (
"testing"