Move templates to an internal package

This commit is contained in:
2022-02-09 13:01:41 +01:00
parent 992059d17b
commit c2b58f21c8
19 changed files with 37 additions and 11 deletions
+2 -1
View File
@@ -6,9 +6,10 @@ import (
routing "github.com/jackwhelpton/fasthttp-routing/v2"
"github.com/valyala/fasthttp"
"code.tokarch.uk/mainnika/nikita-tokarch-uk/templates"
"code.tokarch.uk/mainnika/nikita-tokarch-uk/pkg/content"
"code.tokarch.uk/mainnika/nikita-tokarch-uk/pkg/ghost/params"
"code.tokarch.uk/mainnika/nikita-tokarch-uk/pkg/templates"
)
// relativeRedirectBytes makes a relative redirect by using http Location header
+1 -1
View File
@@ -6,7 +6,7 @@ import (
routing "github.com/jackwhelpton/fasthttp-routing/v2"
"github.com/valyala/fasthttp"
"code.tokarch.uk/mainnika/nikita-tokarch-uk/pkg/templates"
"code.tokarch.uk/mainnika/nikita-tokarch-uk/templates"
)
var _ routing.DataWriter = (*TemplateWriter)(nil)
+2 -1
View File
@@ -6,9 +6,10 @@ import (
routing "github.com/jackwhelpton/fasthttp-routing/v2"
"github.com/valyala/fasthttp"
"code.tokarch.uk/mainnika/nikita-tokarch-uk/templates"
"code.tokarch.uk/mainnika/nikita-tokarch-uk/pkg/config"
"code.tokarch.uk/mainnika/nikita-tokarch-uk/pkg/ghost"
"code.tokarch.uk/mainnika/nikita-tokarch-uk/pkg/templates"
)
// Routes is the main handler that contains all routes handlers
-18
View File
@@ -1,18 +0,0 @@
{{ with $yk := getYaKey }}
{{ if $yk }}
<!-- Yandex.Metrika counter -->
<script type="text/javascript" >
(function(m,e,t,r,i,k,a){m[i]=m[i]||function(){(m[i].a=m[i].a||[]).push(arguments)};
m[i].l=1*new Date();k=e.createElement(t),a=e.getElementsByTagName(t)[0],k.async=1,k.src=r,a.parentNode.insertBefore(k,a)})
(window, document, "script", "https://mc.yandex.ru/metrika/tag.js", "ym");
ym({{ $yk }}, "init", {
clickmap:true,
trackLinks:true,
accurateTrackBounce:true
});
</script>
<noscript><div><img src="https://mc.yandex.ru/watch/{{ $yk }}" style="position:absolute; left:-9999px;" alt="" /></div></noscript>
<!-- /Yandex.Metrika counter -->
{{ end }}
{{ end }}
-72
View File
@@ -1,72 +0,0 @@
<!doctype html>
<html lang="en_us">
<!-- render head -->
{{ template "head.go.tmpl" . }}
<body>
<section id="this-is-blog" class="root-container blog-container">
<!-- render menu -->
<div class="menu-container">
{{ template "menu.go.tmpl" . }}
</div>
<!-- render top pagination -->
{{ if (gt .Pagination.Page 1) }}
<div class="pagination-container-top">
<ul>
{{ if (gt .Pagination.Page 1) }}
<li>
<a href="{{ getBlogURL }}?page={{ sub .Pagination.Page }}">{{ sub .Pagination.Page }}</a>
</li>
{{ end }}
<li>
{{ .Pagination.Page }} ←
</li>
{{ if (lt .Pagination.Page .Pagination.Total) }}
<li>
<a href="{{ getBlogURL }}?page={{ add .Pagination.Page }}">{{ add .Pagination.Page }}</a>
</li>
{{ end }}
</ul>
</div>
{{ end }}
<!-- render posts -->
<div class="posts-container">
{{ range .Posts }}
{{ template "post.go.tmpl" . }}
{{ end }}
</div>
<!-- render bottom pagination -->
{{ if (gt .Pagination.Total 1) }}
<div class="pagination-container-bottom">
<ul>
{{ if (gt .Pagination.Page 1) }}
<li>
<a href="{{ getBlogURL }}?page={{ sub .Pagination.Page }}">{{ sub .Pagination.Page }}</a>
</li>
{{ end }}
<li>
{{ .Pagination.Page }} ←
</li>
{{ if (lt .Pagination.Page .Pagination.Total) }}
<li>
<a href="{{ getBlogURL }}?page={{ add .Pagination.Page }}">{{ add .Pagination.Page }}</a>
</li>
{{ end }}
</ul>
</div>
{{ end }}
</section>
</body>
</html>
-86
View File
@@ -1,86 +0,0 @@
<!doctype html>
<html lang="en_us">
<!-- render head -->
{{ template "head.go.tmpl" . }}
<body>
<!-- render menu -->
<div class="root-container menu-container">
{{ template "menu.go.tmpl" . }}
</div>
<!-- render error -->
<div class="blog-post-content">
<pre>
<code class="language-c">
/* $NetBSD: yes.c,v 1.5 1997/10/19 14:28:27 mrg Exp $ */
/*
* Copyright (c) 1987, 1993
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/cdefs.h>
#ifndef lint
__COPYRIGHT("@(#) Copyright (c) 1987, 1993\n\
The Regents of the University of California. All rights reserved.\n");
#endif /* not lint */
#ifndef lint
#if 0
static char sccsid[] = "@(#)yes.c 8.1 (Berkeley) 6/6/93";
#endif
__RCSID("$NetBSD: yes.c,v 1.5 1997/10/19 14:28:27 mrg Exp $");
#endif /* not lint */
#include <stdio.h>
int main __P((int, char **));
int
main(argc, argv)
int argc;
char **argv;
{
if (argc > 1)
for(;;)
(void)puts(argv[1]);
else for (;;)
(void)puts("y");
}
</code>
</pre>
</div>
</body>
</html>
-74
View File
@@ -1,74 +0,0 @@
package templates
import (
"html/template"
"net/url"
"sync"
"code.tokarch.uk/mainnika/nikita-tokarch-uk/pkg/config"
)
type Funcs struct {
Version string
Site config.Site
compiledJSAppURL string
initOnce sync.Once
}
func (f *Funcs) init() {
jsAppURL, err := url.Parse(URLJSApp)
if err != nil {
panic(err)
}
{
q := jsAppURL.Query()
q.Add("version", f.Version)
jsAppURL.RawQuery = q.Encode()
}
f.compiledJSAppURL = jsAppURL.String()
}
func (f *Funcs) add(i int) int {
return i + 1
}
func (f *Funcs) sub(i int) int {
return i - 1
}
func (f *Funcs) getJSAppURL() string {
f.initOnce.Do(f.init)
return f.compiledJSAppURL
}
func (f *Funcs) getIndexURL() string {
return URLIndex
}
func (f *Funcs) getBlogURL() string {
return URLBlog
}
func (f *Funcs) getYaKey() string {
return f.Site.YandexKey
}
// Use returns a func map with template helpers functions
func (f *Funcs) Use() template.FuncMap {
return template.FuncMap{
"add": f.add,
"sub": f.sub,
"getJSAppURL": f.getJSAppURL,
"getIndexURL": f.getIndexURL,
"getBlogURL": f.getBlogURL,
"getYaKey": f.getYaKey,
}
}
-11
View File
@@ -1,11 +0,0 @@
<head>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<meta name="description" content="{{ .Description }}"/>
<title>{{ .Title }}</title>
<script type="text/javascript" src="{{ getJSAppURL }}" async></script>
{{ template "analytics.go.tmpl" . }}
</head>
-57
View File
@@ -1,57 +0,0 @@
<!doctype html>
<html lang="en_us">
<!-- render head -->
{{ template "head.go.tmpl" . }}
<body>
<section class="root-container welcome-container">
<!-- render menu -->
<div class="menu-container">
{{ template "menu.go.tmpl" . }}
</div>
<!-- render pinned -->
<div class="pinned-container">
{{ range .Pinned }}
<div class="pinned-post">
{{ template "post.go.tmpl" . }}
</div>
{{ end }}
</div>
<div class="scroll-container">
<a href="#this-is-blog"><span></span></a>
</div>
</section>
<section id="this-is-blog" class="root-container blog-container">
<!-- render posts -->
<div class="posts-container">
{{ range .Posts }}
{{ template "post.go.tmpl" . }}
{{ end }}
</div>
<!-- render bottom pagination -->
{{ if (gt .Pagination.Total 1) }}
<div class="pagination-container-bottom">
<ul>
<li>
{{ .Pagination.Page }} ←
</li>
<li>
<a href="{{ getBlogURL }}?page={{ add .Pagination.Page }}">{{ add .Pagination.Page }}</a>
</li>
</ul>
</div>
{{ end }}
</section>
</body>
</html>
-9
View File
@@ -1,9 +0,0 @@
<div class="menu columns-on-mobile">
<div class="menu-logo"><a href="{{ getIndexURL }}"><span>nikita</span>.tokarch.uk</a></div>
<ul class="menu-nav columns-on-tablet columns-on-mobile">
<li class="menu-nav-li1"><a href="#this-is-blog"><i class="bi bi-journal-arrow-down"></i> blog</a></li>
<li class="menu-nav-li2"><a class="external" target="_blank" href="https://github.com/mainnika"><i class="bi bi-github"></i> github</a></li>
<li class="menu-nav-li3"><a class="external" target="_blank" href="https://www.linkedin.com/in/mainnika"><i class="bi bi-linkedin"></i> linkedin</a></li>
<li class="menu-nav-li4"><a class="external" target="_blank" href="https://www.instagram.com/mainnika"><i class="bi bi-instagram"></i> instagram</a></li>
</ul>
</div>
-24
View File
@@ -1,24 +0,0 @@
<div class="blog-post">
<!-- render post header -->
<div class="blog-post-head">
<h1>{{ .Title }}</h1>
</div>
{{ if .FImage }}
<div class="blog-post-head-image">
<img src="{{ .FImage }}"></img>
</div>
{{ end }}
<!-- render post -->
<div class="blog-post-content">
{{ .HTML }}
</div>
<!-- render post tailer -->
<div class="blog-post-tailer">
<h3>… … …</h3>
</div>
</div>
-93
View File
@@ -1,93 +0,0 @@
package templates
import (
"bytes"
"embed"
"fmt"
"html/template"
"io/fs"
"reflect"
"github.com/sirupsen/logrus"
)
// Static go-templates source
//go:embed blog.go.tmpl
//go:embed error.go.tmpl
//go:embed head.go.tmpl
//go:embed index.go.tmpl
//go:embed menu.go.tmpl
//go:embed post.go.tmpl
//go:embed analytics.go.tmpl
var content embed.FS
// List of compiled go-templates
var Templates *template.Template = template.New("")
// Load embeded templates
func Load(funcs *Funcs) (err error) {
Templates.Funcs(funcs.Use())
tmplNames, err := fs.Glob(content, "*.go.tmpl")
if err != nil {
return fmt.Errorf("cannot match templates names using glob, %w", err)
}
buf := bytes.NewBuffer(nil)
for _, name := range tmplNames {
buf.Reset()
tmplContent, err := content.Open(name)
if err != nil {
return fmt.Errorf("cannot open template content, name:%s, %w", name, err)
}
size, err := buf.ReadFrom(tmplContent)
if err != nil {
return fmt.Errorf("cannot read template content, name:%s, %w", name, err)
}
tmpl, err := Templates.New(name).Parse(buf.String())
if err != nil {
return fmt.Errorf("cannot parse template, name:%s, %w", name, err)
}
logrus.Debugf("Found template: %s, size:%d", tmpl.Name(), size)
}
logrus.Debugf("Templates loading complete%s", Templates.DefinedTemplates())
return
}
// MustLookup wraps lookup function for the root template namespace
func MustLookup(name string) *template.Template {
tmpl := Templates.Lookup(name)
if tmpl == nil {
panic(fmt.Errorf("cannot find template %s", name))
}
return tmpl
}
// MustGetTemplateOf returns template which is mapped to the content data
func MustGetTemplateOf(content interface{}) (template *template.Template) {
el := reflect.TypeOf(content)
numField := el.NumField()
for i := 0; i < numField; i++ {
field := el.Field(i)
tag := field.Tag
found, ok := tag.Lookup("template")
if !ok {
continue
}
return MustLookup(found)
}
panic(fmt.Errorf("content %v does not have a template tag", content))
}
-12
View File
@@ -1,12 +0,0 @@
package templates
const (
URLPostfix = "aspx"
URLRoot = "/"
URLSlug = "/<slug:[^/\\.]*>." + URLPostfix
URLIndex = "/index." + URLPostfix
URLBlog = "/blog." + URLPostfix
URLPost = "/post." + URLPostfix
URLJSApp = "/js-bin/app.js"
)