-
Notifications
You must be signed in to change notification settings - Fork 0
/
site.go
560 lines (450 loc) · 13.6 KB
/
site.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
package hugolib
import (
"fmt"
"github.com/spf13/afero"
bp "github.com/sunwei/hugo-playground/bufferpool"
"github.com/sunwei/hugo-playground/common/maps"
"github.com/sunwei/hugo-playground/common/text"
"github.com/sunwei/hugo-playground/config"
"github.com/sunwei/hugo-playground/deps"
"github.com/sunwei/hugo-playground/helpers"
"github.com/sunwei/hugo-playground/identity"
"github.com/sunwei/hugo-playground/langs"
"github.com/sunwei/hugo-playground/lazy"
"github.com/sunwei/hugo-playground/log"
"github.com/sunwei/hugo-playground/markup/converter"
"github.com/sunwei/hugo-playground/media"
"github.com/sunwei/hugo-playground/output"
"github.com/sunwei/hugo-playground/publisher"
"github.com/sunwei/hugo-playground/resources/page"
"github.com/sunwei/hugo-playground/source"
"github.com/sunwei/hugo-playground/tpl"
"html/template"
"io"
"path"
"path/filepath"
"sort"
"strings"
"time"
)
// Site contains all the information relevant for constructing a static
// site. The basic flow of information is as follows:
//
// 1. A list of Files is parsed and then converted into Pages.
//
// 2. Pages contain sections (based on the file they were generated from),
// aliases and slugs (included in a pages frontmatter) which are the
// various targets that will get generated. There will be canonical
// listing. The canonical path can be overruled based on a pattern.
//
// 3. Taxonomies are created via configuration and will present some aspect of
// the final page and typically a perm url.
//
// 4. All Pages are passed through a template based on their desired
// layout based on numerous different elements.
//
// 5. The entire collection of files is written to disk.
type Site struct {
language *langs.Language
siteBucket *pagesMapBucket
// Output formats defined in site config per Page Kind, or some defaults
// if not set.
// Output formats defined in Page front matter will override these.
outputFormats map[string]output.Formats
// All the output formats and media types available for this site.
// These values will be merged from the Hugo defaults, the site config and,
// finally, the language settings.
outputFormatsConfig output.Formats
mediaTypesConfig media.Types
// We render each site for all the relevant output formats in serial with
// this rendering context pointing to the current one.
rc *siteRenderingContext
siteCfg siteConfigHolder
// The func used to title case titles.
titleFunc func(s string) string
// newSite with above infos
// The owning container. When multiple languages, there will be multiple
// sites .
h *HugoSites
*PageCollections
Sections Taxonomy
Info *SiteInfo
// The output formats that we need to render this site in. This slice
// will be fixed once set.
// This will be the union of Site.Pages' outputFormats.
// This slice will be sorted.
renderFormats output.Formats
// Logger etc.
*deps.Deps `json:"-"`
siteRefLinker
publisher publisher.Publisher
// Shortcut to the home page. Note that this may be nil if
// home page, for some odd reason, is disabled.
home *pageState
}
type siteRenderingContext struct {
output.Format
}
type siteRefLinker struct {
s *Site
notFoundURL string
}
type SiteInfo struct {
title string
relativeURLs bool
owner *HugoSites
s *Site
}
// newSite creates a new site with the given configuration.
func newSite(cfg deps.DepsCfg) (*Site, error) {
var (
mediaTypesConfig []map[string]any
outputFormatsConfig []map[string]any
siteOutputFormatsConfig output.Formats
siteMediaTypesConfig media.Types
err error
)
// [{toml}, {html}, {markdown}, {plain}]
log.Process("media.DecodeTypes", "set default media types")
siteMediaTypesConfig, err = media.DecodeTypes(mediaTypesConfig...)
if err != nil {
return nil, err
}
// [{HTML}, {JSON}, {MARKDOWN}]
log.Process("output.DecodeFormats", "set default output formats based on media types, and customized output formats configuration")
siteOutputFormatsConfig, err = output.DecodeFormats(siteMediaTypesConfig, outputFormatsConfig...)
if err != nil {
return nil, err
}
// Site output formats source
log.Process("site output formats", "map siteOutputFormats to every hugo page types(KindPage, KindHome...)")
outputFormats, err := createSiteOutputFormats(siteOutputFormatsConfig, nil, true)
if err != nil {
return nil, err
}
// KindTaxonomy, KindTerm like section title
titleFunc := helpers.GetTitleFunc("")
siteConfig := siteConfigHolder{
timeout: 30 * time.Second, // page content output init timeout
}
var siteBucket *pagesMapBucket
s := &Site{
language: cfg.Language,
siteBucket: siteBucket,
outputFormats: outputFormats,
outputFormatsConfig: siteOutputFormatsConfig,
mediaTypesConfig: siteMediaTypesConfig,
siteCfg: siteConfig,
titleFunc: titleFunc,
rc: &siteRenderingContext{output.HTMLFormat},
}
return s, nil
}
type siteConfigHolder struct {
timeout time.Duration
}
func (s *Site) initializeSiteInfo() error {
// Assemble dependencies to be used in hugo.Deps.
s.Info = &SiteInfo{
title: "title",
relativeURLs: s.Cfg.GetBool("relativeURLs"),
owner: s.h,
s: s,
}
return nil
}
func (s *SiteInfo) Pages() page.Pages {
return s.s.Pages()
}
func (s *SiteInfo) RegularPages() page.Pages {
return s.s.RegularPages()
}
func (s *SiteInfo) AllPages() page.Pages {
return s.s.AllPages()
}
func (s *SiteInfo) AllRegularPages() page.Pages {
return s.s.AllRegularPages()
}
func (s *SiteInfo) Title() string {
return s.title
}
func (s *SiteInfo) Site() page.Site {
return s
}
func (s *SiteInfo) Data() map[string]any {
return s.s.h.Data()
}
// Current returns the currently rendered Site.
// If that isn't set yet, which is the situation before we start rendering,
// if will return the Site itself.
func (s *SiteInfo) Current() page.Site {
if s.s.h.currentSite == nil {
return s
}
return s.s.h.currentSite.Info
}
func (s *SiteInfo) String() string {
return fmt.Sprintf("Site(%q)", s.title)
}
func (s *SiteInfo) BaseURL() template.URL {
return template.URL(s.s.PathSpec.BaseURL.String())
}
func (s *Site) isEnabled(kind string) bool {
if kind == kindUnknown {
panic("Unknown kind")
}
return true
}
func (s *Site) process(config BuildCfg) (err error) {
log.Process("HugoSites Build process", "site initialize with title and owner")
if err = s.initialize(); err != nil {
err = fmt.Errorf("initialize: %w", err)
return
}
if err = s.readAndProcessContent(config); err != nil {
err = fmt.Errorf("readAndProcessContent: %w", err)
fmt.Println("read and process content err")
fmt.Printf("%#v", err)
return
}
return err
}
func (s *Site) initialize() (err error) {
return s.initializeSiteInfo()
}
func (s *Site) readAndProcessContent(buildConfig BuildCfg, filenames ...string) error {
log.Process("readAndProcessContent", "new source spec with PathSpec, ContentInclusionFilter and BaseFs Content.Fs")
sourceSpec := source.NewSourceSpec(s.PathSpec, buildConfig.ContentInclusionFilter, s.BaseFs.Content.Fs)
proc := newPagesProcessor(s.h, sourceSpec)
c := newPagesCollector(sourceSpec, s.h.getContentMaps(), proc, filenames...)
log.Process("readAndProcessContent", "collect content with PagesProcessor")
if err := c.Collect(); err != nil {
return err
}
return nil
}
func (s *Site) publish(path string, r io.Reader, fs afero.Fs) (err error) {
return helpers.WriteToDisk(filepath.Clean(path), r, fs)
}
func (s *Site) newPage(
n *contentNode,
parentbBucket *pagesMapBucket,
kind, title string,
sections ...string) *pageState {
m := map[string]any{}
if title != "" {
m["title"] = title
}
p, err := newPageFromMeta(
n,
parentbBucket,
m,
&pageMeta{
s: s,
kind: kind,
sections: sections,
})
if err != nil {
panic(err)
}
return p
}
func (s *SiteInfo) Params() maps.Params {
return s.s.Language().Params()
}
func (s *Site) Language() *langs.Language {
return s.language
}
func (s *Site) kindFromFileInfoOrSections(fi *fileInfo, sections []string) string {
if fi.TranslationBaseName() == "_index" {
if fi.Dir() == "" {
return page.KindHome
}
return s.kindFromSections(sections)
}
return page.KindPage
}
func (s *Site) kindFromSections(sections []string) string {
if len(sections) == 0 {
return page.KindHome
}
return s.kindFromSectionPath(path.Join(sections...))
}
func (s *Site) kindFromSectionPath(sectionPath string) string {
return page.KindSection
}
func (s *Site) shouldBuild(p page.Page) bool {
return true
}
// Sites is a convenience method to get all the Hugo sites/languages configured.
func (s *SiteInfo) Sites() page.Sites {
return s.s.h.siteInfos()
}
// hookRendererTemplate is the canonical implementation of all hooks.ITEMRenderer,
// where ITEM is the thing being hooked.
type hookRendererTemplate struct {
templateHandler tpl.TemplateHandler
identity.SearchProvider
templ tpl.Template
resolvePosition func(ctx any) text.Position
}
func (p *pageState) getContentConverter() converter.Converter {
var err error
p.m.contentConverterInit.Do(func() {
markup := p.m.markup
if markup == "html" {
// Only used for shortcode inner content.
markup = "markdown"
}
p.m.contentConverter, err = p.m.newContentConverter(p, markup)
})
if err != nil {
fmt.Printf("Failed to create content converter: %v", err)
}
return p.m.contentConverter
}
func (s *Site) initInit(init *lazy.Init, pctx pageContext) bool {
_, err := init.Do()
if err != nil {
fmt.Printf("fatal error %v", pctx.wrapError(err))
}
return err == nil
}
// pageContext provides contextual information about this page, for error
// logging and similar.
type pageContext interface {
posOffset(offset int) text.Position
wrapError(err error) error
getContentConverter() converter.Converter
addDependency(dep identity.Provider)
}
// This is all the kinds we can expect to find in .Site.Pages.
var allKindsInPages = []string{page.KindPage, page.KindHome, page.KindSection, page.KindTerm, page.KindTaxonomy}
func (s *Site) initRenderFormats() {
formatSet := make(map[string]bool)
formats := output.Formats{}
s.pageMap.pageTrees.WalkRenderable(func(s string, n *contentNode) bool {
// empty
for _, f := range n.p.m.configuredOutputFormats {
if !formatSet[f.Name] {
formats = append(formats, f)
formatSet[f.Name] = true
}
}
return false
})
// media type - format
// site output format - render format
// Add the per kind configured output formats
for _, kind := range allKindsInPages {
if siteFormats, found := s.outputFormats[kind]; found {
for _, f := range siteFormats {
if !formatSet[f.Name] {
formats = append(formats, f)
formatSet[f.Name] = true
}
}
}
}
sort.Sort(formats)
// HTML
s.renderFormats = formats
}
func (s *Site) render(ctx *siteRenderContext) (err error) {
log.Process("Site render", "render pages")
if err = s.renderPages(ctx); err != nil {
return
}
if ctx.outIdx == 0 {
log.Process("Site render", "render 404")
if err = s.render404(); err != nil {
return
}
}
return
}
func (s *Site) renderAndWritePage(name string, targetPath string, p *pageState, templ tpl.Template) error {
renderBuffer := bp.GetBuffer()
defer bp.PutBuffer(renderBuffer)
of := p.outputFormat()
log.Process("render and write page", "render for template")
if err := s.renderForTemplate(p.Kind(), of.Name, p, renderBuffer, templ); err != nil {
return err
}
if renderBuffer.Len() == 0 {
return nil
}
isHTML := of.IsHTML
pd := publisher.Descriptor{
Src: renderBuffer,
TargetPath: targetPath,
OutputFormat: p.outputFormat(),
}
if isHTML {
if s.Info.relativeURLs {
fmt.Println("based on default configuration, should never been here")
pd.AbsURLPath = s.absURLPath(targetPath)
}
// For performance reasons we only inject the Hugo generator tag on the home page.
if p.IsHome() {
pd.AddHugoGeneratorTag = !s.Cfg.GetBool("disableHugoGeneratorInject")
}
}
log.Process("render and write page", "publish page")
return s.publisher.Publish(pd)
}
func (s *Site) renderForTemplate(name, outputFormat string, d any, w io.Writer, templ tpl.Template) (err error) {
if templ == nil {
fmt.Printf("missing layout name: %s, output format: %s", name, outputFormat)
return nil
}
if err = s.Tmpl().Execute(templ, w, d); err != nil {
return fmt.Errorf("render of %q failed: %w", name, err)
}
return
}
func (s *Site) absURLPath(targetPath string) string {
var path string
if s.Info.relativeURLs {
path = helpers.GetDottedRelativePath(targetPath)
} else {
url := s.PathSpec.BaseURL.String()
if !strings.HasSuffix(url, "/") {
url += "/"
}
path = url
}
return path
}
func (s *Site) lookupLayouts(layouts ...string) tpl.Template {
for _, l := range layouts {
if templ, found := s.Tmpl().Lookup(l); found {
return templ
}
}
return nil
}
func newSiteRefLinker(cfg config.Provider, s *Site) (siteRefLinker, error) {
notFoundURL := cfg.GetString("refLinksNotFoundURL")
return siteRefLinker{s: s, notFoundURL: notFoundURL}, nil
}
func (s siteRefLinker) logNotFound(ref, what string, p page.Page, position text.Position) {
if position.IsValid() {
fmt.Printf("[%s] REF_NOT_FOUND: Ref %q: %s: %s", s.s.Lang(), ref, position.String(), what)
} else if p == nil {
fmt.Printf("[%s] REF_NOT_FOUND: Ref %q: %s", s.s.Lang(), ref, what)
} else {
fmt.Printf("[%s] REF_NOT_FOUND: Ref %q from page %q: %s", s.s.Lang(), ref, p.Pathc(), what)
}
}
func (s *siteRefLinker) refLink(ref string, source any, relative bool, outputFormat string) (string, error) {
return "", nil
}
func (s *Site) errorCollator(results <-chan error, errs chan<- error) {
var errors []error
for e := range results {
errors = append(errors, e)
}
errs <- s.h.pickOneAndLogTheRest(errors)
close(errs)
}