hugolib: Add OutputFormats with permalinks to Page
This commit is contained in:
parent
baa29f6534
commit
1b2be5e878
4 changed files with 70 additions and 6 deletions
|
@ -15,6 +15,7 @@ package hugolib
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"html/template"
|
"html/template"
|
||||||
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"github.com/spf13/hugo/output"
|
"github.com/spf13/hugo/output"
|
||||||
|
@ -109,3 +110,46 @@ func (p *PageOutput) Render(layout ...string) template.HTML {
|
||||||
func (p *Page) Render(layout ...string) template.HTML {
|
func (p *Page) Render(layout ...string) template.HTML {
|
||||||
return p.mainPageOutput.Render(layout...)
|
return p.mainPageOutput.Render(layout...)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// OutputFormats holds a list of the relevant output formats for a given resource.
|
||||||
|
type OutputFormats []*OutputFormat
|
||||||
|
|
||||||
|
// And OutputFormat links to a representation of a resource.
|
||||||
|
type OutputFormat struct {
|
||||||
|
f output.Format
|
||||||
|
p *Page
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO(bep) outputs consider just save this wrapper on Page.
|
||||||
|
// OutputFormats gives the output formats for this Page.
|
||||||
|
func (p *Page) OutputFormats() OutputFormats {
|
||||||
|
var o OutputFormats
|
||||||
|
for _, f := range p.outputFormats {
|
||||||
|
o = append(o, &OutputFormat{f: f, p: p})
|
||||||
|
}
|
||||||
|
return o
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get gets a OutputFormat given its name, i.e. json, html etc.
|
||||||
|
// It returns nil if not found.
|
||||||
|
func (o OutputFormats) Get(name string) *OutputFormat {
|
||||||
|
name = strings.ToLower(name)
|
||||||
|
for _, f := range o {
|
||||||
|
if strings.ToLower(f.f.Name) == name {
|
||||||
|
return f
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Permalink returns the absolute permalink to this output format.
|
||||||
|
func (o *OutputFormat) Permalink() string {
|
||||||
|
rel := o.p.createRelativePermalinkForOutputFormat(o.f)
|
||||||
|
return o.p.s.permalink(rel)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Permalink returns the relative permalink to this output format.
|
||||||
|
func (o *OutputFormat) RelPermalink() string {
|
||||||
|
rel := o.p.createRelativePermalinkForOutputFormat(o.f)
|
||||||
|
return o.p.s.PathSpec.PrependBasePath(rel)
|
||||||
|
}
|
||||||
|
|
|
@ -219,15 +219,23 @@ func (p *Page) createRelativePermalink() string {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Choose the main output format. In most cases, this will be HTML.
|
// Choose the main output format. In most cases, this will be HTML.
|
||||||
outFormat := p.outputFormats[0]
|
f := p.outputFormats[0]
|
||||||
tp, err := p.createTargetPath(outFormat)
|
|
||||||
|
return p.createRelativePermalinkForOutputFormat(f)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Page) createRelativePermalinkForOutputFormat(f output.Format) string {
|
||||||
|
tp, err := p.createTargetPath(f)
|
||||||
|
|
||||||
if err != nil {
|
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.FullFilePath(), err)
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
// For /index.json etc. we must use the full path.
|
||||||
tp = strings.TrimSuffix(tp, outFormat.BaseFilename())
|
if strings.HasSuffix(f.BaseFilename(), "html") {
|
||||||
|
tp = strings.TrimSuffix(tp, f.BaseFilename())
|
||||||
|
}
|
||||||
|
|
||||||
return p.s.PathSpec.URLizeFilename(tp)
|
return p.s.PathSpec.URLizeFilename(tp)
|
||||||
}
|
}
|
||||||
|
|
|
@ -65,7 +65,7 @@ category = "categories"
|
||||||
|
|
||||||
pageTemplate := `---
|
pageTemplate := `---
|
||||||
title: "%s"
|
title: "%s"
|
||||||
outputs: ["json"]
|
outputs: ["html", "json"]
|
||||||
---
|
---
|
||||||
# Doc
|
# Doc
|
||||||
`
|
`
|
||||||
|
@ -88,10 +88,19 @@ outputs: ["json"]
|
||||||
|
|
||||||
require.NotNil(t, home)
|
require.NotNil(t, home)
|
||||||
|
|
||||||
require.Len(t, home.outputFormats, 1)
|
require.Len(t, home.outputFormats, 2)
|
||||||
|
|
||||||
// TODO(bep) output assert template/text
|
// TODO(bep) output assert template/text
|
||||||
|
|
||||||
th.assertFileContent("public/index.json", "List JSON")
|
th.assertFileContent("public/index.json", "List JSON")
|
||||||
|
|
||||||
|
of := home.OutputFormats()
|
||||||
|
require.Len(t, of, 2)
|
||||||
|
require.Nil(t, of.Get("Hugo"))
|
||||||
|
require.NotNil(t, of.Get("json"))
|
||||||
|
json := of.Get("JSON")
|
||||||
|
require.NotNil(t, json)
|
||||||
|
require.Equal(t, "/blog/index.json", json.RelPermalink())
|
||||||
|
require.Equal(t, "http://example.com/blog/index.json", json.Permalink())
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,6 +23,9 @@ import (
|
||||||
var (
|
var (
|
||||||
// An ordered list of built-in output formats
|
// An ordered list of built-in output formats
|
||||||
// See https://www.ampproject.org/learn/overview/
|
// See https://www.ampproject.org/learn/overview/
|
||||||
|
// TODO
|
||||||
|
// <link rel="amphtml" href="{{ .Permalink }}">
|
||||||
|
// canonical
|
||||||
AMPType = Format{
|
AMPType = Format{
|
||||||
Name: "AMP",
|
Name: "AMP",
|
||||||
MediaType: media.HTMLType,
|
MediaType: media.HTMLType,
|
||||||
|
|
Loading…
Add table
Reference in a new issue