2018-08-05 05:13:49 -04:00
|
|
|
// Copyright 2018 The Hugo Authors. All rights reserved.
|
2015-12-10 17:19:38 -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.
|
|
|
|
|
2013-10-01 17:26:21 -04:00
|
|
|
package transform
|
|
|
|
|
|
|
|
import (
|
2015-03-17 19:36:48 -04:00
|
|
|
"bytes"
|
|
|
|
"io"
|
2023-02-18 17:43:26 -05:00
|
|
|
"os"
|
2016-07-06 17:23:44 -04:00
|
|
|
|
2017-06-13 12:42:45 -04:00
|
|
|
bp "github.com/gohugoio/hugo/bufferpool"
|
2022-05-02 10:07:52 -04:00
|
|
|
"github.com/gohugoio/hugo/common/herrors"
|
|
|
|
"github.com/gohugoio/hugo/hugofs"
|
2013-10-01 17:26:21 -04:00
|
|
|
)
|
|
|
|
|
2018-08-05 05:13:49 -04:00
|
|
|
// Transformer is the func that needs to be implemented by a transformation step.
|
|
|
|
type Transformer func(ft FromTo) error
|
2013-10-01 17:26:21 -04:00
|
|
|
|
2018-08-05 05:13:49 -04:00
|
|
|
// 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
|
2013-11-05 17:28:06 -05:00
|
|
|
|
2018-08-05 05:13:49 -04:00
|
|
|
io.Reader
|
|
|
|
}
|
2013-11-05 17:28:06 -05:00
|
|
|
|
2018-08-05 05:13:49 -04:00
|
|
|
// FromTo is sent to each transformation step in the chain.
|
|
|
|
type FromTo interface {
|
|
|
|
From() BytesReader
|
|
|
|
To() io.Writer
|
2013-10-01 17:26:21 -04:00
|
|
|
}
|
|
|
|
|
2018-08-05 05:13:49 -04:00
|
|
|
// 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
|
2014-01-03 18:36:53 -05:00
|
|
|
}
|
|
|
|
|
2018-08-05 05:13:49 -04:00
|
|
|
// NewEmpty creates a new slice of transformers with a capacity of 20.
|
|
|
|
func NewEmpty() Chain {
|
|
|
|
return make(Chain, 0, 20)
|
2015-03-17 19:36:48 -04:00
|
|
|
}
|
|
|
|
|
2015-03-18 20:55:49 -04:00
|
|
|
// Implements contentTransformer
|
2015-03-18 21:09:42 -04:00
|
|
|
// Content is read from the from-buffer and rewritten to to the to-buffer.
|
2015-03-17 19:36:48 -04:00
|
|
|
type fromToBuffer struct {
|
|
|
|
from *bytes.Buffer
|
|
|
|
to *bytes.Buffer
|
|
|
|
}
|
|
|
|
|
2018-08-05 05:13:49 -04:00
|
|
|
func (ft fromToBuffer) From() BytesReader {
|
|
|
|
return ft.from
|
2015-05-15 18:11:39 -04:00
|
|
|
}
|
|
|
|
|
2018-08-05 05:13:49 -04:00
|
|
|
func (ft fromToBuffer) To() io.Writer {
|
|
|
|
return ft.to
|
2015-03-17 19:36:48 -04:00
|
|
|
}
|
|
|
|
|
2018-08-05 05:13:49 -04:00
|
|
|
// 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 {
|
2018-08-05 05:23:53 -04:00
|
|
|
if len(*c) == 0 {
|
2018-08-05 05:13:49 -04:00
|
|
|
_, err := io.Copy(to, from)
|
2018-08-05 05:23:53 -04:00
|
|
|
return err
|
|
|
|
}
|
2015-03-17 19:36:48 -04:00
|
|
|
|
|
|
|
b1 := bp.GetBuffer()
|
|
|
|
defer bp.PutBuffer(b1)
|
|
|
|
|
2018-08-05 05:13:49 -04:00
|
|
|
if _, err := b1.ReadFrom(from); err != nil {
|
2016-11-03 19:44:49 -04:00
|
|
|
return err
|
|
|
|
}
|
2015-03-17 19:36:48 -04:00
|
|
|
|
|
|
|
b2 := bp.GetBuffer()
|
|
|
|
defer bp.PutBuffer(b2)
|
|
|
|
|
2018-08-05 05:13:49 -04:00
|
|
|
fb := &fromToBuffer{from: b1, to: b2}
|
2015-03-17 19:36:48 -04:00
|
|
|
|
|
|
|
for i, tr := range *c {
|
|
|
|
if i > 0 {
|
|
|
|
if fb.from == b1 {
|
|
|
|
fb.from = b2
|
|
|
|
fb.to = b1
|
|
|
|
fb.to.Reset()
|
|
|
|
} else {
|
|
|
|
fb.from = b1
|
|
|
|
fb.to = b2
|
|
|
|
fb.to.Reset()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-05 05:13:49 -04:00
|
|
|
if err := tr(fb); err != nil {
|
2022-05-02 10:07:52 -04:00
|
|
|
// Write output to a temp file so it can be read by the user for trouble shooting.
|
|
|
|
filename := "output.html"
|
2023-02-18 17:43:26 -05:00
|
|
|
tempfile, ferr := os.CreateTemp("", "hugo-transform-error")
|
2022-05-02 10:07:52 -04:00
|
|
|
if ferr == nil {
|
|
|
|
filename = tempfile.Name()
|
|
|
|
defer tempfile.Close()
|
|
|
|
_, _ = io.Copy(tempfile, fb.from)
|
2022-05-14 09:51:04 -04:00
|
|
|
return herrors.NewFileErrorFromFile(err, filename, hugofs.Os, nil)
|
2022-05-02 10:07:52 -04:00
|
|
|
}
|
2022-05-15 05:40:34 -04:00
|
|
|
return herrors.NewFileErrorFromName(err, filename).UpdateContent(fb.from, nil)
|
2022-05-02 10:07:52 -04:00
|
|
|
|
2018-08-05 05:13:49 -04:00
|
|
|
}
|
2015-03-17 19:36:48 -04:00
|
|
|
}
|
|
|
|
|
2018-08-05 05:13:49 -04:00
|
|
|
_, err := fb.to.WriteTo(to)
|
2016-11-03 19:44:49 -04:00
|
|
|
return err
|
2013-10-01 17:26:21 -04:00
|
|
|
}
|