media

package
v0.136.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 15, 2024 License: Apache-2.0 Imports: 12 Imported by: 167

Documentation

Overview

Package media contains Media Type (MIME type) related types and functions.

Index

Constants

View Source
const (
	DefaultDelimiter = "."
)

Variables

View Source
var Builtin = BuiltinTypes{
	CalendarType:   Type{Type: "text/calendar"},
	CSSType:        Type{Type: "text/css"},
	SCSSType:       Type{Type: "text/x-scss"},
	SASSType:       Type{Type: "text/x-sass"},
	CSVType:        Type{Type: "text/csv"},
	HTMLType:       Type{Type: "text/html"},
	JavascriptType: Type{Type: "text/javascript"},
	TypeScriptType: Type{Type: "text/typescript"},
	TSXType:        Type{Type: "text/tsx"},
	JSXType:        Type{Type: "text/jsx"},

	JSONType:           Type{Type: "application/json"},
	WebAppManifestType: Type{Type: "application/manifest+json"},
	RSSType:            Type{Type: "application/rss+xml"},
	XMLType:            Type{Type: "application/xml"},
	SVGType:            Type{Type: "image/svg+xml"},
	TextType:           Type{Type: "text/plain"},
	TOMLType:           Type{Type: "application/toml"},
	YAMLType:           Type{Type: "application/yaml"},

	PNGType:  Type{Type: "image/png"},
	JPEGType: Type{Type: "image/jpeg"},
	GIFType:  Type{Type: "image/gif"},
	TIFFType: Type{Type: "image/tiff"},
	BMPType:  Type{Type: "image/bmp"},
	WEBPType: Type{Type: "image/webp"},

	TrueTypeFontType: Type{Type: "font/ttf"},
	OpenTypeFontType: Type{Type: "font/otf"},

	PDFType:              Type{Type: "application/pdf"},
	MarkdownType:         Type{Type: "text/markdown"},
	AsciiDocType:         Type{Type: "text/asciidoc"},
	PandocType:           Type{Type: "text/pandoc"},
	ReStructuredTextType: Type{Type: "text/rst"},
	EmacsOrgModeType:     Type{Type: "text/org"},

	AVIType:  Type{Type: "video/x-msvideo"},
	MPEGType: Type{Type: "video/mpeg"},
	MP4Type:  Type{Type: "video/mp4"},
	OGGType:  Type{Type: "video/ogg"},
	WEBMType: Type{Type: "video/webm"},
	GPPType:  Type{Type: "video/3gpp"},

	WasmType: Type{Type: "application/wasm"},

	OctetType: Type{Type: "application/octet-stream"},
}
View Source
var DefaultPathParser = &paths.PathParser{
	IsContentExt: func(ext string) bool {
		return DefaultContentTypes.IsContentSuffix(ext)
	},
}

TODO(bep) get rid of this.

Functions

func DecodeTypes

func DecodeTypes(in map[string]any) (*config.ConfigNamespace[map[string]MediaTypeConfig, Types], error)

DecodeTypes decodes the given map of media types.

func InitMediaType added in v0.112.0

func InitMediaType(m *Type)

Types

type BuiltinTypes added in v0.112.0

type BuiltinTypes struct {
	CalendarType   Type
	CSSType        Type
	SCSSType       Type
	SASSType       Type
	CSVType        Type
	HTMLType       Type
	JavascriptType Type
	TypeScriptType Type
	TSXType        Type
	JSXType        Type

	JSONType           Type
	WebAppManifestType Type
	RSSType            Type
	XMLType            Type
	SVGType            Type
	TextType           Type
	TOMLType           Type
	YAMLType           Type

	// Common image types
	PNGType  Type
	JPEGType Type
	GIFType  Type
	TIFFType Type
	BMPType  Type
	WEBPType Type

	// Common font types
	TrueTypeFontType Type
	OpenTypeFontType Type

	// Common document types
	PDFType              Type
	MarkdownType         Type
	EmacsOrgModeType     Type
	AsciiDocType         Type
	PandocType           Type
	ReStructuredTextType Type

	// Common video types
	AVIType  Type
	MPEGType Type
	MP4Type  Type
	OGGType  Type
	WEBMType Type
	GPPType  Type

	// wasm
	WasmType Type

	OctetType Type
}

type ContentTypes added in v0.126.0

type ContentTypes struct {
	HTML             Type
	Markdown         Type
	AsciiDoc         Type
	Pandoc           Type
	ReStructuredText Type
	EmacsOrgMode     Type
	// contains filtered or unexported fields
}

ContentTypes holds the media types that are considered content in Hugo.

var DefaultContentTypes ContentTypes

func (ContentTypes) FromTypes added in v0.126.0

func (t ContentTypes) FromTypes(types Types) ContentTypes

FromTypes creates a new ContentTypes updated with the values from the given Types.

func (ContentTypes) IsContentFile added in v0.126.0

func (t ContentTypes) IsContentFile(filename string) bool

IsContentFile returns whether the given filename is a content file.

func (ContentTypes) IsContentSuffix added in v0.126.0

func (t ContentTypes) IsContentSuffix(suffix string) bool

func (ContentTypes) IsHTMLSuffix added in v0.126.0

func (t ContentTypes) IsHTMLSuffix(suffix string) bool

IsHTMLSuffix returns whether the given suffix is a HTML media type.

