Refactor everything
This commit is contained in:
parent
b56ce43c80
commit
57903d4724
45 changed files with 514 additions and 416 deletions
38
internal/templates/main.go
Normal file
38
internal/templates/main.go
Normal file
|
@ -0,0 +1,38 @@
|
|||
package templates
|
||||
|
||||
import (
|
||||
"embed"
|
||||
"html/template"
|
||||
"log/slog"
|
||||
|
||||
"git.a71.su/Andrew71/hibiscus-txt/internal/config"
|
||||
"git.a71.su/Andrew71/hibiscus-txt/internal/lang"
|
||||
)
|
||||
|
||||
// pages contains the HTML templates used by the app.
|
||||
//
|
||||
//go:embed pages
|
||||
var pages embed.FS
|
||||
|
||||
// EmbeddedPage returns contents of a file in Pages while "handling" potential errors.
|
||||
func EmbeddedPage(name string) []byte {
|
||||
data, err := pages.ReadFile(name)
|
||||
if err != nil {
|
||||
slog.Error("error reading embedded file", "err", err)
|
||||
}
|
||||
return data
|
||||
}
|
||||
|
||||
var templateFuncs = map[string]interface{}{
|
||||
"translate": lang.Translate,
|
||||
"info": func() config.AppInfo { return config.Info },
|
||||
"config": func() config.Config { return config.Cfg },
|
||||
}
|
||||
var Edit = template.Must(template.New("").Funcs(templateFuncs).ParseFS(pages, "pages/base.html", "pages/edit.html"))
|
||||
var View = template.Must(template.New("").Funcs(templateFuncs).ParseFS(pages, "pages/base.html", "pages/entry.html"))
|
||||
var List = template.Must(template.New("").Funcs(templateFuncs).ParseFS(pages, "pages/base.html", "pages/list.html"))
|
||||
|
||||
var Info = template.Must(template.New("").Funcs(templateFuncs).ParseFS(pages, "pages/base.html", "pages/info.html"))
|
||||
|
||||
var Template404 = template.Must(template.New("404").Funcs(templateFuncs).ParseFS(pages, "pages/error/404.html"))
|
||||
var Template500 = template.Must(template.New("500").Funcs(templateFuncs).ParseFS(pages, "pages/error/500.html"))
|
41
internal/templates/pages/base.html
Normal file
41
internal/templates/pages/base.html
Normal file
|
@ -0,0 +1,41 @@
|
|||
{{ define "header" }}
|
||||
<header>
|
||||
<h1>{{ config.Title }}</h1>
|
||||
<p>{{ translate "time.date" }} <span id="today-date">a place</span> <span id="grace" hidden>({{ translate "time.grace" }})</span></p>
|
||||
</header>
|
||||
{{ end }}
|
||||
|
||||
{{- define "base" -}}
|
||||
<!DOCTYPE html>
|
||||
<html lang="{{ translate "lang" }}">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<link rel="manifest" href="/public/manifest.json" />
|
||||
<link rel="icon" type="image/x-icon" href="/public/favicon.ico">
|
||||
<link rel="stylesheet" href="/public/main.css">
|
||||
{{- if config.Theme -}}<link rel="stylesheet" href="/public/themes/{{ config.Theme }}.css">{{ end }}
|
||||
<script src="/public/main.js"></script>
|
||||
<title>Hibiscus.txt</title>
|
||||
</head>
|
||||
<body>
|
||||
{{- template "header" . -}}
|
||||
<main>
|
||||
{{- template "main" . -}}
|
||||
</main>
|
||||
{{- template "footer" . -}}
|
||||
<script defer>
|
||||
const langName="{{ config.Language }}";
|
||||
const timeZone="{{ config.Timezone }}";
|
||||
beginTimeUpdater()
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
{{ end }}
|
||||
|
||||
{{ define "footer" }}
|
||||
<footer id="footer">
|
||||
<p><a href="/">{{ translate "link.today" }}</a> | <a href="/day">{{ translate "link.days" }}</a> | <a href="/notes">{{ translate "link.notes" }}</a>
|
||||
<span style="float:right;"><a class="no-accent" href="/info" title="{{ translate "link.info" }}">v{{ info.Version }}</a></span></p>
|
||||
</footer>
|
||||
{{ end }}
|
7
internal/templates/pages/edit.html
Normal file
7
internal/templates/pages/edit.html
Normal file
|
@ -0,0 +1,7 @@
|
|||
{{ define "main" }}
|
||||
<form method="POST">
|
||||
<h2><label for="text">{{ .Title }}:</label></h2>
|
||||
<textarea id="text" cols="40" rows="15" name="text">{{ .Content }}</textarea>
|
||||
<button type="submit">{{ translate "button.save" }}</button>
|
||||
</form>
|
||||
{{ end }}
|
4
internal/templates/pages/entry.html
Normal file
4
internal/templates/pages/entry.html
Normal file
|
@ -0,0 +1,4 @@
|
|||
{{ define "main" }}
|
||||
<h2><label for="text">{{ .Title }}</label></h2>
|
||||
<textarea id="text" cols="40" rows="15" readonly>{{ .Content }}</textarea>
|
||||
{{ end }}
|
19
internal/templates/pages/error/404.html
Normal file
19
internal/templates/pages/error/404.html
Normal file
|
@ -0,0 +1,19 @@
|
|||
{{- define "404" -}}
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<link rel="icon" type="image/x-icon" href="/public/favicon.ico">
|
||||
<link rel="stylesheet" href="/public/main.css">
|
||||
<title>Error 404</title>
|
||||
</head>
|
||||
<body>
|
||||
<main>
|
||||
<h1>Error 404 - Not Found</h1>
|
||||
<p>{{ translate "error.404" }}</p>
|
||||
<p><a href="/">{{ translate "error.prompt" }}</a></p>
|
||||
</main>
|
||||
</body>
|
||||
</html>
|
||||
{{ end }}
|
19
internal/templates/pages/error/500.html
Normal file
19
internal/templates/pages/error/500.html
Normal file
|
@ -0,0 +1,19 @@
|
|||
{{- define "500" -}}
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<link rel="icon" type="image/x-icon" href="/public/favicon.ico">
|
||||
<link rel="stylesheet" href="/public/main.css">
|
||||
<title>Error 500</title>
|
||||
</head>
|
||||
<body>
|
||||
<main>
|
||||
<h1>Error 500 - Internal Server Error</h1>
|
||||
<p>{{ translate "error.500" }}</p>
|
||||
<p><a href="/">{{ translate "error.prompt" }}</a></p>
|
||||
</main>
|
||||
</body>
|
||||
</html>
|
||||
{{ end }}
|
9
internal/templates/pages/info.html
Normal file
9
internal/templates/pages/info.html
Normal file
|
@ -0,0 +1,9 @@
|
|||
{{ define "main" }}
|
||||
<h2>{{ translate "title.info" }}</h2>
|
||||
<ul>
|
||||
<li>{{ translate "info.version" }} - {{ info.Version }} (<a href="{{ .SourceLink }}">{{ translate "info.version.link" }}</a>)</li>
|
||||
<li><a href="/config">{{ translate "info.config" }}</a></li>
|
||||
<li><a href="/readme">{{ translate "info.readme" }}</a></li>
|
||||
<li><a href="/api/export" download="hibiscus">{{ translate "info.export" }}</a></li>
|
||||
</ul>
|
||||
{{ end }}
|
9
internal/templates/pages/list.html
Normal file
9
internal/templates/pages/list.html
Normal file
|
@ -0,0 +1,9 @@
|
|||
{{ define "main" }}
|
||||
<h2 class="list-title">{{ .Title }}</h2>
|
||||
<p class="list-desc">{{ .Description }}</p>
|
||||
<ul>
|
||||
{{ range .Entries }}
|
||||
<li><a href="/{{.Link}}">{{.Title}}</a></li>
|
||||
{{ end }}
|
||||
</ul>
|
||||
{{end}}
|
Loading…
Add table
Add a link
Reference in a new issue