- 1、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。。
- 2、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载。
- 3、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
第
go开源Hugo站点构建三步曲之集结渲染
目录AssemblePageState动手实践-ShowMetheCodeofCreateaPageStateRender动手实践-ShowMetheCodeofPublish
Assemble
Assemble所做的事情很纯粹,那就是创建站点页面实例-pageState。因为支持多站点,contentMaps有多个。所以Assemble不仅要创建pageState,还需要管理好所有的pages,这就用到了PageMaps。
typepageMapstruct{
s*Site
*contentMap
typepageMapsstruct{
workers*para.Workers
pmaps[]*pageMap
实际上pageMap就是由contentMap组合而来的。而contentMap中的组成树的结点就是contentNode。
正好,每个contentNode又对应一个pageState。
typecontentNodestruct{
p*pageState
//Setifsourceisafile.
//Wewillsoongetothersources.
fihugofs.FileMetaInfo
//Thesourcepath.Unixslashes.Noleadingslash.
pathstring
所以Assemble不仅要为前面Process处理过生成的contentNode创建pageState,还要补齐一些缺失的contentNode,如Section。
PageState
可以看出,Assemble的重点就是组建PageState,那她到底长啥样:
typepageStatestruct{
//Thisslicewillbeofsamelengthasthenumberofglobalsliceofoutput
//formats(forallsites).
pageOutputs[]*pageOutput
//Thiswillbeshiftedoutwhenwestarttorenderanewoutputformat.
*pageOutput
//Commonforalloutputformats.
*pageCommon
从注解中可以看出普通信息将由pageCommon提供,而输出信息则由pageOutput提供。比较特殊的是pageOutputs,是pageOutput的数组。在基础架构中,对这一点有作分析。这要归因于Hugo的多站点渲染策略-允许在不同的站点中重用其它站点的页面。
//hugo-playground/hugolib/page__new.go
//line97
//Prepareoutputformatsforallsites.
//Wedothisevenifthispagedoesnotgetrenderedon
//itsown.Itmaybereferencedvia.Site.GetPageand
//itwillthenneedanoutputformat.
ps.pageOutputs=make([]*pageOutput,len(ps.s.h.renderFormats))
那在Assemble中Hugo是如何组织pageState实例的呢?
从上图中,可以看出Assemble阶段主要是新建pageState。其中pageOutput在这一阶段只是一个占位符,空的nopPageOutput。pageCommon则是在这一阶段给赋予了很多的信息,像meta相关的信息,及各种细节信息的providers。
动手实践-ShowMetheCodeofCreateaPageState
packagemain
import(
fmt
html/template
funcmain(){
outputFormats:=createOutputFormats()
renderFormats:=initRenderFormats(outputFormats)
s:=sit
文档评论(0)