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"
|
|
|
|
"github.com/spf13/nitro"
|
|
|
|
"html/template"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
//"sync"
|
|
|
|
)
|
|
|
|
|
2013-07-19 03:10:42 -04:00
|
|
|
const slash = string(os.PathSeparator)
|
|
|
|
|
2013-08-07 20:21:22 -04:00
|
|
|
var DefaultTimer = nitro.Initalize()
|
|
|
|
|
2013-07-04 11:32:55 -04:00
|
|
|
type Site struct {
|
2013-08-09 21:07:35 -04:00
|
|
|
Config Config
|
2013-07-04 11:32:55 -04:00
|
|
|
Pages Pages
|
|
|
|
Tmpl *template.Template
|
|
|
|
Indexes IndexList
|
|
|
|
Files []string
|
|
|
|
Directories []string
|
|
|
|
Sections Index
|
|
|
|
Info SiteInfo
|
|
|
|
Shortcodes map[string]ShortcodeFunc
|
|
|
|
timer *nitro.B
|
|
|
|
}
|
|
|
|
|
|
|
|
type SiteInfo struct {
|
|
|
|
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-01 14:55:18 -04:00
|
|
|
func (site *Site) Build() (err error) {
|
|
|
|
if err = site.Process(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
2013-08-02 19:48:09 -04:00
|
|
|
if err = site.Render(); err != nil {
|
2013-08-07 20:34:17 -04:00
|
|
|
fmt.Printf("Error rendering site: %s\n", err)
|
|
|
|
fmt.Printf("Available templates:")
|
|
|
|
for _, template := range site.Tmpl.Templates() {
|
|
|
|
fmt.Printf("\t%s\n", template.Name())
|
|
|
|
}
|
2013-08-02 19:48:09 -04:00
|
|
|
return
|
|
|
|
}
|
2013-07-04 11:32:55 -04:00
|
|
|
site.Write()
|
2013-08-01 14:55:18 -04:00
|
|
|
return nil
|
2013-07-04 11:32:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (site *Site) Analyze() {
|
|
|
|
site.Process()
|
|
|
|
site.checkDescriptions()
|
|
|
|
}
|
|
|
|
|
2013-08-02 16:30:26 -04:00
|
|
|
func (site *Site) Process() (err error) {
|
2013-07-04 11:32:55 -04:00
|
|
|
site.initialize()
|
|
|
|
site.prepTemplates()
|
2013-08-07 20:21:22 -04:00
|
|
|
site.timerStep("initialize & template prep")
|
2013-07-04 11:32:55 -04:00
|
|
|
site.CreatePages()
|
2013-08-02 16:30:26 -04:00
|
|
|
site.setupPrevNext()
|
2013-08-07 20:21:22 -04:00
|
|
|
site.timerStep("import pages")
|
2013-08-01 14:55:18 -04:00
|
|
|
if err = site.BuildSiteMeta(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
2013-08-07 20:21:22 -04:00
|
|
|
site.timerStep("build indexes")
|
2013-08-01 14:55:18 -04:00
|
|
|
return
|
2013-07-04 11:32:55 -04:00
|
|
|
}
|
|
|
|
|
2013-08-02 19:48:09 -04:00
|
|
|
func (site *Site) Render() (err error) {
|
2013-08-10 10:35:34 -04:00
|
|
|
site.RenderAliases()
|
2013-08-10 10:44:22 -04:00
|
|
|
site.timerStep("render and write aliases")
|
2013-07-04 11:32:55 -04:00
|
|
|
site.ProcessShortcodes()
|
2013-08-07 20:21:22 -04:00
|
|
|
site.timerStep("render shortcodes")
|
2013-07-10 00:14:02 -04:00
|
|
|
site.AbsUrlify()
|
2013-08-07 20:21:22 -04:00
|
|
|
site.timerStep("absolute URLify")
|
2013-07-11 23:26:03 -04:00
|
|
|
site.RenderIndexes()
|
2013-08-03 03:09:28 -04:00
|
|
|
site.RenderIndexesIndexes()
|
2013-08-07 20:21:22 -04:00
|
|
|
site.timerStep("render and write indexes")
|
2013-07-11 23:26:03 -04:00
|
|
|
site.RenderLists()
|
2013-08-07 20:21:22 -04:00
|
|
|
site.timerStep("render and write lists")
|
2013-08-02 19:48:09 -04:00
|
|
|
if err = site.RenderPages(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
2013-08-07 20:21:22 -04:00
|
|
|
site.timerStep("render pages")
|
2013-07-04 11:32:55 -04:00
|
|
|
site.RenderHomePage()
|
2013-08-07 20:21:22 -04:00
|
|
|
site.timerStep("render and write homepage")
|
2013-08-02 19:48:09 -04:00
|
|
|
return
|
2013-07-04 11:32:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (site *Site) Write() {
|
|
|
|
site.WritePages()
|
2013-08-07 20:21:22 -04:00
|
|
|
site.timerStep("write pages")
|
2013-07-04 11:32:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (site *Site) checkDescriptions() {
|
|
|
|
for _, p := range site.Pages {
|
|
|
|
if len(p.Description) < 60 {
|
|
|
|
fmt.Print(p.FileName + " ")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Site) prepTemplates() {
|
|
|
|
var templates = template.New("")
|
|
|
|
|
|
|
|
funcMap := template.FuncMap{
|
|
|
|
"urlize": Urlize,
|
|
|
|
"gt": Gt,
|
|
|
|
"isset": IsSet,
|
|
|
|
"echoParam": ReturnWhenSet,
|
|
|
|
}
|
|
|
|
|
|
|
|
templates.Funcs(funcMap)
|
|
|
|
|
2013-08-10 10:35:34 -04:00
|
|
|
s.Tmpl = templates
|
|
|
|
s.primeTemplates()
|
|
|
|
s.loadTemplates()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Site) loadTemplates() {
|
2013-07-04 11:32:55 -04:00
|
|
|
walker := func(path string, fi os.FileInfo, err error) error {
|
|
|
|
if err != nil {
|
|
|
|
PrintErr("Walker: ", err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if !fi.IsDir() {
|
|
|
|
filetext, err := ioutil.ReadFile(path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
text := string(filetext)
|
2013-08-12 16:57:47 -04:00
|
|
|
t := s.Tmpl.New(s.generateTemplateNameFrom(path))
|
2013-07-04 11:32:55 -04:00
|
|
|
template.Must(t.Parse(text))
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-08-12 16:57:47 -04:00
|
|
|
filepath.Walk(s.absLayoutDir(), walker)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Site) generateTemplateNameFrom(path string) (name string) {
|
|
|
|
name = filepath.ToSlash(path[len(s.absLayoutDir())+1:])
|
|
|
|
return
|
2013-08-10 10:35:34 -04:00
|
|
|
}
|
2013-07-04 11:32:55 -04:00
|
|
|
|
2013-08-10 10:35:34 -04:00
|
|
|
func (s *Site) primeTemplates() {
|
2013-08-10 10:41:10 -04:00
|
|
|
alias := "<!DOCTYPE html>\n <html>\n <head>\n <link rel=\"canonical\" href=\"{{ .Permalink }}\"/>\n <meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\" />\n <meta http-equiv=\"refresh\" content=\"0;url={{ .Permalink }}\" />\n </head>\n </html>"
|
2013-08-10 10:35:34 -04:00
|
|
|
|
|
|
|
t := s.Tmpl.New("alias")
|
|
|
|
template.Must(t.Parse(alias))
|
2013-07-04 11:32:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Site) initialize() {
|
|
|
|
site := s
|
|
|
|
|
|
|
|
s.checkDirectories()
|
|
|
|
|
|
|
|
walker := func(path string, fi os.FileInfo, err error) error {
|
|
|
|
if err != nil {
|
|
|
|
PrintErr("Walker: ", err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if fi.IsDir() {
|
|
|
|
site.Directories = append(site.Directories, path)
|
|
|
|
return nil
|
|
|
|
} else {
|
|
|
|
site.Files = append(site.Files, path)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-07 20:21:22 -04:00
|
|
|
filepath.Walk(s.Config.GetAbsPath(s.Config.ContentDir), walker)
|
2013-07-04 11:32:55 -04:00
|
|
|
|
2013-08-13 08:43:42 -04:00
|
|
|
s.Info = SiteInfo{
|
|
|
|
BaseUrl: template.URL(s.Config.BaseUrl),
|
|
|
|
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-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() {
|
|
|
|
for i, _ := range s.Pages {
|
2013-07-05 12:03:47 -04:00
|
|
|
s.Pages[i].Content = template.HTML(ShortcodesHandle(string(s.Pages[i].Content), s.Pages[i], 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-07-10 00:14:02 -04:00
|
|
|
for i, _ := range s.Pages {
|
|
|
|
content := string(s.Pages[i].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-07-10 00:14:02 -04:00
|
|
|
s.Pages[i].Content = template.HTML(content)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-04 11:32:55 -04:00
|
|
|
func (s *Site) CreatePages() {
|
|
|
|
for _, fileName := range s.Files {
|
|
|
|
page := NewPage(fileName)
|
|
|
|
page.Site = s.Info
|
|
|
|
page.Tmpl = s.Tmpl
|
2013-07-19 03:10:42 -04:00
|
|
|
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-08-02 16:30:26 -04:00
|
|
|
func (s *Site) setupPrevNext() {
|
|
|
|
for i, _ := range s.Pages {
|
|
|
|
if i < len(s.Pages)-1 {
|
|
|
|
s.Pages[i].Next = s.Pages[i+1]
|
|
|
|
}
|
|
|
|
|
|
|
|
if i > 0 {
|
|
|
|
s.Pages[i].Prev = s.Pages[i-1]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
for i, p := range s.Pages {
|
|
|
|
vals := p.GetParam(plural)
|
|
|
|
|
|
|
|
if vals != nil {
|
|
|
|
for _, idx := range vals.([]string) {
|
|
|
|
s.Indexes[plural].Add(idx, s.Pages[i])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for k, _ := range s.Indexes[plural] {
|
|
|
|
s.Indexes[plural][k].Sort()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, p := range s.Pages {
|
|
|
|
sect := p.GetSection()
|
|
|
|
s.Sections.Add(sect, s.Pages[i])
|
|
|
|
}
|
|
|
|
|
|
|
|
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-10 10:35:34 -04:00
|
|
|
func (s *Site) RenderAliases() error {
|
|
|
|
for i, p := range s.Pages {
|
|
|
|
for _, a := range p.Aliases {
|
|
|
|
content, err := s.RenderThing(s.Pages[i], "alias")
|
|
|
|
if strings.HasSuffix(a, "/") {
|
|
|
|
a = a + "index.html"
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
s.WritePublic(a, content.Bytes())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-08-02 19:48:09 -04:00
|
|
|
func (s *Site) RenderPages() error {
|
2013-07-04 11:32:55 -04:00
|
|
|
for i, _ := range s.Pages {
|
2013-08-02 19:48:09 -04:00
|
|
|
content, err := s.RenderThing(s.Pages[i], s.Pages[i].Layout())
|
|
|
|
if err != nil {
|
2013-08-13 09:23:43 -04:00
|
|
|
var err2 error
|
|
|
|
content, err2 = s.RenderThing(s.Pages[i], "_default/single.html")
|
|
|
|
if err2 != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-08-02 19:48:09 -04:00
|
|
|
}
|
|
|
|
s.Pages[i].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
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Site) WritePages() {
|
|
|
|
for _, p := range s.Pages {
|
2013-07-19 03:10:42 -04:00
|
|
|
s.WritePublic(p.Section+slash+p.OutFile, p.RenderedContent.Bytes())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Site) setOutFile(p *Page) {
|
|
|
|
if len(strings.TrimSpace(p.Slug)) > 0 {
|
|
|
|
// Use Slug if provided
|
2013-08-07 20:21:22 -04:00
|
|
|
if s.Config.UglyUrls {
|
2013-07-19 03:10:42 -04:00
|
|
|
p.OutFile = strings.TrimSpace(p.Slug + "." + p.Extension)
|
|
|
|
} else {
|
2013-08-02 19:48:09 -04:00
|
|
|
p.OutFile = strings.TrimSpace(p.Slug + slash + "index.html")
|
2013-07-19 03:10:42 -04:00
|
|
|
}
|
|
|
|
} else if len(strings.TrimSpace(p.Url)) > 2 {
|
|
|
|
// Use Url if provided & Slug missing
|
|
|
|
p.OutFile = strings.TrimSpace(p.Url)
|
|
|
|
} else {
|
|
|
|
// Fall back to filename
|
|
|
|
_, t := filepath.Split(p.FileName)
|
2013-08-07 20:21:22 -04:00
|
|
|
if s.Config.UglyUrls {
|
2013-07-19 03:10:42 -04:00
|
|
|
p.OutFile = replaceExtension(strings.TrimSpace(t), p.Extension)
|
|
|
|
} else {
|
|
|
|
file, _ := fileExt(strings.TrimSpace(t))
|
2013-08-02 19:48:09 -04:00
|
|
|
p.OutFile = file + slash + "index." + p.Extension
|
2013-07-19 03:10:42 -04:00
|
|
|
}
|
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-08-02 19:48:09 -04:00
|
|
|
url := Urlize(plural + slash + k)
|
2013-07-19 03:10:42 -04:00
|
|
|
plink := url
|
2013-08-07 20:21:22 -04:00
|
|
|
if s.Config.UglyUrls {
|
2013-07-19 03:10:42 -04:00
|
|
|
n.Url = url + ".html"
|
|
|
|
plink = n.Url
|
|
|
|
} else {
|
|
|
|
n.Url = url + "/index.html"
|
|
|
|
}
|
|
|
|
n.Permalink = template.HTML(MakePermalink(string(n.Site.BaseUrl), string(plink)))
|
2013-07-04 11:32:55 -04:00
|
|
|
n.RSSlink = template.HTML(MakePermalink(string(n.Site.BaseUrl), string(url+".xml")))
|
|
|
|
n.Date = o[0].Date
|
|
|
|
n.Data[singular] = o
|
|
|
|
n.Data["Pages"] = o
|
2013-07-19 03:10:42 -04:00
|
|
|
layout := "indexes" + slash + 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-08-07 20:21:22 -04:00
|
|
|
if s.Config.UglyUrls {
|
2013-08-02 19:48:09 -04:00
|
|
|
base = plural + "/" + k
|
2013-07-19 03:10:42 -04:00
|
|
|
} else {
|
2013-08-02 19:48:09 -04:00
|
|
|
base = plural + "/" + k + "/" + "index"
|
2013-07-19 03:10:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
s.WritePublic(base+".html", x.Bytes())
|
2013-07-04 11:32:55 -04:00
|
|
|
|
|
|
|
if a := s.Tmpl.Lookup("rss.xml"); a != nil {
|
|
|
|
// XML Feed
|
|
|
|
y := s.NewXMLBuffer()
|
2013-08-07 20:21:22 -04:00
|
|
|
if s.Config.UglyUrls {
|
2013-07-19 03:10:42 -04:00
|
|
|
n.Url = Urlize(plural + "/" + k + ".xml")
|
|
|
|
} else {
|
2013-08-02 19:48:09 -04:00
|
|
|
n.Url = Urlize(plural + "/" + k + "/" + "index.xml")
|
2013-07-19 03:10:42 -04:00
|
|
|
}
|
|
|
|
n.Permalink = template.HTML(string(n.Site.BaseUrl) + n.Url)
|
2013-07-04 11:32:55 -04:00
|
|
|
s.Tmpl.ExecuteTemplate(y, "rss.xml", n)
|
2013-07-19 03:10:42 -04:00
|
|
|
s.WritePublic(base+".xml", y.Bytes())
|
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-03 03:29:24 -04:00
|
|
|
layout := "indexes" + slash + "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)
|
|
|
|
url := Urlize(plural)
|
|
|
|
n.Url = url + "/index.html"
|
|
|
|
n.Permalink = template.HTML(MakePermalink(string(n.Site.BaseUrl), string(n.Url)))
|
|
|
|
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-03 03:09:28 -04:00
|
|
|
s.WritePublic(plural+slash+"index.html", x.Bytes())
|
2013-08-07 20:21:22 -04:00
|
|
|
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-08-02 19:48:09 -04:00
|
|
|
n.Url = Urlize(section + "/" + "index.html")
|
2013-07-04 11:32:55 -04:00
|
|
|
n.Permalink = template.HTML(MakePermalink(string(n.Site.BaseUrl), string(n.Url)))
|
2013-07-19 03:10:42 -04:00
|
|
|
n.RSSlink = template.HTML(MakePermalink(string(n.Site.BaseUrl), string(section+".xml")))
|
2013-07-04 11:32:55 -04:00
|
|
|
n.Date = data[0].Date
|
|
|
|
n.Data["Pages"] = data
|
2013-08-02 19:48:09 -04:00
|
|
|
layout := "indexes" + slash + section + ".html"
|
2013-07-04 11:32:55 -04:00
|
|
|
|
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
|
|
|
s.WritePublic(section+slash+"index.html", x.Bytes())
|
2013-07-04 11:32:55 -04:00
|
|
|
|
|
|
|
if a := s.Tmpl.Lookup("rss.xml"); a != nil {
|
|
|
|
// XML Feed
|
2013-08-12 22:01:23 -04:00
|
|
|
if s.Config.UglyUrls {
|
2013-08-13 08:43:42 -04:00
|
|
|
n.Url = Urlize(section + ".xml")
|
2013-08-12 22:01:23 -04:00
|
|
|
} else {
|
2013-08-13 08:43:42 -04:00
|
|
|
n.Url = Urlize(section + "/" + "index.xml")
|
2013-08-12 22:01:23 -04:00
|
|
|
}
|
2013-07-19 03:10:42 -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-07-19 03:10:42 -04:00
|
|
|
s.WritePublic(section+slash+"index.xml", y.Bytes())
|
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-07-04 11:32:55 -04:00
|
|
|
n.Url = Urlize(string(n.Site.BaseUrl))
|
2013-07-11 22:55:07 -04:00
|
|
|
n.RSSlink = template.HTML(MakePermalink(string(n.Site.BaseUrl), string("index.xml")))
|
2013-07-04 11:32:55 -04:00
|
|
|
n.Permalink = template.HTML(string(n.Site.BaseUrl))
|
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-07-19 03:10:42 -04:00
|
|
|
s.WritePublic("index.html", x.Bytes())
|
2013-07-04 11:32:55 -04:00
|
|
|
|
|
|
|
if a := s.Tmpl.Lookup("rss.xml"); a != nil {
|
|
|
|
// XML Feed
|
|
|
|
n.Url = Urlize("index.xml")
|
2013-07-19 03:10:42 -04:00
|
|
|
n.Title = "Recent Content"
|
|
|
|
n.Permalink = template.HTML(string(n.Site.BaseUrl) + "index.xml")
|
2013-07-04 11:32:55 -04:00
|
|
|
y := s.NewXMLBuffer()
|
|
|
|
s.Tmpl.ExecuteTemplate(y, "rss.xml", n)
|
2013-07-19 03:10:42 -04:00
|
|
|
s.WritePublic("index.xml", y.Bytes())
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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-07-04 11:32:55 -04:00
|
|
|
fmt.Printf("%d %s created\n", len(s.Indexes[pl]), pl)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Site) NewNode() Node {
|
|
|
|
var y Node
|
|
|
|
y.Data = make(map[string]interface{})
|
|
|
|
y.Site = s.Info
|
|
|
|
|
|
|
|
return y
|
|
|
|
}
|
|
|
|
|
2013-08-02 19:48:09 -04:00
|
|
|
func (s *Site) RenderThing(d interface{}, layout string) (*bytes.Buffer, error) {
|
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
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Site) NewXMLBuffer() *bytes.Buffer {
|
|
|
|
header := "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\" ?>\n"
|
|
|
|
return bytes.NewBufferString(header)
|
|
|
|
}
|
|
|
|
|
2013-07-19 03:10:42 -04:00
|
|
|
func (s *Site) WritePublic(path string, content []byte) {
|
|
|
|
|
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-07-19 03:10:42 -04:00
|
|
|
path, filename := filepath.Split(path)
|
|
|
|
|
2013-08-07 20:21:22 -04:00
|
|
|
path = filepath.FromSlash(s.Config.GetAbsPath(filepath.Join(s.Config.PublishDir, path)))
|
2013-07-26 09:28:26 -04:00
|
|
|
err := mkdirIf(path)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
2013-07-19 03:10:42 -04:00
|
|
|
|
|
|
|
file, _ := os.Create(filepath.Join(path, filename))
|
2013-07-04 11:32:55 -04:00
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
file.Write(content)
|
|
|
|
}
|