Separate template out to its own file

This commit is contained in:
Chandler Swift 2021-10-18 19:32:10 -05:00
parent de7f70b11d
commit 123bd7d62a
Signed by: chandlerswift
GPG key ID: A851D929D52FB93F
2 changed files with 54 additions and 49 deletions

54
main.go
View file

@ -26,6 +26,7 @@ done
*/ */
import ( import (
_ "embed"
"encoding/base64" "encoding/base64"
"flag" "flag"
"fmt" "fmt"
@ -103,59 +104,14 @@ func getSites(largest int, wd selenium.WebDriver) (sites []HatsSite, err error)
return sites, nil return sites, nil
} }
//go:embed template.html
var rawtmpl string
func generateHTML(sites []HatsSite, w io.Writer) error { func generateHTML(sites []HatsSite, w io.Writer) error {
funcs := template.FuncMap{ funcs := template.FuncMap{
"parseTime": func(s string) time.Time { time, _ := time.Parse(time.RFC3339, s); return time }, "parseTime": func(s string) time.Time { time, _ := time.Parse(time.RFC3339, s); return time },
} }
tmpl, err := template.New("main").Funcs(funcs).Parse(`<!DOCTYPE html> tmpl, err := template.New("main").Funcs(funcs).Parse(rawtmpl)
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{n}hats.com domains</title>
<style>
body { max-width: 800px; font-family: sans-serif; margin: auto; }
dt { font-weight: bold; }
img { max-width: 100%; }
h3 { margin-top: 100px; }
* { line-height: 1.5em; }
</style>
</head>
<body>
<h1>{n}hats.com domains</h1>
<h3>Summary</h3>
<ul>
{{range .}}
{{if .Available}}
<li><a href="#{{.DomainName}}">{{.DomainName}}</a> &ndash; Available!</li>
{{else}}
<li><a href="#{{.DomainName}}">{{.DomainName}}</a> &ndash; Registered since {{(parseTime .DomainInfo.CreatedDate).Format "Mon Jan 2 2006" }}</li>
{{end}}
{{end}}
</ul>
{{range .}}
<h3 id="{{.DomainName}}">{{.DomainName}}</h3>
<dl>
<dt>Fetched</dt>
<dd>{{.FetchTime.Format "Mon Jan 2 15:04:05 -0700 MST 2006" }}</dd>
<dt>Owner</dt>
<dd><a href="mailto:{{.Registrant.Email}}">{{.Registrant.Name}} &lt;{{.Registrant.Email}}&gt</a></dd>
{{if not .Available}}
<dt>Since</dt>
<dd>{{(parseTime .DomainInfo.CreatedDate).Format "Mon Jan 2 2006" }}</dd>
{{end}}
{{with .Title}}
<dt>Title</dt>
<dd>{{.}}</dd>
{{end}}
</dl>
{{if .ScreenshotURL}}
<img src="{{.ScreenshotURL}}" alt="screenshot of {{.DomainName}} as of {{.FetchTime.Format "Mon Jan 2 15:04:05 -0700 MST 2006" }}">
{{end}}
{{end}}
</body>
</html>`)
if err != nil { if err != nil {
return err return err
} }

49
template.html Normal file
View file

@ -0,0 +1,49 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{n}hats.com domains</title>
<style>
body { max-width: 800px; font-family: sans-serif; margin: auto; }
dt { font-weight: bold; }
img { max-width: 100%; }
h3 { margin-top: 100px; }
* { line-height: 1.5em; }
</style>
</head>
<body>
<h1>{n}hats.com domains</h1>
<h3>Summary</h3>
<ul>
{{range .}}
{{if .Available}}
<li><a href="#{{.DomainName}}">{{.DomainName}}</a> &ndash; Available!</li>
{{else}}
<li><a href="#{{.DomainName}}">{{.DomainName}}</a> &ndash; Registered since {{(parseTime .DomainInfo.CreatedDate).Format "Mon Jan 2 2006" }}</li>
{{end}}
{{end}}
</ul>
{{range .}}
<h3 id="{{.DomainName}}">{{.DomainName}}</h3>
<dl>
<dt>Fetched</dt>
<dd>{{.FetchTime.Format "Mon Jan 2 15:04:05 -0700 MST 2006" }}</dd>
<dt>Owner</dt>
<dd><a href="mailto:{{.Registrant.Email}}">{{.Registrant.Name}} &lt;{{.Registrant.Email}}&gt</a></dd>
{{if not .Available}}
<dt>Since</dt>
<dd>{{(parseTime .DomainInfo.CreatedDate).Format "Mon Jan 2 2006" }}</dd>
{{end}}
{{with .Title}}
<dt>Title</dt>
<dd>{{.}}</dd>
{{end}}
</dl>
{{if .ScreenshotURL}}
<img src="{{.ScreenshotURL}}" alt="screenshot of {{.DomainName}} as of {{.FetchTime.Format "Mon Jan 2 15:04:05 -0700 MST 2006" }}">
{{end}}
{{end}}
</body>
</html>