github.com/anakojm/hugo-katex@v0.0.0-20231023141351-42d6f5de9c0b/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  	isUserConfigured := true
    70  	for _, d := range output.DefaultFormats {
    71  		if strings.EqualFold(d.Name, f.Name) {
    72  			isUserConfigured = false
    73  		}
    74  	}
    75  	rel := f.Rel
    76  	// If the output format is the canonical format for the content, we want
    77  	// to specify this in the "rel" attribute of an HTML "link" element.
    78  	// However, for custom output formats, we don't want to surprise users by
    79  	// overwriting "rel"
    80  	if isCanonical && !isUserConfigured {
    81  		rel = "canonical"
    82  	}
    83  	return OutputFormat{Rel: rel, Format: f, relPermalink: relPermalink, permalink: permalink}
    84  }
    85  
    86  // Get gets a OutputFormat given its name, i.e. json, html etc.
    87  // It returns nil if none found.
    88  func (o OutputFormats) Get(name string) *OutputFormat {
    89  	for _, f := range o {
    90  		if strings.EqualFold(f.Format.Name, name) {
    91  			return &f
    92  		}
    93  	}
    94  	return nil
    95  }