From b0a9cf0a759e63309ac7e37bb90de161b3ebc051 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Erik=20Pedersen?= Date: Wed, 9 Feb 2022 18:29:49 +0100 Subject: tpl: Use go:embed to load internal templates Fixes #8297 --- magefile.go | 3 +- tpl/tplimpl/embedded/README.md | 5 - tpl/tplimpl/embedded/generate/generate.go | 98 --- tpl/tplimpl/embedded/templates.autogen.go | 761 --------------------- .../templates/shortcodes/1__h_simple_assets.html | 34 + .../templates/shortcodes/__h_simple_assets.html | 34 - tpl/tplimpl/template.go | 31 +- 7 files changed, 61 insertions(+), 905 deletions(-) delete mode 100644 tpl/tplimpl/embedded/README.md delete mode 100644 tpl/tplimpl/embedded/generate/generate.go delete mode 100644 tpl/tplimpl/embedded/templates.autogen.go create mode 100644 tpl/tplimpl/embedded/templates/shortcodes/1__h_simple_assets.html delete mode 100644 tpl/tplimpl/embedded/templates/shortcodes/__h_simple_assets.html diff --git a/magefile.go b/magefile.go index 16f630abc..a406664cb 100644 --- a/magefile.go +++ b/magefile.go @@ -1,3 +1,4 @@ +//go:build mage // +build mage package main @@ -79,7 +80,7 @@ func flagEnv() map[string]string { // Generate autogen packages func Generate() error { generatorPackages := []string{ - "tpl/tplimpl/embedded/generate", + //"tpl/tplimpl/embedded/generate", //"resources/page/generate", } diff --git a/tpl/tplimpl/embedded/README.md b/tpl/tplimpl/embedded/README.md deleted file mode 100644 index 1c01961e1..000000000 --- a/tpl/tplimpl/embedded/README.md +++ /dev/null @@ -1,5 +0,0 @@ - - -## Build Templates - -If you add or modify any template in the templates folder, you also need to run `mage generate` to get the Go code in synch. diff --git a/tpl/tplimpl/embedded/generate/generate.go b/tpl/tplimpl/embedded/generate/generate.go deleted file mode 100644 index 09a253e29..000000000 --- a/tpl/tplimpl/embedded/generate/generate.go +++ /dev/null @@ -1,98 +0,0 @@ -// Copyright 2019 The Hugo Authors. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -//go:generate go run generate.go - -package main - -import ( - "fmt" - "io/ioutil" - "log" - "os" - "path/filepath" - "strings" -) - -func main() { - templateFolder := filepath.Join("..", "templates") - - templatePath := filepath.Join(".", templateFolder) - - file, err := os.Create("../templates.autogen.go") - if err != nil { - log.Fatal(err) - } - defer file.Close() - - var nameValues []string - - err = filepath.Walk(templatePath, func(path string, info os.FileInfo, err error) error { - if err != nil { - return err - } - - if info.IsDir() { - return nil - } - if strings.HasPrefix(info.Name(), ".") { - return nil - } - - templateName := filepath.ToSlash(strings.TrimPrefix(path, templateFolder+string(os.PathSeparator))) - - templateContent, err := ioutil.ReadFile(path) - if err != nil { - return err - } - - nameValues = append(nameValues, nameValue(templateName, string(templateContent))) - - return nil - }) - - if err != nil { - log.Fatal(err) - } - - fmt.Fprint(file, `// Copyright 2019 The Hugo Authors. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -// This file is autogenerated. - -// Package embedded defines the internal templates that Hugo provides. -package embedded - -// EmbeddedTemplates represents all embedded templates. -var EmbeddedTemplates = [][2]string{ -`) - - for _, v := range nameValues { - fmt.Fprint(file, " ", v, ",\n") - } - fmt.Fprint(file, "}\n") -} - -func nameValue(name, value string) string { - return fmt.Sprintf("{`%s`, `%s`}", name, value) -} diff --git a/tpl/tplimpl/embedded/templates.autogen.go b/tpl/tplimpl/embedded/templates.autogen.go deleted file mode 100644 index 61a5e856e..000000000 --- a/tpl/tplimpl/embedded/templates.autogen.go +++ /dev/null @@ -1,761 +0,0 @@ -// Copyright 2019 The Hugo Authors. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -// This file is autogenerated. - -// Package embedded defines the internal templates that Hugo provides. -package embedded - -// EmbeddedTemplates represents all embedded templates. -var EmbeddedTemplates = [][2]string{ - {`_default/robots.txt`, `User-agent: *`}, - {`_default/rss.xml`, `{{- $pctx := . -}} -{{- if .IsHome -}}{{ $pctx = .Site }}{{- end -}} -{{- $pages := slice -}} -{{- if or $.IsHome $.IsSection -}} -{{- $pages = $pctx.RegularPages -}} -{{- else -}} -{{- $pages = $pctx.Pages -}} -{{- end -}} -{{- $limit := .Site.Config.Services.RSS.Limit -}} -{{- if ge $limit 1 -}} -{{- $pages = $pages | first $limit -}} -{{- end -}} -{{- printf "" | safeHTML }} - - - {{ if eq .Title .Site.Title }}{{ .Site.Title }}{{ else }}{{ with .Title }}{{.}} on {{ end }}{{ .Site.Title }}{{ end }} - {{ .Permalink }} - Recent content {{ if ne .Title .Site.Title }}{{ with .Title }}in {{.}} {{ end }}{{ end }}on {{ .Site.Title }} - Hugo -- gohugo.io{{ with .Site.LanguageCode }} - {{.}}{{end}}{{ with .Site.Author.email }} - {{.}}{{ with $.Site.Author.name }} ({{.}}){{end}}{{end}}{{ with .Site.Author.email }} - {{.}}{{ with $.Site.Author.name }} ({{.}}){{end}}{{end}}{{ with .Site.Copyright }} - {{.}}{{end}}{{ if not .Date.IsZero }} - {{ .Date.Format "Mon, 02 Jan 2006 15:04:05 -0700" | safeHTML }}{{ end }} - {{- with .OutputFormats.Get "RSS" -}} - {{ printf "" .Permalink .MediaType | safeHTML }} - {{- end -}} - {{ range $pages }} - - {{ .Title }} - {{ .Permalink }} - {{ .Date.Format "Mon, 02 Jan 2006 15:04:05 -0700" | safeHTML }} - {{ with .Site.Author.email }}{{.}}{{ with $.Site.Author.name }} ({{.}}){{end}}{{end}} - {{ .Permalink }} - {{ .Summary | html }} - - {{ end }} - - -`}, - {`_default/sitemap.xml`, `{{ printf "" | safeHTML }} - - {{ range .Data.Pages }} - {{- if .Permalink -}} - - {{ .Permalink }}{{ if not .Lastmod.IsZero }} - {{ safeHTML ( .Lastmod.Format "2006-01-02T15:04:05-07:00" ) }}{{ end }}{{ with .Sitemap.ChangeFreq }} - {{ . }}{{ end }}{{ if ge .Sitemap.Priority 0.0 }} - {{ .Sitemap.Priority }}{{ end }}{{ if .IsTranslated }}{{ range .Translations }} - {{ end }} - {{ end }} - - {{- end -}} - {{ end }} - -`}, - {`_default/sitemapindex.xml`, `{{ printf "" | safeHTML }} - - {{ range . }} - - {{ .SitemapAbsURL }} - {{ if not .LastChange.IsZero }} - {{ .LastChange.Format "2006-01-02T15:04:05-07:00" | safeHTML }} - {{ end }} - - {{ end }} - -`}, - {`alias.html`, `{{ .Permalink }}`}, - {`disqus.html`, `{{- $pc := .Site.Config.Privacy.Disqus -}} -{{- if not $pc.Disable -}} -{{ if .Site.DisqusShortname }}
- - -comments powered by Disqus{{end}} -{{- end -}}`}, - {`google_analytics.html`, `{{- $pc := .Site.Config.Privacy.GoogleAnalytics -}} -{{- if not $pc.Disable }}{{ with .Site.GoogleAnalytics -}} -{{ if hasPrefix . "G-"}} - - -{{ else if hasPrefix . "UA-" }} - -{{- end -}} -{{- end }}{{ end -}} - -{{- define "__ga_js_set_doNotTrack" -}}{{/* This is also used in the async version. */}} -{{- $pc := .Site.Config.Privacy.GoogleAnalytics -}} -{{- if not $pc.RespectDoNotTrack -}} -var doNotTrack = false; -{{- else -}} -var dnt = (navigator.doNotTrack || window.doNotTrack || navigator.msDoNotTrack); -var doNotTrack = (dnt == "1" || dnt == "yes"); -{{- end -}} -{{- end -}}`}, - {`google_analytics_async.html`, `{{- $pc := .Site.Config.Privacy.GoogleAnalytics -}} -{{- if not $pc.Disable -}} -{{ with .Site.GoogleAnalytics }} - - -{{ end }} -{{- end -}} -`}, - {`google_news.html`, `{{- warnf "The google_news internal template will be removed in a future release. Please remove calls to this template. See https://github.com/gohugoio/hugo/issues/9172 for additional information." -}} -{{- if .IsPage -}} - {{- with .Params.news_keywords -}} - - {{- end -}} -{{- end -}} -`}, - {`opengraph.html`, ` - - - - -{{- with $.Params.images -}} -{{- range first 6 . }}{{ end -}} -{{- else -}} -{{- $images := $.Resources.ByType "image" -}} -{{- $featured := $images.GetMatch "*feature*" -}} -{{- if not $featured }}{{ $featured = $images.GetMatch "{*cover*,*thumbnail*}" }}{{ end -}} -{{- with $featured -}} - -{{- else -}} -{{- with $.Site.Params.images }}{{ end -}} -{{- end -}} -{{- end -}} - -{{- if .IsPage }} -{{- $iso8601 := "2006-01-02T15:04:05-07:00" -}} - -{{ with .PublishDate }}{{ end }} -{{ with .Lastmod }}{{ end }} -{{- end -}} - -{{- with .Params.audio }}{{ end }} -{{- with .Params.locale }}{{ end }} -{{- with .Site.Params.title }}{{ end }} -{{- with .Params.videos }}{{- range . }} - -{{ end }}{{ end }} - -{{- /* If it is part of a series, link to related articles */}} -{{- $permalink := .Permalink }} -{{- $siteSeries := .Site.Taxonomies.series }} -{{ with .Params.series }}{{- range $name := . }} - {{- $series := index $siteSeries ($name | urlize) }} - {{- range $page := first 6 $series.Pages }} - {{- if ne $page.Permalink $permalink }}{{ end }} - {{- end }} -{{ end }}{{ end }} - -{{- /* Facebook Page Admin ID for Domain Insights */}} -{{- with .Site.Social.facebook_admin }}{{ end }} -`}, - {`pagination.html`, `{{- $validFormats := slice "default" "terse" }} - -{{- $msg1 := "When passing a map to the internal pagination template, one of the elements must be named 'page', and it must be set to the context of the current page." }} -{{- $msg2 := "The 'format' specified in the map passed to the internal pagination template is invalid. Valid choices are: %s." }} - -{{- $page := . }} -{{- $format := "default" }} - -{{- if reflect.IsMap . }} - {{- with .page }} - {{- $page = . }} - {{- else }} - {{- errorf $msg1 }} - {{- end }} - {{- with .format }} - {{- $format = lower . }} - {{- end }} -{{- end }} - -{{- if in $validFormats $format }} - {{- if gt $page.Paginator.TotalPages 1 }} - - {{- end }} -{{- else }} - {{- errorf $msg2 (delimit $validFormats ", ") }} -{{- end -}} - -{{/* Format: default -{{/* --------------------------------------------------------------------- */}} -{{- define "partials/inline/pagination/default" }} - {{- with .Paginator }} - {{- $currentPageNumber := .PageNumber }} - - {{- with .First }} - {{- if ne $currentPageNumber .PageNumber }} -
  • - -
  • - {{- else }} -
  • - -
  • - {{- end }} - {{- end }} - - {{- with .Prev }} -
  • - -
  • - {{- else }} -
  • - -
  • - {{- end }} - - {{- $slots := 5 }} - {{- $start := math.Max 1 (sub .PageNumber (math.Floor (div $slots 2))) }} - {{- $end := math.Min .TotalPages (sub (add $start $slots) 1) }} - {{- if lt (add (sub $end $start) 1) $slots }} - {{- $start = math.Max 1 (add (sub $end $slots) 1) }} - {{- end }} - - {{- range $k := seq $start $end }} - {{- if eq $.Paginator.PageNumber $k }} -
  • - {{ $k }} -
  • - {{- else }} -
  • - {{ $k }} -
  • - {{- end }} - {{- end }} - - {{- with .Next }} -
  • - -
  • - {{- else }} -
  • - -
  • - {{- end }} - - {{- with .Last }} - {{- if ne $currentPageNumber .PageNumber }} -
  • - -
  • - {{- else }} -
  • - -
  • - {{- end }} - {{- end }} - {{- end }} -{{- end -}} - -{{/* Format: terse -{{/* --------------------------------------------------------------------- */}} -{{- define "partials/inline/pagination/terse" }} - {{- with .Paginator }} - {{- $currentPageNumber := .PageNumber }} - - {{- with .First }} - {{- if ne $currentPageNumber .PageNumber }} -
  • - -
  • - {{- end }} - {{- end }} - - {{- with .Prev }} -
  • - -
  • - {{- end }} - - {{- $slots := 3 }} - {{- $start := math.Max 1 (sub .PageNumber (math.Floor (div $slots 2))) }} - {{- $end := math.Min .TotalPages (sub (add $start $slots) 1) }} - {{- if lt (add (sub $end $start) 1) $slots }} - {{- $start = math.Max 1 (add (sub $end $slots) 1) }} - {{- end }} - - {{- range $k := seq $start $end }} - {{- if eq $.Paginator.PageNumber $k }} -
  • - {{ $k }} -
  • - {{- else }} -
  • - {{ $k }} -
  • - {{- end }} - {{- end }} - - {{- with .Next }} -
  • - -
  • - {{- end }} - - {{- with .Last }} - {{- if ne $currentPageNumber .PageNumber }} -
  • - -
  • - {{- end }} - {{- end }} - {{- end }} -{{- end -}} -`}, - {`schema.html`, ` - - -{{- if .IsPage -}} -{{- $iso8601 := "2006-01-02T15:04:05-07:00" -}} -{{ with .PublishDate }}{{ end}} -{{ with .Lastmod }}{{ end}} - - -{{- with $.Params.images -}} -{{- range first 6 . -}}{{ end -}} -{{- else -}} -{{- $images := $.Resources.ByType "image" -}} -{{- $featured := $images.GetMatch "*feature*" -}} -{{- if not $featured }}{{ $featured = $images.GetMatch "{*cover*,*thumbnail*}" }}{{ end -}} -{{- with $featured -}} - -{{- else -}} -{{- with $.Site.Params.images -}}{{ end -}} -{{- end -}} -{{- end -}} - - - -{{- end -}} -`}, - {`shortcodes/__h_simple_assets.html`, `{{ define "__h_simple_css" }}{{/* These template definitions are global. */}} -{{- if not (.Page.Scratch.Get "__h_simple_css") -}} -{{/* Only include once */}} -{{- .Page.Scratch.Set "__h_simple_css" true -}} - -{{- end -}} -{{- end -}} -{{- define "__h_simple_icon_play" -}} - -{{- end -}} -`}, - {`shortcodes/figure.html`, ` - {{- if .Get "link" -}} - - {{- end -}} - {{ with .Get - {{- if .Get "link" }}{{ end -}} - {{- if or (or (.Get "title") (.Get "caption")) (.Get "attr") -}} -
    - {{ with (.Get "title") -}} -

    {{ . }}

    - {{- end -}} - {{- if or (.Get "caption") (.Get "attr") -}}

    - {{- .Get "caption" | markdownify -}} - {{- with .Get "attrlink" }} - - {{- end -}} - {{- .Get "attr" | markdownify -}} - {{- if .Get "attrlink" }}{{ end }}

    - {{- end }} -
    - {{- end }} - -`}, - {`shortcodes/gist.html`, ` -`}, - {`shortcodes/highlight.html`, `{{ if len .Params | eq 2 }}{{ highlight (trim .Inner "\n\r") (.Get 0) (.Get 1) }}{{ else }}{{ highlight (trim .Inner "\n\r") (.Get 0) "" }}{{ end }}`}, - {`shortcodes/instagram.html`, `{{- $pc := site.Config.Privacy.Instagram -}} -{{- if not $pc.Disable -}} - {{ $accessToken := site.Config.Services.Instagram.AccessToken }} - {{- if not $accessToken -}} - {{- erroridf "error-missing-instagram-accesstoken" "instagram shortcode: Missing config value for services.instagram.accessToken. This can be set in config.toml, but it is recommended to configure this via the HUGO_SERVICES_INSTAGRAM_ACCESSTOKEN OS environment variable. If you are using a Client Access Token, remember that you must combine it with your App ID using a pipe symbol (|) otherwise the request will fail." -}} - {{- else -}} - {{- if $pc.Simple -}} - {{ template "_internal/shortcodes/instagram_simple.html" . }} - {{- else -}} - {{ $id := .Get 0 }} - {{ $hideCaption := cond (eq (.Get 1) "hidecaption") "1" "0" }} - {{ $headers := dict "Authorization" (printf "Bearer %s" $accessToken) }} - {{ with getJSON "https://graph.facebook.com/v8.0/instagram_oembed/?url=https://instagram.com/p/" $id "/&hidecaption=" $hideCaption $headers }} - {{ .html | safeHTML }} - {{ end }} - {{- end -}} - {{- end -}} -{{- end -}}`}, - {`shortcodes/instagram_simple.html`, `{{- $pc := .Page.Site.Config.Privacy.Instagram -}} -{{- $sc := .Page.Site.Config.Services.Instagram -}} -{{- if not $pc.Disable -}} - {{ $accessToken := site.Config.Services.Instagram.AccessToken }} - {{- if not $accessToken -}} - {{- erroridf "error-missing-instagram-accesstoken" "instagram shortcode: Missing config value for services.instagram.accessToken. This can be set in config.toml, but it is recommended to configure this via the HUGO_SERVICES_INSTAGRAM_ACCESSTOKEN OS environment variable. If you are using a Client Access Token, remember that you must combine it with your App ID using a pipe symbol (|) otherwise the request will fail." -}} - {{- else -}} - {{- $id := .Get 0 -}} - {{- $headers := dict "Authorization" (printf "Bearer %s" $accessToken) -}} - {{- $item := getJSON "https://graph.facebook.com/v8.0/instagram_oembed/?url=https://instagram.com/p/" $id "/&maxwidth=640&omitscript=true" $headers -}} - {{- $class1 := "__h_instagram" -}} - {{- $class2 := "s_instagram_simple" -}} - {{- $hideCaption := (eq (.Get 1) "hidecaption") -}} - {{ with $item }} - {{- $mediaURL := printf "https://instagram.com/p/%s/" $id | safeURL -}} - {{- if not $sc.DisableInlineCSS -}} - {{ template "__h_simple_instagram_css" $ }} - {{- end -}} -
    - - - Instagram Image - -
    - {{ if not $hideCaption }} -

    - - {{ $item.author_name }} - - {{ $item.title}} -

    - {{ end }} - - View More on Instagram - -
    -
    - {{ end }} - {{- end -}} -{{- end -}} - -{{ define "__h_simple_instagram_css" }} - {{ if not (.Page.Scratch.Get "__h_simple_instagram_css") }} - {{/* Only include once */}} - {{ .Page.Scratch.Set "__h_simple_instagram_css" true }} - - {{ end }} -{{ end }}`}, - {`shortcodes/param.html`, `{{- $name := (.Get 0) -}} -{{- with $name -}} -{{- with ($.Page.Param .) }}{{ . }}{{ else }}{{ errorf "Param %q not found: %s" $name $.Position }}{{ end -}} -{{- else }}{{ errorf "Missing param key: %s" $.Position }}{{ end -}}`}, - {`shortcodes/ref.html`, `{{ ref . .Params }}`}, - {`shortcodes/relref.html`, `{{ relref . .Params }}`}, - {`shortcodes/twitter.html`, `{{- $pc := .Page.Site.Config.Privacy.Twitter -}} -{{- if not $pc.Disable -}} - {{- if $pc.Simple -}} - {{- template "_internal/shortcodes/twitter_simple.html" . -}} - {{- else -}} - {{- $msg1 := "The %q shortcode requires two named parameters: user and id. See %s" -}} - {{- $msg2 := "The %q shortcode will soon require two named parameters: user and id. See %s" -}} - {{- if .IsNamedParams -}} - {{- $id := .Get "id" -}} - {{- $user := .Get "user" -}} - {{- if and $id $user -}} - {{- template "render-tweet" (dict "id" $id "user" $user "dnt" $pc.EnableDNT) -}} - {{- else -}} - {{- errorf $msg1 .Name .Position -}} - {{- end -}} - {{- else -}} - {{- $id := .Get 1 -}} - {{- $user := .Get 0 -}} - {{- if eq 1 (len .Params) -}} - {{- $id = .Get 0 -}} - {{- $user = "x" -}} {{/* This triggers a redirect. It works, but may not work forever. */}} - {{- warnf $msg2 .Name .Position -}} - {{- end -}} - {{- template "render-tweet" (dict "id" $id "user" $user "dnt" $pc.EnableDNT) -}} - {{- end -}} - {{- end -}} -{{- end -}} - -{{- define "render-tweet" -}} - {{- $url := printf "https://twitter.com/%v/status/%v" .user .id -}} - {{- $query := querify "url" $url "dnt" .dnt -}} - {{- $request := printf "https://publish.twitter.com/oembed?%s" $query -}} - {{- $json := getJSON $request -}} - {{- $json.html | safeHTML -}} -{{- end -}} -`}, - {`shortcodes/twitter_simple.html`, `{{- $pc := .Page.Site.Config.Privacy.Twitter -}} -{{- $sc := .Page.Site.Config.Services.Twitter -}} -{{- if not $pc.Disable -}} - {{- $msg1 := "The %q shortcode requires two named parameters: user and id. See %s" -}} - {{- $msg2 := "The %q shortcode will soon require two named parameters: user and id. See %s" -}} - {{- if .IsNamedParams -}} - {{- $id := .Get "id" -}} - {{- $user := .Get "user" -}} - {{- if and $id $user -}} - {{- template "render-simple-tweet" (dict "id" $id "user" $user "dnt" $pc.EnableDNT "disableInlineCSS" $sc.DisableInlineCSS "ctx" .) -}} - {{- else -}} - {{- errorf $msg1 .Name .Position -}} - {{- end -}} - {{- else -}} - {{- $id := .Get 1 -}} - {{- $user := .Get 0 -}} - {{- if eq 1 (len .Params) -}} - {{- $id = .Get 0 -}} - {{- $user = "x" -}} {{/* This triggers a redirect. It works, but may not work forever. */}} - {{- warnf $msg2 .Name .Position -}} - {{- end -}} - {{- template "render-simple-tweet" (dict "id" $id "user" $user "dnt" $pc.EnableDNT "disableInlineCSS" $sc.DisableInlineCSS "ctx" .) -}} - {{- end -}} -{{- end -}} - -{{- define "render-simple-tweet" -}} - {{- $url := printf "https://twitter.com/%v/status/%v" .user .id -}} - {{- $query := querify "url" $url "dnt" .dnt "omit_script" true -}} - {{- $request := printf "https://publish.twitter.com/oembed?%s" $query -}} - {{- $json := getJSON $request -}} - {{- if not .disableInlineCSS -}} - {{- template "__h_simple_twitter_css" .ctx -}} - {{- end }} - {{ $json.html | safeHTML -}} -{{- end -}} - -{{- define "__h_simple_twitter_css" -}} - {{- if not (.Page.Scratch.Get "__h_simple_twitter_css") -}} - {{/* Only include once */}} - {{- .Page.Scratch.Set "__h_simple_twitter_css" true }} - - {{- end -}} -{{- end -}} -`}, - {`shortcodes/vimeo.html`, `{{- $pc := .Page.Site.Config.Privacy.Vimeo -}} -{{- if not $pc.Disable -}} -{{- if $pc.Simple -}} -{{ template "_internal/shortcodes/vimeo_simple.html" . }} -{{- else -}} -{{ if .IsNamedParams }}
    - -
    {{ else }} -
    - -
    -{{ end }} -{{- end -}} -{{- end -}}`}, - {`shortcodes/vimeo_simple.html`, `{{- $pc := .Page.Site.Config.Privacy.Vimeo -}} -{{- if not $pc.Disable -}} -{{ $id := .Get "id" | default (.Get 0) }} -{{ $dnt := cond (eq $pc.EnableDNT true) "?dnt=1" "" }} -{{- $item := getJSON (print "https://vimeo.com/api/oembed.json?url=https://vimeo.com/" $id $dnt) -}} -{{ $class := .Get "class" | default (.Get 1) }} -{{ $hasClass := $class }} -{{ $class := $class | default "__h_video" }} -{{ if not $hasClass }} -{{/* If class is set, assume the user wants to provide his own styles. */}} -{{ template "__h_simple_css" $ }} -{{ end }} -{{ $secondClass := "s_video_simple" }} - -{{- end -}} -{{- end -}}`}, - {`shortcodes/youtube.html`, `{{- $pc := .Page.Site.Config.Privacy.YouTube -}} -{{- if not $pc.Disable -}} -{{- $ytHost := cond $pc.PrivacyEnhanced "www.youtube-nocookie.com" "www.youtube.com" -}} -{{- $id := .Get "id" | default (.Get 0) -}} -{{- $class := .Get "class" | default (.Get 1) -}} -{{- $title := .Get "title" | default "YouTube Video" }} -
    - -
    -{{ end -}} -`}, - {`twitter_cards.html`, `{{- with $.Params.images -}} - - -{{ else -}} -{{- $images := $.Resources.ByType "image" -}} -{{- $featured := $images.GetMatch "*feature*" -}} -{{- if not $featured }}{{ $featured = $images.GetMatch "{*cover*,*thumbnail*}" }}{{ end -}} -{{- with $featured -}} - - -{{- else -}} -{{- with $.Site.Params.images -}} - - -{{ else -}} - -{{- end -}} -{{- end -}} -{{- end }} - - -{{ with .Site.Social.twitter -}} - -{{ end -}} -`}, -} diff --git a/tpl/tplimpl/embedded/templates/shortcodes/1__h_simple_assets.html b/tpl/tplimpl/embedded/templates/shortcodes/1__h_simple_assets.html new file mode 100644 index 000000000..da1bb82eb --- /dev/null +++ b/tpl/tplimpl/embedded/templates/shortcodes/1__h_simple_assets.html @@ -0,0 +1,34 @@ +{{ define "__h_simple_css" }}{{/* These template definitions are global. */}} +{{- if not (.Page.Scratch.Get "__h_simple_css") -}} +{{/* Only include once */}} +{{- .Page.Scratch.Set "__h_simple_css" true -}} + +{{- end -}} +{{- end -}} +{{- define "__h_simple_icon_play" -}} + +{{- end -}} diff --git a/tpl/tplimpl/embedded/templates/shortcodes/__h_simple_assets.html b/tpl/tplimpl/embedded/templates/shortcodes/__h_simple_assets.html deleted file mode 100644 index da1bb82eb..000000000 --- a/tpl/tplimpl/embedded/templates/shortcodes/__h_simple_assets.html +++ /dev/null @@ -1,34 +0,0 @@ -{{ define "__h_simple_css" }}{{/* These template definitions are global. */}} -{{- if not (.Page.Scratch.Get "__h_simple_css") -}} -{{/* Only include once */}} -{{- .Page.Scratch.Set "__h_simple_css" true -}} - -{{- end -}} -{{- end -}} -{{- define "__h_simple_icon_play" -}} - -{{- end -}} diff --git a/tpl/tplimpl/template.go b/tpl/tplimpl/template.go index 3ef815e24..66ef7fc21 100644 --- a/tpl/tplimpl/template.go +++ b/tpl/tplimpl/template.go @@ -14,7 +14,10 @@ package tplimpl import ( + "bytes" + "embed" "io" + "io/fs" "os" "path/filepath" "reflect" @@ -40,8 +43,6 @@ import ( "github.com/gohugoio/hugo/hugofs/files" "github.com/pkg/errors" - "github.com/gohugoio/hugo/tpl/tplimpl/embedded" - htmltemplate "github.com/gohugoio/hugo/tpl/internal/go_templates/htmltemplate" texttemplate "github.com/gohugoio/hugo/tpl/internal/go_templates/texttemplate" @@ -664,12 +665,29 @@ func (t *templateHandler) extractIdentifiers(line string) []string { return identifiers } +//go:embed embedded/templates/* +var embededTemplatesFs embed.FS + func (t *templateHandler) loadEmbedded() error { - for _, kv := range embedded.EmbeddedTemplates { - name, templ := kv[0], kv[1] + return fs.WalkDir(embededTemplatesFs, ".", func(path string, d fs.DirEntry, err error) error { + if d == nil || d.IsDir() { + return nil + } + + templb, err := embededTemplatesFs.ReadFile(path) + if err != nil { + return err + } + + // Get the newlines on Windows in line with how we had it back when we used Go Generate + // to write the templates to Go files. + templ := string(bytes.ReplaceAll(templb, []byte("\r\n"), []byte("\n"))) + name := strings.TrimPrefix(filepath.ToSlash(path), "embedded/templates/") + if err := t.AddTemplate(internalPathPrefix+name, templ); err != nil { return err } + if aliases, found := embeddedTemplatesAliases[name]; found { // TODO(bep) avoid reparsing these aliases for _, alias := range aliases { @@ -679,8 +697,9 @@ func (t *templateHandler) loadEmbedded() error { } } } - } - return nil + + return nil + }) } func (t *templateHandler) loadTemplates() error { -- cgit v1.2.3