diff options
author | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2018-01-15 20:40:39 +0100 |
---|---|---|
committer | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2018-01-17 16:22:33 +0100 |
commit | 20c9b6ec81171d1c586ea31d5d08b40b0edaffc6 (patch) | |
tree | 990d2709c1333663dce2ff97f16f8791fef3bac9 | |
parent | f8a119b606d55aa4f31f16e5a3cadc929c99e4f8 (diff) |
resource: Add front matter metadata to Resource
This commit expands the Resource interface with 3 new methods:
* Name
* Title
* Params
All of these can be set in the Page front matter. `Name` will get its default value from the base filename, and is the value used in the ByPrefix and GetByPrefix lookup methods.
Fixes #4244
-rw-r--r-- | hugolib/hugo_sites_build_test.go | 18 | ||||
-rw-r--r-- | hugolib/node_as_page_test.go | 6 | ||||
-rw-r--r-- | hugolib/page.go | 166 | ||||
-rw-r--r-- | hugolib/pageGroup_test.go | 12 | ||||
-rw-r--r-- | hugolib/pageSort.go | 2 | ||||
-rw-r--r-- | hugolib/pageSort_test.go | 10 | ||||
-rw-r--r-- | hugolib/page_bundler_handlers.go | 6 | ||||
-rw-r--r-- | hugolib/page_bundler_test.go | 30 | ||||
-rw-r--r-- | hugolib/page_collections_test.go | 2 | ||||
-rw-r--r-- | hugolib/page_paths.go | 6 | ||||
-rw-r--r-- | hugolib/page_test.go | 14 | ||||
-rw-r--r-- | hugolib/pages_related_test.go | 16 | ||||
-rw-r--r-- | hugolib/pagination.go | 4 | ||||
-rw-r--r-- | hugolib/permalinks.go | 2 | ||||
-rw-r--r-- | hugolib/site.go | 12 | ||||
-rw-r--r-- | hugolib/site_render.go | 6 | ||||
-rw-r--r-- | hugolib/site_sections_test.go | 24 | ||||
-rw-r--r-- | hugolib/site_test.go | 60 | ||||
-rw-r--r-- | hugolib/taxonomy.go | 4 | ||||
-rw-r--r-- | hugolib/taxonomy_test.go | 4 | ||||
-rw-r--r-- | hugolib/testhelpers_test.go | 2 | ||||
-rw-r--r-- | magefile.go | 3 | ||||
-rw-r--r-- | resource/image.go | 2 | ||||
-rw-r--r-- | resource/image_cache.go | 18 | ||||
-rw-r--r-- | resource/image_test.go | 22 | ||||
-rw-r--r-- | resource/resource.go | 171 | ||||
-rw-r--r-- | resource/resource_test.go | 192 |
27 files changed, 627 insertions, 187 deletions
diff --git a/hugolib/hugo_sites_build_test.go b/hugolib/hugo_sites_build_test.go index 429ea9a7c..c48e6b9a4 100644 --- a/hugolib/hugo_sites_build_test.go +++ b/hugolib/hugo_sites_build_test.go @@ -225,7 +225,7 @@ func doTestMultiSitesBuild(t *testing.T, configTemplate, configSuffix string) { gp1 := sites.GetContentPage(filepath.FromSlash("content/sect/doc1.en.md")) require.NotNil(t, gp1) - require.Equal(t, "doc1", gp1.Title) + require.Equal(t, "doc1", gp1.title) gp2 := sites.GetContentPage(filepath.FromSlash("content/dummysect/notfound.md")) require.Nil(t, gp2) @@ -317,9 +317,9 @@ func doTestMultiSitesBuild(t *testing.T, configTemplate, configSuffix string) { require.Len(t, homeEn.Translations(), 3) require.Equal(t, "fr", homeEn.Translations()[0].Lang()) require.Equal(t, "nn", homeEn.Translations()[1].Lang()) - require.Equal(t, "På nynorsk", homeEn.Translations()[1].Title) + require.Equal(t, "På nynorsk", homeEn.Translations()[1].title) require.Equal(t, "nb", homeEn.Translations()[2].Lang()) - require.Equal(t, "På bokmål", homeEn.Translations()[2].Title, configSuffix) + require.Equal(t, "På bokmål", homeEn.Translations()[2].title, configSuffix) require.Equal(t, "Bokmål", homeEn.Translations()[2].Language().LanguageName, configSuffix) sectFr := frSite.getPage(KindSection, "sect") @@ -328,7 +328,7 @@ func doTestMultiSitesBuild(t *testing.T, configTemplate, configSuffix string) { require.Equal(t, "fr", sectFr.Lang()) require.Len(t, sectFr.Translations(), 1) require.Equal(t, "en", sectFr.Translations()[0].Lang()) - require.Equal(t, "Sects", sectFr.Translations()[0].Title) + require.Equal(t, "Sects", sectFr.Translations()[0].title) nnSite := sites.Sites[2] require.Equal(t, "nn", nnSite.Language.Lang) @@ -495,9 +495,9 @@ func TestMultiSitesRebuild(t *testing.T) { require.Len(t, enSite.RegularPages, 6) require.Len(t, enSite.AllPages, 34) require.Len(t, frSite.RegularPages, 5) - require.Equal(t, "new_fr_1", frSite.RegularPages[3].Title) - require.Equal(t, "new_en_2", enSite.RegularPages[0].Title) - require.Equal(t, "new_en_1", enSite.RegularPages[1].Title) + require.Equal(t, "new_fr_1", frSite.RegularPages[3].title) + require.Equal(t, "new_en_2", enSite.RegularPages[0].title) + require.Equal(t, "new_en_1", enSite.RegularPages[1].title) rendered := readDestination(t, fs, "public/en/new1/index.html") require.True(t, strings.Contains(rendered, "new_en_1"), rendered) @@ -531,7 +531,7 @@ func TestMultiSitesRebuild(t *testing.T) { }, func(t *testing.T) { require.Len(t, enSite.RegularPages, 6, "Rename") - require.Equal(t, "new_en_1", enSite.RegularPages[1].Title) + require.Equal(t, "new_en_1", enSite.RegularPages[1].title) rendered := readDestination(t, fs, "public/en/new1renamed/index.html") require.True(t, strings.Contains(rendered, "new_en_1"), rendered) }}, @@ -683,7 +683,7 @@ title = "Svenska" // Veriy Swedish site require.Len(t, svSite.RegularPages, 1) svPage := svSite.RegularPages[0] - require.Equal(t, "Swedish Contentfile", svPage.Title) + require.Equal(t, "Swedish Contentfile", svPage.title) require.Equal(t, "sv", svPage.Lang()) require.Len(t, svPage.Translations(), 2) require.Len(t, svPage.AllTranslations(), 3) diff --git a/hugolib/node_as_page_test.go b/hugolib/node_as_page_test.go index 30408ed40..d0a935290 100644 --- a/hugolib/node_as_page_test.go +++ b/hugolib/node_as_page_test.go @@ -104,7 +104,7 @@ func doTestNodeAsPage(t *testing.T, ugly, preserveTaxonomyNames bool) { require.True(t, home.Path() != "") section2 := nodes[5] - require.Equal(t, "Section2", section2.Title) + require.Equal(t, "Section2", section2.title) pages := sites.findAllPagesByKind(KindPage) require.Len(t, pages, 4) @@ -252,9 +252,9 @@ func doTestNodesWithNoContentFile(t *testing.T, ugly bool) { for _, p := range pages { var want string if ugly { - want = "/" + p.s.PathSpec.URLize(p.Title) + ".html" + want = "/" + p.s.PathSpec.URLize(p.title) + ".html" } else { - want = "/" + p.s.PathSpec.URLize(p.Title) + "/" + want = "/" + p.s.PathSpec.URLize(p.title) + "/" } if p.URL() != want { t.Errorf("Taxonomy term URL mismatch: want %q, got %q", want, p.URL()) diff --git a/hugolib/page.go b/hugolib/page.go index 1e0d1ac83..e7dce9cf7 100644 --- a/hugolib/page.go +++ b/hugolib/page.go @@ -111,6 +111,10 @@ type Page struct { // provided by the Resource object. Resources resource.Resources + // This is the raw front matter metadata that is going to be assigned to + // the Resources above. + resourcesMetadata []map[string]interface{} + // translations will contain references to this page in other language // if available. translations Pages @@ -120,7 +124,7 @@ type Page struct { translationKey string // Params contains configuration defined in the params section of page frontmatter. - Params map[string]interface{} + params map[string]interface{} // Content sections Content template.HTML @@ -214,7 +218,7 @@ type Page struct { Site *SiteInfo `json:"-"` - Title string + title string Description string Keywords []string Data map[string]interface{} @@ -468,7 +472,7 @@ func (p *Page) Param(key interface{}) (interface{}, error) { func (p *Page) traverseDirect(key string) (interface{}, error) { keyStr := strings.ToLower(key) - if val, ok := p.Params[keyStr]; ok { + if val, ok := p.params[keyStr]; ok { return val, nil } @@ -476,7 +480,7 @@ func (p *Page) traverseDirect(key string) (interface{}, error) { } func (p *Page) traverseNested(keySegments []string) (interface{}, error) { - result := traverse(keySegments, p.Params) + result := traverse(keySegments, p.params) if result != nil { return result, nil } @@ -519,7 +523,7 @@ func (p *Page) Author() Author { } func (p *Page) Authors() AuthorList { - authorKeys, ok := p.Params["authors"] + authorKeys, ok := p.params["authors"] if !ok { return AuthorList{} } @@ -757,7 +761,7 @@ func (s *Site) newPageFromFile(fi *fileInfo) *Page { contentType: "", Source: Source{File: fi}, Keywords: []string{}, Sitemap: Sitemap{Priority: -1}, - Params: make(map[string]interface{}), + params: make(map[string]interface{}), translations: make(Pages, 0), sections: sectionsFromDir(fi.Dir()), Site: &s.Info, @@ -927,7 +931,7 @@ func (p *Page) LinkTitle() string { if len(p.linkTitle) > 0 { return p.linkTitle } - return p.Title + return p.title } func (p *Page) shouldBuild() bool { @@ -988,6 +992,22 @@ func (p *Page) RelPermalink() string { return p.relPermalink } +// See resource.Resource +func (p *Page) Name() string { + if p.File != nil { + return p.File.BaseFileName() + } + return p.title +} + +func (p *Page) Title() string { + return p.title +} + +func (p *Page) Params() map[string]interface{} { + return p.params +} + func (p *Page) subResourceTargetPathFactory(base string) string { return path.Join(p.relTargetPathBase, base) } @@ -1094,39 +1114,39 @@ func (p *Page) update(f interface{}) error { loki := strings.ToLower(k) switch loki { case "title": - p.Title = cast.ToString(v) - p.Params[loki] = p.Title + p.title = cast.ToString(v) + p.params[loki] = p.title case "linktitle": p.linkTitle = cast.ToString(v) - p.Params[loki] = p.linkTitle + p.params[loki] = p.linkTitle case "description": p.Description = cast.ToString(v) - p.Params[loki] = p.Description + p.params[loki] = p.Description case "slug": p.Slug = cast.ToString(v) - p.Params[loki] = p.Slug + p.params[loki] = p.Slug case "url": if url := cast.ToString(v); strings.HasPrefix(url, "http://") || strings.HasPrefix(url, "https://") { return fmt.Errorf("Only relative URLs are supported, %v provided", url) } p.URLPath.URL = cast.ToString(v) p.URLPath.frontMatterURL = p.URLPath.URL - p.Params[loki] = p.URLPath.URL + p.params[loki] = p.URLPath.URL case "type": p.contentType = cast.ToString(v) - p.Params[loki] = p.contentType + p.params[loki] = p.contentType case "extension", "ext": p.extension = cast.ToString(v) - p.Params[loki] = p.extension + p.params[loki] = p.extension case "keywords": p.Keywords = cast.ToStringSlice(v) - p.Params[loki] = p.Keywords + p.params[loki] = p.Keywords case "date": p.Date, err = cast.ToTimeE(v) if err != nil { p.s.Log.ERROR.Printf("Failed to parse date '%v' in page %s", v, p.File.Path()) } - p.Params[loki] = p.Date + p.params[loki] = p.Date case "lastmod": p.Lastmod, err = cast.ToTimeE(v) if err != nil { @@ -1135,10 +1155,10 @@ func (p *Page) update(f interface{}) error { case "modified": vv, err := cast.ToTimeE(v) if err == nil { - p.Params[loki] = vv + p.params[loki] = vv modified = vv } else { - p.Params[loki] = cast.ToString(v) + p.params[loki] = cast.ToString(v) } case "outputs": o := cast.ToStringSlice(v) @@ -1150,17 +1170,16 @@ func (p *Page) update(f interface{}) error { p.s.Log.ERROR.Printf("Failed to resolve output formats: %s", err) } else { p.outputFormats = outFormats - p.Params[loki] = outFormats + p.params[loki] = outFormats } } - //p.Params[loki] = p.Keywords case "publishdate", "pubdate": p.PublishDate, err = cast.ToTimeE(v) if err != nil { p.s.Log.ERROR.Printf("Failed to parse publishdate '%v' in page %s", v, p.File.Path()) } - p.Params[loki] = p.PublishDate + p.params[loki] = p.PublishDate case "expirydate", "unpublishdate": p.ExpiryDate, err = cast.ToTimeE(v) if err != nil { @@ -1178,20 +1197,20 @@ func (p *Page) update(f interface{}) error { vv, err := cast.ToTimeE(v) if err == nil { p.PublishDate = vv - p.Params[loki] = p.PublishDate + p.params[loki] = p.PublishDate } else { - p.Params[loki] = cast.ToString(v) + p.params[loki] = cast.ToString(v) } } case "layout": p.Layout = cast.ToString(v) - p.Params[loki] = p.Layout + p.params[loki] = p.Layout case "markup": p.Markup = cast.ToString(v) - p.Params[loki] = p.Markup + p.params[loki] = p.Markup case "weight": p.Weight = cast.ToInt(v) - p.Params[loki] = p.Weight + p.params[loki] = p.Weight case "aliases": p.Aliases = cast.ToStringSlice(v) for _, alias := range p.Aliases { @@ -1199,56 +1218,89 @@ func (p *Page) update(f interface{}) error { return fmt.Errorf("Only relative aliases are supported, %v provided", alias) } } - p.Params[loki] = p.Aliases + p.params[loki] = p.Aliases case "status": p.Status = cast.ToString(v) - p.Params[loki] = p.Status + p.params[loki] = p.Status case "sitemap": p.Sitemap = parseSitemap(cast.ToStringMap(v)) - p.Params[loki] = p.Sitemap + p.params[loki] = p.Sitemap case "iscjklanguage": isCJKLanguage = new(bool) *isCJKLanguage = cast.ToBool(v) case "translationkey": p.translationKey = cast.ToString(v) - p.Params[loki] = p.translationKey + p.params[loki] = p.translationKey + case "resources": + var resources []map[string]interface{} + handled := true + + switch vv := v.(type) { + case []map[interface{}]interface{}: + for _, vvv := range vv { + resources = append(resources, cast.ToStringMap(vvv)) + } + case []map[string]interface{}: + for _, vvv := range vv { + resources = append(resources, vvv) + } + case []interface{}: + for _, vvv := range vv { + switch vvvv := vvv.(type) { + case map[interface{}]interface{}: + resources = append(resources, cast.ToStringMap(vvvv)) + case map[string]interface{}: + resources = append(resources, vvvv) + } + } + default: + handled = false + } + + if handled { + p.params[loki] = resources + p.resourcesMetadata = resources + break + } + fallthrough + default: // If not one of the explicit values, store in Params switch vv := v.(type) { case bool: - p.Params[loki] = vv + p.params[loki] = vv case string: - p.Params[loki] = vv + p.params[loki] = vv case int64, int32, int16, int8, int: - p.Params[loki] = vv + p.params[loki] = vv case float64, float32: - p.Params[loki] = vv + p.params[loki] = vv case time.Time: - p.Params[loki] = vv + p.params[loki] = vv default: // handle array of strings as well switch vvv := vv.(type) { case []interface{}: if len(vvv) > 0 { switch vvv[0].(type) { case map[interface{}]interface{}: // Proper parsing structured array from YAML based FrontMatter - p.Params[loki] = vvv + p.params[loki] = vvv case map[string]interface{}: // Proper parsing structured array from JSON based FrontMatter - p.Params[loki] = vvv + p.params[loki] = vvv case []interface{}: - p.Params[loki] = vvv + p.params[loki] = vvv default: a := make([]string, len(vvv)) for i, u := range vvv { a[i] = cast.ToString(u) } - p.Params[loki] = a + p.params[loki] = a } } else { - p.Params[loki] = []string{} + p.params[loki] = []string{} } default: - p.Params[loki] = vv + p.params[loki] = vv } } } @@ -1263,7 +1315,7 @@ func (p *Page) update(f interface{}) error { } else if published != nil { p.Draft = !*published } - p.Params["draft"] = p.Draft + p.params["draft"] = p.Draft if p.Date.IsZero() { p.Date = p.PublishDate @@ -1277,7 +1329,7 @@ func (p *Page) update(f interface{}) error { fi, err := p.s.Fs.Source.Stat(filepath.Join(p.s.PathSpec.AbsPathify(p.s.Cfg.GetString("contentDir")), p.File.Path())) if err == nil { p.Date = fi.ModTime() - p.Params["date"] = p.Date + p.params["date"] = p.Date } } @@ -1289,9 +1341,9 @@ func (p *Page) update(f interface{}) error { } } - p.Params["lastmod"] = p.Lastmod - p.Params["publishdate"] = p.PublishDate - p.Params["expirydate"] = p.ExpiryDate + p.params["lastmod"] = p.Lastmod + p.params["publishdate"] = p.PublishDate + p.params["expirydate"] = p.ExpiryDate if isCJKLanguage != nil { p.isCJKLanguage = *isCJKLanguage @@ -1302,7 +1354,7 @@ func (p *Page) update(f interface{}) error { p.isCJKLanguage = false } } - p.Params["iscjklanguage"] = p.isCJKLanguage + p.params["iscjklanguage"] = p.isCJKLanguage return nil @@ -1317,7 +1369,7 @@ func (p *Page) getParamToLower(key string) interface{} { } func (p *Page) getParam(key string, stringToLower bool) interface{} { - v := p.Params[strings.ToLower(key)] + v := p.params[strings.ToLower(key)] if v == nil { return nil @@ -1390,7 +1442,7 @@ func (p *Page) HasMenuCurrent(menuID string, me *MenuEntry) bool { // The following logic is kept from back when Hugo had both Page and Node types. // TODO(bep) consolidate / clean - nme := MenuEntry{Page: p, Name: p.Title, URL: p.URL()} + nme := MenuEntry{Page: p, Name: p.title, URL: p.URL()} for _, child := range me.Children { if nme.IsSameResource(child) { @@ -1421,7 +1473,7 @@ func (p *Page) IsMenuCurrent(menuID string, inme *MenuEntry) bool { // The following logic is kept from back when Hugo had both Page and Node types. // TODO(bep) consolidate / clean - me := MenuEntry{Page: p, Name: p.Title, URL: p.URL()} + me := MenuEntry{Page: p, Name: p.title, URL: p.URL()} if !me.IsSameResource(inme) { return false @@ -1465,7 +1517,7 @@ func (p *Page) Menus() PageMenus { p.pageMenusInit.Do(func() { p.pageMenus = PageMenus{} - if ms, ok := p.Params["menu"]; ok { + if ms, ok := p.params["menu"]; ok { link := p.RelPermalink() me := MenuEntry{Page: p, Name: p.LinkTitle(), Weight: p.Weight, URL: link} @@ -1494,16 +1546,16 @@ func (p *Page) Menus() PageMenus { menus, err := cast.ToStringMapE(ms) if err != nil { - p.s.Log.ERROR.Printf("unable to process menus for %q\n", p.Title) + p.s.Log.ERROR.Printf("unable to process menus for %q\n", p.title) } for name, menu := range menus { menuEntry := MenuEntry{Page: p, Name: p.LinkTitle(), URL: link, Weight: p.Weight, Menu: name} if menu != nil { - p.s.Log.DEBUG.Printf("found menu: %q, in %q\n", name, p.Title) + p.s.Log.DEBUG.Printf("found menu: %q, in %q\n", name, p.title) ime, err := cast.ToStringMapE(menu) if err != nil { - p.s.Log.ERROR.Printf("unable to process menus for %q: %s", p.Title, err) + p.s.Log.ERROR.Printf("unable to process menus for %q: %s", p.title, err) } menuEntry.marshallMap(ime) @@ -1805,7 +1857,7 @@ func (p *Page) RelRef(refs ...string) (string, error) { } func (p *Page) String() string { - return fmt.Sprintf("Page(%q)", p.Title) + return fmt.Sprintf("Page(%q)", p.title) } type URLPath struct { @@ -2003,5 +2055,5 @@ func (p *Page) pathOrTitle() string { if p.Path() != "" { return p.Path() } - return p.Title + return p.title } diff --git a/hugolib/pageGroup_test.go b/hugolib/pageGroup_test.go index 8cc381b61..d17e09f8b 100644 --- a/hugolib/pageGroup_test.go +++ b/hugolib/pageGroup_test.go @@ -49,8 +49,8 @@ func preparePageGroupTestPages(t *testing.T) Pages { p.Date = cast.ToTime(src.date) p.PublishDate = cast.ToTime(src.date) p.ExpiryDate = cast.ToTime(src.date) - p.Params["custom_param"] = src.param - p.Params["custom_date"] = cast.ToTime(src.date) + p.params["custom_param"] = src.param + p.params["custom_date"] = cast.ToTime(src.date) pages = append(pages, p) } return pages @@ -253,7 +253,7 @@ func TestGroupByParamCalledWithCapitalLetterString(t *testing.T) { if err != nil { t.Fatalf("failed to prepare test page %s", f) } - p.Params["custom_param"] = testStr + p.params["custom_param"] = testStr pages := Pages{p} groups, err := pages.GroupByParam("custom_param") @@ -268,9 +268,9 @@ func TestGroupByParamCalledWithCapitalLetterString(t *testing.T) { func TestGroupByParamCalledWithSomeUnavailableParams(t *testing.T) { t.Parallel() pages := preparePageGroupTestPages(t) - delete(pages[1].Params, "custom_param") - delete(pages[3].Params, "custom_param") - delete(pages[4].Params, "custom_param") + delete(pages[1].params, "custom_param") + delete(pages[3].params, "custom_param") + delete(pages[4].params, "custom_param") expect := PagesGroup{ {Key: "foo", Pages: Pages{pages[0], pages[2]}}, diff --git a/hugolib/pageSort.go b/hugolib/pageSort.go index 6d2431cec..8e9420e30 100644 --- a/hugolib/pageSort.go +++ b/hugolib/pageSort.go @@ -129,7 +129,7 @@ func (p Pages) ByTitle() Pages { key := "pageSort.ByTitle" title := func(p1, p2 *Page) bool { - return p1.Title < p2.Title + return p1.title < p2.title } pages, _ := spc.get(key, p, pageBy(title).Sort) diff --git a/hugolib/pageSort_test.go b/hugolib/pageSort_test.go index 6379dccbe..d9c0d0761 100644 --- a/hugolib/pageSort_test.go +++ b/hugolib/pageSort_test.go @@ -74,7 +74,7 @@ func TestSortByN(t *testing.T) { assertFunc func(p Pages) bool }{ {(Pages).ByWeight, func(p Pages) bool { return p[0].Weight == 1 }}, - {(Pages).ByTitle, func(p Pages) bool { return p[0].Title == "ab" }}, + {(Pages).ByTitle, func(p Pages) bool { return p[0].title == "ab" }}, {(Pages).ByLinkTitle, func(p Pages) bool { return p[0].LinkTitle() == "abl" }}, {(Pages).ByDate, func(p Pages) bool { return p[0].Date == d4 }}, {(Pages).ByPublishDate, func(p Pages) bool { return p[0].PublishDate == d4 }}, @@ -124,7 +124,7 @@ func TestPageSortByParam(t *testing.T) { s := newTestSite(t) unsorted := createSortTestPages(s, 10) - delete(unsorted[9].Params, "arbitrarily") + delete(unsorted[9].params, "arbitrarily") firstSetValue, _ := unsorted[0].Param(k) secondSetValue, _ := unsorted[1].Param(k) @@ -163,9 +163,9 @@ func setSortVals(dates [4]time.Time, titles [4]string, weights [4]int, pages Pag pages[i].Date = dates[i] pages[i].Lastmod = dates[i] pages[i].Weight = weights[i] - pages[i].Title = titles[i] + pages[i].title = titles[i] // make sure we compare apples and ... apples ... - pages[len(dates)-1-i].linkTitle = pages[i].Title + "l" + pages[len(dates)-1-i].linkTitle = pages[i].title + "l" pages[len(dates)-1-i].PublishDate = dates[i] pages[len(dates)-1-i].ExpiryDate = dates[i] pages[len(dates)-1-i].Content = template.HTML(titles[i] + "_content") @@ -180,7 +180,7 @@ func createSortTestPages(s *Site, num int) Pages { for i := 0; i < num; i++ { p := s.newPage(filepath.FromSlash(fmt.Sprintf("/x/y/p%d.md", i))) - p.Params = map[string]interface{}{ + p.params = map[string]interface{}{ "arbitrarily": map[string]interface{}{ "nested": ("xyz" + fmt.Sprintf("%v", 100-i)), }, diff --git a/hugolib/page_bundler_handlers.go b/hugolib/page_bundler_handlers.go index 8696959dc..996712466 100644 --- a/hugolib/page_bundler_handlers.go +++ b/hugolib/page_bundler_handlers.go @@ -254,6 +254,12 @@ func (c *contentHandlers) parsePage(h contentHandler) contentHandler { return p.Resources[i].RelPermalink() < p.Resources[j].RelPermalink() }) + + // Assign metadata from front matter if set + if len(p.resourcesMetadata) > 0 { + resource.AssignMetadata(p.resourcesMetadata, p.Resources...) + } + } return h(ctx) diff --git a/hugolib/page_bundler_test.go b/hugolib/page_bundler_test.go index 084119241..18e01f446 100644 --- a/hugolib/page_bundler_test.go +++ b/hugolib/page_bundler_test.go @@ -136,8 +136,14 @@ func TestPageBundlerSite(t *testing.T) { "TheContent", "Sunset RelPermalink: /2017/pageslug/sunset1.jpg", "Thumb Width: 123", + "Thumb Name: my-sunset-1", "Short Sunset RelPermalink: /2017/pageslug/sunset2.jpg", "Short Thumb Width: 56", + "1: Image Title: Sunset Galore 1", + "1: Image Params: map[myparam:My Sunny Param]", + "2: Image Title: Sunset Galore 2", + "2: Image Params: map[myparam:My Sunny Param]", + "1: Image myParam: Lower: My Sunny Param Caps: My Sunny Param", ) th.assertFileContent(filepath.FromSlash("/work/public/cpath/2017/pageslug.html"), "TheContent") @@ -205,10 +211,16 @@ date: 2017-10-09 TheContent. ` - pageWithImageShortcodeContent := `--- + pageWithImageShortcodeAndResourceMetadataContent := `--- title: "Bundle Galore" slug: pageslug date: 2017-10-09 +resources: +- src: "*.jpg" + name: "my-sunset-:counter" + title: "Sunset Galore :counter" + params: + myParam: "My Sunny Param" --- TheContent. @@ -227,17 +239,25 @@ TheContent. singleLayout := ` Title: {{ .Title }} Content: {{ .Content }} -{{ $sunset := .Resources.GetByPrefix "sunset1" }} +{{ $sunset := .Resources.GetByPrefix "my-sunset-1" }} {{ with $sunset }} Sunset RelPermalink: {{ .RelPermalink }} {{ $thumb := .Fill "123x123" }} Thumb Width: {{ $thumb.Width }} +Thumb Name: {{ $thumb.Name }} +Thumb Title: {{ $thumb.Title }} |