From f6d8656c83591584581383643d109611d7ed2caa Mon Sep 17 00:00:00 2001 From: "henry.chen" Date: Thu, 5 Jan 2023 09:17:31 +0800 Subject: [PATCH] fix: 1. template read panic 2. optimization variable naming --- pkg/cache/cache.go | 12 +++++++----- pkg/cache/render/render.go | 16 ++++++++-------- pkg/core/eiblog/page/page.go | 8 +++++++- pkg/internal/es.go | 12 ++++++------ tools/tools.go | 35 ++++++++++++++++++----------------- 5 files changed, 46 insertions(+), 37 deletions(-) diff --git a/pkg/cache/cache.go b/pkg/cache/cache.go index f011898..5b49865 100644 --- a/pkg/cache/cache.go +++ b/pkg/cache/cache.go @@ -25,9 +25,11 @@ var ( // Ei eiblog cache Ei *Cache - // regenerate pages chan - PagesCh = make(chan string, 2) - PageSeries = "series-md" + // PagesCh regenerate pages chan + PagesCh = make(chan string, 2) + // PageSeries the page series regenerate flag + PageSeries = "series-md" + // PageArchive the page archive regenerate flag PageArchive = "archive-md" // ArticleStartID article start id @@ -518,7 +520,7 @@ func (c *Cache) regeneratePages() { } buf.WriteString("\n") } - c.PageSeries = string(render.RenderPage(buf.Bytes())) + c.PageSeries = string(render.PageRender(buf.Bytes())) case PageArchive: sort.Sort(c.Archives) buf := bytes.Buffer{} @@ -551,7 +553,7 @@ func (c *Cache) regeneratePages() { } } } - c.PageArchives = string(render.RenderPage(buf.Bytes())) + c.PageArchives = string(render.PageRender(buf.Bytes())) } } } diff --git a/pkg/cache/render/render.go b/pkg/cache/render/render.go index 529f512..09a2f8a 100644 --- a/pkg/cache/render/render.go +++ b/pkg/cache/render/render.go @@ -14,7 +14,7 @@ import ( // blackfriday 配置 const ( - commonHtmlFlags = 0 | + commonHTMLFlags = 0 | blackfriday.HTML_TOC | blackfriday.HTML_USE_XHTML | blackfriday.HTML_USE_SMARTYPANTS | @@ -42,9 +42,9 @@ var ( regHeader = regexp.MustCompile("") ) -// RenderPage 渲染markdown -func RenderPage(md []byte) []byte { - renderer := blackfriday.HtmlRenderer(commonHtmlFlags, "", "") +// PageRender 渲染markdown +func PageRender(md []byte) []byte { + renderer := blackfriday.HtmlRenderer(commonHTMLFlags, "", "") return blackfriday.Markdown(md, renderer, commonExtensions) } @@ -56,12 +56,12 @@ func GenerateExcerptMarkdown(article *model.Article) { index := strings.Index(article.Content, "\r\n") prefix := article.Content[len(blogapp.General.DescPrefix):index] - article.Desc = tools.IgnoreHtmlTag(prefix) + article.Desc = tools.IgnoreHTMLTag(prefix) article.Content = article.Content[index:] } // 查找目录 - content := RenderPage([]byte(article.Content)) + content := PageRender([]byte(article.Content)) index := regHeader.FindIndex(content) if index != nil { article.Header = string(content[0:index[1]]) @@ -73,7 +73,7 @@ func GenerateExcerptMarkdown(article *model.Article) { // excerpt index = regIdentifier.FindStringIndex(article.Content) if index != nil { - article.Excerpt = tools.IgnoreHtmlTag(article.Content[:index[0]]) + article.Excerpt = tools.IgnoreHTMLTag(article.Content[:index[0]]) return } uc := []rune(article.Content) @@ -81,5 +81,5 @@ func GenerateExcerptMarkdown(article *model.Article) { if len(uc) < length { length = len(uc) } - article.Excerpt = tools.IgnoreHtmlTag(string(uc[0:length])) + article.Excerpt = tools.IgnoreHTMLTag(string(uc[0:length])) } diff --git a/pkg/core/eiblog/page/page.go b/pkg/core/eiblog/page/page.go index 620febc..ce870bd 100644 --- a/pkg/core/eiblog/page/page.go +++ b/pkg/core/eiblog/page/page.go @@ -2,6 +2,7 @@ package page import ( + "io/fs" "path/filepath" "text/template" @@ -17,10 +18,15 @@ var htmlTmpl *template.Template func init() { htmlTmpl = template.New("eiblog").Funcs(tools.TplFuncMap) root := filepath.Join(config.WorkDir, "website") - files := tools.ReadDirFiles(root, func(name string) bool { + files := tools.ReadDirFiles(root, func(fi fs.FileInfo) bool { + name := fi.Name() if name == ".DS_Store" { return true } + // should not read template dir + if fi.IsDir() && name == "template" { + return true + } return false }) _, err := htmlTmpl.ParseFiles(files...) diff --git a/pkg/internal/es.go b/pkg/internal/es.go index 9c4aedc..349d3e1 100644 --- a/pkg/internal/es.go +++ b/pkg/internal/es.go @@ -49,7 +49,7 @@ func checkESConfig() error { } // ElasticSearch 搜索文章 -func ElasticSearch(query string, size, from int) (*searchIndexResult, error) { +func ElasticSearch(query string, size, from int) (*SearchIndexResult, error) { if err := checkESConfig(); err != nil { return nil, err } @@ -114,7 +114,7 @@ func ElasticAddIndex(article *model.Article) error { img := tools.PickFirstImage(article.Content) mapping := map[string]interface{}{ "title": article.Title, - "content": tools.IgnoreHtmlTag(article.Content), + "content": tools.IgnoreHTMLTag(article.Content), "slug": article.Slug, "tag": article.Tags, "img": img, @@ -241,8 +241,8 @@ func deleteIndexDocument(index, typ string, ids []string) error { return nil } -// searchIndexResult 查询结果 -type searchIndexResult struct { +// SearchIndexResult 查询结果 +type SearchIndexResult struct { Took float32 `json:"took"` Hits struct { Total int `json:"total"` @@ -264,7 +264,7 @@ type searchIndexResult struct { } // indexQueryDSL 语句查询文档 -func indexQueryDSL(index, typ string, size, from int, dsl []byte) (*searchIndexResult, error) { +func indexQueryDSL(index, typ string, size, from int, dsl []byte) (*SearchIndexResult, error) { rawurl := fmt.Sprintf("%s/%s/%s/_search?size=%d&from=%d", config.Conf.ESHost, index, typ, size, from) resp, err := httpPost(rawurl, dsl) @@ -276,7 +276,7 @@ func indexQueryDSL(index, typ string, size, from int, dsl []byte) (*searchIndexR if err != nil { return nil, err } - result := &searchIndexResult{} + result := &SearchIndexResult{} err = json.Unmarshal(data, result) if err != nil { return nil, err diff --git a/tools/tools.go b/tools/tools.go index 1597809..7beebe5 100644 --- a/tools/tools.go +++ b/tools/tools.go @@ -5,6 +5,7 @@ import ( "crypto/sha256" "fmt" "io" + "io/fs" "io/ioutil" "path" "regexp" @@ -22,13 +23,13 @@ func EncryptPasswd(name, pass string) string { } // ReadDirFiles 读取目录 -func ReadDirFiles(dir string, filter func(name string) bool) (files []string) { +func ReadDirFiles(dir string, filter func(fi fs.FileInfo) bool) (files []string) { fileInfos, err := ioutil.ReadDir(dir) if err != nil { return } for _, fi := range fileInfos { - if filter(fi.Name()) { + if filter(fi) { continue } if fi.IsDir() { @@ -42,19 +43,19 @@ func ReadDirFiles(dir string, filter func(name string) bool) (files []string) { // 2016-10-22T07:03:01 const ( - JUST_NOW = "几秒前" - MINUTES_AGO = "%d分钟前" - HOURS_AGO = "%d小时前" - DAYS_AGO = "%d天前" - MONTH_AGO = "%d月前" - YEARS_AGO = "%d年前" + JustNow = "几秒前" + MinutesAgo = "%d分钟前" + HoursAgo = "%d小时前" + DaysAgo = "%d天前" + MonthAgo = "%d月前" + YearsAgo = "%d年前" ) // ConvertStr 时间转换为间隔 func ConvertStr(str string) string { t, err := time.ParseInLocation("2006-01-02T15:04:05", str, time.UTC) if err != nil { - return JUST_NOW + return JustNow } now := time.Now().UTC() y1, m1, d1 := t.Date() @@ -62,17 +63,17 @@ func ConvertStr(str string) string { h1, mi1, s1 := t.Clock() h2, mi2, s2 := now.Clock() if y := y2 - y1; y > 1 || (y == 1 && m2-m1 >= 0) { - return fmt.Sprintf(YEARS_AGO, y) + return fmt.Sprintf(YearsAgo, y) } else if m := y*12 + int(m2-m1); m > 1 || (m == 1 && d2-d1 >= 0) { - return fmt.Sprintf(MONTH_AGO, m) + return fmt.Sprintf(MonthAgo, m) } else if d := m*dayIn(y1, m1) + d2 - d1; d > 1 || (d == 1 && h2-h1 >= 0) { - return fmt.Sprintf(DAYS_AGO, d) + return fmt.Sprintf(DaysAgo, d) } else if h := d*24 + h2 - h1; h > 1 || (h == 1 && mi2-mi1 >= 0) { - return fmt.Sprintf(HOURS_AGO, h) + return fmt.Sprintf(HoursAgo, h) } else if mi := h*60 + mi2 - mi1; mi > 1 || (mi == 1 && s2-s1 >= 0) { - return fmt.Sprintf(MINUTES_AGO, mi) + return fmt.Sprintf(MinutesAgo, mi) } - return JUST_NOW + return JustNow } // dayIn 获取天数 @@ -120,8 +121,8 @@ var ( regexpEnter = regexp.MustCompile(`\s+`) ) -// IgnoreHtmlTag 去掉 html tag -func IgnoreHtmlTag(src string) string { +// IgnoreHTMLTag 去掉 html tag +func IgnoreHTMLTag(src string) string { // 去除所有尖括号内的HTML代码 src = regexpBrackets.ReplaceAllString(src, "") // 去除换行符