go開源Hugo站點(diǎn)構(gòu)建三步曲之集結(jié)渲染
Assemble
Assemble所做的事情很純粹,那就是創(chuàng)建站點(diǎn)頁(yè)面實(shí)例 - pageState。 因?yàn)橹С侄嗾军c(diǎn),contentMaps有多個(gè)。 所以Assemble不僅要?jiǎng)?chuàng)建pageState,還需要管理好所有的pages,這就用到了PageMaps。
type pageMap struct { s *Site *contentMap } type pageMaps struct { workers *para.Workers pmaps []*pageMap }
實(shí)際上pageMap就是由contentMap組合而來(lái)的。 而contentMap中的組成樹的結(jié)點(diǎn)就是contentNode。
正好,每個(gè)contentNode又對(duì)應(yīng)一個(gè)pageState。
type contentNode struct { p *pageState // Set if source is a file. // We will soon get other sources. fi hugofs.FileMetaInfo // The source path. Unix slashes. No leading slash. path string ... }
所以Assemble不僅要為前面Process處理過(guò)生成的contentNode創(chuàng)建pageState,還要補(bǔ)齊一些缺失的contentNode,如Section。
PageState
可以看出,Assemble的重點(diǎn)就是組建PageState,那她到底長(zhǎng)啥樣:
type pageState struct { // This slice will be of same length as the number of global slice of output // formats (for all sites). pageOutputs []*pageOutput // This will be shifted out when we start to render a new output format. *pageOutput // Common for all output formats. *pageCommon ... }
從注解中可以看出普通信息將由pageCommon提供,而輸出信息則由pageOutput提供。 比較特殊的是pageOutputs,是pageOutput的數(shù)組。 在 基礎(chǔ)架構(gòu)中,對(duì)這一點(diǎn)有作分析。 這要?dú)w因于Hugo的多站點(diǎn)渲染策略 - 允許在不同的站點(diǎn)中重用其它站點(diǎn)的頁(yè)面。
// hugo-playground/hugolib/page__new.go // line 97 // Prepare output formats for all sites. // We do this even if this page does not get rendered on // its own. It may be referenced via .Site.GetPage and // it will then need an output format. ps.pageOutputs = make([]*pageOutput, len(ps.s.h.renderFormats))
那在Assemble中Hugo是如何組織pageState實(shí)例的呢?
從上圖中,可以看出Assemble階段主要是新建pageState。 其中pageOutput在這一階段只是一個(gè)占位符,空的nopPageOutput。 pageCommon則是在這一階段給賦予了很多的信息,像meta相關(guān)的信息,及各種細(xì)節(jié)信息的providers。
動(dòng)手實(shí)踐 - Show Me the Code of Create a PageState
package main import ( "fmt" "html/template" ) func main() { outputFormats := createOutputFormats() renderFormats := initRenderFormats(outputFormats) s := &site{ outputFormats: outputFormats, renderFormats: renderFormats, } ps := &pageState{ pageOutputs: nil, pageOutput: nil, pageCommon: &pageCommon{m: &pageMeta{kind: KindPage}}, } ps.init(s) // prepare ps.pageOutput = ps.pageOutputs[0] // render fmt.Println(ps.targetPaths().TargetFilename) fmt.Println(ps.Content()) fmt.Println(ps.m.kind) } type site struct { outputFormats map[string]Formats renderFormats Formats } type pageState struct { // This slice will be of same length as the number of global slice of output // formats (for all sites). pageOutputs []*pageOutput // This will be shifted out when we start to render a new output format. *pageOutput // Common for all output formats. *pageCommon } func (p *pageState) init(s *site) { pp := newPagePaths(s) p.pageOutputs = make([]*pageOutput, len(s.renderFormats)) for i, f := range s.renderFormats { ft, found := pp.targetPaths[f.Name] if !found { panic("target path not found") } providers := struct{ targetPather }{ft} po := &pageOutput{ f: f, pagePerOutputProviders: providers, ContentProvider: nil, } contentProvider := newPageContentOutput(po) po.ContentProvider = contentProvider p.pageOutputs[i] = po } } func newPageContentOutput(po *pageOutput) *pageContentOutput { cp := &pageContentOutput{ f: po.f, } initContent := func() { cp.content = template.HTML("<p>hello content</p>") } cp.initMain = func() { initContent() } return cp } func newPagePaths(s *site) pagePaths { outputFormats := s.renderFormats targets := make(map[string]targetPathsHolder) for _, f := range outputFormats { target := "/" + "blog" + "/" + f.BaseName + "." + f.MediaType.SubType paths := TargetPaths{ TargetFilename: target, } targets[f.Name] = targetPathsHolder{ paths: paths, } } return pagePaths{ targetPaths: targets, } } type pagePaths struct { targetPaths map[string]targetPathsHolder } type targetPathsHolder struct { paths TargetPaths } func (t targetPathsHolder) targetPaths() TargetPaths { return t.paths } type pageOutput struct { f Format // These interface provides the functionality that is specific for this // output format. pagePerOutputProviders ContentProvider // May be nil. cp *pageContentOutput } // pageContentOutput represents the Page content for a given output format. type pageContentOutput struct { f Format initMain func() content template.HTML } func (p *pageContentOutput) Content() any { p.initMain() return p.content } // these will be shifted out when rendering a given output format. type pagePerOutputProviders interface { targetPather } type targetPather interface { targetPaths() TargetPaths } type TargetPaths struct { // Where to store the file on disk relative to the publish dir. OS slashes. TargetFilename string } type ContentProvider interface { Content() any } type pageCommon struct { m *pageMeta } type pageMeta struct { // kind is the discriminator that identifies the different page types // in the different page collections. This can, as an example, be used // to to filter regular pages, find sections etc. // Kind will, for the pages available to the templates, be one of: // page, home, section, taxonomy and term. // It is of string type to make it easy to reason about in // the templates. kind string } func initRenderFormats( outputFormats map[string]Formats) Formats { return outputFormats[KindPage] } func createOutputFormats() map[string]Formats { m := map[string]Formats{ KindPage: {HTMLFormat}, } return m } const ( KindPage = "page" ) var HTMLType = newMediaType("text", "html") // HTMLFormat An ordered list of built-in output formats. var HTMLFormat = Format{ Name: "HTML", MediaType: HTMLType, BaseName: "index", } func newMediaType(main, sub string) Type { t := Type{ MainType: main, SubType: sub, Delimiter: "."} return t } type Type struct { MainType string `json:"mainType"` // i.e. text SubType string `json:"subType"` // i.e. html Delimiter string `json:"delimiter"` // e.g. "." } type Format struct { // The Name is used as an identifier. Internal output formats (i.e. HTML and RSS) // can be overridden by providing a new definition for those types. Name string `json:"name"` MediaType Type `json:"-"` // The base output file name used when not using "ugly URLs", defaults to "index". BaseName string `json:"baseName"` } type Formats []Format
輸出結(jié)果:
/blog/index.html <p>hello content</p> page Program exited.
Render
基礎(chǔ)信息是由pageCommon提供了,那渲染過(guò)程中的輸出由誰(shuí)提供呢?
沒錯(cuò),輪到pageOutput了:
可以看到,在render階段,pageState的pageOutput得到了最終的處理,為發(fā)布做準(zhǔn)備了。 為了發(fā)布,最重的信息是發(fā)布什么,以及發(fā)布到哪里去。 這些信息都在pageOutput中,其中ContentProvider是提供發(fā)布內(nèi)容的,而targetPathsProvider則是提供發(fā)布地址信息的。 其中地址信息主要來(lái)源于PagePath,這又和站點(diǎn)的RenderFormats和OutputFormats相關(guān),哪下圖所示:
其中OutputFormats, RenderFormats及PageOutput之間的關(guān)系有在 基礎(chǔ)架構(gòu)中有詳細(xì)提到,這里就不再贅述。
// We create a pageOutput for every output format combination, even if this // particular page isn't configured to be rendered to that format. type pageOutput struct { ... // These interface provides the functionality that is specific for this // output format. pagePerOutputProviders page.ContentProvider page.TableOfContentsProvider page.PageRenderProvider // May be nil. cp *pageContentOutput }
其中pageContentOutput正是實(shí)現(xiàn)了ContentProvider接口的實(shí)例。 其中有包含markdown文件原始信息的workContent字段,以及包含處理過(guò)后的內(nèi)容content字段。 如Hugo Shortcode特性。 就是在這里經(jīng)過(guò)contentToRender方法將原始信息進(jìn)行處理,而最終實(shí)現(xiàn)的。
動(dòng)手實(shí)踐 - Show Me the Code of Publish
package main import ( "bytes" "fmt" "io" "os" "path/filepath" ) // publisher needs to know: // 1: what to publish // 2: where to publish func main() { // 1 // src is template executed result // it is the source that we need to publish // take a look at template executor example // https://c.sunwei.xyz/template-executor.html src := &bytes.Buffer{} src.Write([]byte("template executed result")) b := &bytes.Buffer{} transformers := createTransformerChain() if err := transformers.Apply(b, src); err != nil { fmt.Println(err) return } dir, _ := os.MkdirTemp("", "hugo") defer os.RemoveAll(dir) // 2 // targetPath is from pageState // this is where we need to publish // take a look at page state example // https://c.sunwei.xyz/page-state.html targetPath := filepath.Join(dir, "index.html") if err := os.WriteFile( targetPath, bytes.TrimSuffix(b.Bytes(), []byte("\n")), os.ModePerm); err != nil { panic(err) } fmt.Println("1. what to publish: ", string(b.Bytes())) fmt.Println("2. where to publish: ", dir) } func (c *Chain) Apply(to io.Writer, from io.Reader) error { fb := &bytes.Buffer{} if _, err := fb.ReadFrom(from); err != nil { return err } tb := &bytes.Buffer{} ftb := &fromToBuffer{from: fb, to: tb} for i, tr := range *c { if i > 0 { panic("switch from/to and reset to") } if err := tr(ftb); err != nil { continue } } _, err := ftb.to.WriteTo(to) return err } func createTransformerChain() Chain { transformers := NewEmpty() transformers = append(transformers, func(ft FromTo) error { content := ft.From().Bytes() w := ft.To() tc := bytes.Replace( content, []byte("result"), []byte("transferred result"), 1) _, _ = w.Write(tc) return nil }) return transformers } // Chain is an ordered processing chain. The next transform operation will // receive the output from the previous. type Chain []Transformer // Transformer is the func that needs to be implemented by a transformation step. type Transformer func(ft FromTo) error // FromTo is sent to each transformation step in the chain. type FromTo interface { From() BytesReader To() io.Writer } // BytesReader wraps the Bytes method, usually implemented by bytes.Buffer, and an // io.Reader. type BytesReader interface { // Bytes 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 io.Reader } // NewEmpty creates a new slice of transformers with a capacity of 20. func NewEmpty() Chain { return make(Chain, 0, 2) } // Implements contentTransformer // Content is read from the from-buffer and rewritten to to the to-buffer. type fromToBuffer struct { from *bytes.Buffer to *bytes.Buffer } func (ft fromToBuffer) From() BytesReader { return ft.from } func (ft fromToBuffer) To() io.Writer { return ft.to }
輸出結(jié)果:
1. what to publish: template executed transferred result
2. where to publish: /tmp/hugo2834984546
Program exited.
以上就是go開源Hugo站點(diǎn)構(gòu)建三步曲之集結(jié)渲染的詳細(xì)內(nèi)容,更多關(guān)于go Hugo站點(diǎn)構(gòu)建集結(jié)渲染的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Go語(yǔ)言中init函數(shù)特點(diǎn)、用途和注意事項(xiàng)詳解
go語(yǔ)言中有一個(gè)非常神奇的函數(shù)init,它可以在所有程序執(zhí)行開始前被執(zhí)行,并且每個(gè)package下面可以存在多個(gè)init函數(shù),這篇文章主要給大家介紹了關(guān)于Go語(yǔ)言中init函數(shù)特點(diǎn)、用途和注意事項(xiàng)的相關(guān)資料,需要的朋友可以參考下2023-07-07go語(yǔ)言channel實(shí)現(xiàn)多核并行化運(yùn)行的方法
這篇文章主要介紹了go語(yǔ)言channel實(shí)現(xiàn)多核并行化運(yùn)行的方法,實(shí)例分析了channel實(shí)現(xiàn)多核并行化運(yùn)行的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-03-03golang將切片或數(shù)組根據(jù)某個(gè)字段進(jìn)行分組操作
這篇文章主要介紹了golang將切片或數(shù)組根據(jù)某個(gè)字段進(jìn)行分組操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-12-12手把手帶你走進(jìn)Go語(yǔ)言之循環(huán)語(yǔ)句
在不少實(shí)際問(wèn)題中有許多具有規(guī)律性的重復(fù)操作,因此在程序中就需要重復(fù)執(zhí)行某些語(yǔ)句。一組被重復(fù)執(zhí)行的語(yǔ)句稱之為循環(huán)體,能否繼續(xù)重復(fù),決定循環(huán)的終止條件,本文給大家介紹的非常詳細(xì),跟著小編往下看吧2021-09-09Go語(yǔ)言實(shí)現(xiàn)MapReduce的示例代碼
MapReduce是一種備受歡迎的編程模型,它最初由Google開發(fā),用于并行處理大規(guī)模數(shù)據(jù)以提取有價(jià)值的信息,本文將使用GO語(yǔ)言實(shí)現(xiàn)一個(gè)簡(jiǎn)單的MapReduce,需要的可以參考下2023-10-10gorm golang 并發(fā)連接數(shù)據(jù)庫(kù)報(bào)錯(cuò)的解決方法
今天小編就為大家分享一篇gorm golang 并發(fā)連接數(shù)據(jù)庫(kù)報(bào)錯(cuò)的解決方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-07-07