Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix some issues once again #139

Merged
merged 6 commits into from
Feb 7, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 18 additions & 5 deletions build.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,17 @@ func Build() {
}
}
// Compile template
articleTpl = CompileTpl(filepath.Join(themePath, "article.html"), partialTpl, "article")
pageTpl = CompileTpl(filepath.Join(themePath, "page.html"), partialTpl, "page")
archiveTpl = CompileTpl(filepath.Join(themePath, "archive.html"), partialTpl, "archive")
tagTpl = CompileTpl(filepath.Join(themePath, "tag.html"), partialTpl, "tag")
funcCxt := FuncContext{
rootPath: rootPath,
themePath: themePath,
publicPath: publicPath,
global: globalConfig,
currentCwd: themePath,
}
articleTpl = CompileTpl(filepath.Join(themePath, "article.html"), partialTpl, "article", funcCxt)
pageTpl = CompileTpl(filepath.Join(themePath, "page.html"), partialTpl, "page", funcCxt)
archiveTpl = CompileTpl(filepath.Join(themePath, "archive.html"), partialTpl, "archive", funcCxt)
tagTpl = CompileTpl(filepath.Join(themePath, "tag.html"), partialTpl, "tag", funcCxt)
// Clean public folder
cleanPatterns := []string{"post", "tag", "images", "js", "css", "*.html", "favicon.ico", "robots.txt"}
for _, pattern := range cleanPatterns {
Expand Down Expand Up @@ -239,11 +246,17 @@ func Build() {
}, filepath.Join(publicPath, "tag.html"))
// Generate other pages
files, _ = filepath.Glob(filepath.Join(sourcePath, "*.html"))
funcCxt = FuncContext{
rootPath: rootPath,
themePath: themePath,
publicPath: publicPath,
currentCwd: sourcePath,
}
for _, path := range files {
fileExt := strings.ToLower(filepath.Ext(path))
baseName := filepath.Base(path)
if fileExt == ".html" && !strings.HasPrefix(baseName, "_") {
htmlTpl := CompileTpl(path, partialTpl, baseName)
htmlTpl := CompileTpl(path, partialTpl, baseName, funcCxt)
relPath, _ := filepath.Rel(sourcePath, path)
wg.Add(1)
go RenderPage(htmlTpl, globalConfig, filepath.Join(publicPath, relPath))
Expand Down
31 changes: 31 additions & 0 deletions funcs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package main

import (
"html/template"
"os"
"path/filepath"
)

type FuncContext struct {
rootPath string
themePath string
publicPath string
currentCwd string
global *GlobalConfig
}

func (ctx FuncContext) FuncMap() template.FuncMap {
return template.FuncMap{
"i18n": ctx.I18n,
"readFile": ctx.ReadFile,
w568w marked this conversation as resolved.
Show resolved Hide resolved
}
}

func (ctx FuncContext) I18n(val string) string {
return ctx.global.I18n[val]
}

func (ctx FuncContext) ReadFile(path string) template.HTML {
bytes, _ := os.ReadFile(filepath.Join(ctx.currentCwd, path))
return template.HTML(bytes)
}
9 changes: 2 additions & 7 deletions render.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,16 @@ type RenderArticle struct {
}

// Compile html template
func CompileTpl(tplPath string, partialTpl string, name string) template.Template {
func CompileTpl(tplPath string, partialTpl string, name string, funcContext FuncContext) template.Template {
// Read template data from file
html, err := os.ReadFile(tplPath)
if err != nil {
Fatal(err.Error())
}
// Append partial template
htmlStr := string(html) + partialTpl
funcMap := template.FuncMap{
"i18n": func(val string) string {
return globalConfig.I18n[val]
},
}
// Generate html content
tpl, err := template.New(name).Funcs(funcMap).Parse(htmlStr)
tpl, err := template.New(name).Funcs(funcContext.FuncMap()).Parse(htmlStr)
if err != nil {
Fatal(err.Error())
}
Expand Down
13 changes: 13 additions & 0 deletions template/source/ink-blog-tool-en.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,19 @@ site:

> then the variable must be referenced in the page by `{{.Site.Config.MYVAR_aaa}}`.

#### Use Functions (Experimental)

InkPaper defines a minimal set of functions that can be used in HTML pages (except for `.md` source files), such as

``` yaml
{{ readFile "path/to/file" }}
```

This will read the content of the file `path/to/file` and include it in the page without any processing.

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`).

See the source file `funcs.go` for a list of all functions.

### Blog Migrate (Beta)

Expand Down
15 changes: 15 additions & 0 deletions template/source/ink-blog-tool.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,21 @@ site:

> 则在页面中必须使用 `{{.Site.Config.MYVAR_aaa}}` 来引用该变量。


#### 使用函数(实验性)

纸小墨定义了一个最小的函数集合,可在 HTML 页面(除 `.md` 源文件以外)中使用,如

``` yaml
{{ readFile "path/to/file" }}
```

将读取 `path/to/file` 文件的内容并(不做任何处理地)包含入页面中。

对于文件相关的函数,当在 `source` 下执行时,文件路径相对于 `source` 目录;当在其他目录下执行时,文件路径相对于主题(如 `theme`)目录。

所有函数列表见源文件 `funcs.go` 文件。

### 博客迁移(Beta)

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