github.com/graemephi/kahugo@v0.62.3-0.20211121071557-d78c0423784d/resources/page/page_outputformat.go (about) 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/media" 22 "github.com/gohugoio/hugo/output" 23 ) 24 25 // OutputFormats holds a list of the relevant output formats for a given page. 26 type OutputFormats []OutputFormat 27 28 // OutputFormat links to a representation of a resource. 29 type OutputFormat struct { 30 // Rel contains a value that can be used to construct a rel link. 31 // This is value is fetched from the output format definition. 32 // Note that for pages with only one output format, 33 // this method will always return "canonical". 34 // As an example, the AMP output format will, by default, return "amphtml". 35 // 36 // See: 37 // https://www.ampproject.org/docs/guides/deploy/discovery 38 // 39 // Most other output formats will have "alternate" as value for this. 40 Rel string 41 42 Format output.Format 43 44 relPermalink string 45 permalink string 46 } 47 48 // Name returns this OutputFormat's name, i.e. HTML, AMP, JSON etc. 49 func (o OutputFormat) Name() string { 50 return o.Format.Name 51 } 52 53 // MediaType returns this OutputFormat's MediaType (MIME type). 54 func (o OutputFormat) MediaType() media.Type { 55 return o.Format.MediaType 56 } 57 58 // Permalink returns the absolute permalink to this output format. 59 func (o OutputFormat) Permalink() string { 60 return o.permalink 61 } 62 63 // RelPermalink returns the relative permalink to this output format. 64 func (o OutputFormat) RelPermalink() string { 65 return o.relPermalink 66 } 67 68 func NewOutputFormat(relPermalink, permalink string, isCanonical bool, f output.Format) OutputFormat { 69 rel := f.Rel 70 if isCanonical { 71 rel = "canonical" 72 } 73 return OutputFormat{Rel: rel, Format: f, relPermalink: relPermalink, permalink: permalink} 74 } 75 76 // Get gets a OutputFormat given its name, i.e. json, html etc. 77 // It returns nil if none found. 78 func (o OutputFormats) Get(name string) *OutputFormat { 79 for _, f := range o { 80 if strings.EqualFold(f.Format.Name, name) { 81 return &f 82 } 83 } 84 return nil 85 }