@@ -3,6 +3,7 @@ package main
33import (
44 "fmt"
55 "html/template"
6+ "io/ioutil"
67 "os"
78 "path/filepath"
89 "sort"
@@ -64,11 +65,28 @@ func Build() {
6465 themePath = filepath .Join (rootPath , globalConfig .Site .Theme )
6566 publicPath = filepath .Join (rootPath , "public" )
6667 sourcePath = filepath .Join (rootPath , "source" )
68+ // Append all partial html
69+ var partialTpl string
70+ files , _ := filepath .Glob (filepath .Join (themePath , "*.html" ))
71+ for _ , path := range files {
72+ fileExt := strings .ToLower (filepath .Ext (path ))
73+ baseName := strings .ToLower (filepath .Base (path ))
74+ if fileExt == ".html" && strings .HasPrefix (baseName , "_" ) {
75+ html , err := ioutil .ReadFile (path )
76+ if err != nil {
77+ Fatal (err .Error ())
78+ }
79+ tplName := strings .TrimPrefix (baseName , "_" )
80+ tplName = strings .TrimSuffix (tplName , ".html" )
81+ htmlStr := "{{define \" " + tplName + "\" }}" + string (html ) + "{{end}}"
82+ partialTpl += htmlStr
83+ }
84+ }
6785 // Compile template
68- articleTpl = CompileTpl (filepath .Join (themePath , "article.html" ), "article" )
69- pageTpl = CompileTpl (filepath .Join (themePath , "page.html" ), "page" )
70- archiveTpl = CompileTpl (filepath .Join (themePath , "archive.html" ), "archive" )
71- tagTpl = CompileTpl (filepath .Join (themePath , "tag.html" ), "tag" )
86+ articleTpl = CompileTpl (filepath .Join (themePath , "article.html" ), partialTpl , "article" )
87+ pageTpl = CompileTpl (filepath .Join (themePath , "page.html" ), partialTpl , "page" )
88+ archiveTpl = CompileTpl (filepath .Join (themePath , "archive.html" ), partialTpl , "archive" )
89+ tagTpl = CompileTpl (filepath .Join (themePath , "tag.html" ), partialTpl , "tag" )
7290 // Clean public folder
7391 cleanPatterns := []string {"post" , "tag" , "images" , "js" , "css" , "*.html" , "favicon.ico" , "robots.txt" }
7492 for _ , pattern := range cleanPatterns {
@@ -119,21 +137,21 @@ func Build() {
119137 Link : article .Link ,
120138 }
121139 archiveMap [dateYear ] = append (archiveMap [dateYear ], articleInfo )
122- // Render article
123- wg .Add (1 )
124- go RenderPage (articleTpl , article , filepath .Join (publicPath , outPath ))
125140 }
126141 return nil
127142 })
128143 // Sort by date
129144 sort .Sort (articles )
130- // Generate article pages
145+ // Render article
146+ wg .Add (1 )
147+ go RenderArticles (articleTpl , articles )
148+ // Generate article list pages
131149 wg .Add (1 )
132- go RenderArticles ("" , articles , "" )
133- // Generate tags pages
150+ go RenderArticleList ("" , articles , "" )
151+ // Generate article list pages by tag
134152 for tagName , articles := range tagMap {
135153 wg .Add (1 )
136- go RenderArticles (filepath .Join ("tag" , tagName ), articles , tagName )
154+ go RenderArticleList (filepath .Join ("tag" , tagName ), articles , tagName )
137155 }
138156 // Generate archive page
139157 archives := make (Collections , 0 )
@@ -181,12 +199,12 @@ func Build() {
181199 "Site" : globalConfig .Site ,
182200 }, filepath .Join (publicPath , "tag.html" ))
183201 // Generate other pages
184- files , _ : = filepath .Glob (filepath .Join (sourcePath , "*.html" ))
202+ files , _ = filepath .Glob (filepath .Join (sourcePath , "*.html" ))
185203 for _ , path := range files {
186204 fileExt := strings .ToLower (filepath .Ext (path ))
187205 baseName := filepath .Base (path )
188- if fileExt == ".html" {
189- htmlTpl := CompileTpl (path , baseName )
206+ if fileExt == ".html" && ! strings . HasPrefix ( baseName , "_" ) {
207+ htmlTpl := CompileTpl (path , partialTpl , baseName )
190208 relPath , _ := filepath .Rel (sourcePath , path )
191209 wg .Add (1 )
192210 go RenderPage (htmlTpl , globalConfig , filepath .Join (publicPath , relPath ))
0 commit comments