shithub: hugo

Download patch

ref: 1b2be5e878c0bd8c68731389e46bf1c6d3664635
parent: baa29f6534fcd324dbade7dd6c32c90547e3fa4f
author: Bjørn Erik Pedersen <[email protected]>
date: Tue Mar 21 20:25:55 EDT 2017

hugolib: Add OutputFormats with permalinks to Page

--- a/hugolib/page_output.go
+++ b/hugolib/page_output.go
@@ -15,6 +15,7 @@
 
 import (
 	"html/template"
+	"strings"
 	"sync"
 
 	"github.com/spf13/hugo/output"
@@ -108,4 +109,47 @@
 // TODO(bep) output
 func (p *Page) Render(layout ...string) template.HTML {
 	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)
 }
--- a/hugolib/page_paths.go
+++ b/hugolib/page_paths.go
@@ -219,15 +219,23 @@
 	}
 
 	// Choose the main output format. In most cases, this will be HTML.
-	outFormat := p.outputFormats[0]
-	tp, err := p.createTargetPath(outFormat)
+	f := p.outputFormats[0]
 
+	return p.createRelativePermalinkForOutputFormat(f)
+
+}
+
+func (p *Page) createRelativePermalinkForOutputFormat(f output.Format) string {
+	tp, err := p.createTargetPath(f)
+
 	if err != nil {
 		p.s.Log.ERROR.Printf("Failed to create permalink for page %q: %s", p.FullFilePath(), err)
 		return ""
 	}
-
-	tp = strings.TrimSuffix(tp, outFormat.BaseFilename())
+	// For /index.json etc. we must  use the full path.
+	if strings.HasSuffix(f.BaseFilename(), "html") {
+		tp = strings.TrimSuffix(tp, f.BaseFilename())
+	}
 
 	return p.s.PathSpec.URLizeFilename(tp)
 }
--- a/hugolib/site_output_test.go
+++ b/hugolib/site_output_test.go
@@ -65,7 +65,7 @@
 
 	pageTemplate := `---
 title: "%s"
-outputs: ["json"]
+outputs: ["html", "json"]
 ---
 # Doc
 `
@@ -88,10 +88,19 @@
 
 	require.NotNil(t, home)
 
-	require.Len(t, home.outputFormats, 1)
+	require.Len(t, home.outputFormats, 2)
 
 	// TODO(bep) output assert template/text
 
 	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())
 
 }
--- a/output/outputFormat.go
+++ b/output/outputFormat.go
@@ -23,6 +23,9 @@
 var (
 	// An ordered list of built-in output formats
 	// See https://www.ampproject.org/learn/overview/
+	// TODO
+	// <link rel="amphtml" href="{{ .Permalink }}">
+	// canonical
 	AMPType = Format{
 		Name:      "AMP",
 		MediaType: media.HTMLType,