Restructure the go package

This commit is contained in:
2021-12-16 21:57:05 +01:00
parent a9b5ac4df2
commit 39ef200967
26 changed files with 111 additions and 98 deletions
+72
View File
@@ -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>
+82
View File
@@ -0,0 +1,82 @@
<!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 -->
<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>
</body>
</html>
+24
View File
@@ -0,0 +1,24 @@
package templates
import "html/template"
// UseFuncs returns a func map with template helpers functions
func UseFuncs() template.FuncMap {
return template.FuncMap{
"add": func(i int) int {
return i + 1
},
"sub": func(i int) int {
return i - 1
},
"getJSAppURL": func() string {
return URLJSApp
},
"getIndexURL": func() string {
return URLIndex
},
"getBlogURL": func() string {
return URLBlog
},
}
}
+9
View File
@@ -0,0 +1,9 @@
<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>
</head>
+57
View File
@@ -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>
+9
View File
@@ -0,0 +1,9 @@
<div class="menu">
<div class="menu-logo"><a href="{{ getIndexURL }}"><span>nikita</span>.tokarch.uk</a></div>
<ul class="menu-nav">
<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
@@ -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>
+91
View File
@@ -0,0 +1,91 @@
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
var content embed.FS
// List of compiled go-templates
var Templates *template.Template
// Load embeded templates
func init() {
Templates = template.New("")
Templates.Funcs(UseFuncs())
tmplNames, err := fs.Glob(content, "*.go.tmpl")
if err != nil {
panic(err)
}
buf := bytes.NewBuffer(nil)
for _, name := range tmplNames {
buf.Reset()
tmplContent, err := content.Open(name)
if err != nil {
panic(err)
}
size, err := buf.ReadFrom(tmplContent)
if err != nil {
panic(err)
}
tmpl, err := Templates.New(name).Parse(buf.String())
if err != nil {
panic(err)
}
logrus.Debugf("Found template: %s, size:%d", tmpl.Name(), size)
}
logrus.Debugf("Templates loading complete%s", Templates.DefinedTemplates())
}
// 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
}
// GetTemplateOf returns template which is mapped to the content data
func GetTemplateOf(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))
}
+9
View File
@@ -0,0 +1,9 @@
package templates
const (
URLRoot = "/"
URLIndex = "/index.aspx"
URLBlog = "/blog.aspx"
URLPost = "/post.aspx"
URLJSApp = "/js-bin/app.js"
)