From 9bb0905aabac86a0205b56cd400e5fd5ba58bbf5 Mon Sep 17 00:00:00 2001 From: deepzz0 Date: Wed, 11 Jan 2017 01:19:59 +0800 Subject: [PATCH] simple article description --- conf/app.yml | 2 ++ db.go | 6 ++++++ front.go | 8 ++++++++ model.go | 2 ++ setting/setting.go | 1 + views/homeLayout.html | 2 +- 6 files changed, 20 insertions(+), 1 deletion(-) diff --git a/conf/app.yml b/conf/app.yml index 0ae2297..a3894cb 100644 --- a/conf/app.yml +++ b/conf/app.yml @@ -12,6 +12,8 @@ pagesize: 20 length: 400 # 截取预览标识 identifier: +# 文章描述前缀 +description: "Desc:" # 起始ID,预留id不时之需, 不用管 startid: 11 # elasticsearch url diff --git a/db.go b/db.go index efcc1a1..321a9dd 100644 --- a/db.go +++ b/db.go @@ -6,6 +6,7 @@ import ( "fmt" "regexp" "sort" + "strings" "sync" "time" @@ -365,6 +366,11 @@ var reg = regexp.MustCompile(setting.Conf.Identifier) var regH = regexp.MustCompile("") func GenerateExcerptAndRender(artc *Article) { + if strings.HasPrefix(artc.Content, setting.Conf.Description) { + index := strings.Index(artc.Content, "\r\n") + artc.Desc = IgnoreHtmlTag(artc.Content[len(setting.Conf.Description):index]) + } + content := renderPage([]byte(artc.Content)) index := regH.FindIndex(content) if index != nil { diff --git a/front.go b/front.go index 211f28b..1967fce 100644 --- a/front.go +++ b/front.go @@ -71,6 +71,7 @@ func HandleNotFound(c *gin.Context) { h := GetBase() h["Version"] = StaticVersion(c) h["Title"] = "Not Found" + h["Description"] = "404 Not Found" h["Path"] = "" c.Status(http.StatusNotFound) RenderHTMLFront(c, "notfound", h) @@ -80,6 +81,7 @@ func HandleHomePage(c *gin.Context) { h := GetBase() h["Version"] = StaticVersion(c) h["Title"] = Ei.BTitle + " | " + Ei.SubTitle + h["Description"] = "博客首页," + Ei.SubTitle h["Path"] = c.Request.URL.Path h["CurrentPage"] = "blog-home" pn, err := strconv.Atoi(c.Query("pn")) @@ -95,6 +97,7 @@ func HandleSeriesPage(c *gin.Context) { h := GetBase() h["Version"] = StaticVersion(c) h["Title"] = "专题 | " + Ei.BTitle + h["Description"] = "专题列表," + Ei.SubTitle h["Path"] = c.Request.URL.Path h["CurrentPage"] = "series" h["Article"] = Ei.PageSeries @@ -106,6 +109,7 @@ func HandleArchivesPage(c *gin.Context) { h := GetBase() h["Version"] = StaticVersion(c) h["Title"] = "归档 | " + Ei.BTitle + h["Description"] = "博客归档," + Ei.SubTitle h["Path"] = c.Request.URL.Path h["CurrentPage"] = "archives" h["Article"] = Ei.PageArchives @@ -128,9 +132,12 @@ func HandleArticlePage(c *gin.Context) { var name string if path == "blogroll.html" { name = "blogroll" + h["Description"] = "友情连接," + Ei.SubTitle } else if path == "about.html" { name = "about" + h["Description"] = "关于作者," + Ei.SubTitle } else { + h["Description"] = artc.Desc + "," + Ei.SubTitle name = "article" h["Copyright"] = Ei.Copyright if !artc.UpdateTime.IsZero() { @@ -151,6 +158,7 @@ func HandleSearchPage(c *gin.Context) { h := GetBase() h["Version"] = StaticVersion(c) h["Title"] = "站内搜索 | " + Ei.BTitle + h["Description"] = "站内搜索," + Ei.SubTitle h["Path"] = "" h["CurrentPage"] = "search-post" diff --git a/model.go b/model.go index 83f2f8c..d9ad28f 100644 --- a/model.go +++ b/model.go @@ -119,6 +119,8 @@ type Article struct { Header string `bson:"-"` // 预览信息 Excerpt string `bson:"-"` + // 一句话描述,文章第一句 + Desc string `bson:"-"` } type SortArticles []*Article diff --git a/setting/setting.go b/setting/setting.go index 5a1ea32..d3b842f 100644 --- a/setting/setting.go +++ b/setting/setting.go @@ -27,6 +27,7 @@ type Config struct { PageSize int // 后台每页文章数量 Length int // 自动截取预览长度 Identifier string // 截取标示 + Description string // 文章描述前缀 Favicon string // icon地址 StartID int32 // 文章起始id SearchURL string // elasticsearch 地址 diff --git a/views/homeLayout.html b/views/homeLayout.html index 52b4cd3..0af980e 100644 --- a/views/homeLayout.html +++ b/views/homeLayout.html @@ -1 +1 @@ -{{.Title}}{{if .Version}}{{end}}{{if .Version}}{{else}}{{end}}
{{.LayoutContent}}
{{if .Version}}{{else}}{{end}}{{if .Version}}{{else}}{{end}}{{if .Version}}{{else}}{{end}}{{if .Version}}{{else}}{{end}} +{{.Title}}{{if .Version}}{{end}}{{if .Version}}{{else}}{{end}}
{{.LayoutContent}}
{{if .Version}}{{else}}{{end}}{{if .Version}}{{else}}{{end}}{{if .Version}}{{else}}{{end}}{{if .Version}}{{else}}{{end}}