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 6aa727e
Show file tree
Hide file tree
Showing 17 changed files with 76 additions and 90 deletions.
8 changes: 4 additions & 4 deletions commands/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,16 +141,16 @@ func (cc *convertCmd) convertAndSavePage(p *hugolib.Page, site *hugolib.Site, ta
}
}

if p.Filename() == "" {
if p.File().Filename() == "" {
// No content file.
return nil
}

errMsg := fmt.Errorf("Error processing file %q", p.Path())

site.Log.INFO.Println("Attempting to convert", p.LogicalName())
site.Log.INFO.Println("Attempting to convert", p.File().Filename())

f, _ := p.File.(src.ReadableFile)
f, _ := p.File().(src.ReadableFile)
file, err := f.Open()
if err != nil {
site.Log.ERROR.Println(errMsg)
Expand Down Expand Up @@ -186,7 +186,7 @@ func (cc *convertCmd) convertAndSavePage(p *hugolib.Page, site *hugolib.Site, ta

newContent.Write(pf.content)

newFilename := p.Filename()
newFilename := p.File().Filename()

if cc.outputDir != "" {
contentDir := strings.TrimSuffix(newFilename, p.Path())
Expand Down
6 changes: 3 additions & 3 deletions commands/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ List requires a subcommand, e.g. ` + "`hugo list drafts`.",
for _, p := range sites.Pages() {
pp := p.(*hugolib.Page)
if pp.IsDraft() {
jww.FEEDBACK.Println(filepath.Join(pp.File.Dir(), pp.File.LogicalName()))
jww.FEEDBACK.Println(filepath.Join(pp.File().Dir(), pp.File().LogicalName()))
}

}
Expand Down Expand Up @@ -106,7 +106,7 @@ posted in the future.`,
for _, p := range sites.Pages() {
if resource.IsFuture(p) {
pp := p.(*hugolib.Page)
jww.FEEDBACK.Println(filepath.Join(pp.File.Dir(), pp.File.LogicalName()))
jww.FEEDBACK.Println(filepath.Join(pp.File().Dir(), pp.File().LogicalName()))
}

}
Expand Down Expand Up @@ -143,7 +143,7 @@ expired.`,
for _, p := range sites.Pages() {
if resource.IsExpired(p) {
pp := p.(*hugolib.Page)
jww.FEEDBACK.Println(filepath.Join(pp.File.Dir(), pp.File.LogicalName()))
jww.FEEDBACK.Println(filepath.Join(pp.File().Dir(), pp.File().LogicalName()))
}

}
Expand Down
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
Loading

0 comments on commit 6aa727e

Please sign in to comment.