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