Skip to content

Commit

Permalink
refactor: Make templates return results properly (#153)
Browse files Browse the repository at this point in the history
...instead of abusing .Scratch

Fixes #137

* Make markdownify.html and content.html return results properly

* Make author.html return results properly

* Make auto-detect-images.html and images.html return results properly

* Make date.html return results properly

* Make data-attributes.html return result properly

* Make title.html return results properly

* Make toc.html return results properly

* Make relative-url.html return results properly

* Make tree-sections.html return results properly (untested)

* Rolled back change to tree-sections.html, doesn't work

BREAKING CHANGE: Various templates under `utils/partials/` will return results directly rather than via `.Scratch` now.
  • Loading branch information
palant committed May 17, 2020
1 parent 03991eb commit 418f72d
Show file tree
Hide file tree
Showing 23 changed files with 162 additions and 323 deletions.
3 changes: 1 addition & 2 deletions layouts/index.sectionsatom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@
<generator uri="https://gohugo.io/" version="{{ hugo.Version }}">Hugo</generator>
{{- range (where $pages "Section" "in" .Site.Params.mainSections) }}
{{ $page := . }}
{{- partial "utils/author.html" $page -}}
{{- $author := ($page.Scratch.Get "author") -}}
{{- $author := partial "utils/author.html" $page -}}
<entry>
<title type="text">{{ .Title }}</title>
<link rel="alternate" type="text/html" href="{{ .Permalink }}" />
Expand Down
3 changes: 1 addition & 2 deletions layouts/index.sectionsrss.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@
{{ end }}
{{ range (where $pages "Section" "in" .Site.Params.mainSections) }}
{{ $page := . }}
{{- partial "utils/author.html" $page -}}
{{- $author := ($page.Scratch.Get "author") -}}
{{- $author := partial "utils/author.html" $page -}}
<item>
<title>{{ .Title }}</title>
<link>{{ .Permalink }}</link>
Expand Down
7 changes: 2 additions & 5 deletions layouts/partials/components/post-copyright.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{{- $Deliver := . -}}
{{ if and .Site.Params.enablePostCopyright (.Params.displayCopyright | default .Site.Params.displayPostCopyright) }}
{{ partial "utils/author.html" . }}
{{ $author := .Scratch.Get "author" }}
{{ $author := partial "utils/author.html" . }}
{{ with $author.name }}
<ul class="post-copyright">
<li class="copyright-item author">
Expand All @@ -24,9 +23,7 @@
{{ end }}
{{ with $author.copyright }}
{{- $raw := . -}}
{{- partial "utils/markdownify.html" (dict "Deliver" $Deliver "raw" $raw "isContent" false) -}}
{{- $Content := $Deliver.Scratch.Get "Content" -}}
<li class="copyright-item license"><span class="copyright-item-text">{{ i18n "copyrightLicense" }}</span>{{ i18n "colon" }}{{ $Content }}</li>
<li class="copyright-item license"><span class="copyright-item-text">{{ i18n "copyrightLicense" }}</span>{{ i18n "colon" }}{{ partial "utils/markdownify.html" (dict "Deliver" $Deliver "raw" $raw "isContent" false) }}</li>
{{ end }}
</ul>
{{ end }}
Expand Down
7 changes: 3 additions & 4 deletions layouts/partials/components/post-share.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@

{{- $description := .Description | default (partial "utils/summary.html" .) | default .Site.Params.siteDescription | plainify -}}

{{- partial "utils/images.html" . -}}
{{- $images := .Scratch.Get "images" -}}
{{- $images := .Scratch.Add "images" (slice (.Site.Params.siteLogo | absURL)) -}}
{{- $images := (index (.Scratch.Get "images") 0) -}}
{{- $images := partial "utils/images.html" . -}}
{{- $images = union $images (slice (.Site.Params.siteLogo | absURL)) -}}
{{- $images = index $images 0 -}}

{{- $hashtags := newScratch -}}
{{- with .Params.tags -}}
Expand Down
12 changes: 3 additions & 9 deletions layouts/partials/footer.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,17 @@

{{- if .Site.Params.displayPoweredBy }}
{{- $raw := `Powered by [Hugo](https://github.com/gohugoio/hugo) | Theme is [MemE](https://github.com/reuixiy/hugo-theme-meme)` -}}
{{- partial "utils/markdownify.html" (dict "Deliver" $Deliver "raw" $raw "isContent" false) -}}
{{- $Content := .Scratch.Get "Content" -}}
<div class="powered-by">{{ $Content }}</div>
<div class="powered-by">{{ partial "utils/markdownify.html" (dict "Deliver" $Deliver "raw" $raw "isContent" false) }}</div>
{{- end }}

{{- if .Site.Params.displaySiteCopyright }}
{{- $raw := .Site.Copyright -}}
{{- partial "utils/markdownify.html" (dict "Deliver" $Deliver "raw" $raw "isContent" false) -}}
{{- $Content := .Scratch.Get "Content" -}}
<div class="site-copyright">{{ $Content }}</div>
<div class="site-copyright">{{ partial "utils/markdownify.html" (dict "Deliver" $Deliver "raw" $raw "isContent" false) }}</div>
{{- end }}

{{- with .Site.Params.customFooter }}
{{- $raw := . -}}
{{- partial "utils/markdownify.html" (dict "Deliver" $Deliver "raw" $raw "isContent" false) -}}
{{- $Content := $Deliver.Scratch.Get "Content" -}}
<div class="custom-footer">{{ $Content }}</div>
<div class="custom-footer">{{ partial "utils/markdownify.html" (dict "Deliver" $Deliver "raw" $raw "isContent" false) }}</div>
{{- end }}

{{- if and .Site.Params.displayBusuanziSiteUVAndPV (eq hugo.Environment "production") }}
Expand Down
12 changes: 3 additions & 9 deletions layouts/partials/head.html
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,7 @@
<!-- Avoid Baidu Mobile Search Transcoding -->
<meta http-equiv="Cache-Control" content="no-siteapp" />

{{ partial "utils/title.html" . }}
{{- $title := .Scratch.Get "title" -}}
<title>{{ $title }}</title>
<title>{{ (partial "utils/title.html" .).title }}</title>

<!-- CSS -->
{{- partial "style.html" . }}
Expand All @@ -84,13 +82,9 @@
{{- $safariMaskIcon := "icons/safari-pinned-tab.svg" -}}
{{- $safariMaskColor := .Site.Params.safariMaskColor -}}
{{- $appleTouchIcon := "icons/apple-touch-icon.png" -}}
{{- partial "utils/relative-url.html" (dict "Deliver" . "filename" "") -}}
{{- $url := .Scratch.Get "url" -}}
{{- $msApplicationStartURL := $url -}}
{{- $msApplicationStartURL := partial "utils/relative-url.html" (dict "Deliver" . "filename" "") -}}
{{- $msApplicationTileColor := .Site.Params.msApplicationTileColor -}}
{{- partial "utils/relative-url.html" (dict "Deliver" . "filename" "icons/mstile-150x150.png") -}}
{{- $url := .Scratch.Get "url" -}}
{{- $msApplicationTileImage := $url -}}
{{- $msApplicationTileImage := partial "utils/relative-url.html" (dict "Deliver" . "filename" "icons/mstile-150x150.png") -}}
{{- $manifest := "manifest.json" -}}
<link rel="shortcut icon" href="{{ $favicon | relURL }}" type="image/x-icon" />
<link rel="mask-icon" href="{{ $safariMaskIcon | relURL }}" color="{{ $safariMaskColor }}" />
Expand Down
5 changes: 1 addition & 4 deletions layouts/partials/pages/home-poetry.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@
<main class="home">
<div class="poetry">
{{ range .Site.Params.homePoetry }}
{{- $raw := . -}}
{{- partial "utils/markdownify.html" (dict "Deliver" $Deliver "raw" $raw "isContent" false) -}}
{{- $Content := $Deliver.Scratch.Get "Content" -}}
<p>{{ $Content }}</p>
<p>{{ partial "utils/markdownify.html" (dict "Deliver" $Deliver "raw" . "isContent" false) }}</p>
{{ end }}
</div>
{{ with .Site.Menus.home }}
Expand Down
34 changes: 10 additions & 24 deletions layouts/partials/pages/post.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,27 @@
<main class="main single" id="main">
<div class="main-inner">

{{ partial "utils/data-attributes.html" . }}
{{- $smallCaps := .Scratch.Get "smallCaps" -}}
{{- $align := .Scratch.Get "align" -}}
{{- $type := .Scratch.Get "type" -}}
{{- $layout := .Scratch.Get "layout" -}}
{{- $indent := .Scratch.Get "indent" -}}
{{- $tocNum := .Scratch.Get "tocNum" -}}
{{ $attrs := partial "utils/data-attributes.html" . }}

<article class="content post"
{{- if $smallCaps }} data-small-caps="true"{{ end }}
{{- with $align }} data-align="{{ . }}"{{ end }}
{{- with $type }} data-type="{{ . }}"{{ end }}
{{- with $layout }} data-layout="{{ . }}"{{ end }}
{{- if $indent }} data-indent="true"{{ end }}
{{- if $tocNum }} data-toc-num="true"{{ end }}>
{{- if $attrs.smallCaps }} data-small-caps="true"{{ end }}
{{- with $attrs.align }} data-align="{{ . }}"{{ end }}
{{- with $attrs.type }} data-type="{{ . }}"{{ end }}
{{- with $attrs.layout }} data-layout="{{ . }}"{{ end }}
{{- if $attrs.indent }} data-indent="true"{{ end }}
{{- if $attrs.tocNum }} data-toc-num="true"{{ end }}>

<h1 class="post-title">{{ .Title }}</h1>

{{ with .Params.subtitle }}
{{- $raw := . -}}
{{- partial "utils/markdownify.html" (dict "Deliver" $Deliver "raw" $raw "isContent" false) -}}
{{- $Content := $Deliver.Scratch.Get "Content" -}}
<div class="post-subtitle">{{ $Content }}</div>
<div class="post-subtitle">{{ partial "utils/markdownify.html" (dict "Deliver" $Deliver "raw" $raw "isContent" false) }}</div>
{{ end }}

{{ if .Site.Params.displayPostDescription }}
{{ with .Params.description }}
{{- $raw := . -}}
{{- partial "utils/markdownify.html" (dict "Deliver" $Deliver "raw" $raw "isContent" false) -}}
{{- $Content := $Deliver.Scratch.Get "Content" -}}
<div class="post-description">{{ $Content }}</div>
<div class="post-description">{{ partial "utils/markdownify.html" (dict "Deliver" $Deliver "raw" $raw "isContent" false) }}</div>
{{ end }}
{{ end }}

Expand All @@ -43,14 +33,10 @@ <h1 class="post-title">{{ .Title }}</h1>
{{ $enableTOC := .Params.toc | default .Site.Params.enableTOC -}}
{{- if $enableTOC -}}
{{- partial "utils/toc.html" . -}}
{{- $toc := .Scratch.Get "toc" }}
{{ $toc -}}
{{- end -}}

{{- partial "utils/content.html" . -}}
{{- $Content := .Scratch.Get "Content" -}}
<div class="post-body">
{{- $Content -}}
{{ partial "utils/content.html" . }}
</div>

</article>
Expand Down
3 changes: 1 addition & 2 deletions layouts/partials/third-party/disqus.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
{{- partial "utils/title.html" . -}}
{{- $rawTitle := .Scratch.Get "rawTitle" -}}
{{- $rawTitle := (partial "utils/title.html" .).rawTitle -}}
<script>
function loadComments() {
if (typeof DISQUS === 'undefined') {
Expand Down
5 changes: 2 additions & 3 deletions layouts/partials/third-party/service-worker.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
{{ if and .Site.Params.enableServiceWorker (eq hugo.Environment "production") }}
{{- partial "utils/relative-url.html" (dict "Deliver" . "filename" "sw.js") -}}
{{- $url := .Scratch.Get "url" }}
{{- $url := partial "utils/relative-url.html" (dict "Deliver" . "filename" "sw.js") -}}

<div class="app-refresh" id="app-refresh">
<div class="app-refresh-wrap" onclick="location.reload()">
Expand All @@ -18,7 +17,7 @@
}

window.addEventListener('load', function() {
{{ printf `navigator.serviceWorker.register('%s');` $url | safeJS }}
navigator.serviceWorker.register('{{ $url }}');
});
}

Expand Down
33 changes: 17 additions & 16 deletions layouts/partials/utils/author.html
Original file line number Diff line number Diff line change
@@ -1,47 +1,48 @@
{{- .Scratch.Delete "author" -}}
{{- $author := dict -}}
{{- if .Params.original | default .Site.Params.original -}}
{{- with .Site.Author.name -}}
{{- $.Scratch.SetInMap "author" "name" . -}}
{{- $author = merge $author (dict "name" .) -}}
{{- end -}}
{{- with .Site.Author.email -}}
{{- $.Scratch.SetInMap "author" "email" . -}}
{{- $author = merge $author (dict "email" .) -}}
{{- end -}}
{{- with .Site.Author.motto -}}
{{- $.Scratch.SetInMap "author" "motto" . -}}
{{- $author = merge $author (dict "motto" .) -}}
{{- end -}}
{{- with .Site.Author.avatar -}}
{{- $.Scratch.SetInMap "author" "avatar" . -}}
{{- $author = merge $author (dict "avatar" .) -}}
{{- end -}}
{{- $baseURLWithLangFix := (printf "%s%s/" (strings.TrimSuffix "/" .Site.BaseURL) .Site.LanguagePrefix) -}}
{{- with .Site.Author.website | default $baseURLWithLangFix -}}
{{- $.Scratch.SetInMap "author" "website" . -}}
{{- $author = merge $author (dict "website" .) -}}
{{- end -}}
{{- with .Site.Copyright -}}
{{- $.Scratch.SetInMap "author" "copyright" . -}}
{{- $author = merge $author (dict "copyright" .) -}}
{{- end -}}
{{- with .Site.Author.twitter -}}
{{- $.Scratch.SetInMap "author" "twitter" . -}}
{{- $author = merge $author (dict "twitter" .) -}}
{{- end -}}
{{- else -}}
{{- with .Params.author -}}
{{- $.Scratch.SetInMap "author" "name" . -}}
{{- $author = merge $author (dict "name" .) -}}
{{- end -}}
{{- with .Params.email -}}
{{- $.Scratch.SetInMap "author" "email" . -}}
{{- $author = merge $author (dict "email" .) -}}
{{- end -}}
{{- with .Params.motto -}}
{{- $.Scratch.SetInMap "author" "motto" . -}}
{{- $author = merge $author (dict "motto" .) -}}
{{- end -}}
{{- with .Params.avatar -}}
{{- $.Scratch.SetInMap "author" "avatar" . -}}
{{- $author = merge $author (dict "avatar" .) -}}
{{- end -}}
{{- with .Params.website -}}
{{- $.Scratch.SetInMap "author" "website" . -}}
{{- $author = merge $author (dict "website" .) -}}
{{- end -}}
{{- with .Params.copyright -}}
{{- $.Scratch.SetInMap "author" "copyright" . -}}
{{- $author = merge $author (dict "copyright" .) -}}
{{- end -}}
{{- with .Params.twitter -}}
{{- $.Scratch.SetInMap "author" "twitter" . -}}
{{- $author = merge $author (dict "twitter" .) -}}
{{- end -}}
{{- end -}}
{{- end -}}
{{- return $author -}}
28 changes: 5 additions & 23 deletions layouts/partials/utils/auto-detect-images.html
Original file line number Diff line number Diff line change
@@ -1,29 +1,11 @@
<!-- https://gohugo.io/functions/findre/ -->
<!-- https://gohugo.io/functions/replacere/ -->
<!-- https://regex101.com/ -->
{{- $imgs := slice -}}
{{- if and .IsPage (in .Site.Params.mainSections .Section) -}}
{{- $imgs := findRE `<img src="/?([^"]+)` .Content | uniq -}}
{{- with $imgs -}}
{{- $.Scratch.Delete "imgsURL" -}}
{{- range . -}}
{{- $.Scratch.Delete "replacement" -}}
{{- if and $.Site.Params.enableImageHost $.Site.Params.headAlso -}}
{{- if (eq hugo.Environment "production") -}}
{{- $url := replaceRE `<img src="/?([^"]+)` `$1` . -}}
{{- if ne (substr $url 0 4) "http" -}}
{{- $hostURL := $.Site.Params.imageHostURL -}}
{{- $replacement := (printf `%s$1` $hostURL) -}}
{{- $.Scratch.Set "replacement" $replacement -}}
{{- else -}}
{{- $.Scratch.Set "replacement" `$1` -}}
{{- end -}}
{{- end -}}
{{- else -}}
{{- $.Scratch.Set "replacement" `$1` -}}
{{- end -}}
{{- $replacement := $.Scratch.Get "replacement" -}}
{{- $imgsURL := replaceRE `<img src="/?([^"]+)` $replacement . -}}
{{- $.Scratch.Add "imgsURL" (slice $imgsURL) -}}
{{- end -}}
{{- range (findRE `<img src="/?([^"]+)` .Content | uniq) -}}
{{- $url := replaceRE `<img src="/?([^"]+)` `$1` . -}}
{{- $imgs = union $imgs (slice $url) -}}
{{- end -}}
{{- end -}}
{{- return $imgs -}}

0 comments on commit 418f72d

Please sign in to comment.