diff options
author | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2018-08-05 12:13:49 +0300 |
---|---|---|
committer | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2018-08-06 20:58:41 +0300 |
commit | 789ef8c639e4621abd36da530bcb5942ac9297da (patch) | |
tree | f225fc3663affc49805f1d309b77b096d40fc8f6 /transform/chain.go | |
parent | 71931b30b1813b146aaa60f5cdab16c0f9ebebdb (diff) |
Add support for minification of final output
Hugo Pipes added minification support for resources fetched via ´resources.Get` and similar.
This also adds support for minification of the final output for supported output formats: HTML, XML, SVG, CSS, JavaScript, JSON.
To enable, run Hugo with the `--minify` flag:
```bash
hugo --minify
```
This commit is also a major spring cleaning of the `transform` package to allow the new minification step fit into that processing chain.
Fixes #1251
Diffstat (limited to 'transform/chain.go')
-rw-r--r-- | transform/chain.go | 71 |
1 files changed, 40 insertions, 31 deletions
diff --git a/transform/chain.go b/transform/chain.go index f71de94c8..74217dc72 100644 --- a/transform/chain.go +++ b/transform/chain.go @@ -1,4 +1,4 @@ -// Copyright 2015 The Hugo Authors. All rights reserved. +// Copyright 2018 The Hugo Authors. All rights reserved. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -20,67 +20,74 @@ import ( bp "github.com/gohugoio/hugo/bufferpool" ) -type trans func(rw contentTransformer) +// Transformer is the func that needs to be implemented by a transformation step. +type Transformer func(ft FromTo) error -type link trans +// BytesReader wraps the Bytes method, usually implemented by bytes.Buffer, and an +// io.Reader. +type BytesReader interface { + // The slice given by Bytes is valid for use only until the next buffer modification. + // That is, if you want to use this value outside of the current transformer step, + // you need to take a copy. + Bytes() []byte -type chain []link + io.Reader +} -// NewChain creates a chained content transformer given the provided transforms. -func NewChain(trs ...link) chain { - return trs +// FromTo is sent to each transformation step in the chain. +type FromTo interface { + From() BytesReader + To() io.Writer } -// NewEmptyTransforms creates a new slice of transforms with a capacity of 20. -func NewEmptyTransforms() []link { - return make([]link, 0, 20) +// Chain is an ordered processing chain. The next transform operation will +// receive the output from the previous. +type Chain []Transformer + +// New creates a content transformer chain given the provided transform funcs. +func New(trs ...Transformer) Chain { + return trs } -// contentTransformer is an interface that enables rotation of pooled buffers -// in the transformer chain. -type contentTransformer interface { - Path() []byte - Content() []byte - io.Writer +// NewEmpty creates a new slice of transformers with a capacity of 20. +func NewEmpty() Chain { + return make(Chain, 0, 20) } // Implements contentTransformer // Content is read from the from-buffer and rewritten to to the to-buffer. type fromToBuffer struct { - path []byte from *bytes.Buffer to *bytes.Buffer } -func (ft fromToBuffer) Path() []byte { - return ft.path +func (ft fromToBuffer) From() BytesReader { + return ft.from } -func (ft fromToBuffer) Write(p []byte) (n int, err error) { - return ft.to.Write(p) +func (ft fromToBuffer) To() io.Writer { + return ft.to } -func (ft fromToBuffer) Content() []byte { - return ft.from.Bytes() -} - -func (c *chain) Apply(w io.Writer, r io.Reader, p []byte) error { +// Apply passes the given from io.Reader through the transformation chain. +// The result is written to to. +func (c *Chain) Apply(to io.Writer, from io.Reader) error { if len(*c) == 0 { - _, err := io.Copy(w, r) + _, err := io.Copy(to, from) return err } b1 := bp.GetBuffer() defer bp.PutBuffer(b1) - if _, err := b1.ReadFrom(r); err != nil { + if _, err := b1.ReadFrom(from); err != nil { return err } b2 := bp.GetBuffer() defer bp.PutBuffer(b2) - fb := &fromToBuffer{path: p, from: b1, to: b2} + fb := &fromToBuffer{from: b1, to: b2} for i, tr := range *c { if i > 0 { @@ -95,9 +102,11 @@ func (c *chain) Apply(w io.Writer, r io.Reader, p []byte) error { } } - tr(fb) + if err := tr(fb); err != nil { + return err + } } - _, err := fb.to.WriteTo(w) + _, err := fb.to.WriteTo(to) return err } |