| 1 |
// Copyright 2019 The Hugo Authors. All rights reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
// |
| 8 |
// Unless required by applicable law or agreed to in writing, software |
| 9 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 |
// See the License for the specific language governing permissions and |
| 12 |
// limitations under the License. |
| 13 |
|
| 14 |
// Package page contains the core interfaces and types for the Page resource, |
| 15 |
// a core component in Hugo. |
| 16 |
package page |
| 17 |
|
| 18 |
import ( |
| 19 |
"strings" |
| 20 |
|
| 21 |
"github.com/gohugoio/hugo/common/types" |
| 22 |
"github.com/gohugoio/hugo/media" |
| 23 |
"github.com/gohugoio/hugo/output" |
| 24 |
) |
| 25 |
|
| 26 |
// OutputFormats holds a list of the relevant output formats for a given page. |
| 27 |
type OutputFormats []OutputFormat |
| 28 |
|
| 29 |
var _ types.Zeroer = OutputFormat{} |
| 30 |
|
| 31 |
// OutputFormat links to a representation of a resource. |
| 32 |
type OutputFormat struct { |
| 33 |
// Rel contains a value that can be used to construct a rel link. |
| 34 |
// This is value is fetched from the output format definition. |
| 35 |
// Note that for pages with only one output format, |
| 36 |
// this method will always return "canonical". |
| 37 |
// As an example, the AMP output format will, by default, return "amphtml". |
| 38 |
// |
| 39 |
// See: |
| 40 |
// https://www.ampproject.org/docs/guides/deploy/discovery |
| 41 |
// |
| 42 |
// Most other output formats will have "alternate" as value for this. |
| 43 |
Rel string |
| 44 |
|
| 45 |
Format output.Format |
| 46 |
|
| 47 |
relPermalink string |
| 48 |
permalink string |
| 49 |
} |
| 50 |
|
| 51 |
// Name returns this OutputFormat's name, i.e. HTML, AMP, JSON etc. |
| 52 |
func (o OutputFormat) Name() string { |
| 53 |
return o.Format.Name |
| 54 |
} |
| 55 |
|
| 56 |
// MediaType returns this OutputFormat's MediaType (MIME type). |
| 57 |
func (o OutputFormat) MediaType() media.Type { |
| 58 |
return o.Format.MediaType |
| 59 |
} |
| 60 |
|
| 61 |
// Permalink returns the absolute permalink to this output format. |
| 62 |
func (o OutputFormat) Permalink() string { |
| 63 |
return o.permalink |
| 64 |
} |
| 65 |
|
| 66 |
// RelPermalink returns the relative permalink to this output format. |
| 67 |
func (o OutputFormat) RelPermalink() string { |
| 68 |
return o.relPermalink |
| 69 |
} |
| 70 |
|
| 71 |
// IsZero checks whether this OutputFormat is the zero value. |
| 72 |
func (o OutputFormat) IsZero() bool { |
| 73 |
return o.Format.Name == "" |
| 74 |
} |
| 75 |
|
| 76 |
func NewOutputFormat(relPermalink, permalink string, isCanonical bool, f output.Format) OutputFormat { |
| 77 |
isUserConfigured := true |
| 78 |
for _, d := range output.DefaultFormats { |
| 79 |
if strings.EqualFold(d.Name, f.Name) { |
| 80 |
isUserConfigured = false |
| 81 |
} |
| 82 |
} |
| 83 |
rel := f.Rel |
| 84 |
// If the output format is the canonical format for the content, we want |
| 85 |
// to specify this in the "rel" attribute of an HTML "link" element. |
| 86 |
// However, for custom output formats, we don't want to surprise users by |
| 87 |
// overwriting "rel" |
| 88 |
if isCanonical && !isUserConfigured { |
| 89 |
rel = "canonical" |
| 90 |
} |
| 91 |
return OutputFormat{Rel: rel, Format: f, relPermalink: relPermalink, permalink: permalink} |
| 92 |
} |
| 93 |
|
| 94 |
// Get gets a OutputFormat given its name, i.e. json, html etc. |
| 95 |
// It returns a zero OutputFormat if not found. |
| 96 |
func (o OutputFormats) Get(name string) OutputFormat { |
| 97 |
for _, f := range o { |
| 98 |
if strings.EqualFold(f.Format.Name, name) { |
| 99 |
return f |
| 100 |
} |
| 101 |
} |
| 102 |
return OutputFormat{} |
| 103 |
} |
| 104 |
|
| 105 |
// Canonical returns the first canonical OutputFormat for this page, |
| 106 |
// or a zero OutputFormat if not found. |
| 107 |
func (o OutputFormats) Canonical() OutputFormat { |
| 108 |
const canonical = "canonical" |
| 109 |
for _, f := range o { |
| 110 |
if strings.EqualFold(f.Rel, canonical) { |
| 111 |
return f |
| 112 |
} |
| 113 |
} |
| 114 |
return OutputFormat{} |
| 115 |
} |