Skip to content

Commit d5584eb

Browse files
authored
Merge pull request #139 from w568w/master
Fix some issues once again
2 parents 9b26c62 + c47f814 commit d5584eb

11 files changed

Lines changed: 88 additions & 21 deletions

File tree

build.go

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,17 @@ func Build() {
9999
}
100100
}
101101
// Compile template
102-
articleTpl = CompileTpl(filepath.Join(themePath, "article.html"), partialTpl, "article")
103-
pageTpl = CompileTpl(filepath.Join(themePath, "page.html"), partialTpl, "page")
104-
archiveTpl = CompileTpl(filepath.Join(themePath, "archive.html"), partialTpl, "archive")
105-
tagTpl = CompileTpl(filepath.Join(themePath, "tag.html"), partialTpl, "tag")
102+
funcCxt := FuncContext{
103+
rootPath: rootPath,
104+
themePath: themePath,
105+
publicPath: publicPath,
106+
global: globalConfig,
107+
currentCwd: themePath,
108+
}
109+
articleTpl = CompileTpl(filepath.Join(themePath, "article.html"), partialTpl, "article", funcCxt)
110+
pageTpl = CompileTpl(filepath.Join(themePath, "page.html"), partialTpl, "page", funcCxt)
111+
archiveTpl = CompileTpl(filepath.Join(themePath, "archive.html"), partialTpl, "archive", funcCxt)
112+
tagTpl = CompileTpl(filepath.Join(themePath, "tag.html"), partialTpl, "tag", funcCxt)
106113
// Clean public folder
107114
cleanPatterns := []string{"post", "tag", "images", "js", "css", "*.html", "favicon.ico", "robots.txt"}
108115
for _, pattern := range cleanPatterns {
@@ -239,11 +246,18 @@ func Build() {
239246
}, filepath.Join(publicPath, "tag.html"))
240247
// Generate other pages
241248
files, _ = filepath.Glob(filepath.Join(sourcePath, "*.html"))
249+
funcCxt = FuncContext{
250+
rootPath: rootPath,
251+
themePath: themePath,
252+
publicPath: publicPath,
253+
global: globalConfig,
254+
currentCwd: sourcePath,
255+
}
242256
for _, path := range files {
243257
fileExt := strings.ToLower(filepath.Ext(path))
244258
baseName := filepath.Base(path)
245259
if fileExt == ".html" && !strings.HasPrefix(baseName, "_") {
246-
htmlTpl := CompileTpl(path, partialTpl, baseName)
260+
htmlTpl := CompileTpl(path, partialTpl, baseName, funcCxt)
247261
relPath, _ := filepath.Rel(sourcePath, path)
248262
wg.Add(1)
249263
go RenderPage(htmlTpl, globalConfig, filepath.Join(publicPath, relPath))

funcs.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package main
2+
3+
import (
4+
"html/template"
5+
"os"
6+
"path/filepath"
7+
)
8+
9+
type FuncContext struct {
10+
rootPath string
11+
themePath string
12+
publicPath string
13+
currentCwd string
14+
global *GlobalConfig
15+
}
16+
17+
func (ctx FuncContext) FuncMap() template.FuncMap {
18+
return template.FuncMap{
19+
"i18n": ctx.I18n,
20+
"readFile": ctx.ReadFile,
21+
}
22+
}
23+
24+
func (ctx FuncContext) I18n(val string) string {
25+
return ctx.global.I18n[val]
26+
}
27+
28+
func (ctx FuncContext) ReadFile(path string) template.HTML {
29+
bytes, _ := os.ReadFile(filepath.Join(ctx.currentCwd, path))
30+
return template.HTML(bytes)
31+
}

render.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,16 @@ type RenderArticle struct {
2020
}
2121

2222
// Compile html template
23-
func CompileTpl(tplPath string, partialTpl string, name string) template.Template {
23+
func CompileTpl(tplPath string, partialTpl string, name string, funcContext FuncContext) template.Template {
2424
// Read template data from file
2525
html, err := os.ReadFile(tplPath)
2626
if err != nil {
2727
Fatal(err.Error())
2828
}
2929
// Append partial template
3030
htmlStr := string(html) + partialTpl
31-
funcMap := template.FuncMap{
32-
"i18n": func(val string) string {
33-
return globalConfig.I18n[val]
34-
},
35-
}
3631
// Generate html content
37-
tpl, err := template.New(name).Funcs(funcMap).Parse(htmlStr)
32+
tpl, err := template.New(name).Funcs(funcContext.FuncMap()).Parse(htmlStr)
3833
if err != nil {
3934
Fatal(err.Error())
4035
}

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,19 @@ site:
133133
134134
> then the variable must be referenced in the page by `{{.Site.Config.MYVAR_aaa}}`.
135135

136+
#### Use Functions (Experimental)
137+
138+
InkPaper defines a minimal set of functions that can be used in HTML pages (except for `.md` source files), such as
139+
140+
``` yaml
141+
{{ readFile "path/to/file" }}
142+
```
143+
144+
This will read the content of the file `path/to/file` and include it in the page without any processing.
145+
146+
For file-related functions, when executed in the `source` directory, the file path is relative to the `source` directory; when executed in other directories, the file path is relative to the theme (such as `theme`).
147+
148+
See the source file `funcs.go` for a list of all functions.
136149

137150
### Blog Migrate (Beta)
138151

template/source/ink-blog-tool.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,21 @@ site:
135135
136136
> 则在页面中必须使用 `{{.Site.Config.MYVAR_aaa}}` 来引用该变量。
137137

138+
139+
#### 使用函数(实验性)
140+
141+
纸小墨定义了一个最小的函数集合,可在 HTML 页面(除 `.md` 源文件以外)中使用,如
142+
143+
``` yaml
144+
{{ readFile "path/to/file" }}
145+
```
146+
147+
将读取 `path/to/file` 文件的内容并(不做任何处理地)包含入页面中。
148+
149+
对于文件相关的函数,当在 `source` 下执行时,文件路径相对于 `source` 目录;当在其他目录下执行时,文件路径相对于主题(如 `theme`)目录。
150+
151+
所有函数列表见源文件 `funcs.go` 文件。
152+
138153
### 博客迁移(Beta)
139154

140155
纸小墨提供简单的Jeklly/Hexo博客文章格式转换,使用命令:

template/theme/bundle/637.index.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

template/theme/bundle/index.css

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

template/theme/bundle/index.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

template/theme/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
"autoprefixer": "^10.4.2",
1111
"css-loader": "^6.6.0",
1212
"css-minimizer-webpack-plugin": "^4.2.2",
13-
"uglifyjs-webpack-plugin": "^2.2.0",
1413
"file-loader": "^6.2.0",
1514
"mini-css-extract-plugin": "^2.6.0",
1615
"postcss": "^8.4.7",
@@ -20,11 +19,13 @@
2019
"raw-loader": "^4.0.2",
2120
"source-map-loader": "^4.0.1",
2221
"style-loader": "^3.3.1",
22+
"uglifyjs-webpack-plugin": "^2.2.0",
2323
"url-loader": "^4.1.1",
2424
"webpack": "^5.69.1",
2525
"webpack-cli": "^5.0.1"
2626
},
2727
"dependencies": {
28+
"highlight.js": "^11.7.0",
2829
"jquery": "^3.6.3"
2930
}
3031
}

template/theme/source/css/page.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@
8484
}
8585
.preview {
8686
@mixin font-main;
87-
display: inline-block;
87+
display: block;
8888
line-height: 28px;
8989
font-size: 16px;
9090
margin-top: 10px;

0 commit comments

Comments
 (0)