Skip to content

Commit

Permalink
feat: support busuanzi view counter #40
Browse files Browse the repository at this point in the history
  • Loading branch information
reuixiy committed Dec 19, 2019
1 parent 648fba1 commit 09e9976
Show file tree
Hide file tree
Showing 11 changed files with 129 additions and 69 deletions.
18 changes: 18 additions & 0 deletions config-examples/en-us/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -714,6 +714,13 @@ uglyURLs = false
displayReadingTime = true
readingTimeIcon = "clock"

displayBusuanziPagePV = false
busuanziPagePVIcon = "eye"
# Note: render only in production
# environment
# Homepage is not supported yet
# https://busuanzi.ibruce.info/


######################################
# Table of Contents
Expand Down Expand Up @@ -1098,6 +1105,17 @@ uglyURLs = false
# Custom Footer (Markdown supported)
customFooter = ""

displayBusuanziSiteUVAndPV = false
busuanziSiteUVText = "Site UV"
busuanziSiteUVIcon = "user"
busuanziSitePVText = "Site PV"
busuanziSitePVIcon = "eye"
# Note: render only in production
# environment
# Leave it empty("") if you
# don’t need the text or icon
# https://busuanzi.ibruce.info/


######################################
# Multilingual
Expand Down
17 changes: 17 additions & 0 deletions config-examples/zh-cn/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -672,6 +672,14 @@ uglyURLs = false
# 阅读时长的图标
readingTimeIcon = "clock"

# 是否显示不蒜子页面浏览量
displayBusuanziPagePV = false
# 不蒜子页面浏览量的图标
busuanziPagePVIcon = "eye"
# 说明:仅在生产环境(production)下渲染
# 首页暂不支持
# https://busuanzi.ibruce.info/


######################################
# 文章目录
Expand Down Expand Up @@ -1065,6 +1073,15 @@ uglyURLs = false
# 自定义页脚(支持 Markdown)
customFooter = ""

displayBusuanziSiteUVAndPV = false
busuanziSiteUVText = "本站访客数"
busuanziSiteUVIcon = "user"
busuanziSitePVText = "本站访问量"
busuanziSitePVIcon = "eye"
# 说明:仅在生产环境(production)下渲染
# 文本或图标,留空("")则无
# https://busuanzi.ibruce.info/


######################################
# 多语言
Expand Down
2 changes: 2 additions & 0 deletions data/SVG.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ info = '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 192 512" class="ico
question = '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 384 512" class="icon"><path d="M202.021 0C122.202 0 70.503 32.703 29.914 91.026c-7.363 10.58-5.093 25.086 5.178 32.874l43.138 32.709c10.373 7.865 25.132 6.026 33.253-4.148 25.049-31.381 43.63-49.449 82.757-49.449 30.764 0 68.816 19.799 68.816 49.631 0 22.552-18.617 34.134-48.993 51.164-35.423 19.86-82.299 44.576-82.299 106.405V320c0 13.255 10.745 24 24 24h72.471c13.255 0 24-10.745 24-24v-5.773c0-42.86 125.268-44.645 125.268-160.627C377.504 66.256 286.902 0 202.021 0zM192 373.459c-38.196 0-69.271 31.075-69.271 69.271 0 38.195 31.075 69.27 69.271 69.27s69.271-31.075 69.271-69.271-31.075-69.27-69.271-69.27z"/></svg>'
edit = '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 576 512" class="icon"><path d="M402.3 344.9l32-32c5-5 13.7-1.5 13.7 5.7V464c0 26.5-21.5 48-48 48H48c-26.5 0-48-21.5-48-48V112c0-26.5 21.5-48 48-48h273.5c7.1 0 10.7 8.6 5.7 13.7l-32 32c-1.5 1.5-3.5 2.3-5.7 2.3H48v352h352V350.5c0-2.1.8-4.1 2.3-5.6zm156.6-201.8L296.3 405.7l-90.4 10c-26.2 2.9-48.5-19.2-45.6-45.6l10-90.4L432.9 17.1c22.9-22.9 59.9-22.9 82.7 0l43.2 43.2c22.9 22.9 22.9 60 .1 82.8zM460.1 174L402 115.9 216.2 301.8l-7.3 65.3 65.3-7.3L460.1 174zm64.8-79.7l-43.2-43.2c-4.1-4.1-10.8-4.1-14.8 0L436 82l58.1 58.1 30.9-30.9c4-4.2 4-10.8-.1-14.9z"/></svg>'
arrow-up = '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 448 512" class="icon"><path d="M34.9 289.5l-22.2-22.2c-9.4-9.4-9.4-24.6 0-33.9L207 39c9.4-9.4 24.6-9.4 33.9 0l194.3 194.3c9.4 9.4 9.4 24.6 0 33.9L413 289.4c-9.5 9.5-25 9.3-34.3-.4L264 168.6V456c0 13.3-10.7 24-24 24h-32c-13.3 0-24-10.7-24-24V168.6L69.2 289.1c-9.3 9.8-24.8 10-34.3.4z"/></svg>'
eye = '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 576 512" class="icon"><path d="M288 144a110.94 110.94 0 0 0-31.24 5 55.4 55.4 0 0 1 7.24 27 56 56 0 0 1-56 56 55.4 55.4 0 0 1-27-7.24A111.71 111.71 0 1 0 288 144zm284.52 97.4C518.29 135.59 410.93 64 288 64S57.68 135.64 3.48 241.41a32.35 32.35 0 0 0 0 29.19C57.71 376.41 165.07 448 288 448s230.32-71.64 284.52-177.41a32.35 32.35 0 0 0 0-29.19zM288 400c-98.65 0-189.09-55-237.93-144C98.91 167 189.34 112 288 112s189.09 55 237.93 144C477.1 345 386.66 400 288 400z"/></svg>'
user = '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 448 512" class="icon"><path d="M224 256c70.7 0 128-57.3 128-128S294.7 0 224 0 96 57.3 96 128s57.3 128 128 128zm89.6 32h-16.7c-22.2 10.2-46.9 16-72.9 16s-50.6-5.8-72.9-16h-16.7C60.2 288 0 348.2 0 422.4V464c0 26.5 21.5 48 48 48h352c26.5 0 48-21.5 48-48v-41.6c0-74.2-60.2-134.4-134.4-134.4z"/></svg>'

