node to page: Misc. TODO-fixes

Updates #2297
This commit is contained in:
Bjørn Erik Pedersen 2016-11-16 17:52:03 +01:00
parent 524eb16686
commit 62e9e7e6ba
5 changed files with 81 additions and 82 deletions

View file

@ -191,9 +191,9 @@ func (h *HugoSites) renderCrossSitesArtifacts() error {
func (h *HugoSites) assignMissingTranslations() error {
// This looks heavy, but it should be a small number of nodes by now.
allPages := h.findAllPagesByNodeTypeNotIn(KindPage)
allPages := h.findAllPagesByKindNotIn(KindPage)
for _, nodeType := range []string{KindHome, KindSection, KindTaxonomy, KindTaxonomyTerm} {
nodes := h.findPagesByNodeTypeIn(nodeType, allPages)
nodes := h.findPagesByKindIn(nodeType, allPages)
// Assign translations
for _, t1 := range nodes {
@ -216,7 +216,7 @@ func (h *HugoSites) createMissingPages() error {
for _, s := range h.Sites {
// home pages
home := s.findPagesByNodeType(KindHome)
home := s.findPagesByKind(KindHome)
if len(home) > 1 {
panic("Too many homes")
}
@ -229,8 +229,8 @@ func (h *HugoSites) createMissingPages() error {
// taxonomy list and terms pages
taxonomies := s.Language.GetStringMapString("taxonomies")
if len(taxonomies) > 0 {
taxonomyPages := s.findPagesByNodeType(KindTaxonomy)
taxonomyTermsPages := s.findPagesByNodeType(KindTaxonomyTerm)
taxonomyPages := s.findPagesByKind(KindTaxonomy)
taxonomyTermsPages := s.findPagesByKind(KindTaxonomyTerm)
for _, plural := range taxonomies {
tax := s.Taxonomies[plural]
foundTaxonomyPage := false
@ -264,7 +264,7 @@ func (h *HugoSites) createMissingPages() error {
}
}
sectionPages := s.findPagesByNodeType(KindSection)
sectionPages := s.findPagesByKind(KindSection)
if len(sectionPages) < len(s.Sections) {
for name, section := range s.Sections {
// A section may be created for the root content folder if a
@ -544,21 +544,20 @@ func (s *Site) updateBuildStats(page *Page) {
}
}
// TODO(bep) np remove
func (h *HugoSites) findAllPagesByNodeType(n string) Pages {
return h.Sites[0].findAllPagesByNodeType(n)
func (h *HugoSites) findPagesByKindNotIn(kind string, inPages Pages) Pages {
return h.Sites[0].findPagesByKindNotIn(kind, inPages)
}
func (h *HugoSites) findPagesByNodeTypeNotIn(n string, inPages Pages) Pages {
return h.Sites[0].findPagesByNodeTypeNotIn(n, inPages)
func (h *HugoSites) findPagesByKindIn(kind string, inPages Pages) Pages {
return h.Sites[0].findPagesByKindIn(kind, inPages)
}
func (h *HugoSites) findPagesByNodeTypeIn(n string, inPages Pages) Pages {
return h.Sites[0].findPagesByNodeTypeIn(n, inPages)
func (h *HugoSites) findAllPagesByKind(kind string) Pages {
return h.findPagesByKindIn(kind, h.Sites[0].AllPages)
}
func (h *HugoSites) findAllPagesByNodeTypeNotIn(n string) Pages {
return h.findPagesByNodeTypeNotIn(n, h.Sites[0].AllPages)
func (h *HugoSites) findAllPagesByKindNotIn(kind string) Pages {
return h.findPagesByKindNotIn(kind, h.Sites[0].AllPages)
}
// Convenience func used in tests to build a single site/language excluding render phase.

View file

@ -76,7 +76,7 @@ func TestNodesAsPage(t *testing.T) {
assertFileContent(t, filepath.Join("public", "sect1", "regular1", "index.html"), false, "Single Title: Page 01", "Content Page 01")
h := s.owner
nodes := h.findAllPagesByNodeTypeNotIn(KindPage)
nodes := h.findAllPagesByKindNotIn(KindPage)
require.Len(t, nodes, 6)
home := nodes[5] // oldest
@ -89,7 +89,7 @@ func TestNodesAsPage(t *testing.T) {
section2 := nodes[3]
require.Equal(t, "Section2", section2.Title)
pages := h.findAllPagesByNodeType(KindPage)
pages := h.findAllPagesByKind(KindPage)
require.Len(t, pages, 4)
first := pages[0]
@ -119,7 +119,7 @@ func TestNodesAsPage(t *testing.T) {
assertFileContent(t, filepath.Join("public", "sect1", "page", "2", "index.html"), false,
"Pag: Page 02")
sections := h.findAllPagesByNodeType(KindSection)
sections := h.findAllPagesByKind(KindSection)
require.Len(t, sections, 2)
@ -180,7 +180,7 @@ func TestNodesWithNoContentFile(t *testing.T) {
}
// Home page
homePages := s.findIndexNodesByNodeType(KindHome)
homePages := s.findPagesByKind(KindHome)
require.Len(t, homePages, 1)
homePage := homePages[0]

View file

@ -87,25 +87,35 @@ type Page struct {
// This collection will be nil for regular pages.
Pages Pages
Params map[string]interface{}
Content template.HTML
Summary template.HTML
Aliases []string
Status string
Images []Image
Videos []Video
// translations will contain references to this page in other language
// if available.
translations Pages
// Params contains configuration defined in the params section of page frontmatter.
Params map[string]interface{}
// Content sections
Content template.HTML
Summary template.HTML
TableOfContents template.HTML
Aliases []string
Images []Image
Videos []Video
Truncated bool
Draft bool
Status string
PublishDate time.Time
ExpiryDate time.Time
Markup string
// PageMeta contains page stats such as word count etc.
PageMeta
translations Pages
// Markup contains the markup type for the content.
Markup string
extension string
contentType string
@ -114,7 +124,8 @@ type Page struct {
Layout string
layoutsCalculated []string
linkTitle string
linkTitle string
frontmatter []byte
// rawContent isn't "raw" as in the same as in the content file.
@ -126,19 +137,29 @@ type Page struct {
// state telling if this is a "new page" or if we have rendered it previously.
rendered bool
contentShortCodes map[string]func() (string, error)
shortcodes map[string]shortcode
plain string // TODO should be []byte
plainWords []string
plainInit sync.Once
plainWordsInit sync.Once
// whether the content is in a CJK language.
isCJKLanguage bool
// shortcode state
contentShortCodes map[string]func() (string, error)
shortcodes map[string]shortcode
// the content stripped for HTML
plain string // TODO should be []byte
plainWords []string
plainInit sync.Once
plainWordsInit sync.Once
// rendering configuration
renderingConfig *helpers.Blackfriday
renderingConfigInit sync.Once
pageMenus PageMenus
pageMenusInit sync.Once
isCJKLanguage bool
PageMeta
// menus
pageMenus PageMenus
pageMenusInit sync.Once
Source
Position `json:"-"`
GitInfo *gitmap.GitInfo
@ -155,7 +176,6 @@ type Page struct {
// isn't accomanied by one.
sections []string
// TODO(bep) np Site added to page, keep?
site *Site
// Pulled over from Node. TODO(bep) np reorg and group (embed)
@ -173,6 +193,7 @@ type Page struct {
Sitemap Sitemap
RSSLink template.HTML
URLPath
paginator *Pager
@ -549,7 +570,6 @@ func (p *Page) layouts(l ...string) []string {
return p.layoutsCalculated
}
// TODO(bep) np taxonomy etc.
switch p.Kind {
case KindHome:
return []string{"index.html", "_default/list.html"}
@ -580,7 +600,7 @@ func (p *Page) layouts(l ...string) []string {
return layouts(p.Type(), layout)
}
// TODO(bep) np consolidate and test these NodeType switches
// TODO(bep) consolidate and test these KindHome switches (see other layouts methods)s
// rssLayouts returns RSS layouts to use for the RSS version of this page, nil
// if no RSS should be rendered.
func (p *Page) rssLayouts() []string {
@ -600,7 +620,6 @@ func (p *Page) rssLayouts() []string {
}
return nil
}
func layouts(types string, layout string) (layouts []string) {
@ -1441,7 +1460,7 @@ func (p *Page) prepareData(s *Site) error {
switch p.Kind {
case KindPage:
case KindHome:
pages = s.findPagesByNodeTypeNotIn(KindHome, s.Pages)
pages = s.findPagesByKindNotIn(KindHome, s.Pages)
case KindSection:
sectionData, ok := s.Sections[p.sections[0]]
if !ok {
@ -1721,19 +1740,18 @@ func kindFromFilename(filename string) string {
return kindUnknown
}
func (p *Page) setNodeTypeVars(s *Site) {
func (p *Page) setValuesForKind(s *Site) {
if p.Kind == kindUnknown {
// This is either a taxonomy list, taxonomy term or a section
nodeType := s.nodeTypeFromSections(p.sections)
nodeType := s.kindFromSections(p.sections)
if nodeType == kindUnknown {
panic(fmt.Sprintf("Unable to determine node type from %q", p.sections))
panic(fmt.Sprintf("Unable to determine page kind from %q", p.sections))
}
p.Kind = nodeType
}
// TODO(bep) np node URL
// Set Node URL
switch p.Kind {
case KindHome:
p.URLPath.URL = "/"

View file

@ -39,8 +39,8 @@ type PageCollections struct {
}
func (c *PageCollections) refreshPageCaches() {
c.indexPages = c.findPagesByNodeTypeNotIn(KindPage, c.Pages)
c.RegularPages = c.findPagesByNodeTypeIn(KindPage, c.Pages)
c.indexPages = c.findPagesByKindNotIn(KindPage, c.Pages)
c.RegularPages = c.findPagesByKindIn(KindPage, c.Pages)
// TODO(bep) np remove eventually
for _, n := range c.Pages {
@ -58,14 +58,8 @@ func newPageCollectionsFromPages(pages Pages) *PageCollections {
return &PageCollections{rawAllPages: pages}
}
// TODO(bep) np clean and remove finders
func (c *PageCollections) findPagesByNodeType(n string) Pages {
return c.findPagesByNodeTypeIn(n, c.Pages)
}
func (c *PageCollections) getPage(typ string, path ...string) *Page {
pages := c.findPagesByNodeTypeIn(typ, c.Pages)
pages := c.findPagesByKindIn(typ, c.Pages)
if len(pages) == 0 {
return nil
@ -93,36 +87,28 @@ func (c *PageCollections) getPage(typ string, path ...string) *Page {
return nil
}
func (c *PageCollections) findIndexNodesByNodeType(n string) Pages {
return c.findPagesByNodeTypeIn(n, c.indexPages)
}
func (*PageCollections) findPagesByNodeTypeIn(n string, inPages Pages) Pages {
func (*PageCollections) findPagesByKindIn(kind string, inPages Pages) Pages {
var pages Pages
for _, p := range inPages {
if p.Kind == n {
if p.Kind == kind {
pages = append(pages, p)
}
}
return pages
}
func (*PageCollections) findPagesByNodeTypeNotIn(n string, inPages Pages) Pages {
func (*PageCollections) findPagesByKindNotIn(kind string, inPages Pages) Pages {
var pages Pages
for _, p := range inPages {
if p.Kind != n {
if p.Kind != kind {
pages = append(pages, p)
}
}
return pages
}
func (c *PageCollections) findAllPagesByNodeType(n string) Pages {
return c.findPagesByNodeTypeIn(n, c.Pages)
}
func (c *PageCollections) findRawAllPagesByNodeType(n string) Pages {
return c.findPagesByNodeTypeIn(n, c.rawAllPages)
func (c *PageCollections) findPagesByKind(kind string) Pages {
return c.findPagesByKindIn(kind, c.Pages)
}
func (c *PageCollections) addPage(page *Page) {

View file

@ -1330,18 +1330,14 @@ func (s *Site) buildSiteMeta() (err error) {
s.assembleTaxonomies()
// TODO(bep) np
for _, p := range s.AllPages {
// setNodeTypeVars needs taxonomies
p.setNodeTypeVars(s)
// this depends on taxonomies
p.setValuesForKind(s)
}
// assembleSections: Needs pages (temp lookup)
s.assembleSections()
// TODO(bep) np Site.LastMod
pages := s.Pages
s.Info.LastChange = pages[0].Lastmod
s.Info.LastChange = s.Pages[0].Lastmod
return
}
@ -1530,8 +1526,8 @@ func (s *Site) assembleSections() {
s.Sections = make(Taxonomy)
s.Info.Sections = s.Sections
// TODO(bep) np check these vs the caches
regularPages := s.findPagesByNodeType(KindPage)
sectionPages := s.findPagesByNodeType(KindSection)
regularPages := s.findPagesByKind(KindPage)
sectionPages := s.findPagesByKind(KindSection)
for i, p := range regularPages {
s.Sections.add(p.Section(), WeightedPage{regularPages[i].Weight, regularPages[i]}, s.Info.preserveTaxonomyNames)
@ -1558,7 +1554,7 @@ func (s *Site) assembleSections() {
}
}
func (s *Site) nodeTypeFromSections(sections []string) string {
func (s *Site) kindFromSections(sections []string) string {
if _, isTaxonomy := s.Taxonomies[sections[0]]; isTaxonomy {
if len(sections) == 1 {
return KindTaxonomyTerm