Skip to content

Commit

Permalink
feat: add 404.html
Browse files Browse the repository at this point in the history
  • Loading branch information
fuxiaohei committed May 9, 2022
1 parent 129107c commit 76ed8ea
Show file tree
Hide file tree
Showing 17 changed files with 153 additions and 69 deletions.
2 changes: 1 addition & 1 deletion cmd/pugo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ var (
Name: "version",
Usage: "print the version of PuGo",
Action: func(c *cli.Context) error {
fmt.Println(constants.AppName(), "v"+version)
fmt.Println(constants.AppName(), version)
return nil
},
},
Expand Down
5 changes: 4 additions & 1 deletion pkg/cmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ func NewServer() *cli.Command {

generator.Generate(opt)

s := server.New(opt.OutputDir, c.Int("port"))
s := server.New(server.ServerOption{
Port: c.Int("port"),
Dir: opt.OutputDir,
})
s.Run()

return nil
Expand Down
31 changes: 18 additions & 13 deletions pkg/core/generator/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,20 @@ import (
)

type renderBaseParams struct {
Ctx *Context
Render *theme.Render
OutputDir string
Ctx *Context
Render *theme.Render
OutputDir string
SiteTitle string
SiteDescription string
}

func newRenderBaseParams(siteData *SiteData, context *Context, opt *Option) renderBaseParams {
return renderBaseParams{
Ctx: context,
Render: siteData.Render,
OutputDir: opt.OutputDir,
Ctx: context,
Render: siteData.Render,
OutputDir: opt.OutputDir,
SiteTitle: siteData.SiteConfig.Title,
SiteDescription: siteData.SiteConfig.Description,
}
}

Expand All @@ -26,8 +30,6 @@ func Render(siteData *SiteData, context *Context, opt *Option) error {
if err := renderPosts(&renderPostsParams{
renderBaseParams: renderBase,
Posts: siteData.Posts,
SiteDesc: siteData.SiteConfig.Description,
SiteTitle: siteData.SiteConfig.Title,
}); err != nil {
zlog.Warnf("render posts failed: %v", err)
return err
Expand All @@ -37,7 +39,6 @@ func Render(siteData *SiteData, context *Context, opt *Option) error {
Pager: siteData.PostsPager,
Posts: siteData.Posts,
PostPageLinkFormat: siteData.BuildConfig.PostPageLinkFormat,
SiteTitle: siteData.SiteConfig.Title,
}
if err := renderPostLists(postListParams); err != nil {
zlog.Warnf("render post lists failed: %v", err)
Expand All @@ -52,15 +53,13 @@ func Render(siteData *SiteData, context *Context, opt *Option) error {
Tags: siteData.Tags,
PostPerPage: siteData.BuildConfig.PostPerPage,
TagPageLinkFormat: siteData.BuildConfig.TagPageLinkFormat,
SiteTitle: siteData.SiteConfig.Title,
}); err != nil {
zlog.Warnf("render tags failed: %v", err)
return err
}
if err := renderArchives(&renderArchivesParams{
renderBaseParams: renderBase,
Posts: siteData.Posts,
SiteTitle: siteData.SiteConfig.Title,
ArchivesLink: siteData.BuildConfig.ArchivesLink,
}); err != nil {
zlog.Warnf("render archives failed: %v", err)
Expand All @@ -70,13 +69,19 @@ func Render(siteData *SiteData, context *Context, opt *Option) error {
if err := renderPages(&renderPagesParams{
renderBaseParams: renderBase,
Pages: siteData.Pages,
SiteDesc: siteData.SiteConfig.Description,
SiteTitle: siteData.SiteConfig.Title,
}); err != nil {
zlog.Warnf("render pages failed: %v", err)
return err
}

if err := renderErrorPage(&renderErrorPageParams{
renderBaseParams: renderBase,
SiteTitle: siteData.SiteConfig.Title,
}); err != nil {
zlog.Warnf("render error page failed: %v", err)
return err
}

// render feed
out, err := feed.Render(&feed.RenderParams{
Posts: siteData.Posts,
Expand Down
4 changes: 2 additions & 2 deletions pkg/core/generator/render_archives.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
type renderArchivesParams struct {
renderBaseParams
Posts []*models.Post
SiteTitle string
ArchivesLink string
}

Expand All @@ -23,7 +22,8 @@ func renderArchives(params *renderArchivesParams) error {
extData := map[string]interface{}{
"archives": archives,
"current": map[string]interface{}{
"Title": params.SiteTitle,
"Title": params.SiteTitle,
"Description": params.SiteDescription,
},
}
tplData := params.Ctx.createTemplateData(extData)
Expand Down
32 changes: 32 additions & 0 deletions pkg/core/generator/render_errorpage.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package generator

import (
"bytes"
"path/filepath"
"pugo/pkg/utils/zlog"
)

type renderErrorPageParams struct {
renderBaseParams
SiteTitle string
}

func renderErrorPage(params *renderErrorPageParams) error {
notFoundTpl := params.Render.GetTemplate("404")
tplData := params.Ctx.createTemplateData(map[string]interface{}{
"current": map[string]interface{}{
"Title": params.SiteTitle,
},
})

buf := bytes.NewBuffer(nil)
if err := params.Render.Execute(buf, notFoundTpl, tplData); err != nil {
zlog.Warn("failed to render 404", "err", err)
return err
}
link := "/404.html"
dstFile := filepath.Join(params.OutputDir, link)
params.Ctx.SetOutput(dstFile, link, buf)
zlog.Infof("404 generated: %s", dstFile)
return nil
}
6 changes: 2 additions & 4 deletions pkg/core/generator/render_pages.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ import (

type renderPagesParams struct {
renderBaseParams
Pages []*models.Page
SiteDesc string
SiteTitle string
Pages []*models.Page
}

func renderPages(params *renderPagesParams) error {
Expand All @@ -28,7 +26,7 @@ func renderPages(params *renderPagesParams) error {
if page.Descripition != "" {
return page.Descripition
}
return params.SiteDesc
return params.SiteDescription
}
)
// build each page
Expand Down
10 changes: 4 additions & 6 deletions pkg/core/generator/render_posts.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ import (

type renderPostsParams struct {
renderBaseParams
Posts []*models.Post
SiteDesc string
SiteTitle string
Posts []*models.Post
}

func renderPosts(params *renderPostsParams) error {
Expand All @@ -28,7 +26,7 @@ func renderPosts(params *renderPostsParams) error {
if post.Descripition != "" {
return post.Descripition
}
return params.SiteDesc
return params.SiteDescription
}
)

Expand Down Expand Up @@ -81,7 +79,6 @@ type renderPostListsParams struct {
Pager *models.Pager
Posts []*models.Post
PostPageLinkFormat string
SiteTitle string
}

func buildPostListTemplateData(params *renderPostListsParams, page int) (map[string]interface{}, *models.PagerItem) {
Expand All @@ -90,7 +87,8 @@ func buildPostListTemplateData(params *renderPostListsParams, page int) (map[str
"posts": models.PostsPageList(params.Posts, pageItem),
"pager": pageItem,
"current": map[string]interface{}{
"Title": params.SiteTitle,
"Title": params.SiteTitle,
"Description": params.SiteDescription,
},
})
return tplData, pageItem
Expand Down
4 changes: 2 additions & 2 deletions pkg/core/generator/render_tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ type renderTagsParams struct {
Tags []*models.TagPosts
PostPerPage int
TagPageLinkFormat string
SiteTitle string
}

func renderTags(params *renderTagsParams) error {
Expand All @@ -39,7 +38,8 @@ func renderTags(params *renderTagsParams) error {
"pager": pageItem,
"tag": tagData.Tag,
"current": map[string]interface{}{
"Title": tagData.Tag.Name + "-" + params.SiteTitle,
"Title": tagData.Tag.Name + "-" + params.SiteTitle,
"Description": tagData.Tag.Name + " - " + params.SiteDescription,
},
})

Expand Down
19 changes: 11 additions & 8 deletions pkg/core/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,24 @@ import (

// Server is the server.
type Server struct {
dir string
port int
opt ServerOption
}

type ServerOption struct {
Port int
Dir string
}

// New returns a new server.
func New(dir string, port int) *Server {
func New(opt ServerOption) *Server {
return &Server{
dir: dir,
port: port,
opt: opt,
}
}

// Run runs the server.
func (s *Server) Run() error {
http.Handle("/", http.FileServer(http.Dir(s.dir)))
zlog.Infof("listening on port %d, serving %s", s.port, s.dir)
return http.ListenAndServe(":"+fmt.Sprintf("%d", s.port), nil)
http.Handle("/", http.FileServer(http.Dir(s.opt.Dir)))
zlog.Infof("listening on port %d, serving %s", s.opt.Port, s.opt.Dir)
return http.ListenAndServe(":"+fmt.Sprintf("%d", s.opt.Port), nil)
}
26 changes: 14 additions & 12 deletions pkg/core/theme/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,24 @@ package theme

// Config is the theme config.
type Config struct {
Name string `toml:"name"`
IndexTemplate string `toml:"index_template"`
Extension []string `toml:"extension"`
StaticDirs []string `toml:"static_dirs"`
EnableDarkMode bool `toml:"enable_dark_mode"`
ShowPuGoVersion bool `toml:"show_pugo_version"`
Name string `toml:"name"`
IndexTemplate string `toml:"index_template"`
NotFoundTemplate string `toml:"not_found_template"`
Extension []string `toml:"extension"`
StaticDirs []string `toml:"static_dirs"`
EnableDarkMode bool `toml:"enable_dark_mode"`
ShowPuGoVersion bool `toml:"show_pugo_version"`
}

// NewDefaultConfig returns default theme config
func NewDefaultConfig() *Config {
return &Config{
Name: "theme",
Extension: []string{".html"},
IndexTemplate: "post-list.html",
StaticDirs: []string{"static"},
EnableDarkMode: false,
ShowPuGoVersion: false,
Name: "theme",
Extension: []string{".html"},
IndexTemplate: "post-list.html",
NotFoundTemplate: "404.html",
StaticDirs: []string{"static"},
EnableDarkMode: false,
ShowPuGoVersion: false,
}
}
13 changes: 12 additions & 1 deletion pkg/core/theme/theme.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,18 @@ func (r *Render) Execute(w io.Writer, name string, data interface{}) error {

// GetIndexTemplate gets index template
func (r *Render) GetIndexTemplate() string {
return r.config.IndexTemplate
return r.GetTemplate("index")
}

// GetTemplate gets template by name
func (r *Render) GetTemplate(name string) string {
if name == "index" || name == "home" {
return r.config.IndexTemplate
}
if name == "notfound" || name == "error" || name == "404" || name == "not-found" {
return r.config.NotFoundTemplate
}
return ""
}

// GetDir gets theme dir
Expand Down
19 changes: 19 additions & 0 deletions themes/default/404.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<!DOCTYPE html>
<html>
{{template "head.html" .}}

<body>
{{template "header.html" .}}
<main class="main">
<div class="main-container">
<div class="not-found">
<h1>404</h1>
<p>Sorry, this page does not exist.</p>
<p>You can head back to the <a href="/">homepage</a>.</p>
</div>
</div>
</main>
{{template "footer.html" .}}
</body>

</html>
2 changes: 1 addition & 1 deletion themes/default/footer.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<div class="footer-right">
{{- if .server.Local}}<span>Local Server</span><span class="post-meta-gap">|</span>{{end -}}
<span>Base on <a href="https://github.com/fuxiaohei/pugo" target="_blank"
class="footer-item">{{.pugo.Name}}{{if .theme.ShowPuGoVersion}} v{{.pugo.Version}}{{end}}</a></span>
class="footer-item">{{.pugo.Name}}{{if .theme.ShowPuGoVersion}} {{.pugo.Version}}{{end}}</a></span>
</div>
</div>
</footer>
Expand Down
1 change: 1 addition & 0 deletions themes/default/partial/analytics.html
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,5 @@
{{end -}}
{{end -}}


{{end -}}
33 changes: 16 additions & 17 deletions themes/default/partial/sidebar.html
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
<div class="sidebar">
<div class="sidebar-profile">
<div class="profile-card mx-auto">
<img class="profile-avatar" src="{{.author.AvatarLink}}" alt="">
<div class="profile-name">{{.author.Name}}</div>
<div class="profile-bio">{{.author.Bio}}</div>
{{if .author.HasSocials}}{{template "partial/social.html" .}}{{end}}
<div class="main-sidebar">
<div class="sidebar">
<div class="sidebar-profile">
<div class="profile-card mx-auto">
<img class="profile-avatar" src="{{.author.AvatarLink}}" alt="">
<div class="profile-name">{{.author.Name}}</div>
<div class="profile-bio">{{.author.Bio}}</div>
{{if .author.HasSocials}}{{template "partial/social.html" .}}{{end}}
</div>
</div>
</div>
<div class="sidebar-tags">
<h4 class="tags-title">Tags</h4>
<div class="tags-list">
{{range .tags}}<a
class=""
href="{{.Link}}">
{{.Name}}<span
class="tags-post-count">{{.PostCount}}</span>
</a>{{end}}
<div class="sidebar-tags">
<h4 class="tags-title">Tags</h4>
<div class="tags-list">
{{range .tags}}<a class="" href="{{.Link}}">
{{.Name}}<span class="tags-post-count">{{.PostCount}}</span>
</a>{{end}}
</div>
</div>
</div>
</div>
Loading

0 comments on commit 76ed8ea

Please sign in to comment.