# Chinese Zodiac
# Extract from https://github.com/forsigner/fexo/blob/master/source/fonts/fontello.ttf
Expand Down
20 changes: 9 additions & 11 deletions layouts/partials/components/keywords.html
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,15 @@
{{- end -}}
{{- with .Section -}}
{{- if and (eq $.Site.Params.categoryBy "sections") (in $.Site.Params.mainSections $.Section) -}}
{{- with $.File -}}
{{- $.Scratch.Delete "Dir" -}}
{{- $.Scratch.Delete "mainSections" -}}
{{- $sections := split (strings.TrimSuffix "/" .Dir) "/" -}}
{{- range $sections -}}
{{- $section := . -}}
{{- $.Scratch.Add "Dir" (printf `/%s` $section) -}}
{{- with $.Site.GetPage ($.Scratch.Get "Dir") -}}
{{- if (eq .Kind "section") -}}
{{- $.Scratch.Add "mainSections" (slice ($section | anchorize)) -}}
{{- end -}}
{{- $.Scratch.Delete "Dir" -}}
{{- $.Scratch.Delete "mainSections" -}}
{{- $sections := split (strings.TrimSuffix "/" $.File.Dir) "/" -}}
{{- range $sections -}}
{{- $section := . -}}
{{- $.Scratch.Add "Dir" (printf `/%s` $section) -}}
{{- with $.Site.GetPage ($.Scratch.Get "Dir") -}}
{{- if (eq .Kind "section") -}}
{{- $.Scratch.Add "mainSections" (slice ($section | anchorize)) -}}
{{- end -}}
{{- end -}}
{{- end -}}
Expand Down
14 changes: 14 additions & 0 deletions layouts/partials/footer.html
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,20 @@
<div class="custom-footer">{{ . | markdownify | safeHTML }}</div>
{{ end }}

{{ if and .Site.Params.displayBusuanziSiteUVAndPV (eq hugo.Environment "production") }}
{{ $busuanziSiteUVIcon := replace (index $.Site.Data.SVG $.Site.Params.busuanziSiteUVIcon) "icon" "icon busuanzi-site-uv" }}
{{ $busuanziSitePVIcon := replace (index $.Site.Data.SVG $.Site.Params.busuanziSitePVIcon) "icon" "icon busuanzi-site-pv" }}
<div class="busuanzi-site-uv-and-pv">
<span id="busuanzi_container_site_uv">
{{ printf `%s %s %s` .Site.Params.busuanziSiteUVText $busuanziSiteUVIcon `<span id="busuanzi_value_site_uv"></span>` | safeHTML }}
</span>
{{- print " | " -}}
<span id="busuanzi_container_site_pv">
{{ printf `%s %s %s` .Site.Params.busuanziSitePVText $busuanziSitePVIcon `<span id="busuanzi_value_site_pv"></span>` | safeHTML }}
</span>
</div>
{{ end }}

