package main
import (
"context"
"fmt"
"log"
"math/rand"
"strings"
)
type HTMLData struct {
Title string
Description string
Keywords string
Author string
CanonicalURL string
Content string
ParseResult []ParseResult
}
var style = `
html {
max-width: 70ch;
padding: calc(1vmin + 0.5rem);
margin-inline: auto;
font-size: clamp(1em, 0.909em + 0.45vmin, 1.25em);
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
color-scheme: light dark;
}
h1 {
font-size: 2.5rem;
font-weight: 600;
margin: 0;
}
.title {
text-align: center;
}
.search {
display: flex;
justify-content: center;
margin-top: 1rem;
margin-inline: auto;
}
.leftright {
display: flex;
justify-content: center;
}
.left {
margin-right: auto;
}
.right {
margin-left: auto;
}
textarea {
width: 100%;
height: 10rem;
resize: vertical;
}
.messages {
list-style-type: none;
}
.error {
color: red;
}
.success {
color: green;
}
`
const (
// errorListItem is shown after adding a feed. It shows if error or success.
errorListItem = `
%s - %s `
// htmlTemplate is the HTML template for the entire page.
htmlTemplate = `
%s
%s
%s
`
)
func buildErrorList(parseResults []ParseResult) string {
var errorBuilder strings.Builder
if len(parseResults) > 0 {
errorBuilder.WriteString("")
for _, result := range parseResults {
// CSS class for the list item. Green for success, red for error.
listItemClass := "success"
if result.IsError {
listItemClass = "error"
}
errorBuilder.WriteString(fmt.Sprintf(errorListItem, listItemClass, result.FeedURL, result.FeedURL, result.Msg))
}
errorBuilder.WriteString(" ")
}
return errorBuilder.String()
}
func FullHTML(h HTMLData) string {
statusMsg := buildErrorList(h.ParseResult)
feedCount, err := DB.CountFeeds(context.Background())
if err != nil {
log.Fatalf("DB.CountFeeds(): %v", err)
feedCount = 0
}
databaseSize, err := GetDBSize()
if err != nil {
databaseSize = "0 KiB"
log.Println("Error getting database size:", err)
}
funMsg := FunMsg[rand.Intn(len(FunMsg))]
return fmt.Sprintf(htmlTemplate, h.Description, h.Keywords, h.Author, h.CanonicalURL, h.Title, style, statusMsg, feedCount, databaseSize, h.Content, funMsg)
}