Skip to content

Commit

Permalink
Work
Browse files Browse the repository at this point in the history
  • Loading branch information
bep committed Jan 4, 2019
1 parent b0793b4 commit 47056a7
Show file tree
Hide file tree
Showing 15 changed files with 69 additions and 83 deletions.
2 changes: 1 addition & 1 deletion hugolib/gitinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func (g *gitInfo) forPage(p *Page) (*gitmap.GitInfo, bool) {
return nil, false
}

name := strings.TrimPrefix(filepath.ToSlash(p.Filename()), g.contentDir)
name := strings.TrimPrefix(filepath.ToSlash(p.File().Filename()), g.contentDir)
name = strings.TrimPrefix(name, "/")

return g.repo.Files[name], true
Expand Down
8 changes: 4 additions & 4 deletions hugolib/hugo_sites.go
Original file line number Diff line number Diff line change
Expand Up @@ -454,8 +454,8 @@ func (cfg *BuildCfg) shouldRender(p *Page) bool {
return true
}

if cfg.whatChanged != nil && p.File != nil {
return cfg.whatChanged.files[p.File.Filename()]
if cfg.whatChanged != nil && p.File() != nil {
return cfg.whatChanged.files[p.File().Filename()]
}

return false
Expand Down Expand Up @@ -706,7 +706,7 @@ func (h *HugoSites) Pages() page.Pages {

func handleShortcodes(p *PageWithoutContent, rawContentCopy []byte) ([]byte, error) {
if p.shortcodeState != nil && p.shortcodeState.contentShortcodes.Len() > 0 {
p.s.Log.DEBUG.Printf("Replace %d shortcodes in %q", p.shortcodeState.contentShortcodes.Len(), p.BaseFileName())
p.s.Log.DEBUG.Printf("Replace %d shortcodes in %q", p.shortcodeState.contentShortcodes.Len(), p.File().BaseFileName())
err := p.shortcodeState.executeShortcodesForDelta(p)

if err != nil {
Expand All @@ -717,7 +717,7 @@ func handleShortcodes(p *PageWithoutContent, rawContentCopy []byte) ([]byte, err
rawContentCopy, err = replaceShortcodeTokens(rawContentCopy, shortcodePlaceholderPrefix, p.shortcodeState.renderedShortcodes)

if err != nil {
p.s.Log.FATAL.Printf("Failed to replace shortcode tokens in %s:\n%s", p.BaseFileName(), err.Error())
p.s.Log.FATAL.Printf("Failed to replace shortcode tokens in %s:\n%s", p.File().BaseFileName(), err.Error())
}
}

Expand Down
4 changes: 2 additions & 2 deletions hugolib/hugo_sites_build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -627,11 +627,11 @@ func assertShouldNotBuild(t *testing.T, sites *HugoSites) {
for _, p := range s.rawAllPages {
pp := p.(*Page)
// No HTML when not processed
require.Equal(t, pp.shouldBuild(), bytes.Contains(pp.workContent, []byte("</")), pp.BaseFileName()+": "+string(pp.workContent))
require.Equal(t, pp.shouldBuild(), bytes.Contains(pp.workContent, []byte("</")), pp.File().BaseFileName()+": "+string(pp.workContent))

require.Equal(t, pp.shouldBuild(), pp.content() != "", fmt.Sprintf("%v:%v", pp.content(), pp.shouldBuild()))

require.Equal(t, pp.shouldBuild(), pp.content() != "", pp.BaseFileName())
require.Equal(t, pp.shouldBuild(), pp.content() != "", pp.File().BaseFileName())

}
}
Expand Down
60 changes: 34 additions & 26 deletions hugolib/page.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ type Page struct {
// menus
pageMenus PageMenus

source.File
sourceFile source.File

Position `json:"-"`

Expand Down Expand Up @@ -288,6 +288,18 @@ func stackTrace(length int) string {
return string(trace)
}

func (p *Page) File() source.File {
return p.sourceFile
}

func (p *Page) Dir() string {
// TODO(bep) page deprecate this and possible others. Use .File...
return p.File().Dir()
}

func (p *Page) Path() string {
return p.File().Path()
}
func (p *Page) Kind() string {
return p.kind
}
Expand Down Expand Up @@ -487,7 +499,7 @@ func (p *Page) BundleType() string {
return "branch"
}

var source interface{} = p.File
source := p.File()
if fi, ok := source.(*fileInfo); ok {
switch fi.bundleTp {
case bundleBranch:
Expand Down Expand Up @@ -527,7 +539,7 @@ type Position struct {

func findPagePosByFilename(ps page.Pages, filename string) int {
for i, x := range ps {
if x.(*Page).Filename() == filename {
if x.File().Filename() == filename {
return i
}
}
Expand All @@ -545,8 +557,8 @@ func findPagePosByFilnamePrefix(ps page.Pages, prefix string) int {

// Find the closest match
for i, x := range ps {
if strings.HasPrefix(x.(*Page).Filename(), prefix) {
diff := len(x.(*Page).Filename()) - prefixLen
if strings.HasPrefix(x.File().Filename(), prefix) {
diff := len(x.File().Filename()) - prefixLen
if lenDiff == -1 || diff < lenDiff {
lenDiff = diff
currPos = i
Expand All @@ -560,7 +572,7 @@ func findPagePosByFilnamePrefix(ps page.Pages, prefix string) int {
// will return -1 if not found
func findPagePos(ps page.Pages, page *Page) int {
for i, x := range ps {
if x.(*Page).Filename() == page.Filename() {
if x.File().Filename() == page.File().Filename() {
return i
}
}
Expand Down Expand Up @@ -637,7 +649,7 @@ func (p *Page) Authors() AuthorList {
}

func (p *Page) UniqueID() string {
return p.File.UniqueID()
return p.sourceFile.UniqueID()
}

// Returns the page as summary and main.
Expand Down Expand Up @@ -773,12 +785,12 @@ func (p *Page) getRenderingConfig() *helpers.BlackFriday {
p.renderingConfig = &bf

if p.Language() == nil {
panic(fmt.Sprintf("nil language for %s with source lang %s", p.BaseFileName(), p.lang))
panic(fmt.Sprintf("nil language for %s with source lang %s", p.File().BaseFileName(), p.lang))
}

pageParam := cast.ToStringMap(bfParam)
if err := mapstructure.Decode(pageParam, &p.renderingConfig); err != nil {
p.s.Log.FATAL.Printf("Failed to get rendering config for %s:\n%s", p.BaseFileName(), err.Error())
p.s.Log.FATAL.Printf("Failed to get rendering config for %s:\n%s", p.File().BaseFileName(), err.Error())
}

})
Expand All @@ -803,7 +815,7 @@ func (s *Site) newPageFromFile(fi *fileInfo) *Page {
pageContentInit: &pageContentInit{},
kind: kindFromFileInfo(fi),
contentType: "",
File: fi,
sourceFile: fi,
Keywords: []string{}, Sitemap: Sitemap{Priority: -1},
params: make(map[string]interface{}),
translations: make(page.Pages, 0),
Expand Down Expand Up @@ -836,7 +848,7 @@ func (p *Page) Section() string {
if p.Kind() == KindSection || p.Kind() == KindTaxonomy || p.Kind() == KindTaxonomyTerm {
return p.sections[0]
}
return p.File.Section()
return p.File().Section()
}

func (s *Site) newPageFrom(buf io.Reader, name string) (*Page, error) {
Expand Down Expand Up @@ -978,10 +990,10 @@ func (p *Page) TranslationKey() string {
}

if p.IsNode() {
return path.Join(p.Kind(), path.Join(p.sections...), p.TranslationBaseName())
return path.Join(p.Kind(), path.Join(p.sections...), p.File().TranslationBaseName())
}

return path.Join(p.Kind(), filepath.ToSlash(p.Dir()), p.TranslationBaseName())
return path.Join(p.Kind(), filepath.ToSlash(p.Dir()), p.File().TranslationBaseName())
}

func (p *Page) LinkTitle() string {
Expand Down Expand Up @@ -1174,8 +1186,8 @@ func (p *Page) updateMetaData(frontmatter map[string]interface{}) error {
maps.ToLower(frontmatter)

var mtime time.Time
if p.FileInfo() != nil {
mtime = p.FileInfo().ModTime()
if p.File().FileInfo() != nil {
mtime = p.File().FileInfo().ModTime()
}

var gitAuthorDate time.Time
Expand All @@ -1188,7 +1200,7 @@ func (p *Page) updateMetaData(frontmatter map[string]interface{}) error {
Params: p.params,
Dates: &p.Dates,
PageURLs: &p.URLPath,
BaseFilename: p.ContentBaseName(),
BaseFilename: p.File().ContentBaseName(),
ModTime: mtime,
GitAuthorDate: gitAuthorDate,
}
Expand Down Expand Up @@ -1250,7 +1262,7 @@ func (p *Page) updateMetaData(frontmatter map[string]interface{}) error {
case "headless":
// For now, only the leaf bundles ("index.md") can be headless (i.e. produce no output).
// We may expand on this in the future, but that gets more complex pretty fast.
if p.TranslationBaseName() == "index" {
if p.File().TranslationBaseName() == "index" {
p.headless = cast.ToBool(v)
}
p.params[loki] = p.headless
Expand Down Expand Up @@ -1377,12 +1389,12 @@ func (p *Page) updateMetaData(frontmatter map[string]interface{}) error {
p.Markup = helpers.GuessType(p.Markup)
if p.Markup == "unknown" {
// Fall back to file extension (might also return "unknown")
p.Markup = helpers.GuessType(p.Ext())
p.Markup = helpers.GuessType(p.File().Ext())
}

if draft != nil && published != nil {
p.Draft = *draft
p.s.Log.WARN.Printf("page %q has both draft and published settings in its frontmatter. Using draft.", p.Filename())
p.s.Log.WARN.Printf("page %q has both draft and published settings in its frontmatter. Using draft.", p.File().Filename())
} else if draft != nil {
p.Draft = *draft
} else if published != nil {
Expand Down Expand Up @@ -1632,10 +1644,6 @@ func (p *Page) RawContent() string {
return string(p.source.parsed.Input()[p.source.posMainContent:])
}

func (p *Page) FullFilePath() string {
return filepath.Join(p.Dir(), p.LogicalName())
}

// SourceRef returns the canonical, absolute fully-qualifed logical reference used by
// methods such as GetPage and ref/relref shortcodes to refer to
// this page. It is prefixed with a "/".
Expand All @@ -1645,7 +1653,7 @@ func (p *Page) FullFilePath() string {
// For pages that do not (sections witout content page etc.), it returns the
// virtual path, consistent with where you would add a source file.
func (p *Page) SourceRef() string {
if p.File != nil {
if p.File() != nil {
sourcePath := p.Path()
if sourcePath != "" {
return "/" + filepath.ToSlash(sourcePath)
Expand Down Expand Up @@ -2021,8 +2029,8 @@ func (p *Page) setValuesForKind(s *Site) {

// Used in error logs.
func (p *Page) pathOrTitle() string {
if p.Filename() != "" {
return p.Filename()
if p.File().Filename() != "" {
return p.File().Filename()
}
return p.title
}
Expand Down
2 changes: 1 addition & 1 deletion hugolib/page_content.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ func (p *Page) parse(reader io.Reader) error {
parsed: parseResult,
}

p.lang = p.File.Lang()
p.lang = p.File().Lang()

if p.s != nil && p.s.owner != nil {
gi, enabled := p.s.owner.gitInfo.forPage(p)
Expand Down
4 changes: 2 additions & 2 deletions hugolib/page_errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ func (p *Page) errWithFileContext(err error) error {

err, _ = herrors.WithFileContextForFile(
err,
p.Filename(),
p.Filename(),
p.File().Filename(),
p.File().Filename(),
p.s.SourceSpec.Fs.Source,
herrors.SimpleLineMatcher)

Expand Down
4 changes: 2 additions & 2 deletions hugolib/page_paths.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func (p *Page) initTargetPathDescriptor() error {
if p.Slug != "" {
d.BaseName = p.Slug
} else {
d.BaseName = p.TranslationBaseName()
d.BaseName = p.File().TranslationBaseName()
}

if p.shouldAddLanguagePrefix() {
Expand Down Expand Up @@ -299,7 +299,7 @@ func (p *Page) createRelativeTargetPathForOutputFormat(f output.Format) string {
tp, err := p.createTargetPath(f, p.s.owner.IsMultihost())

if err != nil {
p.s.Log.ERROR.Printf("Failed to create permalink for page %q: %s", p.FullFilePath(), err)
p.s.Log.ERROR.Printf("Failed to create permalink for page %q: %s", p.File().Filename(), err)
return ""
}

Expand Down
2 changes: 1 addition & 1 deletion hugolib/pagebundler_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func (c *handlerContext) ext() string {
if c.currentPage.Markup != "" {
return c.currentPage.Markup
}
return c.currentPage.Ext()
return c.currentPage.File().Ext()
}

if c.bundle != nil {
Expand Down
4 changes: 2 additions & 2 deletions hugolib/pagecollections.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,9 @@ func (c *PageCollections) refreshPageCaches() {
}

// Ref/Relref supports this potentially ambiguous lookup.
add(pp.LogicalName(), p)
add(pp.File().LogicalName(), p)

translationBaseName := pp.TranslationBaseName()
translationBaseName := pp.File().TranslationBaseName()

dir, _ := path.Split(sourceRef)
dir = strings.TrimSuffix(dir, "/")
Expand Down
4 changes: 2 additions & 2 deletions hugolib/permalinks.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,10 +158,10 @@ func pageToPermalinkTitle(p *Page, _ string) (string, error) {

// pageToPermalinkFilename returns the URL-safe form of the filename
func pageToPermalinkFilename(p *Page, _ string) (string, error) {
name := p.File.TranslationBaseName()
name := p.File().TranslationBaseName()
if name == "index" {
// Page bundles; the directory name will hopefully have a better name.
dir := strings.TrimSuffix(p.File.Dir(), helpers.FilePathSeparator)
dir := strings.TrimSuffix(p.File().Dir(), helpers.FilePathSeparator)
_, name = filepath.Split(dir)
}

Expand Down
8 changes: 4 additions & 4 deletions hugolib/site.go
Original file line number Diff line number Diff line change
Expand Up @@ -862,7 +862,7 @@ func (s *Site) processPartial(events []fsnotify.Event) (whatChanged, error) {
// pages that keeps a reference to the changed shortcode.
pagesWithShortcode := h.findPagesByShortcode(shortcode)
for _, p := range pagesWithShortcode {
contentFilesChanged = append(contentFilesChanged, p.(*Page).File.Filename())
contentFilesChanged = append(contentFilesChanged, p.(*Page).File().Filename())
}
}

Expand Down Expand Up @@ -1537,7 +1537,7 @@ func (s *Site) assembleTaxonomies() {
w := pp.getParamToLower(plural + "_weight")
weight, err := cast.ToIntE(w)
if err != nil {
s.Log.ERROR.Printf("Unable to convert taxonomy weight %#v to int for %s", w, pp.File.Path())
s.Log.ERROR.Printf("Unable to convert taxonomy weight %#v to int for %s", w, pp.File().Path())
// weight will equal zero, so let the flow continue
}

Expand All @@ -1559,7 +1559,7 @@ func (s *Site) assembleTaxonomies() {
s.taxonomiesOrigKey[fmt.Sprintf("%s-%s", plural, s.PathSpec.MakePathSanitized(v))] = v
}
} else {
s.Log.ERROR.Printf("Invalid %s in %s\n", plural, pp.File.Path())
s.Log.ERROR.Printf("Invalid %s in %s\n", plural, pp.File().Path())
}
}
}
Expand Down Expand Up @@ -1818,7 +1818,7 @@ func (s *Site) newNodePage(typ string, sections ...string) *Page {
pageInit: &pageInit{},
pageContentInit: &pageContentInit{},
kind: typ,
File: &source.FileInfo{},
sourceFile: &source.FileInfo{},
data: make(map[string]interface{}),
Site: &s.Info,
sections: sections,
Expand Down
2 changes: 1 addition & 1 deletion hugolib/site_render.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ func pageRenderer(s *Site, pages <-chan *Page, results chan<- error, wg *sync.Wa

s.Log.DEBUG.Printf("Render %s to %q with layouts %q", pageOutput.Kind(), targetPath, layouts)

if err := s.renderAndWritePage(&s.PathSpec.ProcessingStats.Pages, "page "+pageOutput.FullFilePath(), targetPath, pageOutput, layouts...); err != nil {
if err := s.renderAndWritePage(&s.PathSpec.ProcessingStats.Pages, "page "+pageOutput.File().Filename(), targetPath, pageOutput, layouts...); err != nil {
results <- err
}

Expand Down
6 changes: 3 additions & 3 deletions hugolib/site_sections_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ PAG|{{ .Title }}|{{ $sect.InSection . }}
b := getPage(p, "/empty3/b")
assert.NotNil(b)
assert.Len(b.Pages(), 1)
assert.Equal("empty3.md", b.Pages()[0].(*Page).File.LogicalName())
assert.Equal("empty3.md", b.Pages()[0].File().LogicalName())

}},
{"empty3", func(p page.Page) {
Expand All @@ -176,8 +176,8 @@ PAG|{{ .Title }}|{{ $sect.InSection . }}
{"top", func(p page.Page) {
assert.Equal("Tops", p.Title())
assert.Len(p.Pages(), 2)
assert.Equal("mypage2.md", p.Pages()[0].(*Page).LogicalName())
assert.Equal("mypage3.md", p.Pages()[1].(*Page).LogicalName())
assert.Equal("mypage2.md", p.Pages()[0].File().LogicalName())
assert.Equal("mypage3.md", p.Pages()[1].File().LogicalName())
home := p.Parent()
assert.True(home.IsHome())
assert.Len(p.Sections(), 0)
Expand Down
7 changes: 6 additions & 1 deletion resources/page/page.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,13 @@ type pageAddons6 interface {

Summary() template.HTML

Section() string
Dir() string
Path() string
UniqueID() string

// TODO(bep) page consider what to do.
source.File
File() source.File
}

//
Expand Down
Loading

0 comments on commit 47056a7

Please sign in to comment.