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-12-05 09:29:41 -05:00
|
|
|
"github.com/spf13/hugo/helpers"
|
2013-09-05 01:28:59 -04:00
|
|
|
"github.com/spf13/hugo/source"
|
2013-08-30 20:18:05 -04:00
|
|
|
"github.com/spf13/hugo/target"
|
2013-09-03 18:38:20 -04:00
|
|
|
"github.com/spf13/hugo/template/bundle"
|
2013-09-17 17:16:06 -04:00
|
|
|
"github.com/spf13/hugo/transform"
|
2013-07-04 11:32:55 -04:00
|
|
|
"github.com/spf13/nitro"
|
2013-09-03 18:38:20 -04:00
|
|
|
"html/template"
|
2013-09-18 12:15:46 -04:00
|
|
|
"io"
|
2013-09-29 02:07:41 -04:00
|
|
|
"net/url"
|
2013-07-04 11:32:55 -04:00
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2013-11-05 00:28:08 -05:00
|
|
|
var _ = transform.AbsURL
|
|
|
|
|
2013-10-03 09:44:45 -04:00
|
|
|
var DefaultTimer *nitro.B
|
2013-08-07 20:21:22 -04:00
|
|
|
|
2013-09-29 02:07:41 -04:00
|
|
|
func MakePermalink(base *url.URL, path *url.URL) *url.URL {
|
2013-09-25 00:24:49 -04:00
|
|
|
return base.ResolveReference(path)
|
2013-09-03 18:38:20 -04:00
|
|
|
}
|
|
|
|
|
2013-10-17 23:57:25 -04:00
|
|
|
// Site contains all the information relevant for constructing a static
|
2013-09-01 00:13:04 -04:00
|
|
|
// 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
|
2013-11-18 04:35:56 -05:00
|
|
|
// various targets that will get generated. There will be canonical
|
|
|
|
// listing. The canonical path can be overruled based on a pattern.
|
2013-09-01 00:13:04 -04:00
|
|
|
//
|
|
|
|
// 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
|
2013-11-18 04:35:56 -05:00
|
|
|
// layout based on numerous different elements.
|
2013-09-01 00:13:04 -04:00
|
|
|
//
|
|
|
|
// 5. The entire collection of files is written to disk.
|
2013-07-04 11:32:55 -04:00
|
|
|
type Site struct {
|
2013-11-05 00:28:08 -05:00
|
|
|
Config Config
|
|
|
|
Pages Pages
|
|
|
|
Tmpl bundle.Template
|
|
|
|
Indexes IndexList
|
|
|
|
Source source.Input
|
|
|
|
Sections Index
|
|
|
|
Info SiteInfo
|
|
|
|
Shortcodes map[string]ShortcodeFunc
|
|
|
|
timer *nitro.B
|
|
|
|
Target target.Output
|
|
|
|
Alias target.AliasPublisher
|
|
|
|
Completed chan bool
|
|
|
|
RunMode runmode
|
2013-11-10 15:04:51 -05:00
|
|
|
params map[string]interface{}
|
2013-07-04 11:32:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
type SiteInfo struct {
|
2013-09-03 15:41:13 -04:00
|
|
|
BaseUrl template.URL
|
2013-10-25 18:40:55 -04:00
|
|
|
Indexes IndexList
|
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-11-18 04:35:56 -05:00
|
|
|
Permalinks PermalinkOverrides
|
2013-11-10 15:04:51 -05:00
|
|
|
Params map[string]interface{}
|
2013-07-04 11:32:55 -04:00
|
|
|
}
|
|
|
|
|
2013-10-25 18:03:14 -04:00
|
|
|
type runmode struct {
|
|
|
|
Watching bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Site) Running() bool {
|
|
|
|
return s.RunMode.Watching
|
|
|
|
}
|
|
|
|
|
2013-10-03 09:44:45 -04:00
|
|
|
func init() {
|
|
|
|
DefaultTimer = nitro.Initalize()
|
|
|
|
}
|
|
|
|
|
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-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()
|
2013-09-13 18:06:37 -04:00
|
|
|
s.Alias = &target.HTMLRedirectAlias{
|
|
|
|
PublishDir: s.absPublishDir(),
|
|
|
|
}
|
2013-09-03 23:52:50 -04:00
|
|
|
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) {
|
2013-10-31 18:46:55 -04:00
|
|
|
if err = s.initialize(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
2013-08-31 20:29:19 -04:00
|
|
|
s.prepTemplates()
|
|
|
|
s.timerStep("initialize & template prep")
|
2013-09-04 19:57:17 -04:00
|
|
|
if err = s.CreatePages(); err != nil {
|
2013-10-31 18:46:55 -04:00
|
|
|
return
|
2013-09-04 19:57:17 -04:00
|
|
|
}
|
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-09-20 20:03:43 -04:00
|
|
|
func (s *Site) setupPrevNext() {
|
|
|
|
for i, page := range s.Pages {
|
|
|
|
if i < len(s.Pages)-1 {
|
|
|
|
page.Next = s.Pages[i+1]
|
|
|
|
}
|
|
|
|
|
|
|
|
if i > 0 {
|
|
|
|
page.Prev = s.Pages[i-1]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-31 20:29:19 -04:00
|
|
|
func (s *Site) Render() (err error) {
|
2013-09-13 17:46:34 -04:00
|
|
|
if err = s.RenderAliases(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
2013-08-31 20:29:19 -04:00
|
|
|
s.timerStep("render and write aliases")
|
|
|
|
if err = s.RenderIndexes(); err != nil {
|
2013-08-12 19:10:38 -04:00
|
|
|
return
|
|
|
|
}
|
2013-08-31 20:29:19 -04:00
|
|
|
s.timerStep("render and write indexes")
|
2013-10-07 09:07:14 -04:00
|
|
|
s.RenderIndexesIndexes()
|
|
|
|
s.timerStep("render & write index indexes")
|
2013-10-09 23:38:51 -04:00
|
|
|
if err = s.RenderLists(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
2013-08-31 20:29:19 -04:00
|
|
|
s.timerStep("render and write lists")
|
|
|
|
if err = s.RenderPages(); err != nil {
|
2013-08-02 19:48:09 -04:00
|
|
|
return
|
|
|
|
}
|
2013-09-13 17:46:34 -04:00
|
|
|
s.timerStep("render and write pages")
|
2013-08-31 20:29:19 -04:00
|
|
|
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) 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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-25 00:24:49 -04:00
|
|
|
func (s *Site) initialize() (err error) {
|
|
|
|
if err = s.checkDirectories(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-07-04 11:32:55 -04:00
|
|
|
|
2013-08-30 17:38:33 -04:00
|
|
|
staticDir := s.Config.GetAbsPath(s.Config.StaticDir + "/")
|
2013-08-15 14:58:34 -04:00
|
|
|
|
2013-09-05 01:28:59 -04:00
|
|
|
s.Source = &source.Filesystem{
|
|
|
|
AvoidPaths: []string{staticDir},
|
|
|
|
Base: s.absContentDir(),
|
2013-07-04 11:32:55 -04:00
|
|
|
}
|
|
|
|
|
2013-09-12 19:17:53 -04:00
|
|
|
s.initializeSiteInfo()
|
|
|
|
|
|
|
|
s.Shortcodes = make(map[string]ShortcodeFunc)
|
2013-09-25 00:24:49 -04:00
|
|
|
return
|
2013-09-12 19:17:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Site) initializeSiteInfo() {
|
2013-08-13 08:43:42 -04:00
|
|
|
s.Info = SiteInfo{
|
2013-11-18 04:35:56 -05:00
|
|
|
BaseUrl: template.URL(s.Config.BaseUrl),
|
|
|
|
Title: s.Config.Title,
|
|
|
|
Recent: &s.Pages,
|
|
|
|
Config: &s.Config,
|
|
|
|
Params: s.Config.Params,
|
|
|
|
Permalinks: s.Config.Permalinks,
|
2013-08-13 08:43:42 -04:00
|
|
|
}
|
2013-07-04 11:32:55 -04:00
|
|
|
}
|
|
|
|
|
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-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-09-25 00:24:49 -04:00
|
|
|
func (s *Site) checkDirectories() (err error) {
|
2013-10-31 18:46:55 -04:00
|
|
|
/*
|
|
|
|
if b, _ := dirExists(s.absLayoutDir()); !b {
|
|
|
|
return fmt.Errorf("No layout directory found, expecting to find it at " + s.absLayoutDir())
|
|
|
|
}
|
|
|
|
*/
|
2013-08-07 20:21:22 -04:00
|
|
|
if b, _ := dirExists(s.absContentDir()); !b {
|
2013-09-25 00:24:49 -04:00
|
|
|
return fmt.Errorf("No source directory found, expecting to find it at " + s.absContentDir())
|
2013-07-04 11:32:55 -04:00
|
|
|
}
|
2013-09-25 00:24:49 -04:00
|
|
|
return
|
2013-07-04 11:32:55 -04:00
|
|
|
}
|
|
|
|
|
2013-09-04 19:57:17 -04:00
|
|
|
func (s *Site) CreatePages() (err error) {
|
2013-09-29 02:07:05 -04:00
|
|
|
if s.Source == nil {
|
2013-10-31 18:46:55 -04:00
|
|
|
panic(fmt.Sprintf("s.Source not set %s", s.absContentDir()))
|
2013-09-29 02:07:05 -04:00
|
|
|
}
|
|
|
|
if len(s.Source.Files()) < 1 {
|
2013-10-31 18:46:55 -04:00
|
|
|
return fmt.Errorf("No source files found in %s", s.absContentDir())
|
2013-09-29 02:07:05 -04:00
|
|
|
}
|
2013-09-05 01:28:59 -04:00
|
|
|
for _, file := range s.Source.Files() {
|
2013-09-20 20:03:43 -04:00
|
|
|
page, err := ReadFrom(file.Contents, file.LogicalName)
|
2013-09-04 19:57:17 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-07-04 11:32:55 -04:00
|
|
|
page.Site = s.Info
|
|
|
|
page.Tmpl = s.Tmpl
|
2013-09-20 20:03:43 -04:00
|
|
|
page.Section = file.Section
|
|
|
|
page.Dir = file.Dir
|
2013-12-06 23:32:00 -05:00
|
|
|
|
2013-12-06 23:57:24 -05:00
|
|
|
//Handling short codes prior to Conversion to HTML
|
|
|
|
page.ProcessShortcodes(s.Tmpl)
|
2013-12-06 23:32:00 -05:00
|
|
|
|
|
|
|
err = page.Convert()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
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-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)
|
2013-10-24 18:24:47 -04:00
|
|
|
weight := p.GetParam(plural + "_weight")
|
|
|
|
if weight == nil {
|
|
|
|
weight = 0
|
|
|
|
}
|
2013-07-04 11:32:55 -04:00
|
|
|
|
|
|
|
if vals != nil {
|
2013-08-20 16:39:26 -04:00
|
|
|
v, ok := vals.([]string)
|
|
|
|
if ok {
|
|
|
|
for _, idx := range v {
|
2013-12-20 09:10:05 -05:00
|
|
|
x := WeightedPage{weight.(int), p}
|
2013-10-17 23:57:25 -04:00
|
|
|
|
|
|
|
s.Indexes[plural].Add(idx, x)
|
2013-08-20 16:39:26 -04:00
|
|
|
}
|
|
|
|
} else {
|
2013-09-25 00:24:49 -04:00
|
|
|
if s.Config.Verbose {
|
|
|
|
fmt.Fprintf(os.Stderr, "Invalid %s in %s\n", plural, p.File.FileName)
|
|
|
|
}
|
2013-07-04 11:32:55 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-11-24 16:48:57 -05:00
|
|
|
for k := range s.Indexes[plural] {
|
2013-07-04 11:32:55 -04:00
|
|
|
s.Indexes[plural][k].Sort()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-07 09:07:14 -04:00
|
|
|
for i, p := range s.Pages {
|
2013-12-20 09:10:05 -05:00
|
|
|
s.Sections.Add(p.Section, WeightedPage{s.Pages[i].Weight, s.Pages[i]})
|
2013-07-04 11:32:55 -04:00
|
|
|
}
|
|
|
|
|
2013-11-24 16:48:57 -05:00
|
|
|
for k := range s.Sections {
|
2013-07-04 11:32:55 -04:00
|
|
|
s.Sections[k].Sort()
|
|
|
|
}
|
|
|
|
|
2013-10-25 18:40:55 -04:00
|
|
|
s.Info.Indexes = s.Indexes
|
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 {
|
2013-11-24 16:48:57 -05:00
|
|
|
for k := range p.Params {
|
2013-08-13 13:46:05 -04:00
|
|
|
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-09-25 00:24:49 -04:00
|
|
|
plink, err := p.Permalink()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := s.WriteAlias(a, template.HTML(plink)); err != nil {
|
2013-08-31 00:24:25 -04:00
|
|
|
return err
|
|
|
|
}
|
2013-08-10 10:35:34 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-09-18 17:21:27 -04:00
|
|
|
func (s *Site) RenderPages() (err error) {
|
2013-08-31 20:47:21 -04:00
|
|
|
for _, p := range s.Pages {
|
2013-10-07 00:57:45 -04:00
|
|
|
var layout []string
|
2013-09-18 17:21:27 -04:00
|
|
|
|
|
|
|
if !p.IsRenderable() {
|
2013-10-07 00:57:45 -04:00
|
|
|
self := "__" + p.TargetPath()
|
|
|
|
_, err := s.Tmpl.New(self).Parse(string(p.Content))
|
2013-09-18 17:21:27 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-10-07 00:57:45 -04:00
|
|
|
layout = append(layout, self)
|
2013-09-18 17:21:27 -04:00
|
|
|
} else {
|
2013-10-07 00:57:45 -04:00
|
|
|
layout = append(layout, p.Layout()...)
|
|
|
|
layout = append(layout, "_default/single.html")
|
2013-09-18 17:21:27 -04:00
|
|
|
}
|
|
|
|
|
2013-10-07 00:57:45 -04:00
|
|
|
err := s.render(p, p.TargetPath(), layout...)
|
2013-08-31 00:24:25 -04:00
|
|
|
if err != nil {
|
2013-09-13 13:30:27 -04:00
|
|
|
return err
|
2013-08-31 00:24:25 -04:00
|
|
|
}
|
2013-07-04 11:32:55 -04:00
|
|
|
}
|
2013-09-13 13:30:27 -04:00
|
|
|
return nil
|
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-10-17 23:57:25 -04:00
|
|
|
n.Date = o[0].Page.Date
|
2013-07-04 11:32:55 -04:00
|
|
|
n.Data[singular] = o
|
2013-10-24 18:24:47 -04:00
|
|
|
n.Data["Pages"] = o.Pages()
|
2013-08-13 17:47:14 -04:00
|
|
|
layout := "indexes/" + singular + ".html"
|
2013-07-19 03:10:42 -04:00
|
|
|
|
|
|
|
var base string
|
2013-09-01 12:56:58 -04:00
|
|
|
base = plural + "/" + k
|
2013-10-02 11:05:57 -04:00
|
|
|
err := s.render(n, base+".html", layout)
|
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-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-10-02 09:34:41 -04:00
|
|
|
err := s.render(n, base+".xml", "rss.xml")
|
2013-08-31 00:24:25 -04:00
|
|
|
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]
|
2013-10-31 09:49:29 -04:00
|
|
|
// keep the following just for legacy reasons
|
|
|
|
n.Data["OrderedIndex"] = s.Indexes[plural]
|
2013-08-03 03:09:28 -04:00
|
|
|
|
2013-10-02 09:34:41 -04:00
|
|
|
err := s.render(n, plural+"/index.html", layout)
|
2013-08-31 00:24:25 -04:00
|
|
|
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-10-17 23:57:25 -04:00
|
|
|
n.Date = data[0].Page.Date
|
2013-10-24 18:24:47 -04:00
|
|
|
n.Data["Pages"] = data.Pages()
|
2013-08-13 17:47:14 -04:00
|
|
|
layout := "indexes/" + section + ".html"
|
2013-07-04 11:32:55 -04:00
|
|
|
|
2013-10-02 11:05:57 -04:00
|
|
|
err := s.render(n, section, layout, "_default/indexes.html")
|
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 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-10-09 23:38:51 -04:00
|
|
|
err = s.render(n, section+".xml", "rss.xml")
|
|
|
|
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-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-11-14 09:37:58 -05:00
|
|
|
n.Data["Pages"] = s.Pages
|
2013-10-08 12:33:57 -04:00
|
|
|
err := s.render(n, "/", "index.html")
|
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-10-09 23:38:51 -04:00
|
|
|
err := s.render(n, ".xml", "rss.xml")
|
2013-10-07 09:23:42 -04:00
|
|
|
if err != nil {
|
|
|
|
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-10-02 09:34:41 -04:00
|
|
|
return s.render(n, "404.html", "404.html")
|
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-29 02:07:41 -04:00
|
|
|
func permalink(s *Site, plink string) template.HTML {
|
2013-09-25 00:24:49 -04:00
|
|
|
base, err := url.Parse(string(s.Config.BaseUrl))
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
path, err := url.Parse(plink)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return template.HTML(MakePermalink(base, path).String())
|
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-10-02 09:34:41 -04:00
|
|
|
func (s *Site) render(d interface{}, out string, layouts ...string) (err error) {
|
2013-10-01 22:58:15 -04:00
|
|
|
|
2013-10-02 09:34:41 -04:00
|
|
|
layout := s.findFirstLayout(layouts...)
|
2013-10-02 11:05:57 -04:00
|
|
|
if layout == "" {
|
|
|
|
if s.Config.Verbose {
|
|
|
|
fmt.Printf("Unable to locate layout: %s\n", layouts)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2013-10-02 17:25:16 -04:00
|
|
|
|
2013-11-01 01:14:11 -04:00
|
|
|
absURL, err := transform.AbsURL(s.Config.BaseUrl)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2013-11-05 17:28:06 -05:00
|
|
|
transformer := transform.NewChain(absURL...)
|
2013-10-02 17:25:16 -04:00
|
|
|
|
2013-11-05 00:29:37 -05:00
|
|
|
var renderBuffer *bytes.Buffer
|
2013-10-02 09:34:41 -04:00
|
|
|
|
2013-11-05 00:28:08 -05:00
|
|
|
if strings.HasSuffix(out, ".xml") {
|
2013-11-05 00:29:37 -05:00
|
|
|
renderBuffer = s.NewXMLBuffer()
|
2013-11-05 00:28:08 -05:00
|
|
|
} else {
|
2013-11-05 00:29:37 -05:00
|
|
|
renderBuffer = new(bytes.Buffer)
|
2013-11-05 00:28:08 -05:00
|
|
|
}
|
|
|
|
|
2013-11-05 00:29:37 -05:00
|
|
|
err = s.renderThing(d, layout, renderBuffer)
|
2013-11-05 00:28:08 -05:00
|
|
|
if err != nil {
|
|
|
|
// Behavior here should be dependent on if running in server or watch mode.
|
|
|
|
fmt.Println(fmt.Errorf("Rendering error: %v", err))
|
|
|
|
if !s.Running() {
|
|
|
|
os.Exit(-1)
|
|
|
|
}
|
|
|
|
}
|
2013-10-02 17:25:16 -04:00
|
|
|
|
2013-11-05 00:28:08 -05:00
|
|
|
var outBuffer = new(bytes.Buffer)
|
2013-11-05 00:29:37 -05:00
|
|
|
if strings.HasSuffix(out, ".xml") {
|
|
|
|
outBuffer = renderBuffer
|
|
|
|
} else {
|
|
|
|
transformer.Apply(outBuffer, renderBuffer)
|
|
|
|
}
|
|
|
|
|
2013-11-05 00:28:08 -05:00
|
|
|
return s.WritePublic(out, outBuffer)
|
2013-10-01 22:58:15 -04:00
|
|
|
}
|
|
|
|
|
2013-10-02 11:05:57 -04:00
|
|
|
func (s *Site) findFirstLayout(layouts ...string) (layout string) {
|
|
|
|
for _, layout = range layouts {
|
2013-10-02 09:34:41 -04:00
|
|
|
if s.Tmpl.Lookup(layout) != nil {
|
2013-10-02 11:05:57 -04:00
|
|
|
return
|
2013-10-02 09:34:41 -04:00
|
|
|
}
|
2013-08-23 17:47:03 -04:00
|
|
|
}
|
2013-10-02 11:05:57 -04:00
|
|
|
return ""
|
2013-07-04 11:32:55 -04:00
|
|
|
}
|
|
|
|
|
2013-11-05 00:28:08 -05:00
|
|
|
func (s *Site) renderThing(d interface{}, layout string, w io.Writer) error {
|
2013-10-02 09:34:41 -04:00
|
|
|
// If the template doesn't exist, then return, but leave the Writer open
|
|
|
|
if s.Tmpl.Lookup(layout) == nil {
|
|
|
|
return fmt.Errorf("Layout not found: %s", layout)
|
2013-08-27 05:09:50 -04:00
|
|
|
}
|
2013-11-05 00:28:08 -05:00
|
|
|
//defer w.Close()
|
2013-10-02 09:34:41 -04:00
|
|
|
return s.Tmpl.ExecuteTemplate(w, layout, d)
|
2013-08-27 05:09:50 -04:00
|
|
|
}
|
|
|
|
|
2013-11-05 00:28:08 -05:00
|
|
|
func (s *Site) NewXMLBuffer() *bytes.Buffer {
|
2013-07-04 11:32:55 -04:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2013-10-02 17:25:16 -04:00
|
|
|
func (s *Site) WritePublic(path string, reader io.Reader) (err error) {
|
2013-09-03 23:52:50 -04:00
|
|
|
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-10-01 18:19:16 -04:00
|
|
|
return s.Target.Publish(path, reader)
|
2013-07-04 11:32:55 -04:00
|
|
|
}
|
2013-09-12 19:17:53 -04:00
|
|
|
|
2013-09-13 00:18:13 -04:00
|
|
|
func (s *Site) WriteAlias(path string, permalink template.HTML) (err error) {
|
2013-09-12 19:17:53 -04:00
|
|
|
if s.Alias == nil {
|
|
|
|
s.initTarget()
|
2013-09-13 00:18:13 -04:00
|
|
|
s.Alias = &target.HTMLRedirectAlias{
|
|
|
|
PublishDir: s.absPublishDir(),
|
|
|
|
}
|
2013-09-12 19:17:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if s.Config.Verbose {
|
|
|
|
fmt.Println(path)
|
|
|
|
}
|
|
|
|
|
2013-09-13 00:18:13 -04:00
|
|
|
return s.Alias.Publish(path, permalink)
|
2013-09-12 19:17:53 -04:00
|
|
|
}
|