mirror of
https://github.com/gohugoio/hugo.git
synced 2025-03-20 04:12:03 +00:00
Nitro timer is encapsulated.
Remove the need for NewSite by relying on appropriate defaults. Renamed site.c to site.Config to allow Sites to be created outside the package.
This commit is contained in:
parent
e26b43f6d9
commit
309db474c7
3 changed files with 64 additions and 45 deletions
|
@ -41,7 +41,7 @@ func TestNewPageWithFilePath(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSettingOutFileOnPageContainsCorrectSlashes(t *testing.T) {
|
func TestSettingOutFileOnPageContainsCorrectSlashes(t *testing.T) {
|
||||||
s := NewSite(&Config{})
|
s := &Site{Config: Config{}}
|
||||||
p := NewPage(filepath.Join("sub", "foobar"))
|
p := NewPage(filepath.Join("sub", "foobar"))
|
||||||
s.setOutFile(p)
|
s.setOutFile(p)
|
||||||
}
|
}
|
||||||
|
|
103
hugolib/site.go
103
hugolib/site.go
|
@ -30,8 +30,10 @@ import (
|
||||||
|
|
||||||
const slash = string(os.PathSeparator)
|
const slash = string(os.PathSeparator)
|
||||||
|
|
||||||
|
var DefaultTimer = nitro.Initalize()
|
||||||
|
|
||||||
type Site struct {
|
type Site struct {
|
||||||
c Config
|
Config Config
|
||||||
Pages Pages
|
Pages Pages
|
||||||
Tmpl *template.Template
|
Tmpl *template.Template
|
||||||
Indexes IndexList
|
Indexes IndexList
|
||||||
|
@ -56,8 +58,11 @@ func (s *Site) getFromIndex(kind string, name string) Pages {
|
||||||
return s.Indexes[kind][name]
|
return s.Indexes[kind][name]
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewSite(config *Config) *Site {
|
func (s *Site) timerStep(step string) {
|
||||||
return &Site{c: *config, timer: nitro.Initalize()}
|
if s.timer == nil {
|
||||||
|
s.timer = DefaultTimer
|
||||||
|
}
|
||||||
|
s.timer.Step(step)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (site *Site) Build() (err error) {
|
func (site *Site) Build() (err error) {
|
||||||
|
@ -79,39 +84,39 @@ func (site *Site) Analyze() {
|
||||||
func (site *Site) Process() (err error) {
|
func (site *Site) Process() (err error) {
|
||||||
site.initialize()
|
site.initialize()
|
||||||
site.prepTemplates()
|
site.prepTemplates()
|
||||||
site.timer.Step("initialize & template prep")
|
site.timerStep("initialize & template prep")
|
||||||
site.CreatePages()
|
site.CreatePages()
|
||||||
site.setupPrevNext()
|
site.setupPrevNext()
|
||||||
site.timer.Step("import pages")
|
site.timerStep("import pages")
|
||||||
if err = site.BuildSiteMeta(); err != nil {
|
if err = site.BuildSiteMeta(); err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
site.timer.Step("build indexes")
|
site.timerStep("build indexes")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (site *Site) Render() (err error) {
|
func (site *Site) Render() (err error) {
|
||||||
site.ProcessShortcodes()
|
site.ProcessShortcodes()
|
||||||
site.timer.Step("render shortcodes")
|
site.timerStep("render shortcodes")
|
||||||
site.AbsUrlify()
|
site.AbsUrlify()
|
||||||
site.timer.Step("absolute URLify")
|
site.timerStep("absolute URLify")
|
||||||
site.RenderIndexes()
|
site.RenderIndexes()
|
||||||
site.RenderIndexesIndexes()
|
site.RenderIndexesIndexes()
|
||||||
site.timer.Step("render and write indexes")
|
site.timerStep("render and write indexes")
|
||||||
site.RenderLists()
|
site.RenderLists()
|
||||||
site.timer.Step("render and write lists")
|
site.timerStep("render and write lists")
|
||||||
if err = site.RenderPages(); err != nil {
|
if err = site.RenderPages(); err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
site.timer.Step("render pages")
|
site.timerStep("render pages")
|
||||||
site.RenderHomePage()
|
site.RenderHomePage()
|
||||||
site.timer.Step("render and write homepage")
|
site.timerStep("render and write homepage")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (site *Site) Write() {
|
func (site *Site) Write() {
|
||||||
site.WritePages()
|
site.WritePages()
|
||||||
site.timer.Step("write pages")
|
site.timerStep("write pages")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (site *Site) checkDescriptions() {
|
func (site *Site) checkDescriptions() {
|
||||||
|
@ -146,14 +151,14 @@ func (s *Site) prepTemplates() {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
text := string(filetext)
|
text := string(filetext)
|
||||||
name := path[len(s.c.GetAbsPath(s.c.LayoutDir))+1:]
|
name := path[len(s.Config.GetAbsPath(s.Config.LayoutDir))+1:]
|
||||||
t := templates.New(name)
|
t := templates.New(name)
|
||||||
template.Must(t.Parse(text))
|
template.Must(t.Parse(text))
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
filepath.Walk(s.c.GetAbsPath(s.c.LayoutDir), walker)
|
filepath.Walk(s.Config.GetAbsPath(s.Config.LayoutDir), walker)
|
||||||
|
|
||||||
s.Tmpl = templates
|
s.Tmpl = templates
|
||||||
}
|
}
|
||||||
|
@ -178,21 +183,33 @@ func (s *Site) initialize() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
filepath.Walk(s.c.GetAbsPath(s.c.ContentDir), walker)
|
filepath.Walk(s.Config.GetAbsPath(s.Config.ContentDir), walker)
|
||||||
|
|
||||||
s.Info = SiteInfo{BaseUrl: template.URL(s.c.BaseUrl), Title: s.c.Title, Config: &s.c}
|
s.Info = SiteInfo{BaseUrl: template.URL(s.Config.BaseUrl), Title: s.Config.Title, Config: &s.Config}
|
||||||
|
|
||||||
s.Shortcodes = make(map[string]ShortcodeFunc)
|
s.Shortcodes = make(map[string]ShortcodeFunc)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *Site) absLayoutDir() string {
|
||||||
|
return s.Config.GetAbsPath(s.Config.LayoutDir)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Site) absContentDir() string {
|
||||||
|
return s.Config.GetAbsPath(s.Config.ContentDir)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Site) absPublishDir() string {
|
||||||
|
return s.Config.GetAbsPath(s.Config.PublishDir)
|
||||||
|
}
|
||||||
|
|
||||||
func (s *Site) checkDirectories() {
|
func (s *Site) checkDirectories() {
|
||||||
if b, _ := dirExists(s.c.GetAbsPath(s.c.LayoutDir)); !b {
|
if b, _ := dirExists(s.absLayoutDir()); !b {
|
||||||
FatalErr("No layout directory found, expecting to find it at " + s.c.GetAbsPath(s.c.LayoutDir))
|
FatalErr("No layout directory found, expecting to find it at " + s.absLayoutDir())
|
||||||
}
|
}
|
||||||
if b, _ := dirExists(s.c.GetAbsPath(s.c.ContentDir)); !b {
|
if b, _ := dirExists(s.absContentDir()); !b {
|
||||||
FatalErr("No source directory found, expecting to find it at " + s.c.GetAbsPath(s.c.ContentDir))
|
FatalErr("No source directory found, expecting to find it at " + s.absContentDir())
|
||||||
}
|
}
|
||||||
mkdirIf(s.c.GetAbsPath(s.c.PublishDir))
|
mkdirIf(s.absPublishDir())
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Site) ProcessShortcodes() {
|
func (s *Site) ProcessShortcodes() {
|
||||||
|
@ -204,11 +221,11 @@ func (s *Site) ProcessShortcodes() {
|
||||||
func (s *Site) AbsUrlify() {
|
func (s *Site) AbsUrlify() {
|
||||||
for i, _ := range s.Pages {
|
for i, _ := range s.Pages {
|
||||||
content := string(s.Pages[i].Content)
|
content := string(s.Pages[i].Content)
|
||||||
content = strings.Replace(content, " src=\"/", " src=\""+s.c.BaseUrl, -1)
|
content = strings.Replace(content, " src=\"/", " src=\""+s.Config.BaseUrl+"/", -1)
|
||||||
content = strings.Replace(content, " src='/", " src='"+s.c.BaseUrl, -1)
|
content = strings.Replace(content, " src='/", " src='"+s.Config.BaseUrl+"/", -1)
|
||||||
content = strings.Replace(content, " href='/", " href='"+s.c.BaseUrl, -1)
|
content = strings.Replace(content, " href='/", " href='"+s.Config.BaseUrl+"/", -1)
|
||||||
content = strings.Replace(content, " href=\"/", " href=\""+s.c.BaseUrl, -1)
|
content = strings.Replace(content, " href=\"/", " href=\""+s.Config.BaseUrl+"/", -1)
|
||||||
baseWithoutTrailingSlash := strings.TrimRight(s.c.BaseUrl, "/")
|
baseWithoutTrailingSlash := strings.TrimRight(s.Config.BaseUrl, "/")
|
||||||
content = strings.Replace(content, baseWithoutTrailingSlash+"//", baseWithoutTrailingSlash+"/", -1)
|
content = strings.Replace(content, baseWithoutTrailingSlash+"//", baseWithoutTrailingSlash+"/", -1)
|
||||||
s.Pages[i].Content = template.HTML(content)
|
s.Pages[i].Content = template.HTML(content)
|
||||||
}
|
}
|
||||||
|
@ -220,7 +237,7 @@ func (s *Site) CreatePages() {
|
||||||
page.Site = s.Info
|
page.Site = s.Info
|
||||||
page.Tmpl = s.Tmpl
|
page.Tmpl = s.Tmpl
|
||||||
s.setOutFile(page)
|
s.setOutFile(page)
|
||||||
if s.c.BuildDrafts || !page.Draft {
|
if s.Config.BuildDrafts || !page.Draft {
|
||||||
s.Pages = append(s.Pages, page)
|
s.Pages = append(s.Pages, page)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -244,7 +261,7 @@ func (s *Site) BuildSiteMeta() (err error) {
|
||||||
s.Indexes = make(IndexList)
|
s.Indexes = make(IndexList)
|
||||||
s.Sections = make(Index)
|
s.Sections = make(Index)
|
||||||
|
|
||||||
for _, plural := range s.c.Indexes {
|
for _, plural := range s.Config.Indexes {
|
||||||
s.Indexes[plural] = make(Index)
|
s.Indexes[plural] = make(Index)
|
||||||
for i, p := range s.Pages {
|
for i, p := range s.Pages {
|
||||||
vals := p.GetParam(plural)
|
vals := p.GetParam(plural)
|
||||||
|
@ -272,7 +289,7 @@ func (s *Site) BuildSiteMeta() (err error) {
|
||||||
s.Info.Indexes = s.Indexes.BuildOrderedIndexList()
|
s.Info.Indexes = s.Indexes.BuildOrderedIndexList()
|
||||||
|
|
||||||
if len(s.Pages) == 0 {
|
if len(s.Pages) == 0 {
|
||||||
return errors.New(fmt.Sprintf("Unable to build site metadata, no pages found in directory %s", s.c.ContentDir))
|
return errors.New(fmt.Sprintf("Unable to build site metadata, no pages found in directory %s", s.Config.ContentDir))
|
||||||
}
|
}
|
||||||
s.Info.LastChange = s.Pages[0].Date
|
s.Info.LastChange = s.Pages[0].Date
|
||||||
|
|
||||||
|
@ -304,7 +321,7 @@ func (s *Site) WritePages() {
|
||||||
func (s *Site) setOutFile(p *Page) {
|
func (s *Site) setOutFile(p *Page) {
|
||||||
if len(strings.TrimSpace(p.Slug)) > 0 {
|
if len(strings.TrimSpace(p.Slug)) > 0 {
|
||||||
// Use Slug if provided
|
// Use Slug if provided
|
||||||
if s.c.UglyUrls {
|
if s.Config.UglyUrls {
|
||||||
p.OutFile = strings.TrimSpace(p.Slug + "." + p.Extension)
|
p.OutFile = strings.TrimSpace(p.Slug + "." + p.Extension)
|
||||||
} else {
|
} else {
|
||||||
p.OutFile = strings.TrimSpace(p.Slug + slash + "index.html")
|
p.OutFile = strings.TrimSpace(p.Slug + slash + "index.html")
|
||||||
|
@ -315,7 +332,7 @@ func (s *Site) setOutFile(p *Page) {
|
||||||
} else {
|
} else {
|
||||||
// Fall back to filename
|
// Fall back to filename
|
||||||
_, t := filepath.Split(p.FileName)
|
_, t := filepath.Split(p.FileName)
|
||||||
if s.c.UglyUrls {
|
if s.Config.UglyUrls {
|
||||||
p.OutFile = replaceExtension(strings.TrimSpace(t), p.Extension)
|
p.OutFile = replaceExtension(strings.TrimSpace(t), p.Extension)
|
||||||
} else {
|
} else {
|
||||||
file, _ := fileExt(strings.TrimSpace(t))
|
file, _ := fileExt(strings.TrimSpace(t))
|
||||||
|
@ -325,13 +342,13 @@ func (s *Site) setOutFile(p *Page) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Site) RenderIndexes() error {
|
func (s *Site) RenderIndexes() error {
|
||||||
for singular, plural := range s.c.Indexes {
|
for singular, plural := range s.Config.Indexes {
|
||||||
for k, o := range s.Indexes[plural] {
|
for k, o := range s.Indexes[plural] {
|
||||||
n := s.NewNode()
|
n := s.NewNode()
|
||||||
n.Title = strings.Title(k)
|
n.Title = strings.Title(k)
|
||||||
url := Urlize(plural + slash + k)
|
url := Urlize(plural + slash + k)
|
||||||
plink := url
|
plink := url
|
||||||
if s.c.UglyUrls {
|
if s.Config.UglyUrls {
|
||||||
n.Url = url + ".html"
|
n.Url = url + ".html"
|
||||||
plink = n.Url
|
plink = n.Url
|
||||||
} else {
|
} else {
|
||||||
|
@ -349,7 +366,7 @@ func (s *Site) RenderIndexes() error {
|
||||||
}
|
}
|
||||||
|
|
||||||
var base string
|
var base string
|
||||||
if s.c.UglyUrls {
|
if s.Config.UglyUrls {
|
||||||
base = plural + "/" + k
|
base = plural + "/" + k
|
||||||
} else {
|
} else {
|
||||||
base = plural + "/" + k + "/" + "index"
|
base = plural + "/" + k + "/" + "index"
|
||||||
|
@ -360,7 +377,7 @@ func (s *Site) RenderIndexes() error {
|
||||||
if a := s.Tmpl.Lookup("rss.xml"); a != nil {
|
if a := s.Tmpl.Lookup("rss.xml"); a != nil {
|
||||||
// XML Feed
|
// XML Feed
|
||||||
y := s.NewXMLBuffer()
|
y := s.NewXMLBuffer()
|
||||||
if s.c.UglyUrls {
|
if s.Config.UglyUrls {
|
||||||
n.Url = Urlize(plural + "/" + k + ".xml")
|
n.Url = Urlize(plural + "/" + k + ".xml")
|
||||||
} else {
|
} else {
|
||||||
n.Url = Urlize(plural + "/" + k + "/" + "index.xml")
|
n.Url = Urlize(plural + "/" + k + "/" + "index.xml")
|
||||||
|
@ -374,10 +391,10 @@ func (s *Site) RenderIndexes() error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Site) RenderIndexesIndexes() {
|
func (s *Site) RenderIndexesIndexes() (err error) {
|
||||||
layout := "indexes" + slash + "indexes.html"
|
layout := "indexes" + slash + "indexes.html"
|
||||||
if s.Tmpl.Lookup(layout) != nil {
|
if s.Tmpl.Lookup(layout) != nil {
|
||||||
for singular, plural := range s.c.Indexes {
|
for singular, plural := range s.Config.Indexes {
|
||||||
n := s.NewNode()
|
n := s.NewNode()
|
||||||
n.Title = strings.Title(plural)
|
n.Title = strings.Title(plural)
|
||||||
url := Urlize(plural)
|
url := Urlize(plural)
|
||||||
|
@ -388,10 +405,12 @@ func (s *Site) RenderIndexesIndexes() {
|
||||||
n.Data["Index"] = s.Indexes[plural]
|
n.Data["Index"] = s.Indexes[plural]
|
||||||
n.Data["OrderedIndex"] = s.Info.Indexes[plural]
|
n.Data["OrderedIndex"] = s.Info.Indexes[plural]
|
||||||
|
|
||||||
x := s.RenderThing(n, layout)
|
x, err := s.RenderThing(n, layout)
|
||||||
s.WritePublic(plural+slash+"index.html", x.Bytes())
|
s.WritePublic(plural+slash+"index.html", x.Bytes())
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Site) RenderLists() error {
|
func (s *Site) RenderLists() error {
|
||||||
|
@ -455,7 +474,7 @@ func (s *Site) RenderHomePage() error {
|
||||||
|
|
||||||
func (s *Site) Stats() {
|
func (s *Site) Stats() {
|
||||||
fmt.Printf("%d pages created \n", len(s.Pages))
|
fmt.Printf("%d pages created \n", len(s.Pages))
|
||||||
for _, pl := range s.c.Indexes {
|
for _, pl := range s.Config.Indexes {
|
||||||
fmt.Printf("%d %s created\n", len(s.Indexes[pl]), pl)
|
fmt.Printf("%d %s created\n", len(s.Indexes[pl]), pl)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -481,13 +500,13 @@ func (s *Site) NewXMLBuffer() *bytes.Buffer {
|
||||||
|
|
||||||
func (s *Site) WritePublic(path string, content []byte) {
|
func (s *Site) WritePublic(path string, content []byte) {
|
||||||
|
|
||||||
if s.c.Verbose {
|
if s.Config.Verbose {
|
||||||
fmt.Println(path)
|
fmt.Println(path)
|
||||||
}
|
}
|
||||||
|
|
||||||
path, filename := filepath.Split(path)
|
path, filename := filepath.Split(path)
|
||||||
|
|
||||||
path = filepath.FromSlash(s.c.GetAbsPath(filepath.Join(s.c.PublishDir, path)))
|
path = filepath.FromSlash(s.Config.GetAbsPath(filepath.Join(s.Config.PublishDir, path)))
|
||||||
err := mkdirIf(path)
|
err := mkdirIf(path)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
4
main.go
4
main.go
|
@ -102,7 +102,7 @@ func main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
if *checkMode {
|
if *checkMode {
|
||||||
site := hugolib.NewSite(config)
|
site := hugolib.Site{Config: *config}
|
||||||
site.Analyze()
|
site.Analyze()
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
}
|
}
|
||||||
|
@ -143,7 +143,7 @@ func serve(port string, config *hugolib.Config) {
|
||||||
|
|
||||||
func buildSite(config *hugolib.Config) (site *hugolib.Site, err error) {
|
func buildSite(config *hugolib.Config) (site *hugolib.Site, err error) {
|
||||||
startTime := time.Now()
|
startTime := time.Now()
|
||||||
site = hugolib.NewSite(config)
|
site = &hugolib.Site{Config: *config}
|
||||||
err = site.Build()
|
err = site.Build()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
|
|
Loading…
Reference in a new issue