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:
Noah Campbell 2013-08-07 17:21:22 -07:00
parent e26b43f6d9
commit 309db474c7
3 changed files with 64 additions and 45 deletions

View file

@ -41,7 +41,7 @@ func TestNewPageWithFilePath(t *testing.T) {
}
func TestSettingOutFileOnPageContainsCorrectSlashes(t *testing.T) {
s := NewSite(&Config{})
s := &Site{Config: Config{}}
p := NewPage(filepath.Join("sub", "foobar"))
s.setOutFile(p)
}

View file

@ -30,8 +30,10 @@ import (
const slash = string(os.PathSeparator)
var DefaultTimer = nitro.Initalize()
type Site struct {
c Config
Config Config
Pages Pages
Tmpl *template.Template
Indexes IndexList
@ -56,8 +58,11 @@ func (s *Site) getFromIndex(kind string, name string) Pages {
return s.Indexes[kind][name]
}
func NewSite(config *Config) *Site {
return &Site{c: *config, timer: nitro.Initalize()}
func (s *Site) timerStep(step string) {
if s.timer == nil {
s.timer = DefaultTimer
}
s.timer.Step(step)
}
func (site *Site) Build() (err error) {
@ -79,39 +84,39 @@ func (site *Site) Analyze() {
func (site *Site) Process() (err error) {
site.initialize()
site.prepTemplates()
site.timer.Step("initialize & template prep")
site.timerStep("initialize & template prep")
site.CreatePages()
site.setupPrevNext()
site.timer.Step("import pages")
site.timerStep("import pages")
if err = site.BuildSiteMeta(); err != nil {
return
}
site.timer.Step("build indexes")
site.timerStep("build indexes")
return
}
func (site *Site) Render() (err error) {
site.ProcessShortcodes()
site.timer.Step("render shortcodes")
site.timerStep("render shortcodes")
site.AbsUrlify()
site.timer.Step("absolute URLify")
site.timerStep("absolute URLify")
site.RenderIndexes()
site.RenderIndexesIndexes()
site.timer.Step("render and write indexes")
site.timerStep("render and write indexes")
site.RenderLists()
site.timer.Step("render and write lists")
site.timerStep("render and write lists")
if err = site.RenderPages(); err != nil {
return
}
site.timer.Step("render pages")
site.timerStep("render pages")
site.RenderHomePage()
site.timer.Step("render and write homepage")
site.timerStep("render and write homepage")
return
}
func (site *Site) Write() {
site.WritePages()
site.timer.Step("write pages")
site.timerStep("write pages")
}
func (site *Site) checkDescriptions() {
@ -146,14 +151,14 @@ func (s *Site) prepTemplates() {
return err
}
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)
template.Must(t.Parse(text))
}
return nil
}
filepath.Walk(s.c.GetAbsPath(s.c.LayoutDir), walker)
filepath.Walk(s.Config.GetAbsPath(s.Config.LayoutDir), walker)
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)
}
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() {
if b, _ := dirExists(s.c.GetAbsPath(s.c.LayoutDir)); !b {
FatalErr("No layout directory found, expecting to find it at " + s.c.GetAbsPath(s.c.LayoutDir))
if b, _ := dirExists(s.absLayoutDir()); !b {
FatalErr("No layout directory found, expecting to find it at " + s.absLayoutDir())
}
if b, _ := dirExists(s.c.GetAbsPath(s.c.ContentDir)); !b {
FatalErr("No source directory found, expecting to find it at " + s.c.GetAbsPath(s.c.ContentDir))
if b, _ := dirExists(s.absContentDir()); !b {
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() {
@ -204,11 +221,11 @@ func (s *Site) ProcessShortcodes() {
func (s *Site) AbsUrlify() {
for i, _ := range s.Pages {
content := string(s.Pages[i].Content)
content = strings.Replace(content, " src=\"/", " src=\""+s.c.BaseUrl, -1)
content = strings.Replace(content, " src='/", " src='"+s.c.BaseUrl, -1)
content = strings.Replace(content, " href='/", " href='"+s.c.BaseUrl, -1)
content = strings.Replace(content, " href=\"/", " href=\""+s.c.BaseUrl, -1)
baseWithoutTrailingSlash := strings.TrimRight(s.c.BaseUrl, "/")
content = strings.Replace(content, " src=\"/", " src=\""+s.Config.BaseUrl+"/", -1)
content = strings.Replace(content, " src='/", " src='"+s.Config.BaseUrl+"/", -1)
content = strings.Replace(content, " href='/", " href='"+s.Config.BaseUrl+"/", -1)
content = strings.Replace(content, " href=\"/", " href=\""+s.Config.BaseUrl+"/", -1)
baseWithoutTrailingSlash := strings.TrimRight(s.Config.BaseUrl, "/")
content = strings.Replace(content, baseWithoutTrailingSlash+"//", baseWithoutTrailingSlash+"/", -1)
s.Pages[i].Content = template.HTML(content)
}
@ -220,7 +237,7 @@ func (s *Site) CreatePages() {
page.Site = s.Info
page.Tmpl = s.Tmpl
s.setOutFile(page)
if s.c.BuildDrafts || !page.Draft {
if s.Config.BuildDrafts || !page.Draft {
s.Pages = append(s.Pages, page)
}
}
@ -244,7 +261,7 @@ func (s *Site) BuildSiteMeta() (err error) {
s.Indexes = make(IndexList)
s.Sections = make(Index)
for _, plural := range s.c.Indexes {
for _, plural := range s.Config.Indexes {
s.Indexes[plural] = make(Index)
for i, p := range s.Pages {
vals := p.GetParam(plural)
@ -272,7 +289,7 @@ func (s *Site) BuildSiteMeta() (err error) {
s.Info.Indexes = s.Indexes.BuildOrderedIndexList()
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
@ -304,7 +321,7 @@ func (s *Site) WritePages() {
func (s *Site) setOutFile(p *Page) {
if len(strings.TrimSpace(p.Slug)) > 0 {
// Use Slug if provided
if s.c.UglyUrls {
if s.Config.UglyUrls {
p.OutFile = strings.TrimSpace(p.Slug + "." + p.Extension)
} else {
p.OutFile = strings.TrimSpace(p.Slug + slash + "index.html")
@ -315,7 +332,7 @@ func (s *Site) setOutFile(p *Page) {
} else {
// Fall back to filename
_, t := filepath.Split(p.FileName)
if s.c.UglyUrls {
if s.Config.UglyUrls {
p.OutFile = replaceExtension(strings.TrimSpace(t), p.Extension)
} else {
file, _ := fileExt(strings.TrimSpace(t))
@ -325,13 +342,13 @@ func (s *Site) setOutFile(p *Page) {
}
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] {
n := s.NewNode()
n.Title = strings.Title(k)
url := Urlize(plural + slash + k)
plink := url
if s.c.UglyUrls {
if s.Config.UglyUrls {
n.Url = url + ".html"
plink = n.Url
} else {
@ -349,7 +366,7 @@ func (s *Site) RenderIndexes() error {
}
var base string
if s.c.UglyUrls {
if s.Config.UglyUrls {
base = plural + "/" + k
} else {
base = plural + "/" + k + "/" + "index"
@ -360,7 +377,7 @@ func (s *Site) RenderIndexes() error {
if a := s.Tmpl.Lookup("rss.xml"); a != nil {
// XML Feed
y := s.NewXMLBuffer()
if s.c.UglyUrls {
if s.Config.UglyUrls {
n.Url = Urlize(plural + "/" + k + ".xml")
} else {
n.Url = Urlize(plural + "/" + k + "/" + "index.xml")
@ -374,10 +391,10 @@ func (s *Site) RenderIndexes() error {
return nil
}
func (s *Site) RenderIndexesIndexes() {
func (s *Site) RenderIndexesIndexes() (err error) {
layout := "indexes" + slash + "indexes.html"
if s.Tmpl.Lookup(layout) != nil {
for singular, plural := range s.c.Indexes {
for singular, plural := range s.Config.Indexes {
n := s.NewNode()
n.Title = strings.Title(plural)
url := Urlize(plural)
@ -388,10 +405,12 @@ func (s *Site) RenderIndexesIndexes() {
n.Data["Index"] = s.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())
return err
}
}
return
}
func (s *Site) RenderLists() error {
@ -455,7 +474,7 @@ func (s *Site) RenderHomePage() error {
func (s *Site) Stats() {
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)
}
}
@ -481,13 +500,13 @@ func (s *Site) NewXMLBuffer() *bytes.Buffer {
func (s *Site) WritePublic(path string, content []byte) {
if s.c.Verbose {
if s.Config.Verbose {
fmt.Println(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)
if err != nil {

View file

@ -102,7 +102,7 @@ func main() {
}
if *checkMode {
site := hugolib.NewSite(config)
site := hugolib.Site{Config: *config}
site.Analyze()
os.Exit(0)
}
@ -143,7 +143,7 @@ func serve(port string, config *hugolib.Config) {
func buildSite(config *hugolib.Config) (site *hugolib.Site, err error) {
startTime := time.Now()
site = hugolib.NewSite(config)
site = &hugolib.Site{Config: *config}
err = site.Build()
if err != nil {
return