mirror of
https://github.com/gohugoio/hugo.git
synced 2025-08-30 22:39:58 +02:00
Make Page an interface
The main motivation of this commit is to add a `page.Page` interface to replace the very file-oriented `hugolib.Page` struct. This is all a preparation step for issue #5074, "pages from other data sources". But this also fixes a set of annoying limitations, especially related to custom output formats, and shortcodes. Most notable changes: * The inner content of shortcodes using the `{{%` as the outer-most delimiter will now be sent to the content renderer, e.g. Blackfriday. This means that any markdown will partake in the global ToC and footnote context etc. * The Custom Output formats are now "fully virtualized". This removes many of the current limitations. * The taxonomy list type now has a reference to the `Page` object. This improves the taxonomy template `.Title` situation and make common template constructs much simpler. See #5074 Fixes #5763 Fixes #5758 Fixes #5090 Fixes #5204 Fixes #4695 Fixes #5607 Fixes #5707 Fixes #5719 Fixes #3113 Fixes #5706 Fixes #5767 Fixes #5723 Fixes #5769 Fixes #5770 Fixes #5771 Fixes #5759 Fixes #5776 Fixes #5777 Fixes #5778
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
// Copyright 2018 The Hugo Authors. All rights reserved.
|
||||
// Copyright 2019 The Hugo Authors. All rights reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
@@ -21,6 +21,8 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/gohugoio/hugo/output"
|
||||
|
||||
"github.com/gohugoio/hugo/common/herrors"
|
||||
|
||||
"github.com/gohugoio/hugo/hugofs"
|
||||
@@ -37,7 +39,8 @@ import (
|
||||
)
|
||||
|
||||
var (
|
||||
_ TemplateExecutor = (*TemplateAdapter)(nil)
|
||||
_ TemplateExecutor = (*TemplateAdapter)(nil)
|
||||
_ TemplateInfoProvider = (*TemplateAdapter)(nil)
|
||||
)
|
||||
|
||||
// TemplateHandler manages the collection of templates.
|
||||
@@ -53,17 +56,47 @@ type TemplateHandler interface {
|
||||
RebuildClone()
|
||||
}
|
||||
|
||||
// TemplateVariants describes the possible variants of a template.
|
||||
// All of these may be empty.
|
||||
type TemplateVariants struct {
|
||||
Language string
|
||||
OutputFormat output.Format
|
||||
}
|
||||
|
||||
// TemplateFinder finds templates.
|
||||
type TemplateFinder interface {
|
||||
TemplateLookup
|
||||
TemplateLookupVariant
|
||||
}
|
||||
|
||||
type TemplateLookup interface {
|
||||
Lookup(name string) (Template, bool)
|
||||
}
|
||||
|
||||
type TemplateLookupVariant interface {
|
||||
// TODO(bep) this currently only works for shortcodes.
|
||||
// We may unify and expand this variant pattern to the
|
||||
// other templates, but we need this now for the shortcodes to
|
||||
// quickly determine if a shortcode has a template for a given
|
||||
// output format.
|
||||
// It returns the template, if it was found or not and if there are
|
||||
// alternative representations (output format, language).
|
||||
// We are currently only interested in output formats, so we should improve
|
||||
// this for speed.
|
||||
LookupVariant(name string, variants TemplateVariants) (Template, bool, bool)
|
||||
}
|
||||
|
||||
// Template is the common interface between text/template and html/template.
|
||||
type Template interface {
|
||||
Execute(wr io.Writer, data interface{}) error
|
||||
Name() string
|
||||
}
|
||||
|
||||
// TemplateInfoProvider provides some contextual information about a template.
|
||||
type TemplateInfoProvider interface {
|
||||
TemplateInfo() Info
|
||||
}
|
||||
|
||||
// TemplateParser is used to parse ad-hoc templates, e.g. in the Resource chain.
|
||||
type TemplateParser interface {
|
||||
Parse(name, tpl string) (Template, error)
|
||||
@@ -92,6 +125,8 @@ type TemplateAdapter struct {
|
||||
Template
|
||||
Metrics metrics.Provider
|
||||
|
||||
Info Info
|
||||
|
||||
// The filesystem where the templates are stored.
|
||||
Fs afero.Fs
|
||||
|
||||
@@ -133,6 +168,10 @@ func (t *TemplateAdapter) Execute(w io.Writer, data interface{}) (execErr error)
|
||||
return
|
||||
}
|
||||
|
||||
func (t *TemplateAdapter) TemplateInfo() Info {
|
||||
return t.Info
|
||||
}
|
||||
|
||||
// The identifiers may be truncated in the log, e.g.
|
||||
// "executing "main" at <$scaled.SRelPermalin...>: can't evaluate field SRelPermalink in type *resource.Image"
|
||||
var identifiersRe = regexp.MustCompile("at \\<(.*?)(\\.{3})?\\>:")
|
||||
|
Reference in New Issue
Block a user