func (ContentTypes) IsIndexContentFile added in v0.126.0

func (t ContentTypes) IsIndexContentFile(filename string) bool

IsIndexContentFile returns whether the given filename is an index content file.

func (ContentTypes) Types added in v0.126.0

func (t ContentTypes) Types() Types

Types is a slice of media types.

type MediaTypeConfig added in v0.112.0

type MediaTypeConfig struct {
	// The file suffixes used for this media type.
	Suffixes []string
	// Delimiter used before suffix.
	Delimiter string
}

Hold the configuration for a given media type.

type SuffixInfo added in v0.82.0

type SuffixInfo struct {
	// Suffix is the suffix without the delimiter, e.g. "xml".
	Suffix string `json:"suffix"`

	// FullSuffix is the suffix with the delimiter, e.g. ".xml".
	FullSuffix string `json:"fullSuffix"`
}

SuffixInfo holds information about a Media Type's suffix.

type Type

type Type struct {
	// The full MIME type string, e.g. "application/rss+xml".
	Type string `json:"-"`

	// The top-level type name, e.g. "application".
	MainType string `json:"mainType"`
	// The subtype name, e.g. "rss".
	SubType string `json:"subType"`
	// The delimiter before the suffix, e.g. ".".
	Delimiter string `json:"delimiter"`

	// FirstSuffix holds the first suffix defined for this MediaType.
	FirstSuffix SuffixInfo `json:"-"`

	// E.g. "jpg,jpeg"
	// Stored as a string to make Type comparable.
	// For internal use only.
	SuffixesCSV string `json:"-"`
	// contains filtered or unexported fields
}

MediaType (also known as MIME type and content type) is a two-part identifier for file formats and format contents transmitted on the Internet. For Hugo's use case, we use the top-level type name / subtype name + suffix. One example would be application/svg+xml If suffix is not provided, the sub type will be used. <docsmeta>{ "name": "MediaType" }</docsmeta>

func FromContent added in v0.91.0

func FromContent(types Types, extensionHints []string, content []byte) Type

FromContent resolve the Type primarily using http.DetectContentType. If http.DetectContentType resolves to application/octet-stream, a zero Type is returned. If http.DetectContentType resolves to text/plain or application/xml, we try to get more specific using types and ext.

func FromString

func FromString(t string) (Type, error)

FromString creates a new Type given a type string on the form MainType/SubType and an optional suffix, e.g. "text/html" or "text/html+html".

func FromStringAndExt added in v0.45.1

func FromStringAndExt(t string, ext ...string) (Type, error)

FromStringAndExt creates a Type from a MIME string and a given extensions

func (Type) IsHTML added in v0.126.0

func (m Type) IsHTML() bool

For internal use.

func (Type) IsMarkdown added in v0.126.0

func (m Type) IsMarkdown() bool

For internal use.

func (Type) IsText added in v0.91.0

func (m Type) IsText() bool

IsText returns whether this Type is a text format. Note that this may currently return false negatives. TODO(bep) improve For internal use.

func (Type) IsZero added in v0.74.3

func (m Type) IsZero() bool

IsZero reports whether this Type represents a zero value. For internal use.

func (Type) MarshalJSON

func (m Type) MarshalJSON() ([]byte, error)

MarshalJSON returns the JSON encoding of m. For internal use.

func (Type) String

func (m Type) String() string

For internal use.

func (Type) Suffixes added in v0.45.1

func (m Type) Suffixes() []string

Suffixes returns all valid file suffixes for this type.

type Types

type Types []Type

Types is a slice of media types. <docsmeta>{ "name": "MediaTypes" }</docsmeta>

var DefaultTypes Types

DefaultTypes is the default media types supported by Hugo.

func (Types) BySuffix added in v0.49.1

func (t Types) BySuffix(suffix string) []Type

BySuffix will return all media types matching a suffix.

func (Types) GetBestMatch added in v0.126.0

func (t Types) GetBestMatch(s string) (Type, bool)

GetBestMatch returns the best match for the given media type string.

func (Types) GetByMainSubType added in v0.45.1

func (t Types) GetByMainSubType(mainType, subType string) (tp Type, found bool)

GetByMainSubType gets a media type given a main and a sub type e.g. "text" and "plain". It will return false if no format could be found, or if the combination given is ambiguous. The lookup is case insensitive.

func (Types) GetBySubType added in v0.126.0

func (t Types) GetBySubType(subType string) (tp Type, found bool)

GetBySubType gets a media type given a sub type e.g. "plain".

func (Types) GetBySuffix

func (t Types) GetBySuffix(suffix string) (tp Type, si SuffixInfo, found bool)

GetBySuffix gets a media type given as suffix, e.g. "html". It will return false if no format could be found, or if the suffix given is ambiguous. The lookup is case insensitive.

func (Types) GetByType

func (t Types) GetByType(tp string) (Type, bool)

GetByType returns a media type for tp.

func (Types) GetFirstBySuffix added in v0.45.1

func (t Types) GetFirstBySuffix(suffix string) (Type, SuffixInfo, bool)

GetFirstBySuffix will return the first type matching the given suffix.

func (Types) IsTextSuffix added in v0.112.0

func (t Types) IsTextSuffix(suffix string) bool

func (Types) Len

func (t Types) Len() int

func (Types) Less

func (t Types) Less(i, j int) bool

func (Types) Swap

func (t Types) Swap(i, j int)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL