2019-01-02 06:33:26 -05:00
|
|
|
// Copyright 2019 The Hugo Authors. All rights reserved.
|
2017-03-02 09:35:25 -05:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package media
|
|
|
|
|
|
|
|
import (
|
2017-04-05 10:18:53 -04:00
|
|
|
"encoding/json"
|
2018-08-28 08:18:12 -04:00
|
|
|
"errors"
|
2017-03-02 09:35:25 -05:00
|
|
|
"fmt"
|
2017-04-03 16:39:37 -04:00
|
|
|
"sort"
|
2017-04-03 11:00:23 -04:00
|
|
|
"strings"
|
2017-03-02 09:35:25 -05:00
|
|
|
|
2021-03-11 03:18:01 -05:00
|
|
|
"github.com/spf13/cast"
|
|
|
|
|
2018-08-28 08:18:12 -04:00
|
|
|
"github.com/gohugoio/hugo/common/maps"
|
|
|
|
|
2017-04-03 16:39:37 -04:00
|
|
|
"github.com/mitchellh/mapstructure"
|
|
|
|
)
|
2017-04-03 11:00:23 -04:00
|
|
|
|
2017-06-20 02:45:52 -04:00
|
|
|
const (
|
|
|
|
defaultDelimiter = "."
|
|
|
|
)
|
|
|
|
|
2017-08-02 08:25:05 -04:00
|
|
|
// Type (also known as MIME type and content type) is a two-part identifier for
|
2017-03-02 09:35:25 -05:00
|
|
|
// file formats and format contents transmitted on the Internet.
|
|
|
|
// For Hugo's use case, we use the top-level type name / subtype name + suffix.
|
2018-07-10 05:55:22 -04:00
|
|
|
// One example would be application/svg+xml
|
2017-03-02 09:35:25 -05:00
|
|
|
// If suffix is not provided, the sub type will be used.
|
|
|
|
// See // https://en.wikipedia.org/wiki/Media_type
|
|
|
|
type Type struct {
|
2021-03-11 03:18:01 -05:00
|
|
|
MainType string `json:"mainType"` // i.e. text
|
|
|
|
SubType string `json:"subType"` // i.e. html
|
|
|
|
Delimiter string `json:"delimiter"` // e.g. "."
|
|
|
|
|
|
|
|
// FirstSuffix holds the first suffix defined for this Type.
|
|
|
|
FirstSuffix SuffixInfo `json:"firstSuffix"`
|
2018-07-10 05:55:22 -04:00
|
|
|
|
2018-08-28 08:18:12 -04:00
|
|
|
// This is the optional suffix after the "+" in the MIME type,
|
2020-12-16 06:11:32 -05:00
|
|
|
// e.g. "xml" in "application/rss+xml".
|
2018-08-28 08:18:12 -04:00
|
|
|
mimeSuffix string
|
2018-07-10 05:55:22 -04:00
|
|
|
|
2021-03-11 03:18:01 -05:00
|
|
|
// E.g. "jpg,jpeg"
|
|
|
|
// Stored as a string to make Type comparable.
|
|
|
|
suffixesCSV string
|
|
|
|
}
|
2018-07-10 05:55:22 -04:00
|
|
|
|
2021-03-11 03:18:01 -05:00
|
|
|
// SuffixInfo holds information about a Type's suffix.
|
|
|
|
type SuffixInfo struct {
|
|
|
|
Suffix string `json:"suffix"`
|
|
|
|
FullSuffix string `json:"fullSuffix"`
|
2017-03-02 09:35:25 -05:00
|
|
|
}
|
|
|
|
|
2021-03-11 03:18:01 -05:00
|
|
|
// FromStringAndExt creates a Type from a MIME string and a given extension.
|
2018-07-10 05:55:22 -04:00
|
|
|
func FromStringAndExt(t, ext string) (Type, error) {
|
|
|
|
tp, err := fromString(t)
|
|
|
|
if err != nil {
|
|
|
|
return tp, err
|
|
|
|
}
|
2021-03-11 03:18:01 -05:00
|
|
|
tp.suffixesCSV = strings.TrimPrefix(ext, ".")
|
|
|
|
tp.Delimiter = defaultDelimiter
|
|
|
|
tp.init()
|
2018-07-10 05:55:22 -04:00
|
|
|
return tp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// FromString creates a new Type given a type string on the form MainType/SubType and
|
2017-04-03 16:39:37 -04:00
|
|
|
// an optional suffix, e.g. "text/html" or "text/html+html".
|
2018-07-10 05:55:22 -04:00
|
|
|
func fromString(t string) (Type, error) {
|
2017-04-03 16:39:37 -04:00
|
|
|
t = strings.ToLower(t)
|
|
|
|
parts := strings.Split(t, "/")
|
|
|
|
if len(parts) != 2 {
|
|
|
|
return Type{}, fmt.Errorf("cannot parse %q as a media type", t)
|
|
|
|
}
|
|
|
|
mainType := parts[0]
|
|
|
|
subParts := strings.Split(parts[1], "+")
|
|
|
|
|
Add Hugo Piper with SCSS support and much more
Before this commit, you would have to use page bundles to do image processing etc. in Hugo.
This commit adds
* A new `/assets` top-level project or theme dir (configurable via `assetDir`)
* A new template func, `resources.Get` which can be used to "get a resource" that can be further processed.
This means that you can now do this in your templates (or shortcodes):
```bash
{{ $sunset := (resources.Get "images/sunset.jpg").Fill "300x200" }}
```
This also adds a new `extended` build tag that enables powerful SCSS/SASS support with source maps. To compile this from source, you will also need a C compiler installed:
```
HUGO_BUILD_TAGS=extended mage install
```
Note that you can use output of the SCSS processing later in a non-SCSSS-enabled Hugo.
The `SCSS` processor is a _Resource transformation step_ and it can be chained with the many others in a pipeline:
```bash
{{ $css := resources.Get "styles.scss" | resources.ToCSS | resources.PostCSS | resources.Minify | resources.Fingerprint }}
<link rel="stylesheet" href="{{ $styles.RelPermalink }}" integrity="{{ $styles.Data.Digest }}" media="screen">
```
The transformation funcs above have aliases, so it can be shortened to:
```bash
{{ $css := resources.Get "styles.scss" | toCSS | postCSS | minify | fingerprint }}
<link rel="stylesheet" href="{{ $styles.RelPermalink }}" integrity="{{ $styles.Data.Digest }}" media="screen">
```
A quick tip would be to avoid the fingerprinting part, and possibly also the not-superfast `postCSS` when you're doing development, as it allows Hugo to be smarter about the rebuilding.
Documentation will follow, but have a look at the demo repo in https://github.com/bep/hugo-sass-test
New functions to create `Resource` objects:
* `resources.Get` (see above)
* `resources.FromString`: Create a Resource from a string.
New `Resource` transformation funcs:
* `resources.ToCSS`: Compile `SCSS` or `SASS` into `CSS`.
* `resources.PostCSS`: Process your CSS with PostCSS. Config file support (project or theme or passed as an option).
* `resources.Minify`: Currently supports `css`, `js`, `json`, `html`, `svg`, `xml`.
* `resources.Fingerprint`: Creates a fingerprinted version of the given Resource with Subresource Integrity..
* `resources.Concat`: Concatenates a list of Resource objects. Think of this as a poor man's bundler.
* `resources.ExecuteAsTemplate`: Parses and executes the given Resource and data context (e.g. .Site) as a Go template.
Fixes #4381
Fixes #4903
Fixes #4858
2018-02-20 04:02:14 -05:00
|
|
|
subType := strings.Split(subParts[0], ";")[0]
|
|
|
|
|
2017-04-03 16:39:37 -04:00
|
|
|
var suffix string
|
|
|
|
|
2018-07-10 05:55:22 -04:00
|
|
|
if len(subParts) > 1 {
|
2017-04-03 16:39:37 -04:00
|
|
|
suffix = subParts[1]
|
|
|
|
}
|
|
|
|
|
2018-08-28 08:18:12 -04:00
|
|
|
return Type{MainType: mainType, SubType: subType, mimeSuffix: suffix}, nil
|
2017-04-03 16:39:37 -04:00
|
|
|
}
|
|
|
|
|
2018-07-10 05:55:22 -04:00
|
|
|
// Type returns a string representing the main- and sub-type of a media type, e.g. "text/css".
|
|
|
|
// A suffix identifier will be appended after a "+" if set, e.g. "image/svg+xml".
|
2017-03-22 06:03:42 -04:00
|
|
|
// Hugo will register a set of default media types.
|
|
|
|
// These can be overridden by the user in the configuration,
|
|
|
|
// by defining a media type with the same Type.
|
|
|
|
func (m Type) Type() string {
|
2018-07-10 05:55:22 -04:00
|
|
|
// Examples are
|
|
|
|
// image/svg+xml
|
|
|
|
// text/css
|
2018-08-28 08:18:12 -04:00
|
|
|
if m.mimeSuffix != "" {
|
2019-10-06 15:22:10 -04:00
|
|
|
return m.MainType + "/" + m.SubType + "+" + m.mimeSuffix
|
2018-07-10 05:55:22 -04:00
|
|
|
}
|
2019-10-06 15:22:10 -04:00
|
|
|
return m.MainType + "/" + m.SubType
|
2017-03-02 09:35:25 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m Type) String() string {
|
2018-07-10 05:55:22 -04:00
|
|
|
return m.Type()
|
2017-03-02 09:35:25 -05:00
|
|
|
}
|
|
|
|
|
2021-03-11 03:18:01 -05:00
|
|
|
// Suffixes returns all valid file suffixes for this type.
|
|
|
|
func (m Type) Suffixes() []string {
|
|
|
|
if m.suffixesCSV == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return strings.Split(m.suffixesCSV, ",")
|
2018-07-10 05:55:22 -04:00
|
|
|
}
|
|
|
|
|
2021-03-11 03:18:01 -05:00
|
|
|
func (m *Type) init() {
|
|
|
|
m.FirstSuffix.FullSuffix = ""
|
|
|
|
m.FirstSuffix.Suffix = ""
|
|
|
|
if suffixes := m.Suffixes(); suffixes != nil {
|
|
|
|
m.FirstSuffix.Suffix = suffixes[0]
|
|
|
|
m.FirstSuffix.FullSuffix = m.Delimiter + m.FirstSuffix.Suffix
|
2018-07-10 05:55:22 -04:00
|
|
|
}
|
2021-03-11 03:18:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// WithDelimiterAndSuffixes is used in tests.
|
|
|
|
func WithDelimiterAndSuffixes(t Type, delimiter, suffixesCSV string) Type {
|
|
|
|
t.Delimiter = delimiter
|
|
|
|
t.suffixesCSV = suffixesCSV
|
|
|
|
t.init()
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
|
|
|
func newMediaType(main, sub string, suffixes []string) Type {
|
|
|
|
t := Type{MainType: main, SubType: sub, suffixesCSV: strings.Join(suffixes, ","), Delimiter: defaultDelimiter}
|
|
|
|
t.init()
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
|
|
|
func newMediaTypeWithMimeSuffix(main, sub, mimeSuffix string, suffixes []string) Type {
|
|
|
|
mt := newMediaType(main, sub, suffixes)
|
|
|
|
mt.mimeSuffix = mimeSuffix
|
|
|
|
mt.init()
|
|
|
|
return mt
|
2017-06-20 02:45:52 -04:00
|
|
|
}
|
|
|
|
|
2018-09-06 18:24:23 -04:00
|
|
|
// Definitions from https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types etc.
|
|
|
|
// Note that from Hugo 0.44 we only set Suffix if it is part of the MIME type.
|
2017-03-02 09:35:25 -05:00
|
|
|
var (
|
2021-03-11 03:18:01 -05:00
|
|
|
CalendarType = newMediaType("text", "calendar", []string{"ics"})
|
|
|
|
CSSType = newMediaType("text", "css", []string{"css"})
|
|
|
|
SCSSType = newMediaType("text", "x-scss", []string{"scss"})
|
|
|
|
SASSType = newMediaType("text", "x-sass", []string{"sass"})
|
|
|
|
CSVType = newMediaType("text", "csv", []string{"csv"})
|
|
|
|
HTMLType = newMediaType("text", "html", []string{"html"})
|
|
|
|
JavascriptType = newMediaType("application", "javascript", []string{"js"})
|
|
|
|
TypeScriptType = newMediaType("application", "typescript", []string{"ts"})
|
|
|
|
TSXType = newMediaType("text", "tsx", []string{"tsx"})
|
|
|
|
JSXType = newMediaType("text", "jsx", []string{"jsx"})
|
|
|
|
|
|
|
|
JSONType = newMediaType("application", "json", []string{"json"})
|
|
|
|
RSSType = newMediaTypeWithMimeSuffix("application", "rss", "xml", []string{"xml"})
|
|
|
|
XMLType = newMediaType("application", "xml", []string{"xml"})
|
|
|
|
SVGType = newMediaTypeWithMimeSuffix("image", "svg", "xml", []string{"svg"})
|
|
|
|
TextType = newMediaType("text", "plain", []string{"txt"})
|
|
|
|
TOMLType = newMediaType("application", "toml", []string{"toml"})
|
|
|
|
YAMLType = newMediaType("application", "yaml", []string{"yaml", "yml"})
|
2018-07-10 05:55:22 -04:00
|
|
|
|
2019-01-02 06:33:26 -05:00
|
|
|
// Common image types
|
2021-03-11 03:18:01 -05:00
|
|
|
PNGType = newMediaType("image", "png", []string{"png"})
|
|
|
|
JPEGType = newMediaType("image", "jpeg", []string{"jpg", "jpeg"})
|
|
|
|
GIFType = newMediaType("image", "gif", []string{"gif"})
|
|
|
|
TIFFType = newMediaType("image", "tiff", []string{"tif", "tiff"})
|
|
|
|
BMPType = newMediaType("image", "bmp", []string{"bmp"})
|
2019-01-02 06:33:26 -05:00
|
|
|
|
2019-10-16 09:10:32 -04:00
|
|
|
// Common video types
|
2021-03-11 03:18:01 -05:00
|
|
|
AVIType = newMediaType("video", "x-msvideo", []string{"avi"})
|
|
|
|
MPEGType = newMediaType("video", "mpeg", []string{"mpg", "mpeg"})
|
|
|
|
MP4Type = newMediaType("video", "mp4", []string{"mp4"})
|
|
|
|
OGGType = newMediaType("video", "ogg", []string{"ogv"})
|
|
|
|
WEBMType = newMediaType("video", "webm", []string{"webm"})
|
|
|
|
GPPType = newMediaType("video", "3gpp", []string{"3gpp", "3gp"})
|
|
|
|
|
|
|
|
OctetType = newMediaType("application", "octet-stream", nil)
|
2017-03-02 09:35:25 -05:00
|
|
|
)
|
|
|
|
|
2018-09-06 18:24:23 -04:00
|
|
|
// DefaultTypes is the default media types supported by Hugo.
|
2017-04-03 16:39:37 -04:00
|
|
|
var DefaultTypes = Types{
|
|
|
|
CalendarType,
|
|
|
|
CSSType,
|
|
|
|
CSVType,
|
Add Hugo Piper with SCSS support and much more
Before this commit, you would have to use page bundles to do image processing etc. in Hugo.
This commit adds
* A new `/assets` top-level project or theme dir (configurable via `assetDir`)
* A new template func, `resources.Get` which can be used to "get a resource" that can be further processed.
This means that you can now do this in your templates (or shortcodes):
```bash
{{ $sunset := (resources.Get "images/sunset.jpg").Fill "300x200" }}
```
This also adds a new `extended` build tag that enables powerful SCSS/SASS support with source maps. To compile this from source, you will also need a C compiler installed:
```
HUGO_BUILD_TAGS=extended mage install
```
Note that you can use output of the SCSS processing later in a non-SCSSS-enabled Hugo.
The `SCSS` processor is a _Resource transformation step_ and it can be chained with the many others in a pipeline:
```bash
{{ $css := resources.Get "styles.scss" | resources.ToCSS | resources.PostCSS | resources.Minify | resources.Fingerprint }}
<link rel="stylesheet" href="{{ $styles.RelPermalink }}" integrity="{{ $styles.Data.Digest }}" media="screen">
```
The transformation funcs above have aliases, so it can be shortened to:
```bash
{{ $css := resources.Get "styles.scss" | toCSS | postCSS | minify | fingerprint }}
<link rel="stylesheet" href="{{ $styles.RelPermalink }}" integrity="{{ $styles.Data.Digest }}" media="screen">
```
A quick tip would be to avoid the fingerprinting part, and possibly also the not-superfast `postCSS` when you're doing development, as it allows Hugo to be smarter about the rebuilding.
Documentation will follow, but have a look at the demo repo in https://github.com/bep/hugo-sass-test
New functions to create `Resource` objects:
* `resources.Get` (see above)
* `resources.FromString`: Create a Resource from a string.
New `Resource` transformation funcs:
* `resources.ToCSS`: Compile `SCSS` or `SASS` into `CSS`.
* `resources.PostCSS`: Process your CSS with PostCSS. Config file support (project or theme or passed as an option).
* `resources.Minify`: Currently supports `css`, `js`, `json`, `html`, `svg`, `xml`.
* `resources.Fingerprint`: Creates a fingerprinted version of the given Resource with Subresource Integrity..
* `resources.Concat`: Concatenates a list of Resource objects. Think of this as a poor man's bundler.
* `resources.ExecuteAsTemplate`: Parses and executes the given Resource and data context (e.g. .Site) as a Go template.
Fixes #4381
Fixes #4903
Fixes #4858
2018-02-20 04:02:14 -05:00
|
|
|
SCSSType,
|
|
|
|
SASSType,
|
2017-04-03 16:39:37 -04:00
|
|
|
HTMLType,
|
|
|
|
JavascriptType,
|
2020-07-12 06:47:14 -04:00
|
|
|
TypeScriptType,
|
|
|
|
TSXType,
|
|
|
|
JSXType,
|
2017-04-03 16:39:37 -04:00
|
|
|
JSONType,
|
|
|
|
RSSType,
|
|
|
|
XMLType,
|
Add Hugo Piper with SCSS support and much more
Before this commit, you would have to use page bundles to do image processing etc. in Hugo.
This commit adds
* A new `/assets` top-level project or theme dir (configurable via `assetDir`)
* A new template func, `resources.Get` which can be used to "get a resource" that can be further processed.
This means that you can now do this in your templates (or shortcodes):
```bash
{{ $sunset := (resources.Get "images/sunset.jpg").Fill "300x200" }}
```
This also adds a new `extended` build tag that enables powerful SCSS/SASS support with source maps. To compile this from source, you will also need a C compiler installed:
```
HUGO_BUILD_TAGS=extended mage install
```
Note that you can use output of the SCSS processing later in a non-SCSSS-enabled Hugo.
The `SCSS` processor is a _Resource transformation step_ and it can be chained with the many others in a pipeline:
```bash
{{ $css := resources.Get "styles.scss" | resources.ToCSS | resources.PostCSS | resources.Minify | resources.Fingerprint }}
<link rel="stylesheet" href="{{ $styles.RelPermalink }}" integrity="{{ $styles.Data.Digest }}" media="screen">
```
The transformation funcs above have aliases, so it can be shortened to:
```bash
{{ $css := resources.Get "styles.scss" | toCSS | postCSS | minify | fingerprint }}
<link rel="stylesheet" href="{{ $styles.RelPermalink }}" integrity="{{ $styles.Data.Digest }}" media="screen">
```
A quick tip would be to avoid the fingerprinting part, and possibly also the not-superfast `postCSS` when you're doing development, as it allows Hugo to be smarter about the rebuilding.
Documentation will follow, but have a look at the demo repo in https://github.com/bep/hugo-sass-test
New functions to create `Resource` objects:
* `resources.Get` (see above)
* `resources.FromString`: Create a Resource from a string.
New `Resource` transformation funcs:
* `resources.ToCSS`: Compile `SCSS` or `SASS` into `CSS`.
* `resources.PostCSS`: Process your CSS with PostCSS. Config file support (project or theme or passed as an option).
* `resources.Minify`: Currently supports `css`, `js`, `json`, `html`, `svg`, `xml`.
* `resources.Fingerprint`: Creates a fingerprinted version of the given Resource with Subresource Integrity..
* `resources.Concat`: Concatenates a list of Resource objects. Think of this as a poor man's bundler.
* `resources.ExecuteAsTemplate`: Parses and executes the given Resource and data context (e.g. .Site) as a Go template.
Fixes #4381
Fixes #4903
Fixes #4858
2018-02-20 04:02:14 -05:00
|
|
|
SVGType,
|
2017-04-03 16:39:37 -04:00
|
|
|
TextType,
|
Add Hugo Piper with SCSS support and much more
Before this commit, you would have to use page bundles to do image processing etc. in Hugo.
This commit adds
* A new `/assets` top-level project or theme dir (configurable via `assetDir`)
* A new template func, `resources.Get` which can be used to "get a resource" that can be further processed.
This means that you can now do this in your templates (or shortcodes):
```bash
{{ $sunset := (resources.Get "images/sunset.jpg").Fill "300x200" }}
```
This also adds a new `extended` build tag that enables powerful SCSS/SASS support with source maps. To compile this from source, you will also need a C compiler installed:
```
HUGO_BUILD_TAGS=extended mage install
```
Note that you can use output of the SCSS processing later in a non-SCSSS-enabled Hugo.
The `SCSS` processor is a _Resource transformation step_ and it can be chained with the many others in a pipeline:
```bash
{{ $css := resources.Get "styles.scss" | resources.ToCSS | resources.PostCSS | resources.Minify | resources.Fingerprint }}
<link rel="stylesheet" href="{{ $styles.RelPermalink }}" integrity="{{ $styles.Data.Digest }}" media="screen">
```
The transformation funcs above have aliases, so it can be shortened to:
```bash
{{ $css := resources.Get "styles.scss" | toCSS | postCSS | minify | fingerprint }}
<link rel="stylesheet" href="{{ $styles.RelPermalink }}" integrity="{{ $styles.Data.Digest }}" media="screen">
```
A quick tip would be to avoid the fingerprinting part, and possibly also the not-superfast `postCSS` when you're doing development, as it allows Hugo to be smarter about the rebuilding.
Documentation will follow, but have a look at the demo repo in https://github.com/bep/hugo-sass-test
New functions to create `Resource` objects:
* `resources.Get` (see above)
* `resources.FromString`: Create a Resource from a string.
New `Resource` transformation funcs:
* `resources.ToCSS`: Compile `SCSS` or `SASS` into `CSS`.
* `resources.PostCSS`: Process your CSS with PostCSS. Config file support (project or theme or passed as an option).
* `resources.Minify`: Currently supports `css`, `js`, `json`, `html`, `svg`, `xml`.
* `resources.Fingerprint`: Creates a fingerprinted version of the given Resource with Subresource Integrity..
* `resources.Concat`: Concatenates a list of Resource objects. Think of this as a poor man's bundler.
* `resources.ExecuteAsTemplate`: Parses and executes the given Resource and data context (e.g. .Site) as a Go template.
Fixes #4381
Fixes #4903
Fixes #4858
2018-02-20 04:02:14 -05:00
|
|
|
OctetType,
|
2018-12-21 10:21:13 -05:00
|
|
|
YAMLType,
|
|
|
|
TOMLType,
|
2019-01-02 06:33:26 -05:00
|
|
|
PNGType,
|
2019-12-19 09:04:18 -05:00
|
|
|
JPEGType,
|
2019-10-16 09:10:32 -04:00
|
|
|
AVIType,
|
|
|
|
MPEGType,
|
|
|
|
MP4Type,
|
|
|
|
OGGType,
|
|
|
|
WEBMType,
|
|
|
|
GPPType,
|
2017-04-03 16:39:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
sort.Sort(DefaultTypes)
|
|
|
|
}
|
|
|
|
|
2018-09-06 18:24:23 -04:00
|
|
|
// Types is a slice of media types.
|
2017-04-03 16:39:37 -04:00
|
|
|
type Types []Type
|
|
|
|
|
|
|
|
func (t Types) Len() int { return len(t) }
|
|
|
|
func (t Types) Swap(i, j int) { t[i], t[j] = t[j], t[i] }
|
|
|
|
func (t Types) Less(i, j int) bool { return t[i].Type() < t[j].Type() }
|
|
|
|
|
2018-09-06 18:24:23 -04:00
|
|
|
// GetByType returns a media type for tp.
|
2017-04-03 16:39:37 -04:00
|
|
|
func (t Types) GetByType(tp string) (Type, bool) {
|
|
|
|
for _, tt := range t {
|
|
|
|
if strings.EqualFold(tt.Type(), tp) {
|
|
|
|
return tt, true
|
|
|
|
}
|
|
|
|
}
|
2018-07-10 05:55:22 -04:00
|
|
|
|
|
|
|
if !strings.Contains(tp, "+") {
|
|
|
|
// Try with the main and sub type
|
|
|
|
parts := strings.Split(tp, "/")
|
|
|
|
if len(parts) == 2 {
|
|
|
|
return t.GetByMainSubType(parts[0], parts[1])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-03 16:39:37 -04:00
|
|
|
return Type{}, false
|
|
|
|
}
|
|
|
|
|
2018-08-28 08:18:12 -04:00
|
|
|
// BySuffix will return all media types matching a suffix.
|
|
|
|
func (t Types) BySuffix(suffix string) []Type {
|
2021-03-11 03:18:01 -05:00
|
|
|
suffix = strings.ToLower(suffix)
|
2018-08-28 08:18:12 -04:00
|
|
|
var types []Type
|
|
|
|
for _, tt := range t {
|
2021-03-11 03:18:01 -05:00
|
|
|
if tt.hasSuffix(suffix) {
|
2018-08-28 08:18:12 -04:00
|
|
|
types = append(types, tt)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return types
|
|
|
|
}
|
|
|
|
|
2021-03-11 03:18:01 -05:00
|
|
|
// GetFirstBySuffix will return the first type matching the given suffix.
|
|
|
|
func (t Types) GetFirstBySuffix(suffix string) (Type, SuffixInfo, bool) {
|
|
|
|
suffix = strings.ToLower(suffix)
|
Add Hugo Piper with SCSS support and much more
Before this commit, you would have to use page bundles to do image processing etc. in Hugo.
This commit adds
* A new `/assets` top-level project or theme dir (configurable via `assetDir`)
* A new template func, `resources.Get` which can be used to "get a resource" that can be further processed.
This means that you can now do this in your templates (or shortcodes):
```bash
{{ $sunset := (resources.Get "images/sunset.jpg").Fill "300x200" }}
```
This also adds a new `extended` build tag that enables powerful SCSS/SASS support with source maps. To compile this from source, you will also need a C compiler installed:
```
HUGO_BUILD_TAGS=extended mage install
```
Note that you can use output of the SCSS processing later in a non-SCSSS-enabled Hugo.
The `SCSS` processor is a _Resource transformation step_ and it can be chained with the many others in a pipeline:
```bash
{{ $css := resources.Get "styles.scss" | resources.ToCSS | resources.PostCSS | resources.Minify | resources.Fingerprint }}
<link rel="stylesheet" href="{{ $styles.RelPermalink }}" integrity="{{ $styles.Data.Digest }}" media="screen">
```
The transformation funcs above have aliases, so it can be shortened to:
```bash
{{ $css := resources.Get "styles.scss" | toCSS | postCSS | minify | fingerprint }}
<link rel="stylesheet" href="{{ $styles.RelPermalink }}" integrity="{{ $styles.Data.Digest }}" media="screen">
```
A quick tip would be to avoid the fingerprinting part, and possibly also the not-superfast `postCSS` when you're doing development, as it allows Hugo to be smarter about the rebuilding.
Documentation will follow, but have a look at the demo repo in https://github.com/bep/hugo-sass-test
New functions to create `Resource` objects:
* `resources.Get` (see above)
* `resources.FromString`: Create a Resource from a string.
New `Resource` transformation funcs:
* `resources.ToCSS`: Compile `SCSS` or `SASS` into `CSS`.
* `resources.PostCSS`: Process your CSS with PostCSS. Config file support (project or theme or passed as an option).
* `resources.Minify`: Currently supports `css`, `js`, `json`, `html`, `svg`, `xml`.
* `resources.Fingerprint`: Creates a fingerprinted version of the given Resource with Subresource Integrity..
* `resources.Concat`: Concatenates a list of Resource objects. Think of this as a poor man's bundler.
* `resources.ExecuteAsTemplate`: Parses and executes the given Resource and data context (e.g. .Site) as a Go template.
Fixes #4381
Fixes #4903
Fixes #4858
2018-02-20 04:02:14 -05:00
|
|
|
for _, tt := range t {
|
2021-03-11 03:18:01 -05:00
|
|
|
if tt.hasSuffix(suffix) {
|
|
|
|
return tt, SuffixInfo{
|
|
|
|
FullSuffix: tt.Delimiter + suffix,
|
|
|
|
Suffix: suffix,
|
|
|
|
}, true
|
Add Hugo Piper with SCSS support and much more
Before this commit, you would have to use page bundles to do image processing etc. in Hugo.
This commit adds
* A new `/assets` top-level project or theme dir (configurable via `assetDir`)
* A new template func, `resources.Get` which can be used to "get a resource" that can be further processed.
This means that you can now do this in your templates (or shortcodes):
```bash
{{ $sunset := (resources.Get "images/sunset.jpg").Fill "300x200" }}
```
This also adds a new `extended` build tag that enables powerful SCSS/SASS support with source maps. To compile this from source, you will also need a C compiler installed:
```
HUGO_BUILD_TAGS=extended mage install
```
Note that you can use output of the SCSS processing later in a non-SCSSS-enabled Hugo.
The `SCSS` processor is a _Resource transformation step_ and it can be chained with the many others in a pipeline:
```bash
{{ $css := resources.Get "styles.scss" | resources.ToCSS | resources.PostCSS | resources.Minify | resources.Fingerprint }}
<link rel="stylesheet" href="{{ $styles.RelPermalink }}" integrity="{{ $styles.Data.Digest }}" media="screen">
```
The transformation funcs above have aliases, so it can be shortened to:
```bash
{{ $css := resources.Get "styles.scss" | toCSS | postCSS | minify | fingerprint }}
<link rel="stylesheet" href="{{ $styles.RelPermalink }}" integrity="{{ $styles.Data.Digest }}" media="screen">
```
A quick tip would be to avoid the fingerprinting part, and possibly also the not-superfast `postCSS` when you're doing development, as it allows Hugo to be smarter about the rebuilding.
Documentation will follow, but have a look at the demo repo in https://github.com/bep/hugo-sass-test
New functions to create `Resource` objects:
* `resources.Get` (see above)
* `resources.FromString`: Create a Resource from a string.
New `Resource` transformation funcs:
* `resources.ToCSS`: Compile `SCSS` or `SASS` into `CSS`.
* `resources.PostCSS`: Process your CSS with PostCSS. Config file support (project or theme or passed as an option).
* `resources.Minify`: Currently supports `css`, `js`, `json`, `html`, `svg`, `xml`.
* `resources.Fingerprint`: Creates a fingerprinted version of the given Resource with Subresource Integrity..
* `resources.Concat`: Concatenates a list of Resource objects. Think of this as a poor man's bundler.
* `resources.ExecuteAsTemplate`: Parses and executes the given Resource and data context (e.g. .Site) as a Go template.
Fixes #4381
Fixes #4903
Fixes #4858
2018-02-20 04:02:14 -05:00
|
|
|
}
|
|
|
|
}
|
2021-03-11 03:18:01 -05:00
|
|
|
return Type{}, SuffixInfo{}, false
|
Add Hugo Piper with SCSS support and much more
Before this commit, you would have to use page bundles to do image processing etc. in Hugo.
This commit adds
* A new `/assets` top-level project or theme dir (configurable via `assetDir`)
* A new template func, `resources.Get` which can be used to "get a resource" that can be further processed.
This means that you can now do this in your templates (or shortcodes):
```bash
{{ $sunset := (resources.Get "images/sunset.jpg").Fill "300x200" }}
```
This also adds a new `extended` build tag that enables powerful SCSS/SASS support with source maps. To compile this from source, you will also need a C compiler installed:
```
HUGO_BUILD_TAGS=extended mage install
```
Note that you can use output of the SCSS processing later in a non-SCSSS-enabled Hugo.
The `SCSS` processor is a _Resource transformation step_ and it can be chained with the many others in a pipeline:
```bash
{{ $css := resources.Get "styles.scss" | resources.ToCSS | resources.PostCSS | resources.Minify | resources.Fingerprint }}
<link rel="stylesheet" href="{{ $styles.RelPermalink }}" integrity="{{ $styles.Data.Digest }}" media="screen">
```
The transformation funcs above have aliases, so it can be shortened to:
```bash
{{ $css := resources.Get "styles.scss" | toCSS | postCSS | minify | fingerprint }}
<link rel="stylesheet" href="{{ $styles.RelPermalink }}" integrity="{{ $styles.Data.Digest }}" media="screen">
```
A quick tip would be to avoid the fingerprinting part, and possibly also the not-superfast `postCSS` when you're doing development, as it allows Hugo to be smarter about the rebuilding.
Documentation will follow, but have a look at the demo repo in https://github.com/bep/hugo-sass-test
New functions to create `Resource` objects:
* `resources.Get` (see above)
* `resources.FromString`: Create a Resource from a string.
New `Resource` transformation funcs:
* `resources.ToCSS`: Compile `SCSS` or `SASS` into `CSS`.
* `resources.PostCSS`: Process your CSS with PostCSS. Config file support (project or theme or passed as an option).
* `resources.Minify`: Currently supports `css`, `js`, `json`, `html`, `svg`, `xml`.
* `resources.Fingerprint`: Creates a fingerprinted version of the given Resource with Subresource Integrity..
* `resources.Concat`: Concatenates a list of Resource objects. Think of this as a poor man's bundler.
* `resources.ExecuteAsTemplate`: Parses and executes the given Resource and data context (e.g. .Site) as a Go template.
Fixes #4381
Fixes #4903
Fixes #4858
2018-02-20 04:02:14 -05:00
|
|
|
}
|
|
|
|
|
2017-04-03 16:39:37 -04:00
|
|
|
// 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.
|
2021-03-11 03:18:01 -05:00
|
|
|
func (t Types) GetBySuffix(suffix string) (tp Type, si SuffixInfo, found bool) {
|
|
|
|
suffix = strings.ToLower(suffix)
|
2017-04-03 16:39:37 -04:00
|
|
|
for _, tt := range t {
|
2021-03-11 03:18:01 -05:00
|
|
|
if tt.hasSuffix(suffix) {
|
2017-04-03 16:39:37 -04:00
|
|
|
if found {
|
|
|
|
// ambiguous
|
|
|
|
found = false
|
|
|
|
return
|
|
|
|
}
|
|
|
|
tp = tt
|
2021-03-11 03:18:01 -05:00
|
|
|
si = SuffixInfo{
|
|
|
|
FullSuffix: tt.Delimiter + suffix,
|
|
|
|
Suffix: suffix,
|
|
|
|
}
|
2017-04-03 16:39:37 -04:00
|
|
|
found = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-03-11 03:18:01 -05:00
|
|
|
func (m Type) hasSuffix(suffix string) bool {
|
|
|
|
return strings.Contains(m.suffixesCSV, suffix)
|
2018-07-10 05:55:22 -04:00
|
|
|
}
|
|
|
|
|
2018-09-06 18:24:23 -04:00
|
|
|
// GetByMainSubType gets a media type given a main and a sub type e.g. "text" and "plain".
|
2018-07-10 05:55:22 -04:00
|
|
|
// It will return false if no format could be found, or if the combination given
|
|
|
|
// is ambiguous.
|
|
|
|
// The lookup is case insensitive.
|
|
|
|
func (t Types) GetByMainSubType(mainType, subType string) (tp Type, found bool) {
|
|
|
|
for _, tt := range t {
|
|
|
|
if strings.EqualFold(mainType, tt.MainType) && strings.EqualFold(subType, tt.SubType) {
|
|
|
|
if found {
|
|
|
|
// ambiguous
|
|
|
|
found = false
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
tp = tt
|
|
|
|
found = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-08-28 08:18:12 -04:00
|
|
|
func suffixIsRemoved() error {
|
|
|
|
return errors.New(`MediaType.Suffix is removed. Before Hugo 0.44 this was used both to set a custom file suffix and as way
|
2018-07-10 05:55:22 -04:00
|
|
|
to augment the mediatype definition (what you see after the "+", e.g. "image/svg+xml").
|
|
|
|
|
|
|
|
This had its limitations. For one, it was only possible with one file extension per MIME type.
|
|
|
|
|
|
|
|
Now you can specify multiple file suffixes using "suffixes", but you need to specify the full MIME type
|
|
|
|
identifier:
|
|
|
|
|
|
|
|
[mediaTypes]
|
|
|
|
[mediaTypes."image/svg+xml"]
|
|
|
|
suffixes = ["svg", "abc" ]
|
|
|
|
|
|
|
|
In most cases, it will be enough to just change:
|
|
|
|
|
|
|
|
[mediaTypes]
|
|
|
|
[mediaTypes."my/custom-mediatype"]
|
|
|
|
suffix = "txt"
|
|
|
|
|
|
|
|
To:
|
|
|
|
|
|
|
|
[mediaTypes]
|
|
|
|
[mediaTypes."my/custom-mediatype"]
|
|
|
|
suffixes = ["txt"]
|
|
|
|
|
|
|
|
Note that you can still get the Media Type's suffix from a template: {{ $mediaType.Suffix }}. But this will now map to the MIME type filename.
|
2018-08-28 08:18:12 -04:00
|
|
|
`)
|
2018-07-10 05:55:22 -04:00
|
|
|
}
|
|
|
|
|
2017-04-03 16:39:37 -04:00
|
|
|
// DecodeTypes takes a list of media type configurations and merges those,
|
2017-04-06 09:28:56 -04:00
|
|
|
// in the order given, with the Hugo defaults as the last resort.
|
2018-08-28 08:18:12 -04:00
|
|
|
func DecodeTypes(mms ...map[string]interface{}) (Types, error) {
|
2018-07-10 05:55:22 -04:00
|
|
|
var m Types
|
|
|
|
|
|
|
|
// Maps type string to Type. Type string is the full application/svg+xml.
|
|
|
|
mmm := make(map[string]Type)
|
|
|
|
for _, dt := range DefaultTypes {
|
|
|
|
mmm[dt.Type()] = dt
|
|
|
|
}
|
2017-04-03 16:39:37 -04:00
|
|
|
|
2018-08-28 08:18:12 -04:00
|
|
|
for _, mm := range mms {
|
2017-04-03 16:39:37 -04:00
|
|
|
for k, v := range mm {
|
2018-07-10 05:55:22 -04:00
|
|
|
var mediaType Type
|
2017-04-03 16:39:37 -04:00
|
|
|
|
2018-07-10 05:55:22 -04:00
|
|
|
mediaType, found := mmm[k]
|
2017-04-03 16:39:37 -04:00
|
|
|
if !found {
|
2018-07-10 05:55:22 -04:00
|
|
|
var err error
|
|
|
|
mediaType, err = fromString(k)
|
2017-04-03 16:39:37 -04:00
|
|
|
if err != nil {
|
|
|
|
return m, err
|
|
|
|
}
|
2018-07-10 05:55:22 -04:00
|
|
|
}
|
2017-04-03 16:39:37 -04:00
|
|
|
|
2018-07-10 05:55:22 -04:00
|
|
|
if err := mapstructure.WeakDecode(v, &mediaType); err != nil {
|
|
|
|
return m, err
|
|
|
|
}
|
|
|
|
|
|
|
|
vm := v.(map[string]interface{})
|
2018-08-28 08:18:12 -04:00
|
|
|
maps.ToLower(vm)
|
2018-07-10 05:55:22 -04:00
|
|
|
_, delimiterSet := vm["delimiter"]
|
|
|
|
_, suffixSet := vm["suffix"]
|
|
|
|
|
2018-07-10 17:21:48 -04:00
|
|
|
if suffixSet {
|
2018-08-28 08:18:12 -04:00
|
|
|
return Types{}, suffixIsRemoved()
|
2018-07-10 05:55:22 -04:00
|
|
|
}
|
2017-04-03 16:39:37 -04:00
|
|
|
|
2021-03-11 03:18:01 -05:00
|
|
|
if suffixes, found := vm["suffixes"]; found {
|
|
|
|
mediaType.suffixesCSV = strings.TrimSpace(strings.ToLower(strings.Join(cast.ToStringSlice(suffixes), ",")))
|
|
|
|
}
|
|
|
|
|
2018-07-10 05:55:22 -04:00
|
|
|
// The user may set the delimiter as an empty string.
|
2021-03-11 03:18:01 -05:00
|
|
|
if !delimiterSet && mediaType.suffixesCSV != "" {
|
2018-07-10 05:55:22 -04:00
|
|
|
mediaType.Delimiter = defaultDelimiter
|
|
|
|
}
|
|
|
|
|
2021-03-11 03:18:01 -05:00
|
|
|
mediaType.init()
|
|
|
|
|
2018-07-10 05:55:22 -04:00
|
|
|
mmm[k] = mediaType
|
|
|
|
|
2017-04-03 16:39:37 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-10 05:55:22 -04:00
|
|
|
for _, v := range mmm {
|
|
|
|
m = append(m, v)
|
|
|
|
}
|
2017-04-03 16:39:37 -04:00
|
|
|
sort.Sort(m)
|
|
|
|
|
|
|
|
return m, nil
|
|
|
|
}
|
2017-04-05 10:18:53 -04:00
|
|
|
|
2020-07-21 11:59:03 -04:00
|
|
|
// IsZero reports whether this Type represents a zero value.
|
|
|
|
func (m Type) IsZero() bool {
|
|
|
|
return m.SubType == ""
|
|
|
|
}
|
|
|
|
|
2018-09-06 18:24:23 -04:00
|
|
|
// MarshalJSON returns the JSON encoding of m.
|
2017-09-25 22:25:33 -04:00
|
|
|
func (m Type) MarshalJSON() ([]byte, error) {
|
2017-04-05 10:18:53 -04:00
|
|
|
type Alias Type
|
|
|
|
return json.Marshal(&struct {
|
|
|
|
Alias
|
2021-03-11 03:18:01 -05:00
|
|
|
Type string `json:"type"`
|
|
|
|
String string `json:"string"`
|
|
|
|
Suffixes []string `json:"suffixes"`
|
2017-04-05 10:18:53 -04:00
|
|
|
}{
|
2021-03-11 03:18:01 -05:00
|
|
|
Alias: (Alias)(m),
|
|
|
|
Type: m.Type(),
|
|
|
|
String: m.String(),
|
|
|
|
Suffixes: strings.Split(m.suffixesCSV, ","),
|
2017-04-05 10:18:53 -04:00
|
|
|
})
|
|
|
|
}
|