1// Copyright 2020 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
14package publisher
15
16import (
17	"errors"
18	"io"
19	"net/url"
20	"sync/atomic"
21
22	"github.com/gohugoio/hugo/resources"
23
24	"github.com/gohugoio/hugo/media"
25
26	"github.com/gohugoio/hugo/minifiers"
27
28	bp "github.com/gohugoio/hugo/bufferpool"
29	"github.com/gohugoio/hugo/helpers"
30
31	"github.com/spf13/afero"
32
33	"github.com/gohugoio/hugo/output"
34	"github.com/gohugoio/hugo/transform"
35	"github.com/gohugoio/hugo/transform/livereloadinject"
36	"github.com/gohugoio/hugo/transform/metainject"
37	"github.com/gohugoio/hugo/transform/urlreplacers"
38)
39
40// Descriptor describes the needed publishing chain for an item.
41type Descriptor struct {
42	// The content to publish.
43	Src io.Reader
44
45	// The OutputFormat of the this content.
46	OutputFormat output.Format
47
48	// Where to publish this content. This is a filesystem-relative path.
49	TargetPath string
50
51	// Counter for the end build summary.
52	StatCounter *uint64
53
54	// Configuration that trigger pre-processing.
55	// LiveReload script will be injected if this is != nil
56	LiveReloadBaseURL *url.URL
57
58	// Enable to inject the Hugo generated tag in the header. Is currently only
59	// injected on the home page for HTML type of output formats.
60	AddHugoGeneratorTag bool
61
62	// If set, will replace all relative URLs with this one.
63	AbsURLPath string
64
65	// Enable to minify the output using the OutputFormat defined above to
66	// pick the correct minifier configuration.
67	Minify bool
68}
69
70// DestinationPublisher is the default and currently only publisher in Hugo. This
71// publisher prepares and publishes an item to the defined destination, e.g. /public.
72type DestinationPublisher struct {
73	fs                    afero.Fs
74	min                   minifiers.Client
75	htmlElementsCollector *htmlElementsCollector
76}
77
78// NewDestinationPublisher creates a new DestinationPublisher.
79func NewDestinationPublisher(rs *resources.Spec, outputFormats output.Formats, mediaTypes media.Types) (pub DestinationPublisher, err error) {
80	fs := rs.BaseFs.PublishFs
81	cfg := rs.Cfg
82	var classCollector *htmlElementsCollector
83	if rs.BuildConfig.WriteStats {
84		classCollector = newHTMLElementsCollector()
85	}
86	pub = DestinationPublisher{fs: fs, htmlElementsCollector: classCollector}
87	pub.min, err = minifiers.New(mediaTypes, outputFormats, cfg)
88	return
89}
90
91// Publish applies any relevant transformations and writes the file
92// to its destination, e.g. /public.
93func (p DestinationPublisher) Publish(d Descriptor) error {
94	if d.TargetPath == "" {
95		return errors.New("Publish: must provide a TargetPath")
96	}
97
98	src := d.Src
99
100	transformers := p.createTransformerChain(d)
101
102	if len(transformers) != 0 {
103		b := bp.GetBuffer()
104		defer bp.PutBuffer(b)
105
106		if err := transformers.Apply(b, d.Src); err != nil {
107			return err
108		}
109
110		// This is now what we write to disk.
111		src = b
112	}
113
114	f, err := helpers.OpenFileForWriting(p.fs, d.TargetPath)
115	if err != nil {
116		return err
117	}
118	defer f.Close()
119
120	var w io.Writer = f
121
122	if p.htmlElementsCollector != nil && d.OutputFormat.IsHTML {
123		w = io.MultiWriter(w, newHTMLElementsCollectorWriter(p.htmlElementsCollector))
124	}
125
126	_, err = io.Copy(w, src)
127	if err == nil && d.StatCounter != nil {
128		atomic.AddUint64(d.StatCounter, uint64(1))
129	}
130
131	return err
132}
133
134func (p DestinationPublisher) PublishStats() PublishStats {
135	if p.htmlElementsCollector == nil {
136		return PublishStats{}
137	}
138
139	return PublishStats{
140		HTMLElements: p.htmlElementsCollector.getHTMLElements(),
141	}
142}
143
144type PublishStats struct {
145	HTMLElements HTMLElements `json:"htmlElements"`
146}
147
148// Publisher publishes a result file.
149type Publisher interface {
150	Publish(d Descriptor) error
151	PublishStats() PublishStats
152}
153
154// XML transformer := transform.New(urlreplacers.NewAbsURLInXMLTransformer(path))
155func (p DestinationPublisher) createTransformerChain(f Descriptor) transform.Chain {
156	transformers := transform.NewEmpty()
157
158	isHTML := f.OutputFormat.IsHTML
159
160	if f.AbsURLPath != "" {
161		if isHTML {
162			transformers = append(transformers, urlreplacers.NewAbsURLTransformer(f.AbsURLPath))
163		} else {
164			// Assume XML.
165			transformers = append(transformers, urlreplacers.NewAbsURLInXMLTransformer(f.AbsURLPath))
166		}
167	}
168
169	if isHTML {
170		if f.LiveReloadBaseURL != nil {
171			transformers = append(transformers, livereloadinject.New(*f.LiveReloadBaseURL))
172		}
173
174		// This is only injected on the home page.
175		if f.AddHugoGeneratorTag {
176			transformers = append(transformers, metainject.HugoGenerator)
177		}
178
179	}
180
181	if p.min.MinifyOutput {
182		minifyTransformer := p.min.Transformer(f.OutputFormat.MediaType)
183		if minifyTransformer != nil {
184			transformers = append(transformers, minifyTransformer)
185		}
186	}
187
188	return transformers
189}
190