mirror of
https://github.com/mainnika/nikita-tokarch-uk.git
synced 2026-05-25 01:03:35 +00:00
Move templates to an internal package
This commit is contained in:
@@ -0,0 +1,18 @@
|
||||
{{ 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 }}
|
||||
@@ -0,0 +1,72 @@
|
||||
<!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>
|
||||
@@ -0,0 +1,86 @@
|
||||
<!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>
|
||||
@@ -0,0 +1,73 @@
|
||||
package templates
|
||||
|
||||
import (
|
||||
"html/template"
|
||||
"net/url"
|
||||
"sync"
|
||||
)
|
||||
|
||||
type Funcs struct {
|
||||
Version string
|
||||
|
||||
YandexKey string
|
||||
|
||||
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.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,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
module code.tokarch.uk/mainnika/nikita-tokarch-uk/templates
|
||||
|
||||
go 1.17
|
||||
|
||||
require github.com/sirupsen/logrus v1.8.1
|
||||
|
||||
require golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 // indirect
|
||||
@@ -0,0 +1,10 @@
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE=
|
||||
github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
|
||||
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
|
||||
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
|
||||
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 h1:YyJpGZS1sBuBCzLAR1VEpK193GlqGZbnPFnPV/5Rsb4=
|
||||
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
@@ -0,0 +1,11 @@
|
||||
<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>
|
||||
@@ -0,0 +1,57 @@
|
||||
<!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>
|
||||
@@ -0,0 +1,9 @@
|
||||
<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>
|
||||
@@ -0,0 +1,24 @@
|
||||
<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>
|
||||
@@ -0,0 +1,93 @@
|
||||
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
|
||||
|
||||
// Templates is a container of compiled templates
|
||||
var Templates *template.Template = template.New("")
|
||||
|
||||
// Load embedded 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))
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package templates
|
||||
|
||||
const (
|
||||
URLPostfix = "aspx"
|
||||
|
||||
URLRoot = "/"
|
||||
URLSlug = "/<slug:[^/\\.]*>." + URLPostfix
|
||||
URLIndex = "/index." + URLPostfix
|
||||
URLBlog = "/blog." + URLPostfix
|
||||
URLPost = "/post." + URLPostfix
|
||||
URLJSApp = "/js-bin/app.js"
|
||||
)
|
||||
Reference in New Issue
Block a user