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 (
|
2014-01-29 17:50:31 -05:00
|
|
|
"bytes"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2014-11-20 12:32:21 -05:00
|
|
|
"github.com/spf13/hugo/helpers"
|
|
|
|
"github.com/spf13/hugo/parser"
|
2014-11-28 15:16:57 -05:00
|
|
|
|
|
|
|
"github.com/spf13/cast"
|
|
|
|
"github.com/spf13/hugo/hugofs"
|
2014-10-16 20:20:09 -04:00
|
|
|
"github.com/spf13/hugo/source"
|
2014-11-20 12:32:21 -05:00
|
|
|
"github.com/spf13/hugo/tpl"
|
|
|
|
jww "github.com/spf13/jwalterweatherman"
|
|
|
|
"github.com/spf13/viper"
|
2014-12-07 13:48:00 -05:00
|
|
|
"html/template"
|
|
|
|
"io"
|
|
|
|
"net/url"
|
|
|
|
"path"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
"time"
|
2013-07-04 11:32:55 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type Page struct {
|
2014-10-16 20:20:09 -04:00
|
|
|
Params map[string]interface{}
|
|
|
|
Content template.HTML
|
|
|
|
Summary template.HTML
|
|
|
|
Aliases []string
|
|
|
|
Status string
|
2014-12-09 13:33:55 -05:00
|
|
|
Images []Image
|
|
|
|
Videos []Video
|
2014-10-16 20:20:09 -04:00
|
|
|
TableOfContents template.HTML
|
|
|
|
Truncated bool
|
|
|
|
Draft bool
|
|
|
|
PublishDate time.Time
|
2014-11-20 12:32:21 -05:00
|
|
|
Tmpl tpl.Template
|
2014-10-16 20:20:09 -04:00
|
|
|
Markup string
|
|
|
|
|
Shortcode rewrite, take 2
This commit contains a restructuring and partial rewrite of the shortcode handling.
Prior to this commit rendering of the page content was mingled with handling of the shortcodes. This led to several oddities.
The new flow is:
1. Shortcodes are extracted from page and replaced with placeholders.
2. Shortcodes are processed and rendered
3. Page is processed
4. The placeholders are replaced with the rendered shortcodes
The handling of summaries is also made simpler by this.
This commit also introduces some other chenges:
1. distinction between shortcodes that need further processing and those who do not:
* `{{< >}}`: Typically raw HTML. Will not be processed.
* `{{% %}}`: Will be processed by the page's markup engine (Markdown or (infuture) Asciidoctor)
The above also involves a new shortcode-parser, with lexical scanning inspired by Rob Pike's talk called "Lexical Scanning in Go",
which should be easier to understand, give better error messages and perform better.
2. If you want to exclude a shortcode from being processed (for documentation etc.), the inner part of the shorcode must be commented out, i.e. `{{%/* movie 47238zzb */%}}`. See the updated shortcode section in the documentation for further examples.
The new parser supports nested shortcodes. This isn't new, but has two related design choices worth mentioning:
* The shortcodes will be rendered individually, so If both `{{< >}}` and `{{% %}}` are used in the nested hierarchy, one will be passed through the page's markdown processor, the other not.
* To avoid potential costly overhead of always looking far ahead for a possible closing tag, this implementation looks at the template itself, and is branded as a container with inner content if it contains a reference to `.Inner`
Fixes #565
Fixes #480
Fixes #461
And probably some others.
2014-10-27 16:48:30 -04:00
|
|
|
extension string
|
|
|
|
contentType string
|
|
|
|
renderable bool
|
|
|
|
layout string
|
|
|
|
linkTitle string
|
|
|
|
frontmatter []byte
|
|
|
|
rawContent []byte
|
|
|
|
contentShortCodes map[string]string
|
|
|
|
plain string // TODO should be []byte
|
2014-01-29 17:50:31 -05:00
|
|
|
PageMeta
|
2014-10-16 20:20:09 -04:00
|
|
|
Source
|
2014-01-29 17:50:31 -05:00
|
|
|
Position
|
|
|
|
Node
|
2013-07-04 11:32:55 -04:00
|
|
|
}
|
|
|
|
|
2014-10-16 20:20:09 -04:00
|
|
|
type Source struct {
|
|
|
|
Frontmatter []byte
|
|
|
|
Content []byte
|
|
|
|
source.File
|
2013-07-04 11:32:55 -04:00
|
|
|
}
|
|
|
|
type PageMeta struct {
|
2014-01-29 17:50:31 -05:00
|
|
|
WordCount int
|
|
|
|
FuzzyWordCount int
|
|
|
|
ReadingTime int
|
|
|
|
Weight int
|
2013-07-04 11:32:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
type Position struct {
|
2014-01-29 17:50:31 -05:00
|
|
|
Prev *Page
|
|
|
|
Next *Page
|
2014-04-18 03:23:13 -04:00
|
|
|
PrevInSection *Page
|
|
|
|
NextInSection *Page
|
2013-07-04 11:32:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
type Pages []*Page
|
|
|
|
|
2014-01-27 17:16:28 -05:00
|
|
|
func (p *Page) Plain() string {
|
2014-01-29 17:50:31 -05:00
|
|
|
if len(p.plain) == 0 {
|
Shortcode rewrite, take 2
This commit contains a restructuring and partial rewrite of the shortcode handling.
Prior to this commit rendering of the page content was mingled with handling of the shortcodes. This led to several oddities.
The new flow is:
1. Shortcodes are extracted from page and replaced with placeholders.
2. Shortcodes are processed and rendered
3. Page is processed
4. The placeholders are replaced with the rendered shortcodes
The handling of summaries is also made simpler by this.
This commit also introduces some other chenges:
1. distinction between shortcodes that need further processing and those who do not:
* `{{< >}}`: Typically raw HTML. Will not be processed.
* `{{% %}}`: Will be processed by the page's markup engine (Markdown or (infuture) Asciidoctor)
The above also involves a new shortcode-parser, with lexical scanning inspired by Rob Pike's talk called "Lexical Scanning in Go",
which should be easier to understand, give better error messages and perform better.
2. If you want to exclude a shortcode from being processed (for documentation etc.), the inner part of the shorcode must be commented out, i.e. `{{%/* movie 47238zzb */%}}`. See the updated shortcode section in the documentation for further examples.
The new parser supports nested shortcodes. This isn't new, but has two related design choices worth mentioning:
* The shortcodes will be rendered individually, so If both `{{< >}}` and `{{% %}}` are used in the nested hierarchy, one will be passed through the page's markdown processor, the other not.
* To avoid potential costly overhead of always looking far ahead for a possible closing tag, this implementation looks at the template itself, and is branded as a container with inner content if it contains a reference to `.Inner`
Fixes #565
Fixes #480
Fixes #461
And probably some others.
2014-10-27 16:48:30 -04:00
|
|
|
p.plain = helpers.StripHTML(string(p.Content))
|
2014-01-29 17:50:31 -05:00
|
|
|
}
|
|
|
|
return p.plain
|
2013-10-15 09:15:52 -04:00
|
|
|
}
|
|
|
|
|
2014-08-19 21:27:13 -04:00
|
|
|
func (p *Page) IsNode() bool {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Page) IsPage() bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2014-12-09 13:33:55 -05:00
|
|
|
func (p *Page) Author() Author {
|
|
|
|
authors := p.Authors()
|
|
|
|
|
|
|
|
for _, author := range authors {
|
|
|
|
return author
|
|
|
|
}
|
|
|
|
return Author{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Page) Authors() AuthorList {
|
|
|
|
authorKeys, ok := p.Params["authors"]
|
|
|
|
authors := authorKeys.([]string)
|
|
|
|
if !ok || len(authors) < 1 || len(p.Site.Authors) < 1 {
|
|
|
|
return AuthorList{}
|
|
|
|
}
|
|
|
|
|
|
|
|
al := make(AuthorList)
|
|
|
|
for _, author := range authors {
|
|
|
|
a, ok := p.Site.Authors[author]
|
|
|
|
if ok {
|
|
|
|
al[author] = a
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return al
|
|
|
|
}
|
|
|
|
|
2014-10-01 14:26:43 -04:00
|
|
|
func (p *Page) UniqueId() string {
|
2014-10-16 20:20:09 -04:00
|
|
|
return p.Source.UniqueId()
|
2014-10-01 14:26:43 -04:00
|
|
|
}
|
|
|
|
|
Provide (relative) reference funcs & shortcodes.
- `.Ref` and `.RelRef` take a reference (the logical filename for a
page, including extension and/or a document fragment ID) and return
a permalink (or relative permalink) to the referenced document.
- If the reference is a page name (such as `about.md`), the page
will be discovered and the permalink will be returned: `/about/`
- If the reference is a page name with a fragment (such as
`about.md#who`), the page will be discovered and used to add the
`page.UniqueID()` to the resulting fragment and permalink:
`/about/#who:deadbeef`.
- If the reference is a fragment and `.*Ref` has been called from
a `Node` or `SiteInfo`, it will be returned as is: `#who`.
- If the reference is a fragment and `.*Ref` has been called from
a `Page`, it will be returned with the page’s unique ID:
`#who:deadbeef`.
- `.*Ref` can be called from either `Node`, `SiteInfo` (e.g.,
`Node.Site`), `Page` objects, or `ShortcodeWithPage` objects in
templates.
- `.*Ref` cannot be used in content, so two shortcodes have been
created to provide the functionality to content: `ref` and `relref`.
These are intended to be used within markup, like `[Who]({{% ref
about.md#who %}})` or `<a href="{{% ref about.md#who %}}">Who</a>`.
- There are also `ref` and `relref` template functions (used to create
the shortcodes) that expect a `Page` or `Node` object and the
reference string (e.g., `{{ relref . "about.md" }}` or `{{
"about.md" | ref . }}`). It actually looks for `.*Ref` as defined on
`Node` or `Page` objects.
- Shortcode handling had to use a *differently unique* wrapper in
`createShortcodePlaceholder` because of the way that the `ref` and
`relref` are intended to be used in content.
2014-11-24 01:15:34 -05:00
|
|
|
func (p *Page) Ref(ref string) (string, error) {
|
|
|
|
return p.Node.Site.Ref(ref, p)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Page) RelRef(ref string) (string, error) {
|
|
|
|
return p.Node.Site.RelRef(ref, p)
|
|
|
|
}
|
|
|
|
|
Shortcode rewrite, take 2
This commit contains a restructuring and partial rewrite of the shortcode handling.
Prior to this commit rendering of the page content was mingled with handling of the shortcodes. This led to several oddities.
The new flow is:
1. Shortcodes are extracted from page and replaced with placeholders.
2. Shortcodes are processed and rendered
3. Page is processed
4. The placeholders are replaced with the rendered shortcodes
The handling of summaries is also made simpler by this.
This commit also introduces some other chenges:
1. distinction between shortcodes that need further processing and those who do not:
* `{{< >}}`: Typically raw HTML. Will not be processed.
* `{{% %}}`: Will be processed by the page's markup engine (Markdown or (infuture) Asciidoctor)
The above also involves a new shortcode-parser, with lexical scanning inspired by Rob Pike's talk called "Lexical Scanning in Go",
which should be easier to understand, give better error messages and perform better.
2. If you want to exclude a shortcode from being processed (for documentation etc.), the inner part of the shorcode must be commented out, i.e. `{{%/* movie 47238zzb */%}}`. See the updated shortcode section in the documentation for further examples.
The new parser supports nested shortcodes. This isn't new, but has two related design choices worth mentioning:
* The shortcodes will be rendered individually, so If both `{{< >}}` and `{{% %}}` are used in the nested hierarchy, one will be passed through the page's markdown processor, the other not.
* To avoid potential costly overhead of always looking far ahead for a possible closing tag, this implementation looks at the template itself, and is branded as a container with inner content if it contains a reference to `.Inner`
Fixes #565
Fixes #480
Fixes #461
And probably some others.
2014-10-27 16:48:30 -04:00
|
|
|
// for logging
|
|
|
|
func (p *Page) lineNumRawContentStart() int {
|
|
|
|
return bytes.Count(p.frontmatter, []byte("\n")) + 1
|
|
|
|
}
|
|
|
|
|
2014-01-27 17:16:28 -05:00
|
|
|
func (p *Page) setSummary() {
|
Shortcode rewrite, take 2
This commit contains a restructuring and partial rewrite of the shortcode handling.
Prior to this commit rendering of the page content was mingled with handling of the shortcodes. This led to several oddities.
The new flow is:
1. Shortcodes are extracted from page and replaced with placeholders.
2. Shortcodes are processed and rendered
3. Page is processed
4. The placeholders are replaced with the rendered shortcodes
The handling of summaries is also made simpler by this.
This commit also introduces some other chenges:
1. distinction between shortcodes that need further processing and those who do not:
* `{{< >}}`: Typically raw HTML. Will not be processed.
* `{{% %}}`: Will be processed by the page's markup engine (Markdown or (infuture) Asciidoctor)
The above also involves a new shortcode-parser, with lexical scanning inspired by Rob Pike's talk called "Lexical Scanning in Go",
which should be easier to understand, give better error messages and perform better.
2. If you want to exclude a shortcode from being processed (for documentation etc.), the inner part of the shorcode must be commented out, i.e. `{{%/* movie 47238zzb */%}}`. See the updated shortcode section in the documentation for further examples.
The new parser supports nested shortcodes. This isn't new, but has two related design choices worth mentioning:
* The shortcodes will be rendered individually, so If both `{{< >}}` and `{{% %}}` are used in the nested hierarchy, one will be passed through the page's markdown processor, the other not.
* To avoid potential costly overhead of always looking far ahead for a possible closing tag, this implementation looks at the template itself, and is branded as a container with inner content if it contains a reference to `.Inner`
Fixes #565
Fixes #480
Fixes #461
And probably some others.
2014-10-27 16:48:30 -04:00
|
|
|
|
|
|
|
// at this point, p.rawContent contains placeholders for the short codes,
|
|
|
|
// rendered and ready in p.contentShortcodes
|
|
|
|
|
2014-10-16 20:20:09 -04:00
|
|
|
if bytes.Contains(p.rawContent, helpers.SummaryDivider) {
|
2014-01-29 17:50:31 -05:00
|
|
|
// If user defines split:
|
Shortcode rewrite, take 2
This commit contains a restructuring and partial rewrite of the shortcode handling.
Prior to this commit rendering of the page content was mingled with handling of the shortcodes. This led to several oddities.
The new flow is:
1. Shortcodes are extracted from page and replaced with placeholders.
2. Shortcodes are processed and rendered
3. Page is processed
4. The placeholders are replaced with the rendered shortcodes
The handling of summaries is also made simpler by this.
This commit also introduces some other chenges:
1. distinction between shortcodes that need further processing and those who do not:
* `{{< >}}`: Typically raw HTML. Will not be processed.
* `{{% %}}`: Will be processed by the page's markup engine (Markdown or (infuture) Asciidoctor)
The above also involves a new shortcode-parser, with lexical scanning inspired by Rob Pike's talk called "Lexical Scanning in Go",
which should be easier to understand, give better error messages and perform better.
2. If you want to exclude a shortcode from being processed (for documentation etc.), the inner part of the shorcode must be commented out, i.e. `{{%/* movie 47238zzb */%}}`. See the updated shortcode section in the documentation for further examples.
The new parser supports nested shortcodes. This isn't new, but has two related design choices worth mentioning:
* The shortcodes will be rendered individually, so If both `{{< >}}` and `{{% %}}` are used in the nested hierarchy, one will be passed through the page's markdown processor, the other not.
* To avoid potential costly overhead of always looking far ahead for a possible closing tag, this implementation looks at the template itself, and is branded as a container with inner content if it contains a reference to `.Inner`
Fixes #565
Fixes #480
Fixes #461
And probably some others.
2014-10-27 16:48:30 -04:00
|
|
|
// Split, replace shortcode tokens, then render
|
2014-01-29 17:50:31 -05:00
|
|
|
p.Truncated = true // by definition
|
2014-10-16 20:20:09 -04:00
|
|
|
header := bytes.Split(p.rawContent, helpers.SummaryDivider)[0]
|
Shortcode rewrite, take 2
This commit contains a restructuring and partial rewrite of the shortcode handling.
Prior to this commit rendering of the page content was mingled with handling of the shortcodes. This led to several oddities.
The new flow is:
1. Shortcodes are extracted from page and replaced with placeholders.
2. Shortcodes are processed and rendered
3. Page is processed
4. The placeholders are replaced with the rendered shortcodes
The handling of summaries is also made simpler by this.
This commit also introduces some other chenges:
1. distinction between shortcodes that need further processing and those who do not:
* `{{< >}}`: Typically raw HTML. Will not be processed.
* `{{% %}}`: Will be processed by the page's markup engine (Markdown or (infuture) Asciidoctor)
The above also involves a new shortcode-parser, with lexical scanning inspired by Rob Pike's talk called "Lexical Scanning in Go",
which should be easier to understand, give better error messages and perform better.
2. If you want to exclude a shortcode from being processed (for documentation etc.), the inner part of the shorcode must be commented out, i.e. `{{%/* movie 47238zzb */%}}`. See the updated shortcode section in the documentation for further examples.
The new parser supports nested shortcodes. This isn't new, but has two related design choices worth mentioning:
* The shortcodes will be rendered individually, so If both `{{< >}}` and `{{% %}}` are used in the nested hierarchy, one will be passed through the page's markdown processor, the other not.
* To avoid potential costly overhead of always looking far ahead for a possible closing tag, this implementation looks at the template itself, and is branded as a container with inner content if it contains a reference to `.Inner`
Fixes #565
Fixes #480
Fixes #461
And probably some others.
2014-10-27 16:48:30 -04:00
|
|
|
renderedHeader := p.renderBytes(header)
|
|
|
|
numShortcodesInHeader := bytes.Count(header, []byte(shortcodePlaceholderPrefix))
|
|
|
|
if len(p.contentShortCodes) > 0 {
|
|
|
|
tmpContentWithTokensReplaced, err :=
|
|
|
|
replaceShortcodeTokens(renderedHeader, shortcodePlaceholderPrefix, numShortcodesInHeader, true, p.contentShortCodes)
|
|
|
|
if err != nil {
|
|
|
|
jww.FATAL.Printf("Failed to replace short code tokens in Summary for %s:\n%s", p.BaseFileName(), err.Error())
|
|
|
|
} else {
|
|
|
|
renderedHeader = tmpContentWithTokensReplaced
|
|
|
|
}
|
|
|
|
}
|
|
|
|
p.Summary = helpers.BytesToHTML(renderedHeader)
|
2014-01-29 17:50:31 -05:00
|
|
|
} else {
|
|
|
|
// If hugo defines split:
|
|
|
|
// render, strip html, then split
|
|
|
|
plain := strings.TrimSpace(p.Plain())
|
2014-10-16 20:20:09 -04:00
|
|
|
p.Summary = helpers.BytesToHTML([]byte(helpers.TruncateWordsToWholeSentence(plain, helpers.SummaryLength)))
|
2014-01-29 17:50:31 -05:00
|
|
|
p.Truncated = len(p.Summary) != len(plain)
|
|
|
|
}
|
2014-01-27 17:16:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Page) renderBytes(content []byte) []byte {
|
2014-11-28 15:16:57 -05:00
|
|
|
return helpers.RenderBytes(
|
|
|
|
helpers.RenderingContext{Content: content, PageFmt: p.guessMarkupType(),
|
|
|
|
DocumentId: p.UniqueId(), ConfigFlags: p.getRenderingConfigFlags()})
|
2014-01-27 17:16:28 -05:00
|
|
|
}
|
|
|
|
|
2014-01-28 23:11:05 -05:00
|
|
|
func (p *Page) renderContent(content []byte) []byte {
|
2014-11-28 15:16:57 -05:00
|
|
|
return helpers.RenderBytesWithTOC(helpers.RenderingContext{Content: content, PageFmt: p.guessMarkupType(),
|
|
|
|
DocumentId: p.UniqueId(), ConfigFlags: p.getRenderingConfigFlags()})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Page) getRenderingConfigFlags() map[string]bool {
|
|
|
|
flags := make(map[string]bool)
|
|
|
|
|
|
|
|
pageParam := p.GetParam("blackfriday")
|
|
|
|
siteParam := viper.GetStringMap("blackfriday")
|
|
|
|
|
|
|
|
flags = cast.ToStringMapBool(siteParam)
|
|
|
|
|
|
|
|
if pageParam != nil {
|
|
|
|
pageFlags := cast.ToStringMapBool(pageParam)
|
|
|
|
for key, value := range pageFlags {
|
|
|
|
flags[key] = value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return flags
|
2013-09-03 18:38:20 -04:00
|
|
|
}
|
|
|
|
|
2013-09-04 19:57:17 -04:00
|
|
|
func newPage(filename string) *Page {
|
2014-01-29 17:50:31 -05:00
|
|
|
page := Page{contentType: "",
|
2014-10-16 20:20:09 -04:00
|
|
|
Source: Source{File: *source.NewFile(filename)},
|
2014-05-06 11:02:56 -04:00
|
|
|
Node: Node{Keywords: []string{}, Sitemap: Sitemap{Priority: -1}},
|
2014-01-29 17:50:31 -05:00
|
|
|
Params: make(map[string]interface{})}
|
2014-03-31 13:23:34 -04:00
|
|
|
|
2014-10-16 20:20:09 -04:00
|
|
|
jww.DEBUG.Println("Reading from", page.File.Path())
|
2014-01-29 17:50:31 -05:00
|
|
|
return &page
|
2013-08-13 19:39:24 -04:00
|
|
|
}
|
2013-07-04 11:32:55 -04:00
|
|
|
|
2013-09-18 13:17:43 -04:00
|
|
|
func (p *Page) IsRenderable() bool {
|
2014-01-29 17:50:31 -05:00
|
|
|
return p.renderable
|
2013-09-18 13:17:43 -04:00
|
|
|
}
|
|
|
|
|
2013-07-04 11:32:55 -04:00
|
|
|
func (page *Page) Type() string {
|
2014-01-29 17:50:31 -05:00
|
|
|
if page.contentType != "" {
|
|
|
|
return page.contentType
|
|
|
|
}
|
2014-10-16 20:20:09 -04:00
|
|
|
|
|
|
|
if x := page.Section(); x != "" {
|
2014-01-29 17:50:31 -05:00
|
|
|
return x
|
|
|
|
}
|
2013-07-04 11:32:55 -04:00
|
|
|
|
2014-01-29 17:50:31 -05:00
|
|
|
return "page"
|
2013-07-04 11:32:55 -04:00
|
|
|
}
|
|
|
|
|
2014-10-16 20:20:09 -04:00
|
|
|
func (page *Page) Section() string {
|
|
|
|
return page.Source.Section()
|
|
|
|
}
|
|
|
|
|
2013-10-07 00:57:45 -04:00
|
|
|
func (page *Page) Layout(l ...string) []string {
|
2014-01-29 17:50:31 -05:00
|
|
|
if page.layout != "" {
|
|
|
|
return layouts(page.Type(), page.layout)
|
|
|
|
}
|
2013-10-07 00:57:45 -04:00
|
|
|
|
2014-01-29 17:50:31 -05:00
|
|
|
layout := ""
|
|
|
|
if len(l) == 0 {
|
|
|
|
layout = "single"
|
|
|
|
} else {
|
|
|
|
layout = l[0]
|
|
|
|
}
|
2013-07-04 11:32:55 -04:00
|
|
|
|
2014-01-29 17:50:31 -05:00
|
|
|
return layouts(page.Type(), layout)
|
2013-10-07 00:57:45 -04:00
|
|
|
}
|
2013-07-04 11:32:55 -04:00
|
|
|
|
2013-10-07 00:57:45 -04:00
|
|
|
func layouts(types string, layout string) (layouts []string) {
|
2014-01-29 17:50:31 -05:00
|
|
|
t := strings.Split(types, "/")
|
2014-06-03 17:06:32 -04:00
|
|
|
|
|
|
|
// Add type/layout.html
|
2014-01-29 17:50:31 -05:00
|
|
|
for i := range t {
|
|
|
|
search := t[:len(t)-i]
|
2014-12-07 13:48:00 -05:00
|
|
|
layouts = append(layouts, fmt.Sprintf("%s/%s.html", strings.ToLower(path.Join(search...)), layout))
|
2014-01-29 17:50:31 -05:00
|
|
|
}
|
2014-06-03 17:06:32 -04:00
|
|
|
|
|
|
|
// Add _default/layout.html
|
|
|
|
layouts = append(layouts, fmt.Sprintf("_default/%s.html", layout))
|
|
|
|
|
|
|
|
// Add theme/type/layout.html & theme/_default/layout.html
|
|
|
|
for _, l := range layouts {
|
|
|
|
layouts = append(layouts, "theme/"+l)
|
|
|
|
}
|
|
|
|
|
2014-01-29 17:50:31 -05:00
|
|
|
return
|
2013-07-04 11:32:55 -04:00
|
|
|
}
|
|
|
|
|
2014-05-01 14:11:56 -04:00
|
|
|
func NewPageFrom(buf io.Reader, name string) (page *Page, err error) {
|
|
|
|
p, err := NewPage(name)
|
|
|
|
if err != nil {
|
|
|
|
return p, err
|
|
|
|
}
|
|
|
|
err = p.ReadFrom(buf)
|
|
|
|
|
|
|
|
return p, err
|
|
|
|
}
|
|
|
|
|
2014-05-01 13:20:58 -04:00
|
|
|
func NewPage(name string) (page *Page, err error) {
|
2014-01-29 17:50:31 -05:00
|
|
|
if len(name) == 0 {
|
|
|
|
return nil, errors.New("Zero length page name")
|
|
|
|
}
|
2013-08-05 10:53:58 -04:00
|
|
|
|
2014-01-29 17:50:31 -05:00
|
|
|
// Create new page
|
|
|
|
p := newPage(name)
|
2013-08-05 10:53:58 -04:00
|
|
|
|
2014-05-01 13:20:58 -04:00
|
|
|
return p, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Page) ReadFrom(buf io.Reader) (err error) {
|
2014-01-29 17:50:31 -05:00
|
|
|
// Parse for metadata & body
|
|
|
|
if err = p.parse(buf); err != nil {
|
2014-03-31 13:23:34 -04:00
|
|
|
jww.ERROR.Print(err)
|
2014-01-29 17:50:31 -05:00
|
|
|
return
|
|
|
|
}
|
2013-08-05 10:53:58 -04:00
|
|
|
|
2014-05-01 13:20:58 -04:00
|
|
|
return nil
|
2013-07-04 11:32:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Page) analyzePage() {
|
2014-10-16 20:20:09 -04:00
|
|
|
p.WordCount = helpers.TotalWords(p.Plain())
|
2014-01-29 17:50:31 -05:00
|
|
|
p.FuzzyWordCount = int((p.WordCount+100)/100) * 100
|
|
|
|
p.ReadingTime = int((p.WordCount + 212) / 213)
|
2013-07-04 11:32:55 -04:00
|
|
|
}
|
|
|
|
|
2013-10-02 19:33:51 -04:00
|
|
|
func (p *Page) permalink() (*url.URL, error) {
|
2014-01-29 17:50:31 -05:00
|
|
|
baseUrl := string(p.Site.BaseUrl)
|
2014-12-07 13:48:00 -05:00
|
|
|
dir := strings.TrimSpace(filepath.ToSlash(p.Source.Dir()))
|
2014-01-29 17:50:31 -05:00
|
|
|
pSlug := strings.TrimSpace(p.Slug)
|
|
|
|
pUrl := strings.TrimSpace(p.Url)
|
|
|
|
var permalink string
|
|
|
|
var err error
|
|
|
|
|
2014-02-28 02:30:12 -05:00
|
|
|
if len(pUrl) > 0 {
|
|
|
|
return helpers.MakePermalink(baseUrl, pUrl), nil
|
|
|
|
}
|
|
|
|
|
2014-10-16 20:20:09 -04:00
|
|
|
if override, ok := p.Site.Permalinks[p.Section()]; ok {
|
2014-01-29 17:50:31 -05:00
|
|
|
permalink, err = override.Expand(p)
|
2014-04-23 02:55:43 -04:00
|
|
|
|
2014-01-29 17:50:31 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-02-28 02:30:12 -05:00
|
|
|
// fmt.Printf("have a section override for %q in section %s → %s\n", p.Title, p.Section, permalink)
|
2014-01-29 17:50:31 -05:00
|
|
|
} else {
|
|
|
|
if len(pSlug) > 0 {
|
2014-12-07 13:48:00 -05:00
|
|
|
permalink = helpers.UrlPrep(viper.GetBool("UglyUrls"), path.Join(dir, p.Slug+"."+p.Extension()))
|
2014-01-29 17:50:31 -05:00
|
|
|
} else {
|
2014-11-06 11:52:01 -05:00
|
|
|
_, t := filepath.Split(p.Source.LogicalName())
|
2014-12-07 13:48:00 -05:00
|
|
|
permalink = helpers.UrlPrep(viper.GetBool("UglyUrls"), path.Join(dir, helpers.ReplaceExtension(strings.TrimSpace(t), p.Extension())))
|
2014-01-29 17:50:31 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-27 18:32:09 -05:00
|
|
|
return helpers.MakePermalink(baseUrl, permalink), nil
|
2013-10-02 19:33:51 -04:00
|
|
|
}
|
|
|
|
|
2014-10-16 20:20:09 -04:00
|
|
|
func (p *Page) Extension() string {
|
|
|
|
if p.extension != "" {
|
|
|
|
return p.extension
|
|
|
|
} else {
|
|
|
|
return viper.GetString("DefaultExtension")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-25 18:37:53 -04:00
|
|
|
func (p *Page) LinkTitle() string {
|
2014-01-29 17:50:31 -05:00
|
|
|
if len(p.linkTitle) > 0 {
|
|
|
|
return p.linkTitle
|
|
|
|
} else {
|
|
|
|
return p.Title
|
|
|
|
}
|
2013-10-25 18:37:53 -04:00
|
|
|
}
|
|
|
|
|
2014-05-29 00:48:40 -04:00
|
|
|
func (page *Page) ShouldBuild() bool {
|
|
|
|
if viper.GetBool("BuildFuture") || page.PublishDate.IsZero() || page.PublishDate.Before(time.Now()) {
|
|
|
|
if viper.GetBool("BuildDrafts") || !page.Draft {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2014-08-20 11:09:35 -04:00
|
|
|
func (page *Page) IsDraft() bool {
|
|
|
|
return page.Draft
|
|
|
|
}
|
|
|
|
|
|
|
|
func (page *Page) IsFuture() bool {
|
|
|
|
if page.PublishDate.Before(time.Now()) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2013-10-02 19:33:51 -04:00
|
|
|
func (p *Page) Permalink() (string, error) {
|
2014-01-29 17:50:31 -05:00
|
|
|
link, err := p.permalink()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return link.String(), nil
|
2013-07-04 11:32:55 -04:00
|
|
|
}
|
|
|
|
|
2013-10-02 20:00:21 -04:00
|
|
|
func (p *Page) RelPermalink() (string, error) {
|
2014-01-29 17:50:31 -05:00
|
|
|
link, err := p.permalink()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2013-10-02 20:00:21 -04:00
|
|
|
|
2014-01-29 17:50:31 -05:00
|
|
|
link.Scheme = ""
|
|
|
|
link.Host = ""
|
|
|
|
link.User = nil
|
|
|
|
link.Opaque = ""
|
|
|
|
return link.String(), nil
|
2013-10-02 20:00:21 -04:00
|
|
|
}
|
|
|
|
|
2013-08-05 10:53:58 -04:00
|
|
|
func (page *Page) update(f interface{}) error {
|
2014-05-01 14:11:56 -04:00
|
|
|
if f == nil {
|
|
|
|
return fmt.Errorf("no metadata found")
|
|
|
|
}
|
2014-01-29 17:50:31 -05:00
|
|
|
m := f.(map[string]interface{})
|
2015-01-05 06:44:41 -05:00
|
|
|
var err error
|
2014-01-29 17:50:31 -05:00
|
|
|
for k, v := range m {
|
|
|
|
loki := strings.ToLower(k)
|
|
|
|
switch loki {
|
|
|
|
case "title":
|
2014-04-05 01:26:43 -04:00
|
|
|
page.Title = cast.ToString(v)
|
2014-01-29 17:50:31 -05:00
|
|
|
case "linktitle":
|
2014-04-05 01:26:43 -04:00
|
|
|
page.linkTitle = cast.ToString(v)
|
2014-01-29 17:50:31 -05:00
|
|
|
case "description":
|
2014-04-05 01:26:43 -04:00
|
|
|
page.Description = cast.ToString(v)
|
2014-01-29 17:50:31 -05:00
|
|
|
case "slug":
|
2014-04-05 01:26:43 -04:00
|
|
|
page.Slug = helpers.Urlize(cast.ToString(v))
|
2014-01-29 17:50:31 -05:00
|
|
|
case "url":
|
2014-04-05 01:26:43 -04:00
|
|
|
if url := cast.ToString(v); strings.HasPrefix(url, "http://") || strings.HasPrefix(url, "https://") {
|
2014-01-29 17:50:31 -05:00
|
|
|
return fmt.Errorf("Only relative urls are supported, %v provided", url)
|
|
|
|
}
|
2014-04-05 01:26:43 -04:00
|
|
|
page.Url = helpers.Urlize(cast.ToString(v))
|
2014-01-29 17:50:31 -05:00
|
|
|
case "type":
|
2014-04-05 01:26:43 -04:00
|
|
|
page.contentType = cast.ToString(v)
|
2014-10-16 20:20:09 -04:00
|
|
|
case "extension", "ext":
|
|
|
|
page.extension = cast.ToString(v)
|
2014-01-29 17:50:31 -05:00
|
|
|
case "keywords":
|
2014-04-05 01:26:43 -04:00
|
|
|
page.Keywords = cast.ToStringSlice(v)
|
2014-05-29 00:48:40 -04:00
|
|
|
case "date":
|
2015-01-05 06:44:41 -05:00
|
|
|
page.Date, err = cast.ToTimeE(v)
|
|
|
|
if err != nil {
|
|
|
|
jww.ERROR.Printf("Failed to parse date '%v' in page %s", v, page.File.Path())
|
|
|
|
}
|
2014-05-29 00:48:40 -04:00
|
|
|
case "publishdate", "pubdate":
|
2015-01-05 06:44:41 -05:00
|
|
|
page.PublishDate, err = cast.ToTimeE(v)
|
|
|
|
if err != nil {
|
|
|
|
jww.ERROR.Printf("Failed to parse publishdate '%v' in page %s", v, page.File.Path())
|
|
|
|
}
|
2014-01-29 17:50:31 -05:00
|
|
|
case "draft":
|
2014-04-05 01:26:43 -04:00
|
|
|
page.Draft = cast.ToBool(v)
|
2014-01-29 17:50:31 -05:00
|
|
|
case "layout":
|
2014-04-05 01:26:43 -04:00
|
|
|
page.layout = cast.ToString(v)
|
2014-01-29 17:50:31 -05:00
|
|
|
case "markup":
|
2014-04-05 01:26:43 -04:00
|
|
|
page.Markup = cast.ToString(v)
|
2014-01-29 17:50:31 -05:00
|
|
|
case "weight":
|
2014-04-05 01:26:43 -04:00
|
|
|
page.Weight = cast.ToInt(v)
|
2014-01-29 17:50:31 -05:00
|
|
|
case "aliases":
|
2014-04-05 01:26:43 -04:00
|
|
|
page.Aliases = cast.ToStringSlice(v)
|
2014-01-29 17:50:31 -05:00
|
|
|
for _, alias := range page.Aliases {
|
|
|
|
if strings.HasPrefix(alias, "http://") || strings.HasPrefix(alias, "https://") {
|
|
|
|
return fmt.Errorf("Only relative aliases are supported, %v provided", alias)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case "status":
|
2014-04-05 01:26:43 -04:00
|
|
|
page.Status = cast.ToString(v)
|
2014-05-06 11:02:56 -04:00
|
|
|
case "sitemap":
|
|
|
|
page.Sitemap = parseSitemap(cast.ToStringMap(v))
|
2014-01-29 17:50:31 -05:00
|
|
|
default:
|
|
|
|
// If not one of the explicit values, store in Params
|
|
|
|
switch vv := v.(type) {
|
|
|
|
case bool:
|
|
|
|
page.Params[loki] = vv
|
|
|
|
case string:
|
|
|
|
page.Params[loki] = vv
|
|
|
|
case int64, int32, int16, int8, int:
|
|
|
|
page.Params[loki] = vv
|
|
|
|
case float64, float32:
|
|
|
|
page.Params[loki] = vv
|
|
|
|
case time.Time:
|
|
|
|
page.Params[loki] = vv
|
|
|
|
default: // handle array of strings as well
|
|
|
|
switch vvv := vv.(type) {
|
|
|
|
case []interface{}:
|
|
|
|
var a = make([]string, len(vvv))
|
|
|
|
for i, u := range vvv {
|
2014-04-05 01:26:43 -04:00
|
|
|
a[i] = cast.ToString(u)
|
2014-01-29 17:50:31 -05:00
|
|
|
}
|
|
|
|
page.Params[loki] = a
|
2014-04-23 02:55:43 -04:00
|
|
|
default:
|
|
|
|
page.Params[loki] = vv
|
2014-01-29 17:50:31 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
2013-07-07 00:49:57 -04:00
|
|
|
|
2013-07-04 11:32:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (page *Page) GetParam(key string) interface{} {
|
2014-01-29 17:50:31 -05:00
|
|
|
v := page.Params[strings.ToLower(key)]
|
|
|
|
|
|
|
|
if v == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
switch v.(type) {
|
|
|
|
case bool:
|
2014-04-05 01:26:43 -04:00
|
|
|
return cast.ToBool(v)
|
2014-01-29 17:50:31 -05:00
|
|
|
case string:
|
2014-09-09 16:58:02 -04:00
|
|
|
return strings.ToLower(cast.ToString(v))
|
2014-01-29 17:50:31 -05:00
|
|
|
case int64, int32, int16, int8, int:
|
2014-04-05 01:26:43 -04:00
|
|
|
return cast.ToInt(v)
|
2014-01-29 17:50:31 -05:00
|
|
|
case float64, float32:
|
2014-04-05 01:26:43 -04:00
|
|
|
return cast.ToFloat64(v)
|
2014-01-29 17:50:31 -05:00
|
|
|
case time.Time:
|
2014-04-05 01:26:43 -04:00
|
|
|
return cast.ToTime(v)
|
2014-01-29 17:50:31 -05:00
|
|
|
case []string:
|
2014-10-16 20:20:09 -04:00
|
|
|
return helpers.SliceToLower(v.([]string))
|
2014-11-28 15:16:57 -05:00
|
|
|
case map[interface{}]interface{}:
|
|
|
|
return v
|
2014-01-29 17:50:31 -05:00
|
|
|
}
|
|
|
|
return nil
|
2013-07-04 11:32:55 -04:00
|
|
|
}
|
|
|
|
|
2014-04-23 02:59:19 -04:00
|
|
|
func (page *Page) HasMenuCurrent(menu string, me *MenuEntry) bool {
|
|
|
|
menus := page.Menus()
|
|
|
|
|
|
|
|
if m, ok := menus[menu]; ok {
|
|
|
|
if me.HasChildren() {
|
|
|
|
for _, child := range me.Children {
|
2014-05-14 18:01:13 -04:00
|
|
|
if child.IsEqual(m) {
|
2014-04-23 02:59:19 -04:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-05-14 18:01:13 -04:00
|
|
|
func (page *Page) IsMenuCurrent(menu string, inme *MenuEntry) bool {
|
2014-04-23 02:59:19 -04:00
|
|
|
menus := page.Menus()
|
|
|
|
|
|
|
|
if me, ok := menus[menu]; ok {
|
2014-05-14 18:01:13 -04:00
|
|
|
return me.IsEqual(inme)
|
2014-04-23 02:59:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (page *Page) Menus() PageMenus {
|
|
|
|
ret := PageMenus{}
|
|
|
|
|
|
|
|
if ms, ok := page.Params["menu"]; ok {
|
2014-12-10 23:33:40 -05:00
|
|
|
link, _ := page.Permalink()
|
2014-04-23 02:59:19 -04:00
|
|
|
|
|
|
|
me := MenuEntry{Name: page.LinkTitle(), Weight: page.Weight, Url: link}
|
|
|
|
|
|
|
|
// Could be the name of the menu to attach it to
|
|
|
|
mname, err := cast.ToStringE(ms)
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
me.Menu = mname
|
|
|
|
ret[mname] = &me
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
|
|
|
// Could be an slice of strings
|
|
|
|
mnames, err := cast.ToStringSliceE(ms)
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
for _, mname := range mnames {
|
|
|
|
me.Menu = mname
|
|
|
|
ret[mname] = &me
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Could be a structured menu entry
|
|
|
|
menus, err := cast.ToStringMapE(ms)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
jww.ERROR.Printf("unable to process menus for %q\n", page.Title)
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, menu := range menus {
|
|
|
|
menuEntry := MenuEntry{Name: page.LinkTitle(), Url: link, Weight: page.Weight, Menu: name}
|
|
|
|
jww.DEBUG.Printf("found menu: %q, in %q\n", name, page.Title)
|
|
|
|
|
|
|
|
ime, err := cast.ToStringMapE(menu)
|
|
|
|
if err != nil {
|
|
|
|
jww.ERROR.Printf("unable to process menus for %q\n", page.Title)
|
|
|
|
}
|
|
|
|
|
2014-04-24 18:11:08 -04:00
|
|
|
menuEntry.MarshallMap(ime)
|
2014-04-23 02:59:19 -04:00
|
|
|
ret[name] = &menuEntry
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-09-03 15:41:13 -04:00
|
|
|
func (p *Page) Render(layout ...string) template.HTML {
|
2014-01-29 17:50:31 -05:00
|
|
|
curLayout := ""
|
2013-07-04 11:32:55 -04:00
|
|
|
|
2014-01-29 17:50:31 -05:00
|
|
|
if len(layout) > 0 {
|
|
|
|
curLayout = layout[0]
|
|
|
|
}
|
2013-07-04 11:32:55 -04:00
|
|
|
|
2014-11-20 12:32:21 -05:00
|
|
|
return tpl.ExecuteTemplateToHTML(p, p.Layout(curLayout)...)
|
2013-07-04 11:32:55 -04:00
|
|
|
}
|
|
|
|
|
2013-09-18 17:21:27 -04:00
|
|
|
func (page *Page) guessMarkupType() string {
|
2014-01-29 17:50:31 -05:00
|
|
|
// First try the explicitly set markup from the frontmatter
|
|
|
|
if page.Markup != "" {
|
2014-10-16 20:20:09 -04:00
|
|
|
format := helpers.GuessType(page.Markup)
|
2014-01-29 17:50:31 -05:00
|
|
|
if format != "unknown" {
|
|
|
|
return format
|
|
|
|
}
|
|
|
|
}
|
2013-09-18 17:21:27 -04:00
|
|
|
|
2014-10-16 20:20:09 -04:00
|
|
|
return helpers.GuessType(page.Source.Ext())
|
2013-12-06 23:56:51 -05:00
|
|
|
}
|
|
|
|
|
2014-05-01 13:19:51 -04:00
|
|
|
func (page *Page) detectFrontMatter() (f *parser.FrontmatterType) {
|
|
|
|
return parser.DetectFrontMatter(rune(page.frontmatter[0]))
|
|
|
|
}
|
|
|
|
|
2013-08-05 10:53:58 -04:00
|
|
|
func (page *Page) parse(reader io.Reader) error {
|
2014-05-01 13:19:51 -04:00
|
|
|
psr, err := parser.ReadFrom(reader)
|
2014-01-29 17:50:31 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-07-04 11:32:55 -04:00
|
|
|
|
2014-05-01 13:19:51 -04:00
|
|
|
page.renderable = psr.IsRenderable()
|
|
|
|
page.frontmatter = psr.FrontMatter()
|
|
|
|
meta, err := psr.Metadata()
|
2014-05-01 14:11:56 -04:00
|
|
|
if meta != nil {
|
|
|
|
if err != nil {
|
2014-10-16 20:20:09 -04:00
|
|
|
jww.ERROR.Printf("Error parsing page meta data for %s", page.File.Path())
|
2014-05-01 14:11:56 -04:00
|
|
|
jww.ERROR.Println(err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err = page.update(meta); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-05-01 13:19:51 -04:00
|
|
|
}
|
2013-09-18 13:17:43 -04:00
|
|
|
|
2014-05-01 13:19:51 -04:00
|
|
|
page.rawContent = psr.Content()
|
2013-07-04 11:32:55 -04:00
|
|
|
|
2014-05-01 13:19:51 -04:00
|
|
|
return nil
|
|
|
|
}
|
2013-09-18 13:17:43 -04:00
|
|
|
|
2014-05-01 13:19:51 -04:00
|
|
|
func (page *Page) SetSourceContent(content []byte) {
|
2014-10-16 20:20:09 -04:00
|
|
|
page.Source.Content = content
|
2014-05-01 13:19:51 -04:00
|
|
|
}
|
2013-12-06 23:32:00 -05:00
|
|
|
|
2014-05-01 13:19:51 -04:00
|
|
|
func (page *Page) SetSourceMetaData(in interface{}, mark rune) (err error) {
|
|
|
|
by, err := parser.InterfaceToFrontMatter(in, mark)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2014-01-29 17:50:31 -05:00
|
|
|
}
|
2014-05-11 22:34:44 -04:00
|
|
|
by = append(by, '\n')
|
2014-05-01 13:19:51 -04:00
|
|
|
|
2014-10-16 20:20:09 -04:00
|
|
|
page.Source.Frontmatter = by
|
2013-12-06 23:32:00 -05:00
|
|
|
|
2014-01-29 17:50:31 -05:00
|
|
|
return nil
|
2013-12-06 23:32:00 -05:00
|
|
|
}
|
2013-08-25 00:27:41 -04:00
|
|
|
|
2014-05-02 01:04:48 -04:00
|
|
|
func (page *Page) SafeSaveSourceAs(path string) error {
|
|
|
|
return page.saveSourceAs(path, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (page *Page) SaveSourceAs(path string) error {
|
|
|
|
return page.saveSourceAs(path, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (page *Page) saveSourceAs(path string, safe bool) error {
|
2014-05-01 13:21:37 -04:00
|
|
|
b := new(bytes.Buffer)
|
2014-10-16 20:20:09 -04:00
|
|
|
b.Write(page.Source.Frontmatter)
|
|
|
|
b.Write(page.Source.Content)
|
2014-05-01 13:21:37 -04:00
|
|
|
|
2014-05-02 01:04:48 -04:00
|
|
|
err := page.saveSource(b.Bytes(), path, safe)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
2014-05-01 13:21:37 -04:00
|
|
|
}
|
|
|
|
|
2014-05-02 01:04:48 -04:00
|
|
|
func (page *Page) saveSource(by []byte, inpath string, safe bool) (err error) {
|
2014-11-06 11:52:01 -05:00
|
|
|
if !filepath.IsAbs(inpath) {
|
2014-05-01 13:21:37 -04:00
|
|
|
inpath = helpers.AbsPathify(inpath)
|
|
|
|
}
|
|
|
|
jww.INFO.Println("creating", inpath)
|
2014-05-02 01:04:48 -04:00
|
|
|
|
|
|
|
if safe {
|
2014-11-01 11:57:29 -04:00
|
|
|
err = helpers.SafeWriteToDisk(inpath, bytes.NewReader(by), hugofs.SourceFs)
|
2014-05-02 01:04:48 -04:00
|
|
|
} else {
|
2014-11-01 11:57:29 -04:00
|
|
|
err = helpers.WriteToDisk(inpath, bytes.NewReader(by), hugofs.SourceFs)
|
2014-05-02 01:04:48 -04:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2014-05-01 13:21:37 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-05-02 01:04:48 -04:00
|
|
|
func (page *Page) SaveSource() error {
|
|
|
|
return page.SaveSourceAs(page.FullFilePath())
|
2014-05-01 13:21:37 -04:00
|
|
|
}
|
|
|
|
|
2014-11-20 12:32:21 -05:00
|
|
|
func (p *Page) ProcessShortcodes(t tpl.Template) {
|
Shortcode rewrite, take 2
This commit contains a restructuring and partial rewrite of the shortcode handling.
Prior to this commit rendering of the page content was mingled with handling of the shortcodes. This led to several oddities.
The new flow is:
1. Shortcodes are extracted from page and replaced with placeholders.
2. Shortcodes are processed and rendered
3. Page is processed
4. The placeholders are replaced with the rendered shortcodes
The handling of summaries is also made simpler by this.
This commit also introduces some other chenges:
1. distinction between shortcodes that need further processing and those who do not:
* `{{< >}}`: Typically raw HTML. Will not be processed.
* `{{% %}}`: Will be processed by the page's markup engine (Markdown or (infuture) Asciidoctor)
The above also involves a new shortcode-parser, with lexical scanning inspired by Rob Pike's talk called "Lexical Scanning in Go",
which should be easier to understand, give better error messages and perform better.
2. If you want to exclude a shortcode from being processed (for documentation etc.), the inner part of the shorcode must be commented out, i.e. `{{%/* movie 47238zzb */%}}`. See the updated shortcode section in the documentation for further examples.
The new parser supports nested shortcodes. This isn't new, but has two related design choices worth mentioning:
* The shortcodes will be rendered individually, so If both `{{< >}}` and `{{% %}}` are used in the nested hierarchy, one will be passed through the page's markdown processor, the other not.
* To avoid potential costly overhead of always looking far ahead for a possible closing tag, this implementation looks at the template itself, and is branded as a container with inner content if it contains a reference to `.Inner`
Fixes #565
Fixes #480
Fixes #461
And probably some others.
2014-10-27 16:48:30 -04:00
|
|
|
|
|
|
|
// these short codes aren't used until after Page render,
|
|
|
|
// but processed here to avoid coupling
|
|
|
|
tmpContent, tmpContentShortCodes := extractAndRenderShortcodes(string(p.rawContent), p, t)
|
|
|
|
p.rawContent = []byte(tmpContent)
|
|
|
|
p.contentShortCodes = tmpContentShortCodes
|
|
|
|
|
2014-01-27 17:16:28 -05:00
|
|
|
}
|
|
|
|
|
2014-11-20 12:39:09 -05:00
|
|
|
// TODO(spf13): Remove this entirely
|
|
|
|
// Here for backwards compatibility & testing. Only works in isolation
|
2013-12-06 23:32:00 -05:00
|
|
|
func (page *Page) Convert() error {
|
2014-11-20 12:39:09 -05:00
|
|
|
var h Handler
|
|
|
|
if page.Markup != "" {
|
|
|
|
h = FindHandler(page.Markup)
|
|
|
|
} else {
|
|
|
|
h = FindHandler(page.File.Extension())
|
|
|
|
}
|
|
|
|
if h != nil {
|
|
|
|
h.PageConvert(page, tpl.T())
|
2014-01-29 17:50:31 -05:00
|
|
|
}
|
Shortcode rewrite, take 2
This commit contains a restructuring and partial rewrite of the shortcode handling.
Prior to this commit rendering of the page content was mingled with handling of the shortcodes. This led to several oddities.
The new flow is:
1. Shortcodes are extracted from page and replaced with placeholders.
2. Shortcodes are processed and rendered
3. Page is processed
4. The placeholders are replaced with the rendered shortcodes
The handling of summaries is also made simpler by this.
This commit also introduces some other chenges:
1. distinction between shortcodes that need further processing and those who do not:
* `{{< >}}`: Typically raw HTML. Will not be processed.
* `{{% %}}`: Will be processed by the page's markup engine (Markdown or (infuture) Asciidoctor)
The above also involves a new shortcode-parser, with lexical scanning inspired by Rob Pike's talk called "Lexical Scanning in Go",
which should be easier to understand, give better error messages and perform better.
2. If you want to exclude a shortcode from being processed (for documentation etc.), the inner part of the shorcode must be commented out, i.e. `{{%/* movie 47238zzb */%}}`. See the updated shortcode section in the documentation for further examples.
The new parser supports nested shortcodes. This isn't new, but has two related design choices worth mentioning:
* The shortcodes will be rendered individually, so If both `{{< >}}` and `{{% %}}` are used in the nested hierarchy, one will be passed through the page's markdown processor, the other not.
* To avoid potential costly overhead of always looking far ahead for a possible closing tag, this implementation looks at the template itself, and is branded as a container with inner content if it contains a reference to `.Inner`
Fixes #565
Fixes #480
Fixes #461
And probably some others.
2014-10-27 16:48:30 -04:00
|
|
|
|
2014-11-20 12:39:09 -05:00
|
|
|
//// now we know enough to create a summary of the page and count some words
|
Shortcode rewrite, take 2
This commit contains a restructuring and partial rewrite of the shortcode handling.
Prior to this commit rendering of the page content was mingled with handling of the shortcodes. This led to several oddities.
The new flow is:
1. Shortcodes are extracted from page and replaced with placeholders.
2. Shortcodes are processed and rendered
3. Page is processed
4. The placeholders are replaced with the rendered shortcodes
The handling of summaries is also made simpler by this.
This commit also introduces some other chenges:
1. distinction between shortcodes that need further processing and those who do not:
* `{{< >}}`: Typically raw HTML. Will not be processed.
* `{{% %}}`: Will be processed by the page's markup engine (Markdown or (infuture) Asciidoctor)
The above also involves a new shortcode-parser, with lexical scanning inspired by Rob Pike's talk called "Lexical Scanning in Go",
which should be easier to understand, give better error messages and perform better.
2. If you want to exclude a shortcode from being processed (for documentation etc.), the inner part of the shorcode must be commented out, i.e. `{{%/* movie 47238zzb */%}}`. See the updated shortcode section in the documentation for further examples.
The new parser supports nested shortcodes. This isn't new, but has two related design choices worth mentioning:
* The shortcodes will be rendered individually, so If both `{{< >}}` and `{{% %}}` are used in the nested hierarchy, one will be passed through the page's markdown processor, the other not.
* To avoid potential costly overhead of always looking far ahead for a possible closing tag, this implementation looks at the template itself, and is branded as a container with inner content if it contains a reference to `.Inner`
Fixes #565
Fixes #480
Fixes #461
And probably some others.
2014-10-27 16:48:30 -04:00
|
|
|
page.setSummary()
|
|
|
|
//analyze for raw stats
|
|
|
|
page.analyzePage()
|
|
|
|
|
2014-01-29 17:50:31 -05:00
|
|
|
return nil
|
2013-07-04 11:32:55 -04:00
|
|
|
}
|
|
|
|
|
2014-05-01 13:21:37 -04:00
|
|
|
func (p *Page) FullFilePath() string {
|
2014-11-06 11:52:01 -05:00
|
|
|
return filepath.Join(p.Source.Dir(), p.Source.Path())
|
2014-05-01 13:21:37 -04:00
|
|
|
}
|
|
|
|
|
2013-09-20 20:24:25 -04:00
|
|
|
func (p *Page) TargetPath() (outfile string) {
|
|
|
|
|
2014-01-29 17:50:31 -05:00
|
|
|
// Always use Url if it's specified
|
|
|
|
if len(strings.TrimSpace(p.Url)) > 2 {
|
|
|
|
outfile = strings.TrimSpace(p.Url)
|
|
|
|
|
|
|
|
if strings.HasSuffix(outfile, "/") {
|
|
|
|
outfile = outfile + "index.html"
|
|
|
|
}
|
2014-12-07 13:48:00 -05:00
|
|
|
outfile = filepath.FromSlash(outfile)
|
2014-01-29 17:50:31 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// If there's a Permalink specification, we use that
|
2014-10-16 20:20:09 -04:00
|
|
|
if override, ok := p.Site.Permalinks[p.Section()]; ok {
|
2014-01-29 17:50:31 -05:00
|
|
|
var err error
|
|
|
|
outfile, err = override.Expand(p)
|
|
|
|
if err == nil {
|
|
|
|
if strings.HasSuffix(outfile, "/") {
|
|
|
|
outfile += "index.html"
|
|
|
|
}
|
2014-12-07 13:48:00 -05:00
|
|
|
outfile = filepath.FromSlash(outfile)
|
2014-01-29 17:50:31 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(strings.TrimSpace(p.Slug)) > 0 {
|
2014-10-16 20:20:09 -04:00
|
|
|
outfile = strings.TrimSpace(p.Slug) + "." + p.Extension()
|
2014-01-29 17:50:31 -05:00
|
|
|
} else {
|
|
|
|
// Fall back to filename
|
2014-10-16 20:20:09 -04:00
|
|
|
outfile = helpers.ReplaceExtension(p.Source.LogicalName(), p.Extension())
|
2014-09-09 16:58:02 -04:00
|
|
|
}
|
|
|
|
|
2014-11-06 11:52:01 -05:00
|
|
|
return filepath.Join(p.Source.Dir(), strings.TrimSpace(outfile))
|
2014-10-01 14:26:43 -04:00
|
|
|
}
|