// 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 (
"bytes"
"errors"
"fmt"
"html/template"
"io"
"net/url"
"path"
"path/filepath"
"strings"
"time"
"github.com/russross/blackfriday"
"github.com/spf13/cast"
"github.com/spf13/hugo/helpers"
"github.com/spf13/hugo/parser"
jww "github.com/spf13/jwalterweatherman"
"github.com/spf13/viper"
)
type Page struct {
Status string
Images []string
rawContent []byte
Content template.HTML
Summary template.HTML
TableOfContents template.HTML
Truncated bool
plain string // TODO should be []byte
Params map[string]interface{}
contentType string
Draft bool
PublishDate time.Time
Aliases []string
Tmpl Template
Markup string
renderable bool
layout string
linkTitle string
frontmatter []byte
sourceFrontmatter []byte
sourceContent []byte
PageMeta
File
Position
Node
}
type File struct {
Name, FileName, Extension, Dir string
}
type PageMeta struct {
WordCount int
FuzzyWordCount int
ReadingTime int
Weight int
}
type Position struct {
Prev *Page
Next *Page
}
type Pages []*Page
func (p *Page) Plain() string {
if len(p.plain) == 0 {
p.plain = helpers.StripHTML(StripShortcodes(string(p.renderBytes(p.rawContent))))
}
return p.plain
}
func (p *Page) IsNode() bool {
return false
}
func (p *Page) IsPage() bool {
return true
}
func (p *Page) setSummary() {
if bytes.Contains(p.rawContent, summaryDivider) {
// If user defines split:
// Split then render
p.Truncated = true // by definition
header := bytes.Split(p.rawContent, summaryDivider)[0]
p.Summary = bytesToHTML(p.renderBytes(header))
} else {
// If hugo defines split:
// render, strip html, then split
plain := strings.TrimSpace(p.Plain())
p.Summary = bytesToHTML([]byte(TruncateWordsToWholeSentence(plain, summaryLength)))
p.Truncated = len(p.Summary) != len(plain)
}
}
func stripEmptyNav(in []byte) []byte {
return bytes.Replace(in, []byte("<nav>\n</nav>\n\n"), []byte(``), -1)
}
func bytesToHTML(b []byte) template.HTML {
return template.HTML(string(b))
}
func (p *Page) renderBytes(content []byte) []byte {
return renderBytes(content, p.guessMarkupType(), p.File.Name)
}
func (p *Page) renderContent(content []byte) []byte {
return renderBytesWithTOC(content, p.guessMarkupType(), p.File.Name)
}
func