Skip to content

Commit

Permalink
Merge pull request #139 from w568w/master
Browse files Browse the repository at this point in the history
Fix some issues once again
  • Loading branch information
imeoer authored Feb 7, 2023
2 parents 9b26c62 + c47f814 commit d5584eb
Show file tree
Hide file tree
Showing 11 changed files with 88 additions and 21 deletions.
24 changes: 19 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,18 @@ 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,
global: globalConfig,
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,
}
}

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
1 change: 1 addition & 0 deletions template/theme/bundle/637.index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion template/theme/bundle/index.css

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion template/theme/bundle/index.js

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion template/theme/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
"autoprefixer": "^10.4.2",
"css-loader": "^6.6.0",
"css-minimizer-webpack-plugin": "^4.2.2",
"uglifyjs-webpack-plugin": "^2.2.0",
"file-loader": "^6.2.0",
"mini-css-extract-plugin": "^2.6.0",
"postcss": "^8.4.7",
Expand All @@ -20,11 +19,13 @@
"raw-loader": "^4.0.2",
"source-map-loader": "^4.0.1",
"style-loader": "^3.3.1",
"uglifyjs-webpack-plugin": "^2.2.0",
"url-loader": "^4.1.1",
"webpack": "^5.69.1",
"webpack-cli": "^5.0.1"
},
"dependencies": {
"highlight.js": "^11.7.0",
"jquery": "^3.6.3"
}
}
2 changes: 1 addition & 1 deletion template/theme/source/css/page.css
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@
}
.preview {
@mixin font-main;
display: inline-block;
display: block;
line-height: 28px;
font-size: 16px;
margin-top: 10px;
Expand Down
7 changes: 2 additions & 5 deletions template/theme/source/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@ import $ from 'jquery'
import './jquery.unveil'
import searchTpl from './searchTpl.html'

// backward compatibility for old version of highlight.js
window.hljs = require('./highlight.pack.js')

// pick from underscore
let debounce = function (func, wait, immediate) {
let timeout, args, context, timestamp, result;
Expand Down Expand Up @@ -136,8 +133,8 @@ $(function () {
}
})
// render highlight
$('pre code').each(function (_, block) {
hljs.highlightBlock(block)
import("highlight.js").then(({ default: hljs }) => {
hljs.highlightAll()
})
// append image description
$('img').each(function (_, item) {
Expand Down

0 comments on commit d5584eb

Please sign in to comment.