2013-07-04 11:32:55 -04:00
|
|
|
// Copyright © 2013 Steve Francia <spf@spf13.com>.
|
|
|
|
//
|
|
|
|
// Licensed under the Simple Public License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
// http://opensource.org/licenses/Simple-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package hugolib
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bitbucket.org/pkg/inflect"
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
2013-08-30 20:18:05 -04:00
|
|
|
"github.com/spf13/hugo/target"
|
2013-09-03 18:38:20 -04:00
|
|
|
helpers "github.com/spf13/hugo/template"
|
|
|
|
"github.com/spf13/hugo/template/bundle"
|
2013-07-04 11:32:55 -04:00
|
|
|
"github.com/spf13/nitro"
|
2013-09-03 18:38:20 -04:00
|
|
|
"html/template"
|
2013-07-04 11:32:55 -04:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2013-08-07 20:21:22 -04:00
|
|
|
var DefaultTimer = nitro.Initalize()
|
|
|
|
|
2013-09-03 18:38:20 -04:00
|
|
|
func MakePermalink(domain string, path string) string {
|
|
|
|
return strings.TrimRight(domain, "/") + "/" + strings.TrimLeft(path, "/")
|
|
|
|
}
|
|
|
|
|
|
|
|
func mkdirIf(path string) error {
|
|
|
|
return os.MkdirAll(path, 0777)
|
|
|
|
}
|
|
|
|
|
|
|
|
func FatalErr(str string) {
|
|
|
|
fmt.Println(str)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
func PrintErr(str string, a ...interface{}) {
|
|
|
|
fmt.Fprintln(os.Stderr, str, a)
|
|
|
|
}
|
|
|
|
|
2013-09-01 00:13:04 -04:00
|
|
|
// Site contains all the information relevent for constructing a static
|
|
|
|
// site. The basic flow of information is as follows:
|
|
|
|
//
|
|
|
|
// 1. A list of Files is parsed and then converted into Pages.
|
|
|
|
//
|
|
|
|
// 2. Pages contain sections (based on the file they were generated from),
|
|
|
|
// aliases and slugs (included in a pages frontmatter) which are the
|
|
|
|
// various targets that will get generated. There will be canonical
|
|
|
|
// listing.
|
|
|
|
//
|
|
|
|
// 3. Indexes are created via configuration and will present some aspect of
|
|
|
|
// the final page and typically a perm url.
|
|
|
|
//
|
|
|
|
// 4. All Pages are passed through a template based on their desired
|
|
|
|
// layout based on numerous different elements.
|
|
|
|
//
|
|
|
|
// 5. The entire collection of files is written to disk.
|
2013-07-04 11:32:55 -04:00
|
|
|
type Site struct {
|
2013-09-01 00:07:22 -04:00
|
|
|
Config Config
|
|
|
|
Pages Pages
|
2013-09-03 18:38:20 -04:00
|
|
|
Tmpl bundle.Template
|
2013-09-01 00:07:22 -04:00
|
|
|
Indexes IndexList
|
|
|
|
Files []string
|
|
|
|
Sections Index
|
|
|
|
Info SiteInfo
|
|
|
|
Shortcodes map[string]ShortcodeFunc
|
|
|
|
timer *nitro.B
|
2013-09-03 23:52:50 -04:00
|
|
|
Target target.Output
|
2013-07-04 11:32:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
type SiteInfo struct {
|
2013-09-03 15:41:13 -04:00
|
|
|
BaseUrl template.URL
|
2013-08-03 03:09:28 -04:00
|
|
|
Indexes OrderedIndexList
|
2013-07-04 11:32:55 -04:00
|
|
|
Recent *Pages
|
|
|
|
LastChange time.Time
|
2013-07-19 03:10:42 -04:00
|
|
|
Title string
|
2013-07-26 09:51:07 -04:00
|
|
|
Config *Config
|
2013-07-04 11:32:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Site) getFromIndex(kind string, name string) Pages {
|
|
|
|
return s.Indexes[kind][name]
|
|
|
|
}
|
|
|
|
|
2013-08-07 20:21:22 -04:00
|
|
|
func (s *Site) timerStep(step string) {
|
|
|
|
if s.timer == nil {
|
|
|
|
s.timer = DefaultTimer
|
|
|
|
}
|
|
|
|
s.timer.Step(step)
|
2013-07-04 11:32:55 -04:00
|
|
|
}
|
|
|
|
|
2013-08-31 20:29:19 -04:00
|
|
|
func (s *Site) Build() (err error) {
|
|
|
|
if err = s.Process(); err != nil {
|
2013-08-01 14:55:18 -04:00
|
|
|
return
|
|
|
|
}
|
2013-08-31 20:29:19 -04:00
|
|
|
if err = s.Render(); err != nil {
|
2013-09-01 00:13:04 -04:00
|
|
|
fmt.Printf("Error rendering site: %s\nAvailable templates:\n", err)
|
|
|
|
for _, template := range s.Tmpl.Templates() {
|
|
|
|
fmt.Printf("\t%s\n", template.Name())
|
2013-08-07 20:34:17 -04:00
|
|
|
}
|
2013-08-02 19:48:09 -04:00
|
|
|
return
|
|
|
|
}
|
2013-08-31 20:29:19 -04:00
|
|
|
s.Write()
|
2013-08-01 14:55:18 -04:00
|
|
|
return nil
|
2013-07-04 11:32:55 -04:00
|
|
|
}
|
|
|
|
|
2013-08-31 20:29:19 -04:00
|
|
|
func (s *Site) Analyze() {
|
|
|
|
s.Process()
|
2013-09-03 23:52:50 -04:00
|
|
|
s.initTarget()
|
|
|
|
s.ShowPlan(os.Stdout)
|
2013-07-04 11:32:55 -04:00
|
|
|
}
|
|
|
|
|
2013-08-31 20:47:21 -04:00
|
|
|
func (s *Site) prepTemplates() {
|
2013-09-03 18:38:20 -04:00
|
|
|
s.Tmpl = bundle.NewTemplate()
|
2013-08-31 20:47:21 -04:00
|
|
|
s.Tmpl.LoadTemplates(s.absLayoutDir())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Site) addTemplate(name, data string) error {
|
|
|
|
return s.Tmpl.AddTemplate(name, data)
|
|
|
|
}
|
|
|
|
|
2013-08-31 20:29:19 -04:00
|
|
|
func (s *Site) Process() (err error) {
|
|
|
|
s.initialize()
|
|
|
|
s.prepTemplates()
|
|
|
|
s.timerStep("initialize & template prep")
|
2013-09-04 19:57:17 -04:00
|
|
|
if err = s.CreatePages(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-08-31 20:29:19 -04:00
|
|
|
s.setupPrevNext()
|
|
|
|
s.timerStep("import pages")
|
|
|
|
if err = s.BuildSiteMeta(); err != nil {
|
2013-08-01 14:55:18 -04:00
|
|
|
return
|
|
|
|
}
|
2013-08-31 20:29:19 -04:00
|
|
|
s.timerStep("build indexes")
|
2013-08-01 14:55:18 -04:00
|
|
|
return
|
2013-07-04 11:32:55 -04:00
|
|
|
}
|
|
|
|
|
2013-08-31 20:29:19 -04:00
|
|
|
func (s *Site) Render() (err error) {
|
|
|
|
s.RenderAliases()
|
|
|
|
s.timerStep("render and write aliases")
|
|
|
|
s.ProcessShortcodes()
|
|
|
|
s.timerStep("render shortcodes")
|
|
|
|
s.AbsUrlify()
|
|
|
|
s.timerStep("absolute URLify")
|
|
|
|
if err = s.RenderIndexes(); err != nil {
|
2013-08-12 19:10:38 -04:00
|
|
|
return
|
|
|
|
}
|
2013-08-31 20:29:19 -04:00
|
|
|
s.RenderIndexesIndexes()
|
|
|
|
s.timerStep("render and write indexes")
|
|
|
|
s.RenderLists()
|
|
|
|
s.timerStep("render and write lists")
|
|
|
|
if err = s.RenderPages(); err != nil {
|
2013-08-02 19:48:09 -04:00
|
|
|
return
|
|
|
|
}
|
2013-08-31 20:29:19 -04:00
|
|
|
s.timerStep("render pages")
|
|
|
|
if err = s.RenderHomePage(); err != nil {
|
2013-08-20 09:40:33 -04:00
|
|
|
return
|
2013-08-30 17:38:33 -04:00
|
|
|
}
|
2013-08-31 20:29:19 -04:00
|
|
|
s.timerStep("render and write homepage")
|
2013-08-02 19:48:09 -04:00
|
|
|
return
|
2013-07-04 11:32:55 -04:00
|
|
|
}
|
|
|
|
|
2013-08-31 20:29:19 -04:00
|
|
|
func (s *Site) Write() {
|
|
|
|
s.WritePages()
|
|
|
|
s.timerStep("write pages")
|
2013-07-04 11:32:55 -04:00
|
|
|
}
|
|
|
|
|
2013-08-31 20:29:19 -04:00
|
|
|
func (s *Site) checkDescriptions() {
|
|
|
|
for _, p := range s.Pages {
|
2013-07-04 11:32:55 -04:00
|
|
|
if len(p.Description) < 60 {
|
2013-09-03 23:52:50 -04:00
|
|
|
fmt.Println(p.FileName + " ")
|
2013-07-04 11:32:55 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Site) initialize() {
|
|
|
|
s.checkDirectories()
|
|
|
|
|
2013-08-30 17:38:33 -04:00
|
|
|
staticDir := s.Config.GetAbsPath(s.Config.StaticDir + "/")
|
2013-08-15 14:58:34 -04:00
|
|
|
|
2013-07-04 11:32:55 -04:00
|
|
|
walker := func(path string, fi os.FileInfo, err error) error {
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if fi.IsDir() {
|
2013-08-30 17:38:33 -04:00
|
|
|
if path == staticDir {
|
2013-08-15 14:58:34 -04:00
|
|
|
return filepath.SkipDir
|
|
|
|
}
|
2013-07-04 11:32:55 -04:00
|
|
|
return nil
|
|
|
|
} else {
|
2013-08-12 19:10:38 -04:00
|
|
|
if ignoreDotFile(path) {
|
|
|
|
return nil
|
|
|
|
}
|
2013-08-31 20:29:19 -04:00
|
|
|
s.Files = append(s.Files, path)
|
2013-07-04 11:32:55 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-12 19:10:38 -04:00
|
|
|
filepath.Walk(s.absContentDir(), walker)
|
2013-08-13 08:43:42 -04:00
|
|
|
s.Info = SiteInfo{
|
2013-09-03 15:41:13 -04:00
|
|
|
BaseUrl: template.URL(s.Config.BaseUrl),
|
2013-08-13 08:43:42 -04:00
|
|
|
Title: s.Config.Title,
|
|
|
|
Recent: &s.Pages,
|
|
|
|
Config: &s.Config,
|
|
|
|
}
|
2013-07-04 11:32:55 -04:00
|
|
|
|
|
|
|
s.Shortcodes = make(map[string]ShortcodeFunc)
|
|
|
|
}
|
|
|
|
|
2013-09-03 18:38:20 -04:00
|
|
|
// Check if File / Directory Exists
|
|
|
|
func exists(path string) (bool, error) {
|
|
|
|
_, err := os.Stat(path)
|
|
|
|
if err == nil {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
2013-08-12 19:10:38 -04:00
|
|
|
func ignoreDotFile(path string) bool {
|
|
|
|
return filepath.Base(path)[0] == '.'
|
|
|
|
}
|
|
|
|
|
2013-08-07 20:21:22 -04:00
|
|
|
func (s *Site) absLayoutDir() string {
|
|
|
|
return s.Config.GetAbsPath(s.Config.LayoutDir)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Site) absContentDir() string {
|
|
|
|
return s.Config.GetAbsPath(s.Config.ContentDir)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Site) absPublishDir() string {
|
|
|
|
return s.Config.GetAbsPath(s.Config.PublishDir)
|
|
|
|
}
|
|
|
|
|
2013-07-04 11:32:55 -04:00
|
|
|
func (s *Site) checkDirectories() {
|
2013-08-07 20:21:22 -04:00
|
|
|
if b, _ := dirExists(s.absLayoutDir()); !b {
|
|
|
|
FatalErr("No layout directory found, expecting to find it at " + s.absLayoutDir())
|
2013-07-04 11:32:55 -04:00
|
|
|
}
|
2013-08-07 20:21:22 -04:00
|
|
|
if b, _ := dirExists(s.absContentDir()); !b {
|
|
|
|
FatalErr("No source directory found, expecting to find it at " + s.absContentDir())
|
2013-07-04 11:32:55 -04:00
|
|
|
}
|
2013-08-07 20:21:22 -04:00
|
|
|
mkdirIf(s.absPublishDir())
|
2013-07-04 11:32:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Site) ProcessShortcodes() {
|
2013-08-31 20:47:21 -04:00
|
|
|
for _, page := range s.Pages {
|
2013-09-03 15:41:13 -04:00
|
|
|
page.Content = template.HTML(ShortcodesHandle(string(page.Content), page, s.Tmpl))
|
2013-07-04 11:32:55 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-10 00:14:02 -04:00
|
|
|
func (s *Site) AbsUrlify() {
|
2013-08-09 21:07:35 -04:00
|
|
|
baseWithoutTrailingSlash := strings.TrimRight(s.Config.BaseUrl, "/")
|
2013-08-08 07:30:01 -04:00
|
|
|
baseWithSlash := baseWithoutTrailingSlash + "/"
|
2013-08-31 20:47:21 -04:00
|
|
|
for _, page := range s.Pages {
|
|
|
|
content := string(page.Content)
|
2013-08-08 07:30:01 -04:00
|
|
|
content = strings.Replace(content, " src=\"/", " src=\""+baseWithSlash, -1)
|
|
|
|
content = strings.Replace(content, " src='/", " src='"+baseWithSlash, -1)
|
|
|
|
content = strings.Replace(content, " href='/", " href='"+baseWithSlash, -1)
|
|
|
|
content = strings.Replace(content, " href=\"/", " href=\""+baseWithSlash, -1)
|
|
|
|
content = strings.Replace(content, baseWithoutTrailingSlash+"//", baseWithSlash, -1)
|
2013-09-03 15:41:13 -04:00
|
|
|
page.Content = template.HTML(content)
|
2013-07-10 00:14:02 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-04 19:57:17 -04:00
|
|
|
func (s *Site) CreatePages() (err error) {
|
2013-07-04 11:32:55 -04:00
|
|
|
for _, fileName := range s.Files {
|
2013-09-04 19:57:17 -04:00
|
|
|
f, err := os.Open(fileName)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
page, err := ReadFrom(f, fileName)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-07-04 11:32:55 -04:00
|
|
|
page.Site = s.Info
|
|
|
|
page.Tmpl = s.Tmpl
|
2013-08-14 08:57:14 -04:00
|
|
|
_ = s.setUrlPath(page)
|
|
|
|
s.setOutFile(page)
|
2013-08-07 20:21:22 -04:00
|
|
|
if s.Config.BuildDrafts || !page.Draft {
|
2013-07-04 11:32:55 -04:00
|
|
|
s.Pages = append(s.Pages, page)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
s.Pages.Sort()
|
2013-09-04 19:57:17 -04:00
|
|
|
return
|
2013-07-04 11:32:55 -04:00
|
|
|
}
|
|
|
|
|
2013-08-02 16:30:26 -04:00
|
|
|
func (s *Site) setupPrevNext() {
|
2013-08-31 20:47:21 -04:00
|
|
|
for i, page := range s.Pages {
|
2013-08-02 16:30:26 -04:00
|
|
|
if i < len(s.Pages)-1 {
|
2013-08-31 20:47:21 -04:00
|
|
|
page.Next = s.Pages[i+1]
|
2013-08-02 16:30:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if i > 0 {
|
2013-08-31 20:47:21 -04:00
|
|
|
page.Prev = s.Pages[i-1]
|
2013-08-02 16:30:26 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-14 08:57:14 -04:00
|
|
|
func (s *Site) setUrlPath(p *Page) error {
|
|
|
|
y := strings.TrimPrefix(p.FileName, s.Config.GetAbsPath(s.Config.ContentDir))
|
|
|
|
x := strings.Split(y, string(os.PathSeparator))
|
|
|
|
|
|
|
|
if len(x) <= 1 {
|
2013-08-31 20:47:21 -04:00
|
|
|
return fmt.Errorf("Zero length page name")
|
2013-08-14 08:57:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
p.Section = strings.Trim(x[1], "/\\")
|
|
|
|
p.Path = strings.Trim(strings.Join(x[:len(x)-1], string(os.PathSeparator)), "/\\")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// If Url is provided it is assumed to be the complete relative path
|
|
|
|
// and will override everything
|
|
|
|
// Otherwise path + slug is used if provided
|
|
|
|
// Lastly path + filename is used if provided
|
|
|
|
func (s *Site) setOutFile(p *Page) {
|
|
|
|
// Always use Url if it's specified
|
|
|
|
if len(strings.TrimSpace(p.Url)) > 2 {
|
|
|
|
p.OutFile = strings.TrimSpace(p.Url)
|
2013-08-29 12:37:37 -04:00
|
|
|
|
|
|
|
if strings.HasSuffix(p.OutFile, "/") {
|
|
|
|
p.OutFile = p.OutFile + "index.html"
|
|
|
|
}
|
2013-08-14 08:57:14 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var outfile string
|
|
|
|
if len(strings.TrimSpace(p.Slug)) > 0 {
|
2013-09-01 12:56:58 -04:00
|
|
|
outfile = strings.TrimSpace(p.Slug) + "." + p.Extension
|
2013-08-14 08:57:14 -04:00
|
|
|
} else {
|
|
|
|
// Fall back to filename
|
|
|
|
_, t := filepath.Split(p.FileName)
|
2013-09-01 12:56:58 -04:00
|
|
|
outfile = replaceExtension(strings.TrimSpace(t), p.Extension)
|
2013-08-14 08:57:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
p.OutFile = p.Path + string(os.PathSeparator) + strings.TrimSpace(outfile)
|
|
|
|
}
|
|
|
|
|
2013-08-01 14:55:18 -04:00
|
|
|
func (s *Site) BuildSiteMeta() (err error) {
|
2013-07-04 11:32:55 -04:00
|
|
|
s.Indexes = make(IndexList)
|
|
|
|
s.Sections = make(Index)
|
|
|
|
|
2013-08-07 20:21:22 -04:00
|
|
|
for _, plural := range s.Config.Indexes {
|
2013-07-04 11:32:55 -04:00
|
|
|
s.Indexes[plural] = make(Index)
|
2013-08-31 20:47:21 -04:00
|
|
|
for _, p := range s.Pages {
|
2013-07-04 11:32:55 -04:00
|
|
|
vals := p.GetParam(plural)
|
|
|
|
|
|
|
|
if vals != nil {
|
2013-08-20 16:39:26 -04:00
|
|
|
v, ok := vals.([]string)
|
|
|
|
if ok {
|
|
|
|
for _, idx := range v {
|
2013-08-31 20:47:21 -04:00
|
|
|
s.Indexes[plural].Add(idx, p)
|
2013-08-20 16:39:26 -04:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
PrintErr("Invalid " + plural + " in " + p.File.FileName)
|
2013-07-04 11:32:55 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for k, _ := range s.Indexes[plural] {
|
|
|
|
s.Indexes[plural][k].Sort()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-31 20:47:21 -04:00
|
|
|
for _, p := range s.Pages {
|
|
|
|
s.Sections.Add(p.Section, p)
|
2013-07-04 11:32:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
for k, _ := range s.Sections {
|
|
|
|
s.Sections[k].Sort()
|
|
|
|
}
|
|
|
|
|
|
|
|
s.Info.Indexes = s.Indexes.BuildOrderedIndexList()
|
2013-08-03 03:09:28 -04:00
|
|
|
|
2013-08-01 14:55:18 -04:00
|
|
|
if len(s.Pages) == 0 {
|
2013-08-12 08:31:24 -04:00
|
|
|
return
|
2013-08-01 14:55:18 -04:00
|
|
|
}
|
2013-07-04 11:32:55 -04:00
|
|
|
s.Info.LastChange = s.Pages[0].Date
|
2013-08-07 14:23:24 -04:00
|
|
|
|
|
|
|
// populate pages with site metadata
|
|
|
|
for _, p := range s.Pages {
|
|
|
|
p.Site = s.Info
|
|
|
|
}
|
|
|
|
|
2013-08-01 14:55:18 -04:00
|
|
|
return
|
2013-07-04 11:32:55 -04:00
|
|
|
}
|
|
|
|
|
2013-08-13 13:46:05 -04:00
|
|
|
func (s *Site) possibleIndexes() (indexes []string) {
|
|
|
|
for _, p := range s.Pages {
|
|
|
|
for k, _ := range p.Params {
|
|
|
|
if !inStringArray(indexes, k) {
|
|
|
|
indexes = append(indexes, k)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func inStringArray(arr []string, el string) bool {
|
|
|
|
for _, v := range arr {
|
|
|
|
if v == el {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2013-08-10 10:35:34 -04:00
|
|
|
func (s *Site) RenderAliases() error {
|
2013-08-31 20:47:21 -04:00
|
|
|
for _, p := range s.Pages {
|
2013-08-10 10:35:34 -04:00
|
|
|
for _, a := range p.Aliases {
|
2013-08-30 17:38:33 -04:00
|
|
|
t := "alias"
|
2013-08-16 00:29:46 -04:00
|
|
|
if strings.HasSuffix(a, ".xhtml") {
|
|
|
|
t = "alias-xhtml"
|
|
|
|
}
|
2013-08-31 20:47:21 -04:00
|
|
|
content, err := s.RenderThing(p, t)
|
2013-08-10 10:35:34 -04:00
|
|
|
if strings.HasSuffix(a, "/") {
|
|
|
|
a = a + "index.html"
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-08-31 00:24:25 -04:00
|
|
|
err = s.WritePublic(a, content.Bytes())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-08-10 10:35:34 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-08-02 19:48:09 -04:00
|
|
|
func (s *Site) RenderPages() error {
|
2013-08-31 20:47:21 -04:00
|
|
|
for _, p := range s.Pages {
|
|
|
|
content, err := s.RenderThingOrDefault(p, p.Layout(), "_default/single.html")
|
2013-08-02 19:48:09 -04:00
|
|
|
if err != nil {
|
2013-08-27 05:09:50 -04:00
|
|
|
return err
|
2013-08-02 19:48:09 -04:00
|
|
|
}
|
2013-08-31 20:47:21 -04:00
|
|
|
p.RenderedContent = content
|
2013-07-04 11:32:55 -04:00
|
|
|
}
|
2013-08-02 19:48:09 -04:00
|
|
|
return nil
|
2013-07-04 11:32:55 -04:00
|
|
|
}
|
|
|
|
|
2013-08-31 00:24:25 -04:00
|
|
|
func (s *Site) WritePages() (err error) {
|
2013-07-04 11:32:55 -04:00
|
|
|
for _, p := range s.Pages {
|
2013-08-31 00:24:25 -04:00
|
|
|
err = s.WritePublic(p.OutFile, p.RenderedContent.Bytes())
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2013-07-04 11:32:55 -04:00
|
|
|
}
|
2013-08-31 00:24:25 -04:00
|
|
|
return
|
2013-07-04 11:32:55 -04:00
|
|
|
}
|
|
|
|
|
2013-08-02 19:48:09 -04:00
|
|
|
func (s *Site) RenderIndexes() error {
|
2013-08-07 20:21:22 -04:00
|
|
|
for singular, plural := range s.Config.Indexes {
|
2013-07-04 11:32:55 -04:00
|
|
|
for k, o := range s.Indexes[plural] {
|
|
|
|
n := s.NewNode()
|
|
|
|
n.Title = strings.Title(k)
|
2013-09-03 18:38:20 -04:00
|
|
|
url := helpers.Urlize(plural + "/" + k)
|
2013-09-01 12:56:58 -04:00
|
|
|
n.Url = url + ".html"
|
|
|
|
plink := n.Url
|
2013-08-31 20:47:21 -04:00
|
|
|
n.Permalink = permalink(s, plink)
|
|
|
|
n.RSSlink = permalink(s, url+".xml")
|
2013-07-04 11:32:55 -04:00
|
|
|
n.Date = o[0].Date
|
|
|
|
n.Data[singular] = o
|
|
|
|
n.Data["Pages"] = o
|
2013-08-13 17:47:14 -04:00
|
|
|
layout := "indexes/" + singular + ".html"
|
2013-08-02 19:48:09 -04:00
|
|
|
x, err := s.RenderThing(n, layout)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-07-19 03:10:42 -04:00
|
|
|
|
|
|
|
var base string
|
2013-09-01 12:56:58 -04:00
|
|
|
base = plural + "/" + k
|
2013-08-31 00:24:25 -04:00
|
|
|
err = s.WritePublic(base+".html", x.Bytes())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-07-04 11:32:55 -04:00
|
|
|
|
|
|
|
if a := s.Tmpl.Lookup("rss.xml"); a != nil {
|
|
|
|
// XML Feed
|
|
|
|
y := s.NewXMLBuffer()
|
2013-09-01 12:56:58 -04:00
|
|
|
n.Url = helpers.Urlize(plural + "/" + k + ".xml")
|
2013-08-31 20:47:21 -04:00
|
|
|
n.Permalink = permalink(s, n.Url)
|
2013-07-04 11:32:55 -04:00
|
|
|
s.Tmpl.ExecuteTemplate(y, "rss.xml", n)
|
2013-08-31 00:24:25 -04:00
|
|
|
err = s.WritePublic(base+".xml", y.Bytes())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-07-04 11:32:55 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-08-02 19:48:09 -04:00
|
|
|
return nil
|
2013-07-04 11:32:55 -04:00
|
|
|
}
|
|
|
|
|
2013-08-07 20:21:22 -04:00
|
|
|
func (s *Site) RenderIndexesIndexes() (err error) {
|
2013-08-13 17:47:14 -04:00
|
|
|
layout := "indexes/indexes.html"
|
2013-08-03 03:09:28 -04:00
|
|
|
if s.Tmpl.Lookup(layout) != nil {
|
2013-08-07 20:21:22 -04:00
|
|
|
for singular, plural := range s.Config.Indexes {
|
2013-08-03 03:09:28 -04:00
|
|
|
n := s.NewNode()
|
|
|
|
n.Title = strings.Title(plural)
|
2013-09-03 18:38:20 -04:00
|
|
|
url := helpers.Urlize(plural)
|
2013-08-03 03:09:28 -04:00
|
|
|
n.Url = url + "/index.html"
|
2013-08-31 20:47:21 -04:00
|
|
|
n.Permalink = permalink(s, n.Url)
|
2013-08-03 03:09:28 -04:00
|
|
|
n.Data["Singular"] = singular
|
|
|
|
n.Data["Plural"] = plural
|
|
|
|
n.Data["Index"] = s.Indexes[plural]
|
|
|
|
n.Data["OrderedIndex"] = s.Info.Indexes[plural]
|
|
|
|
|
2013-08-07 20:21:22 -04:00
|
|
|
x, err := s.RenderThing(n, layout)
|
2013-08-31 00:24:25 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = s.WritePublic(plural+"/index.html", x.Bytes())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-08-03 03:09:28 -04:00
|
|
|
}
|
|
|
|
}
|
2013-08-07 20:21:22 -04:00
|
|
|
return
|
2013-08-03 03:09:28 -04:00
|
|
|
}
|
|
|
|
|
2013-08-02 19:48:09 -04:00
|
|
|
func (s *Site) RenderLists() error {
|
2013-07-04 11:32:55 -04:00
|
|
|
for section, data := range s.Sections {
|
|
|
|
n := s.NewNode()
|
|
|
|
n.Title = strings.Title(inflect.Pluralize(section))
|
2013-09-03 18:38:20 -04:00
|
|
|
n.Url = helpers.Urlize(section + "/" + "index.html")
|
2013-08-31 20:47:21 -04:00
|
|
|
n.Permalink = permalink(s, n.Url)
|
|
|
|
n.RSSlink = permalink(s, section+".xml")
|
2013-07-04 11:32:55 -04:00
|
|
|
n.Date = data[0].Date
|
|
|
|
n.Data["Pages"] = data
|
2013-08-13 17:47:14 -04:00
|
|
|
layout := "indexes/" + section + ".html"
|
2013-07-04 11:32:55 -04:00
|
|
|
|
2013-08-27 05:09:50 -04:00
|
|
|
content, err := s.RenderThingOrDefault(n, layout, "_default/index.html")
|
2013-08-02 19:48:09 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-08-31 00:24:25 -04:00
|
|
|
err = s.WritePublic(section+"/index.html", content.Bytes())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-07-04 11:32:55 -04:00
|
|
|
|
|
|
|
if a := s.Tmpl.Lookup("rss.xml"); a != nil {
|
|
|
|
// XML Feed
|
2013-09-03 23:52:50 -04:00
|
|
|
n.Url = helpers.Urlize(section + ".xml")
|
2013-09-03 15:41:13 -04:00
|
|
|
n.Permalink = template.HTML(string(n.Site.BaseUrl) + n.Url)
|
2013-07-04 11:32:55 -04:00
|
|
|
y := s.NewXMLBuffer()
|
|
|
|
s.Tmpl.ExecuteTemplate(y, "rss.xml", n)
|
2013-08-31 00:24:25 -04:00
|
|
|
err = s.WritePublic(section+"/index.xml", y.Bytes())
|
|
|
|
return err
|
2013-07-04 11:32:55 -04:00
|
|
|
}
|
|
|
|
}
|
2013-08-02 19:48:09 -04:00
|
|
|
return nil
|
2013-07-04 11:32:55 -04:00
|
|
|
}
|
|
|
|
|
2013-08-02 19:48:09 -04:00
|
|
|
func (s *Site) RenderHomePage() error {
|
2013-07-04 11:32:55 -04:00
|
|
|
n := s.NewNode()
|
2013-07-11 22:04:57 -04:00
|
|
|
n.Title = n.Site.Title
|
2013-09-03 18:38:20 -04:00
|
|
|
n.Url = helpers.Urlize(string(n.Site.BaseUrl))
|
2013-08-31 20:47:21 -04:00
|
|
|
n.RSSlink = permalink(s, "index.xml")
|
|
|
|
n.Permalink = permalink(s, "")
|
2013-08-12 08:31:24 -04:00
|
|
|
if len(s.Pages) > 0 {
|
|
|
|
n.Date = s.Pages[0].Date
|
|
|
|
if len(s.Pages) < 9 {
|
|
|
|
n.Data["Pages"] = s.Pages
|
|
|
|
} else {
|
|
|
|
n.Data["Pages"] = s.Pages[:9]
|
|
|
|
}
|
2013-07-04 11:32:55 -04:00
|
|
|
}
|
2013-08-02 19:48:09 -04:00
|
|
|
x, err := s.RenderThing(n, "index.html")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-09-01 12:56:58 -04:00
|
|
|
err = s.WritePublic("/", x.Bytes())
|
2013-08-31 00:24:25 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-07-04 11:32:55 -04:00
|
|
|
|
|
|
|
if a := s.Tmpl.Lookup("rss.xml"); a != nil {
|
|
|
|
// XML Feed
|
2013-09-03 18:38:20 -04:00
|
|
|
n.Url = helpers.Urlize("index.xml")
|
2013-07-19 03:10:42 -04:00
|
|
|
n.Title = "Recent Content"
|
2013-08-31 20:47:21 -04:00
|
|
|
n.Permalink = permalink(s, "index.xml")
|
2013-07-04 11:32:55 -04:00
|
|
|
y := s.NewXMLBuffer()
|
|
|
|
s.Tmpl.ExecuteTemplate(y, "rss.xml", n)
|
2013-08-31 00:24:25 -04:00
|
|
|
err = s.WritePublic("index.xml", y.Bytes())
|
|
|
|
return err
|
2013-07-04 11:32:55 -04:00
|
|
|
}
|
2013-08-21 12:21:53 -04:00
|
|
|
|
|
|
|
if a := s.Tmpl.Lookup("404.html"); a != nil {
|
2013-09-03 18:38:20 -04:00
|
|
|
n.Url = helpers.Urlize("404.html")
|
2013-08-21 12:21:53 -04:00
|
|
|
n.Title = "404 Page not found"
|
2013-08-31 20:47:21 -04:00
|
|
|
n.Permalink = permalink(s, "404.html")
|
2013-08-21 12:21:53 -04:00
|
|
|
x, err := s.RenderThing(n, "404.html")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-08-31 00:24:25 -04:00
|
|
|
err = s.WritePublic("404.html", x.Bytes())
|
|
|
|
return err
|
2013-08-21 12:21:53 -04:00
|
|
|
}
|
|
|
|
|
2013-08-02 19:48:09 -04:00
|
|
|
return nil
|
2013-07-04 11:32:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Site) Stats() {
|
|
|
|
fmt.Printf("%d pages created \n", len(s.Pages))
|
2013-08-07 20:21:22 -04:00
|
|
|
for _, pl := range s.Config.Indexes {
|
2013-08-13 17:47:14 -04:00
|
|
|
fmt.Printf("%d %s index created\n", len(s.Indexes[pl]), pl)
|
2013-07-04 11:32:55 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-03 15:41:13 -04:00
|
|
|
func permalink(s *Site, plink string) template.HTML {
|
|
|
|
return template.HTML(MakePermalink(string(s.Info.BaseUrl), plink))
|
2013-08-31 20:47:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Site) NewNode() *Node {
|
|
|
|
return &Node{
|
|
|
|
Data: make(map[string]interface{}),
|
|
|
|
Site: s.Info,
|
|
|
|
}
|
2013-07-04 11:32:55 -04:00
|
|
|
}
|
|
|
|
|
2013-08-02 19:48:09 -04:00
|
|
|
func (s *Site) RenderThing(d interface{}, layout string) (*bytes.Buffer, error) {
|
2013-08-23 17:47:03 -04:00
|
|
|
if s.Tmpl.Lookup(layout) == nil {
|
2013-08-31 20:47:21 -04:00
|
|
|
return nil, fmt.Errorf("Layout not found: %s", layout)
|
2013-08-23 17:47:03 -04:00
|
|
|
}
|
2013-07-04 11:32:55 -04:00
|
|
|
buffer := new(bytes.Buffer)
|
2013-08-02 19:48:09 -04:00
|
|
|
err := s.Tmpl.ExecuteTemplate(buffer, layout, d)
|
|
|
|
return buffer, err
|
2013-07-04 11:32:55 -04:00
|
|
|
}
|
|
|
|
|
2013-08-27 05:09:50 -04:00
|
|
|
func (s *Site) RenderThingOrDefault(d interface{}, layout string, defaultLayout string) (*bytes.Buffer, error) {
|
|
|
|
content, err := s.RenderThing(d, layout)
|
|
|
|
if err != nil {
|
|
|
|
var err2 error
|
|
|
|
content, err2 = s.RenderThing(d, defaultLayout)
|
|
|
|
if err2 == nil {
|
|
|
|
return content, err2
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return content, err
|
|
|
|
}
|
|
|
|
|
2013-07-04 11:32:55 -04:00
|
|
|
func (s *Site) NewXMLBuffer() *bytes.Buffer {
|
|
|
|
header := "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\" ?>\n"
|
|
|
|
return bytes.NewBufferString(header)
|
|
|
|
}
|
|
|
|
|
2013-09-03 23:52:50 -04:00
|
|
|
func (s *Site) initTarget() {
|
2013-09-01 12:56:58 -04:00
|
|
|
if s.Target == nil {
|
|
|
|
s.Target = &target.Filesystem{
|
|
|
|
PublishDir: s.absPublishDir(),
|
|
|
|
UglyUrls: s.Config.UglyUrls,
|
|
|
|
}
|
2013-08-30 20:18:05 -04:00
|
|
|
}
|
2013-09-03 23:52:50 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Site) WritePublic(path string, content []byte) (err error) {
|
|
|
|
s.initTarget()
|
2013-08-30 20:18:05 -04:00
|
|
|
|
2013-08-07 20:21:22 -04:00
|
|
|
if s.Config.Verbose {
|
2013-07-19 03:10:42 -04:00
|
|
|
fmt.Println(path)
|
2013-07-04 11:32:55 -04:00
|
|
|
}
|
|
|
|
|
2013-09-01 12:56:58 -04:00
|
|
|
return s.Target.Publish(path, bytes.NewReader(content))
|
2013-07-04 11:32:55 -04:00
|
|
|
}
|