Skip to content

Commit ce8f69b

Browse files
committed
add prev, next nav
1 parent b1f14d8 commit ce8f69b

16 files changed

Lines changed: 284 additions & 118 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ InkPaper is an static blog generator developed by Golang, No dependencies, Easy
66

77
### Quick Start
88
- Download & Extract [Ink](http://www.inkpaper.io/),Run `ink`
9-
- Open `http://localhost:8888` in browser to preview
9+
- Open `http://localhost:8000` in browser to preview
1010

1111
### Configuration
1212
Edit `config.yml`, use format:

build.go

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package main
33
import (
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))

render.go

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,30 @@ import (
88
"strconv"
99
)
1010

11-
func CompileTpl(tplPath string, name string) template.Template {
11+
type RenderArticle struct {
12+
Article
13+
Next *Article
14+
Prev *Article
15+
}
16+
17+
// Compile html template
18+
func CompileTpl(tplPath string, partialTpl string, name string) template.Template {
1219
// Read template data from file
1320
html, err := ioutil.ReadFile(tplPath)
1421
if err != nil {
1522
Fatal(err.Error())
1623
}
24+
// Append partial template
25+
htmlStr := string(html) + partialTpl
1726
// Generate html content
18-
tpl, err := template.New(name).Parse(string(html))
27+
tpl, err := template.New(name).Parse(htmlStr)
1928
if err != nil {
2029
Fatal(err.Error())
2130
}
2231
return *tpl
2332
}
2433

34+
// Render html file by data
2535
func RenderPage(tpl template.Template, tplData interface{}, outPath string) {
2636
// Create file
2737
outFile, err := os.Create(outPath)
@@ -39,8 +49,29 @@ func RenderPage(tpl template.Template, tplData interface{}, outPath string) {
3949
}
4050
}
4151

42-
// Generate html file by article data
43-
func RenderArticles(rootPath string, articles Collections, tagName string) {
52+
// Generate all article page
53+
func RenderArticles(tpl template.Template, articles Collections) {
54+
defer wg.Done()
55+
articleCount := len(articles)
56+
for i, _ := range articles {
57+
currentArticle := articles[i].(Article)
58+
var renderArticle = RenderArticle{currentArticle, nil, nil}
59+
if i >= 1 {
60+
article := articles[i-1].(Article)
61+
renderArticle.Prev = &article
62+
}
63+
if i <= articleCount-2 {
64+
article := articles[i+1].(Article)
65+
renderArticle.Next = &article
66+
}
67+
outPath := filepath.Join(publicPath, currentArticle.Link)
68+
wg.Add(1)
69+
go RenderPage(tpl, renderArticle, outPath)
70+
}
71+
}
72+
73+
// Generate article list page
74+
func RenderArticleList(rootPath string, articles Collections, tagName string) {
4475
defer wg.Done()
4576
// Create path
4677
pagePath := filepath.Join(publicPath, rootPath)

template/source/images/avatar.png

773 Bytes
Loading

template/source/ink-blog-tool-en.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ InkPaper is an static blog generator developed by Golang, No dependencies, Easy
1818

1919
### Quick Start
2020
- Download & Extract [Ink](http://www.inkpaper.io/),Run `ink`
21-
- Open `http://localhost:8888` in browser to preview
21+
- Open `http://localhost:8000` in browser to preview
2222

2323
### Website Configuration
2424
Edit `config.yml`, use format:

template/source/ink-blog-tool.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ preview: 纸小墨(InkPaper)是一个使用GO语言编写的静态博客构
1818

1919
### 开始上手
2020
- 下载并解压 [Ink](http://www.inkpaper.io/),运行命令 `ink`
21-
- 使用浏览器访问 `http://localhost:8888` 预览
21+
- 使用浏览器访问 `http://localhost:8000` 预览
2222

2323
### 配置网站
2424
编辑`config.yml`,使用如下格式

template/theme/_footer.html

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<footer class="footer">
2+
<span class="copyright">
3+
{{.Site.Title}} ©
4+
<script type="text/javascript">
5+
document.write(new Date().getFullYear());
6+
</script>
7+
</span>
8+
<span class="publish">Powered by <a href="http://www.inkpaper.io/" target="_blank">Ink</a></span>
9+
</footer>

template/theme/_header.html

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<a class="index" href="{{.Site.Root}}/">{{.Site.Title}}</a>
2+
<ul class="menu">
3+
<li class="menu-item"><a href="{{.Site.Root}}/archive.html">ARCHIVE</a></li>
4+
<li class="menu-item"><a href="{{.Site.Root}}/tag.html">TAG</a></li>
5+
</ul>

template/theme/archive.html

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,7 @@
1111
</head>
1212
<body>
1313
<article class="container">
14-
<a class="index" href="{{.Site.Root}}/">{{.Site.Title}}</a>
15-
<ul class="menu">
16-
<li class="menu-item"><a href="{{.Site.Root}}/archive.html">ARCHIVE</a></li>
17-
<li class="menu-item"><a href="{{.Site.Root}}/tag.html">TAG</a></li>
18-
</ul>
14+
{{template "header" .}}
1915
<article class="main archive">
2016
<header class="site">
2117
<h1 class="title">{{.Site.Title}}</h1>
@@ -42,15 +38,7 @@ <h2 class="subtitle">{{.Site.Subtitle}}</h2>
4238
</ul>
4339
</article>
4440
</article>
45-
<footer class="footer">
46-
<span class="copyright">
47-
{{.Site.Title}} ©
48-
<script type="text/javascript">
49-
document.write(new Date().getFullYear());
50-
</script>
51-
</span>
52-
<span class="publish">Powered by <a href="http://www.inkpaper.io/" target="_blank">Ink</a></span>
53-
</footer>
41+
{{template "footer" .}}
5442
</body>
5543
<script src="{{.Site.Root}}/js/jquery-1.11.2.min.js"></script>
5644
<script src="{{.Site.Root}}/js/index.js"></script>

template/theme/article.html

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,15 @@
1212
</head>
1313
<body>
1414
<article class="container">
15-
<a class="index" href="{{.Site.Root}}/">{{.Site.Title}}</a>
16-
<ul class="menu">
17-
<li class="menu-item"><a href="{{.Site.Root}}/archive.html">ARCHIVE</a></li>
18-
<li class="menu-item"><a href="{{.Site.Root}}/tag.html">TAG</a></li>
19-
</ul>
15+
{{template "header" .}}
2016
<article class="main article">
2117
<h1 class="title">{{.Title}}</h1>
2218
<section class="info">
2319
{{if .Author.Avatar}}<span class="avatar" style="background-image: url({{.Author.Avatar}});"></span>{{end}}
2420
{{if .Author.Name}}<a class="name" href="{{.Site.Root}}/about.{{.Author.Id}}.html">{{.Author.Name}}</a>{{end}}
2521
<span class="date" data-time="{{.Date}}">
2622
<span class="from"></span>
27-
{{if .Update}}<span class="date" data-time="{{.Update}}">(<span class="from"></span> Updated)</span>{{end}}
23+
{{if .Update}}<span class="date" data-time="{{.Update}}">(<span class="from"></span> updated)</span>{{end}}
2824
</span>
2925
<span class="tags">{{range .Tags}}<a class="tag" href="{{$.Site.Root}}/tag/{{.}}/index.html">{{.}}</a>{{end}}</span>
3026
</section>
@@ -34,18 +30,24 @@ <h1 class="title">{{.Title}}</h1>
3430
<a class="name" href="{{.Site.Root}}/about.{{.Author.Id}}.html">{{.Author.Name}}</a>
3531
<div class="intro">{{.Author.Intro}}</div>
3632
</section>
33+
<section class="recommend">
34+
{{if .Prev}}
35+
<section class="nav prev{{if .Next}} more{{end}}">
36+
<div class="head">Prev</div>
37+
<a class="link" href="{{.Site.Root}}/{{.Prev.Link}}">{{.Prev.Title}}</a>
38+
</section>
39+
{{end}}
40+
{{if .Next}}
41+
<section class="nav next{{if .Prev}} more{{end}}">
42+
<div class="head">Next</div>
43+
<a class="link" href="{{.Site.Root}}/{{.Next.Link}}">{{.Next.Title}}</a>
44+
</section>
45+
{{end}}
46+
</section>
3747
<section id="disqus_thread"></section>
3848
</article>
3949
</article>
40-
<footer class="footer">
41-
<span class="copyright">
42-
{{.Site.Title}} ©
43-
<script type="text/javascript">
44-
document.write(new Date().getFullYear());
45-
</script>
46-
</span>
47-
<span class="publish">Powered by <a href="http://www.inkpaper.io/" target="_blank">Ink</a></span>
48-
</footer>
50+
{{template "footer" .}}
4951
<script src="{{.Site.Root}}/js/jquery-1.11.2.min.js"></script>
5052
<script src="{{.Site.Root}}/js/highlight.pack.js"></script>
5153
<script src="{{.Site.Root}}/js/jquery.unveil.js"></script>

0 commit comments

Comments
 (0)