]> Sergey Matveev's repositories - sgblog.git/blobdiff - cmd/sgblog/http.go
Move templates to external files, make them HTML5
[sgblog.git] / cmd / sgblog / http.go
index f1e7404b81da51558c08b824971bbca6e6c00a03..6ec97bb4cc48ca1b1610f32e8f56f641f9ac8d31 100644 (file)
@@ -21,6 +21,7 @@ import (
        "bytes"
        "compress/gzip"
        "crypto/sha1"
+       _ "embed"
        "encoding/hex"
        "encoding/xml"
        "errors"
@@ -46,112 +47,6 @@ import (
 const (
        AtomPostsFeed    = "feed.atom"
        AtomCommentsFeed = "comments.atom"
-       TmplHTMLIndex    = `<html>
-<head>
-       <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
-       <meta name="generator" content="SGBlog {{.Version}}">
-       <title>{{.Cfg.Title}} {{if .Topic}}(topic: {{.Topic}}) {{end}}({{.Offset}}-{{.OffsetNext}})</title>
-       {{with .Cfg.CSS}}<link rel="stylesheet" type="text/css" href="{{.}}">{{end}}
-       {{with .Cfg.Webmaster}}<link rev="made" href="mailto:{{.}}">{{end}}
-       {{range .Cfg.GitURLs}}<link rel="vcs-git" href="{{.}}" title="Git repository">{{end}}
-       <link rel="top" href="{{.Cfg.URLPrefix}}/" title="top">
-       <link rel="alternate" title="Posts feed" href="{{.Cfg.AtomBaseURL}}{{.Cfg.URLPrefix}}/{{.AtomPostsFeed}}{{if .Topic}}?topic={{.Topic}}{{end}}" type="application/atom+xml">
-       {{if .CommentsEnabled}}<link rel="alternate" title="Comments feed" href="{{.Cfg.AtomBaseURL}}{{.Cfg.URLPrefix}}/{{.AtomCommentsFeed}}" type="application/atom+xml">{{end}}
-       {{if .Offset}}<link rel="prev" href="{{.Cfg.URLPrefix}}/?offset={{.OffsetPrev}}{{if .Topic}}&topic={{.Topic}}{{end}}" title="prev">{{end}}
-       {{if not .LogEnded}}<link rel="next" href="{{.Cfg.URLPrefix}}/?offset={{.OffsetNext}}{{if .Topic}}&topic={{.Topic}}{{end}}" title="next">{{end}}
-</head>
-<body>
-{{with .Cfg.AboutURL}}[<a href="{{.}}">about</a>]{{end}}
-{{block "links" .}}
-{{if .Offset}}[<a href="{{.Cfg.URLPrefix}}/?offset={{.OffsetPrev}}{{if .Topic}}&topic={{.Topic}}{{end}}">prev</a>]{{end}}
-{{if not .LogEnded}}[<a href="{{.Cfg.URLPrefix}}/?offset={{.OffsetNext}}{{if .Topic}}&topic={{.Topic}}{{end}}">next</a>]{{end}}
-{{end}}
-{{- $Cfg := .Cfg -}}
-{{if .Topics}}<hr/>
-Topics: [<tt><a href="{{$Cfg.URLPrefix}}/">ALL</a></tt>]
-{{range .Topics}}[<tt><a href="{{$Cfg.URLPrefix}}?topic={{.}}">{{.}}</a></tt>]
-{{end}}
-{{end}}
-{{- $TopicsEnabled := .TopicsEnabled -}}
-{{- $datePrev := "0001-01-01" -}}
-<table border=1>
-<tr>
-       <th>N</th><th>When</th><th>Title</th>
-       <th size="5%"><a title="Lines">L</a></th>
-       <th size="5%"><a title="Comments">C</a></th>
-       <th>Linked to</th>
-       {{if .TopicsEnabled}}<th>Topics</th>{{end}}
-</tr>
-{{range .Entries -}}
-{{- $dateCur := .Commit.Author.When.Format "2006-01-02" -}}
-{{- if ne $dateCur $datePrev -}}
-       <tr><td colspan={{if $TopicsEnabled}}7{{else}}7{{end}}><center><tt>{{$dateCur}}</tt></center></td></tr>
-       {{- $datePrev = $dateCur -}}
-{{- end -}}
-<tr>
-       <td>{{.Num}}</td>
-       <td><tt>{{.Commit.Author.When.Format "15:04"}}</tt></td>
-       <td><a href="{{$Cfg.URLPrefix}}/{{.Commit.Hash.String}}">{{.Title}}</a></td>
-       <td>{{.LinesNum}}</td>
-       <td>{{if .CommentsNum}}{{.CommentsNum}}{{else}}&nbsp;{{end}}</td>
-       <td>{{if .DomainURLs}}{{range .DomainURLs}} {{.}} {{end}}{{else}}&nbsp;{{end}}</td>
-       {{if $TopicsEnabled}}<td>{{if .Topics}}{{range .Topics}} <a href="{{$Cfg.URLPrefix}}/?topic={{.}}">{{.}}</a> {{end}}{{else}}&nbsp;{{end}}</td>{{end}}
-</tr>
-{{end}}</table>
-{{template "links" .}}
-</body>
-</html>
-`
-       TmplHTMLEntry = `{{$Cfg := .Cfg}}<html>
-<head>
-       <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
-       <meta name="generator" content="SGBlog {{.Version}}">
-       <title>{{.Title}} ({{.When}})</title>
-       {{with .Cfg.CSS}}<link rel="stylesheet" type="text/css" href="{{.}}">{{end}}
-       {{with .Cfg.Webmaster}}<link rev="made" href="mailto:{{.}}">{{end -}}
-       {{- range .Cfg.GitURLs}}
-       <link rel="vcs-git" href="{{.}}" title="Git repository">{{end}}
-       <link rel="top" href="{{.Cfg.URLPrefix}}/" title="top">
-       <link rel="alternate" title="Comments feed" href="{{.AtomCommentsURL}}" type="application/atom+xml">
-       {{if .Parent}}<link rel="prev" href="{{.Cfg.URLPrefix}}/{{.Parent}}" title="prev">{{end}}
-</head>
-<body>
-{{with .Cfg.AboutURL}}[<a href="{{.}}">about</a>]{{end}}
-[<a href="{{.Cfg.URLPrefix}}/">index</a>]
-{{if .Parent}}[<a href="{{.Cfg.URLPrefix}}/{{.Parent}}">prev</a>]{{end}}
-[<tt><a title="When">{{.When}}</a></tt>]
-[<tt><a title="What">{{.Commit.Hash.String}}</a></tt>]
-
-{{if .Topics}}
-<hr/>
-Topics: {{range .Topics}}[<tt><a href="{{$Cfg.URLPrefix}}?topic={{.}}">{{.}}</a></tt>]{{end}}
-{{end}}
-
-<hr/>
-<h2>{{.Title}}</h2>
-<pre>
-{{range .Lines}}{{. | lineURLize $Cfg.URLPrefix}}
-{{end}}</pre>
-<hr/>
-
-{{if .NoteLines}}Note:<pre>
-{{range .NoteLines}}{{. | lineURLize $Cfg.URLPrefix}}
-{{end}}</pre>
-<hr/>{{end}}
-
-{{if .Cfg.CommentsEmail}}[<a href="mailto:{{.Cfg.CommentsEmail}}?subject={{.TitleEscaped}}">leave comment</a>]{{end}}
-
-<dl>{{range $idx, $comment := .Comments}}
-<dt><a name="comment{{$idx}}"><a href="#comment{{$idx}}">comment {{$idx}}</a>:</dt>
-<dd><pre>
-{{range $comment.HeaderLines}}{{.}}
-{{end}}{{range $comment.BodyLines}}{{. | lineURLize $Cfg.URLPrefix}}
-{{end}}</pre></dd>
-{{end}}</dl>
-
-</body>
-</html>
-`
 )
 
 var (
@@ -164,6 +59,16 @@ var (
                "https":  {},
                "telnet": {},
        }
+
+       //go:embed http-index.tmpl
+       TmplHTMLIndexRaw string
+       TmplHTMLIndex    = template.Must(template.New("http-index").Parse(TmplHTMLIndexRaw))
+
+       //go:embed http-entry.tmpl
+       TmplHTMLEntryRaw string
+       TmplHTMLEntry    = template.Must(template.New("http-entry").Funcs(
+               template.FuncMap{"lineURLize": lineURLizeInTemplate},
+       ).Parse(TmplHTMLEntryRaw))
 )
 
 type TableEntry struct {
@@ -436,9 +341,8 @@ func serveHTTP() {
                if offsetPrev < 0 {
                        offsetPrev = 0
                }
-               tmpl := template.Must(template.New("index").Parse(TmplHTMLIndex))
                os.Stdout.Write([]byte(startHeader(etagHash, gzipWriter != nil)))
-               err = tmpl.Execute(out, struct {
+               err = TmplHTMLIndex.Execute(out, struct {
                        Version          string
                        Cfg              *Cfg
                        Topic            string
@@ -790,11 +694,8 @@ func serveHTTP() {
                        notesLines = strings.Split(string(notesRaw), "\n")
                }
 
-               tmpl := template.New("entry")
-               tmpl = tmpl.Funcs(template.FuncMap{"lineURLize": lineURLizeInTemplate})
-               tmpl = template.Must(tmpl.Parse(TmplHTMLEntry))
                os.Stdout.Write([]byte(startHeader(etagHash, gzipWriter != nil)))
-               err = tmpl.Execute(out, struct {
+               err = TmplHTMLEntry.Execute(out, struct {
                        Version         string
                        Cfg             *Cfg
                        Title           string