Add logo feature

This commit is contained in:
Pim van Pelt
2025-12-03 12:32:36 +01:00
parent 60a149b669
commit 99ad6fbff1
3 changed files with 119 additions and 42 deletions

View File

@@ -168,6 +168,7 @@ type Options struct {
Verbose bool
DryRun bool
ShowIndexFiles bool
WatermarkURL string
}
type FileEntry struct {
@@ -211,17 +212,19 @@ func ProcessDir(topDir string, opts *Options) error {
})
templateData := struct {
DirName string
Entries []FileEntry
DirAppend bool
OutputFile string
IsRoot bool
DirName string
Entries []FileEntry
DirAppend bool
OutputFile string
IsRoot bool
WatermarkURL string
}{
DirName: dirName,
Entries: entries,
DirAppend: opts.DirAppend,
OutputFile: opts.OutputFile,
IsRoot: false, // Local filesystem always shows parent directory
DirName: dirName,
Entries: entries,
DirAppend: opts.DirAppend,
OutputFile: opts.OutputFile,
IsRoot: false, // Local filesystem always shows parent directory
WatermarkURL: opts.WatermarkURL,
}
if opts.DryRun {
@@ -467,6 +470,15 @@ const htmlTemplateString = `<!DOCTYPE html>
overflow-x: hidden;
text-overflow: ellipsis;
color: #999;
display: flex;
align-items: center;
}
.watermark {
height: 24px;
width: auto;
margin-right: 8px;
vertical-align: middle;
}
h1 a {
@@ -943,7 +955,7 @@ const htmlTemplateString = `<!DOCTYPE html>
</defs>
</svg>
<header>
<h1>{{.DirName}}</h1>
<h1>{{if .WatermarkURL}}<img src="{{.WatermarkURL}}" class="watermark" alt="Logo">{{end}}{{.DirName}}</h1>
</header>
<main>
<div class="listing">

View File

@@ -97,17 +97,19 @@ func TestHTMLTemplate(t *testing.T) {
// Test template execution with sample data
data := struct {
DirName string
Entries []FileEntry
DirAppend bool
OutputFile string
IsRoot bool
DirName string
Entries []FileEntry
DirAppend bool
OutputFile string
IsRoot bool
WatermarkURL string
}{
DirName: "test-dir",
Entries: []FileEntry{},
DirAppend: false,
OutputFile: "index.html",
IsRoot: false,
DirName: "test-dir",
Entries: []FileEntry{},
DirAppend: false,
OutputFile: "index.html",
IsRoot: false,
WatermarkURL: "",
}
var buf bytes.Buffer
@@ -159,17 +161,19 @@ func TestHTMLTemplateWithEntries(t *testing.T) {
}
data := struct {
DirName string
Entries []FileEntry
DirAppend bool
OutputFile string
IsRoot bool
DirName string
Entries []FileEntry
DirAppend bool
OutputFile string
IsRoot bool
WatermarkURL string
}{
DirName: "test-dir",
Entries: entries,
DirAppend: false,
OutputFile: "index.html",
IsRoot: false,
DirName: "test-dir",
Entries: entries,
DirAppend: false,
OutputFile: "index.html",
IsRoot: false,
WatermarkURL: "",
}
var buf bytes.Buffer
@@ -196,6 +200,62 @@ func TestHTMLTemplateWithEntries(t *testing.T) {
}
}
func TestHTMLTemplateWithWatermark(t *testing.T) {
tmpl := GetHTMLTemplate()
if tmpl == nil {
t.Fatal("GetHTMLTemplate() returned nil")
}
// Test template execution with watermark
data := struct {
DirName string
Entries []FileEntry
DirAppend bool
OutputFile string
IsRoot bool
WatermarkURL string
}{
DirName: "test-dir",
Entries: []FileEntry{},
DirAppend: false,
OutputFile: "index.html",
IsRoot: false,
WatermarkURL: "https://example.com/logo.svg",
}
var buf bytes.Buffer
err := tmpl.Execute(&buf, data)
if err != nil {
t.Fatalf("Template execution with watermark failed: %v", err)
}
output := buf.String()
// Check that watermark image is included
if !bytes.Contains([]byte(output), []byte(`src="https://example.com/logo.svg"`)) {
t.Error("Template output should contain watermark image URL")
}
if !bytes.Contains([]byte(output), []byte(`class="watermark"`)) {
t.Error("Template output should contain watermark CSS class")
}
// Test without watermark
data.WatermarkURL = ""
buf.Reset()
err = tmpl.Execute(&buf, data)
if err != nil {
t.Fatalf("Template execution without watermark failed: %v", err)
}
outputNoWatermark := buf.String()
// Check that watermark image is NOT included when URL is empty
if bytes.Contains([]byte(outputNoWatermark), []byte(`class="watermark"`)) {
t.Error("Template output should not contain watermark when URL is empty")
}
}
func TestReadDirEntries(t *testing.T) {
// Create a temporary directory with test files
tempDir := t.TempDir()