{{ partial "custom/footer.html" . }}
</div>
</footer>
Expand Down
26 changes: 12 additions & 14 deletions layouts/partials/minimal-footer.html
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,18 @@
{{- end -}}
{{- end -}}
{{ else }}
{{ with .File }}
{{ $sections := split (strings.TrimSuffix "/" (.Dir | default $.Section)) "/" }}
{{ with $sections }}
{{ $.Scratch.Delete "sectionsDirMeta" }}
{{ $.Scratch.Delete "sectionsMeta" }}
{{ $.Scratch.Set "index" 0 }}
{{ range $sections }}
{{ $section := . }}
{{ $.Scratch.Add "sectionsDirMeta" (printf `/%s` $section) }}
{{ with $.Site.GetPage ($.Scratch.Get "sectionsDirMeta") }}
{{ if (eq .Kind "section") }}
{{ $.Scratch.SetInMap "sectionsMeta" (printf `%s/%s` (string ($.Scratch.Get "index")) .RelPermalink) (.Slug | default .Params.url | default .LinkTitle | default $section | lower | anchorize) }}
{{ $.Scratch.Set "index" (add ($.Scratch.Get "index") 1) }}
{{ end }}
{{ $sections := split (strings.TrimSuffix "/" ($.File.Dir | default $.Section)) "/" }}
{{ with $sections }}
{{ $.Scratch.Delete "sectionsDirMeta" }}
{{ $.Scratch.Delete "sectionsMeta" }}
{{ $.Scratch.Set "index" 0 }}
{{ range $sections }}
{{ $section := . }}
{{ $.Scratch.Add "sectionsDirMeta" (printf `/%s` $section) }}
{{ with $.Site.GetPage ($.Scratch.Get "sectionsDirMeta") }}
{{ if (eq .Kind "section") }}
{{ $.Scratch.SetInMap "sectionsMeta" (printf `%s/%s` (string ($.Scratch.Get "index")) .RelPermalink) (.Slug | default .Params.url | default .LinkTitle | default $section | lower | anchorize) }}
{{ $.Scratch.Set "index" (add ($.Scratch.Get "index") 1) }}
{{ end }}
{{ end }}
{{ end }}
Expand Down
2 changes: 1 addition & 1 deletion layouts/partials/page/home-posts.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ <h2 class="post-title">
<a href="{{ .RelPermalink }}" class="summary-title-link">{{ .LinkTitle }}</a>
</h2>
{{ if $.Site.Params.enablePostMetaInHome }}
{{ partial "post-meta.html" . }}
{{ partial "post-meta.html" (dict "Deliver" . "isHome" true) }}
{{ end }}
<summary class="summary">
{{ partial "components/summary.html" . }}
Expand Down
90 changes: 48 additions & 42 deletions layouts/partials/post-meta.html
Original file line number Diff line number Diff line change
@@ -1,83 +1,89 @@
{{ $Deliver := .Deliver }}
{{ $isHome := .isHome }}
<div class="post-meta">
{{ if and .Site.Params.displayPublishedDate (not .PublishDate.IsZero) }}
{{ $icon := replace (index $.Site.Data.SVG $.Site.Params.publishedDateIcon) "icon" "icon post-meta-icon" }}
{{ printf `<time datetime="%s" class="post-meta-item published">%s%s</time>` (.PublishDate.Format "2006-01-02T15:04:05-07:00") $icon (.PublishDate.Format .Site.Params.postMetaDateFormat) | safeHTML }}
{{ if and $Deliver.Site.Params.displayPublishedDate (not $Deliver.PublishDate.IsZero) }}
{{ $icon := replace (index $Deliver.Site.Data.SVG $Deliver.Site.Params.publishedDateIcon) "icon" "icon post-meta-icon" }}
{{ printf `<time datetime="%s" class="post-meta-item published">%s%s</time>` ($Deliver.PublishDate.Format "2006-01-02T15:04:05-07:00") $icon ($Deliver.PublishDate.Format $Deliver.Site.Params.postMetaDateFormat) | safeHTML }}
{{ end }}
{{ if and .Site.Params.displayModifiedDate (not .Lastmod.IsZero) }}
{{ $icon := replace (index $.Site.Data.SVG $.Site.Params.modifiedDateIcon) "icon" "icon post-meta-icon" }}
{{ printf `<time datetime="%s" class="post-meta-item modified">%s%s</time>` (.Lastmod.Format "2006-01-02T15:04:05-07:00") $icon (.Lastmod.Format .Site.Params.postMetaDateFormat) | safeHTML }}
{{ if and $Deliver.Site.Params.displayModifiedDate (not $Deliver.Lastmod.IsZero) }}
{{ $icon := replace (index $Deliver.Site.Data.SVG $Deliver.Site.Params.modifiedDateIcon) "icon" "icon post-meta-icon" }}
{{ printf `<time datetime="%s" class="post-meta-item modified">%s%s</time>` ($Deliver.Lastmod.Format "2006-01-02T15:04:05-07:00") $icon ($Deliver.Lastmod.Format $Deliver.Site.Params.postMetaDateFormat) | safeHTML }}
{{ end }}
{{ if and .Site.Params.displayExpiredDate (not .ExpiryDate.IsZero) }}
{{ $icon := replace (index $.Site.Data.SVG $.Site.Params.expiredDateIcon) "icon" "icon post-meta-icon" }}
{{ printf `<time datetime="%s" class="post-meta-item expired">%s%s</time>` (.ExpiryDate.Format "2006-01-02T15:04:05-07:00") $icon (.ExpiryDate.Format .Site.Params.postMetaDateFormat) | safeHTML }}
{{ if and $Deliver.Site.Params.displayExpiredDate (not $Deliver.ExpiryDate.IsZero) }}
{{ $icon := replace (index $Deliver.Site.Data.SVG $Deliver.Site.Params.expiredDateIcon) "icon" "icon post-meta-icon" }}
{{ printf `<time datetime="%s" class="post-meta-item expired">%s%s</time>` ($Deliver.ExpiryDate.Format "2006-01-02T15:04:05-07:00") $icon ($Deliver.ExpiryDate.Format $Deliver.Site.Params.postMetaDateFormat) | safeHTML }}
{{ end }}
{{ if .Site.Params.displayCategory }}
{{ $icon := replace (index $.Site.Data.SVG $.Site.Params.categoryIcon) "icon" "icon post-meta-icon" | safeHTML }}
{{ if and (eq .Site.Params.categoryBy "sections") (in .Site.Params.mainSections .Section) }}
{{ with .File }}
{{ $sections := split (strings.TrimSuffix "/" (.Dir | default $.Section)) "/" }}
{{ with $sections }}
{{ $.Scratch.Delete "sectionsDirMeta" }}
{{ $.Scratch.Delete "sectionsMeta" }}
{{ $.Scratch.Set "index" 0 }}
{{ range $sections }}
{{ $section := . }}
{{ $.Scratch.Add "sectionsDirMeta" (printf `/%s` $section) }}
{{ with $.Site.GetPage ($.Scratch.Get "sectionsDirMeta") }}
{{ if (eq .Kind "section") }}
{{ $.Scratch.SetInMap "sectionsMeta" (printf `%s/%s` (string ($.Scratch.Get "index")) .RelPermalink) (.LinkTitle | default $section) }}
{{ $.Scratch.Set "index" (add ($.Scratch.Get "index") 1) }}
{{ end }}
{{ if $Deliver.Site.Params.displayCategory }}
{{ $icon := replace (index $Deliver.Site.Data.SVG $Deliver.Site.Params.categoryIcon) "icon" "icon post-meta-icon" | safeHTML }}
{{ if and (eq $Deliver.Site.Params.categoryBy "sections") (in $Deliver.Site.Params.mainSections $Deliver.Section) }}
{{ $sections := split (strings.TrimSuffix "/" ($Deliver.File.Dir | default $Deliver.Section)) "/" }}
{{ with $sections }}
{{ $Deliver.Scratch.Delete "sectionsDirMeta" }}
{{ $Deliver.Scratch.Delete "sectionsMeta" }}
{{ $Deliver.Scratch.Set "index" 0 }}
{{ range $sections }}
{{ $section := . }}
{{ $Deliver.Scratch.Add "sectionsDirMeta" (printf `/%s` $section) }}
{{ with $Deliver.Site.GetPage ($Deliver.Scratch.Get "sectionsDirMeta") }}
{{ if (eq .Kind "section") }}
{{ $Deliver.Scratch.SetInMap "sectionsMeta" (printf `%s/%s` (string ($Deliver.Scratch.Get "index")) .RelPermalink) (.LinkTitle | default $section) }}
{{ $Deliver.Scratch.Set "index" (add ($Deliver.Scratch.Get "index") 1) }}
{{ end }}
{{ end }}
{{ end }}
{{ end }}
{{ $sections := .Scratch.Get "sectionsMeta" }}
{{ $sections := $Deliver.Scratch.Get "sectionsMeta" }}
{{ with $sections }}
{{ $length := (sub (len $sections) 1) }}
<span class="post-meta-item category">
{{- $icon -}}
{{- range $link, $title := $sections -}}
{{- $index := $link | replaceRE `(\d+)/.+` `$1` | int -}}
{{- if lt $index $length -}}
{{- $.Scratch.Set "delimiter" ($.Site.Params.categoryDelimiter | default "/") -}}
{{- $Deliver.Scratch.Set "delimiter" ($Deliver.Site.Params.categoryDelimiter | default "/") -}}
{{- else -}}
{{- $.Scratch.Set "delimiter" "" -}}
{{- $Deliver.Scratch.Set "delimiter" "" -}}
{{- end -}}
{{- $delimiter := ($.Scratch.Get "delimiter") -}}
{{- $delimiter := ($Deliver.Scratch.Get "delimiter") -}}
{{- printf `<a href="%s" class="category-link">%s</a>%s` ($link | replaceRE `\d+/(.+)` `$1`) $title $delimiter | safeHTML -}}
{{- end -}}
</span>
{{ end }}
{{ end }}
{{ if eq .Site.Params.categoryBy "categories" }}
{{ with .Params.categories }}
{{ if eq $Deliver.Site.Params.categoryBy "categories" }}
{{ with $Deliver.Params.categories }}
{{ $length := sub (len .) 1 }}
<span class="post-meta-item category">
{{- $icon -}}
{{- range $index, $category := . -}}
{{- $url := urls.Parse ($category | urlize) -}}
{{- $path := $url.Path -}}
{{- if lt $index $length -}}
{{- $.Scratch.Set "delimiter" ($.Site.Params.categoryDelimiter | default "/") -}}
{{- $Deliver.Scratch.Set "delimiter" ($Deliver.Site.Params.categoryDelimiter | default "/") -}}
{{- else -}}
{{- $.Scratch.Set "delimiter" "" -}}
{{- $Deliver.Scratch.Set "delimiter" "" -}}
{{- end -}}
{{- $delimiter := ($.Scratch.Get "delimiter") -}}
{{- with $.Site.GetPage (printf `/categories/%s` $path) -}}
{{- $delimiter := ($Deliver.Scratch.Get "delimiter") -}}
{{- with $Deliver.Site.GetPage (printf `/categories/%s` $path) -}}
{{- printf `<a href="%s" class="category-link">%s</a>%s` .RelPermalink (.LinkTitle | default $category) $delimiter | safeHTML -}}
{{- end -}}
{{- end -}}
</span>
{{ end }}
{{ end }}
{{ end }}
{{ if .Site.Params.displayWordCount }}
{{ $icon := replace (index $.Site.Data.SVG $.Site.Params.wordCountIcon) "icon" "icon post-meta-icon" | safeHTML }}
<span class="post-meta-item wordcount">{{ $icon }}{{ .WordCount }}</span>
{{ if $Deliver.Site.Params.displayWordCount }}
{{ $icon := replace (index $Deliver.Site.Data.SVG $Deliver.Site.Params.wordCountIcon) "icon" "icon post-meta-icon" | safeHTML }}
<span class="post-meta-item wordcount">{{ $icon }}{{ $Deliver.WordCount }}</span>
{{ end }}
{{ if $Deliver.Site.Params.displayReadingTime }}
{{ $icon := replace (index $Deliver.Site.Data.SVG $Deliver.Site.Params.readingTimeIcon) "icon" "icon post-meta-icon" | safeHTML }}
<span class="post-meta-item reading-time">{{ $icon }}{{ $Deliver.ReadingTime }}&nbsp;{{ i18n "minute" $Deliver.ReadingTime }}</span>
{{ end }}
{{ if .Site.Params.displayReadingTime }}
{{ $icon := replace (index $.Site.Data.SVG $.Site.Params.readingTimeIcon) "icon" "icon post-meta-icon" | safeHTML }}
<span class="post-meta-item readingtime">{{ $icon }}{{ .ReadingTime }}&nbsp;{{ i18n "minute" .ReadingTime }}</span>
{{ if and $Deliver.Site.Params.displayBusuanziPagePV (eq hugo.Environment "production") }}
{{ if not $isHome }}
{{ $icon := replace (index $Deliver.Site.Data.SVG $Deliver.Site.Params.busuanziPagePVIcon) "icon" "icon post-meta-icon" | safeHTML }}
<span class="post-meta-item busuanzi-page-pv" id="busuanzi_container_page_pv">{{ $icon }}<span id="busuanzi_value_page_pv"></span></span>
{{ end }}
{{ end }}
</div>
Loading

0 comments on commit 09e9976

Please sign in to comment.