feat: parse the Key: value header with a "-" terminator

This commit is contained in:
tgiachi 2026-11-15 09:41:54 +01:00
commit a3b0a8028e
2 changed files with 243 additions and 0 deletions

103
internal/header/header.go Normal file
View file

@ -0,0 +1,103 @@
// SPDX-License-Identifier: GPL-3.0-or-later
// Package header reads the header of a page or a news file: lines of
// "Key: value" ended by a line that holds a single "-", then the body.
package header
import (
"fmt"
"strings"
)
// terminator is the line that ends a header.
const terminator = "-"
// Header is the ordered set of keys and values of one file.
type Header struct {
keys []string
values map[string]string
}
// Parse splits src into its header and its body. The name is used in error
// messages, which also carry the line number. A leading byte order mark and
// CRLF line endings are accepted; the body is returned with LF endings and
// without the blank lines that follow the terminator.
func Parse(name string, src []byte) (Header, string, error) {
text := strings.TrimPrefix(string(src), "\xef\xbb\xbf")
text = strings.ReplaceAll(text, "\r\n", "\n")
lines := strings.Split(text, "\n")
h := Header{values: make(map[string]string)}
for i, line := range lines {
number := i + 1
if strings.TrimRight(line, " \t") == terminator {
body := strings.TrimLeft(strings.Join(lines[i+1:], "\n"), "\n")
return h, body, nil
}
if strings.TrimSpace(line) == "" {
continue
}
key, value, err := splitLine(line)
if err != nil {
return Header{}, "", fmt.Errorf("%s:%d: %w", name, number, err)
}
if _, dup := h.values[key]; dup {
return Header{}, "", fmt.Errorf("%s:%d: duplicate key %q", name, number, key)
}
h.keys = append(h.keys, key)
h.values[key] = value
}
return Header{}, "", fmt.Errorf("%s:%d: the header is not ended by a line with a single %q", name, len(lines), terminator)
}
// splitLine splits "Key: value" at the first colon followed by a space, or at
// a colon that ends the line (an empty value).
func splitLine(line string) (key, value string, err error) {
if i := strings.Index(line, ": "); i >= 0 {
key, value = line[:i], line[i+2:]
} else if strings.HasSuffix(line, ":") {
key = strings.TrimSuffix(line, ":")
} else {
return "", "", fmt.Errorf("line %q is not of the form \"Key: value\"", line)
}
key = strings.TrimSpace(key)
if key == "" {
return "", "", fmt.Errorf("empty key in line %q", line)
}
return key, strings.TrimSpace(value), nil
}
// Get returns the value of key, or an empty string when the key is absent.
func (h Header) Get(key string) string {
return h.values[key]
}
// Has reports whether key is present, even with an empty value.
func (h Header) Has(key string) bool {
_, ok := h.values[key]
return ok
}
// Require returns an error naming name and the first missing key of keys.
func (h Header) Require(name string, keys ...string) error {
for _, k := range keys {
if !h.Has(k) {
return fmt.Errorf("%s: missing key %q", name, k)
}
}
return nil
}
// Only returns an error naming name and the first key that is not in allowed.
func (h Header) Only(name string, allowed ...string) error {
ok := make(map[string]bool, len(allowed))
for _, k := range allowed {
ok[k] = true
}
for _, k := range h.keys {
if !ok[k] {
return fmt.Errorf("%s: unknown key %q (allowed: %s)", name, k, strings.Join(allowed, ", "))
}
}
return nil
}

View file

@ -0,0 +1,140 @@
// SPDX-License-Identifier: GPL-3.0-or-later
package header
import (
"strings"
"testing"
)
// TestParse checks the accepted shapes of a header and the body that follows it.
func TestParse(t *testing.T) {
tests := []struct {
name string
src string
wantKeys map[string]string
wantBody string
}{
{
name: "owner sample",
src: "Title: GNU/Linux Day 2026\nAuthor: Matteo Bini\nDate: 2026-09-18T17:00:45+0200\n" +
"Categories: Linux Day, FLUG\n-\nThe text.\n",
wantKeys: map[string]string{
"Title": "GNU/Linux Day 2026", "Author": "Matteo Bini",
"Date": "2026-09-18T17:00:45+0200", "Categories": "Linux Day, FLUG",
},
wantBody: "The text.\n",
},
{
name: "value with a colon and a space",
src: "Title: GNU/Linux Day 2026: programme\n-\nBody\n",
wantKeys: map[string]string{"Title": "GNU/Linux Day 2026: programme"},
wantBody: "Body\n",
},
{
name: "byte order mark",
src: "\ufeffTitle: With BOM\n-\nBody\n",
wantKeys: map[string]string{"Title": "With BOM"},
wantBody: "Body\n",
},
{
name: "CRLF line endings",
src: "Title: Windows\r\nAuthor: Someone\r\n-\r\nLine one\r\nLine two\r\n",
wantKeys: map[string]string{"Title": "Windows", "Author": "Someone"},
wantBody: "Line one\nLine two\n",
},
{
name: "blank lines after the terminator are dropped",
src: "Title: Gap\n-\n\n\nBody\n",
wantKeys: map[string]string{"Title": "Gap"},
wantBody: "Body\n",
},
{
name: "only the first dash line ends the header",
src: "Title: Dashes\n-\nBefore\n-\nAfter\n",
wantKeys: map[string]string{"Title": "Dashes"},
wantBody: "Before\n-\nAfter\n",
},
{
name: "empty value",
src: "Title: Empty\nCategories:\n-\nBody\n",
wantKeys: map[string]string{"Title": "Empty", "Categories": ""},
wantBody: "Body\n",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
h, body, err := Parse("file.md", []byte(tt.src))
if err != nil {
t.Fatalf("Parse() error = %v", err)
}
for k, want := range tt.wantKeys {
if !h.Has(k) {
t.Errorf("Has(%q) = false", k)
}
if got := h.Get(k); got != want {
t.Errorf("Get(%q) = %q, want %q", k, got, want)
}
}
if body != tt.wantBody {
t.Errorf("body = %q, want %q", body, tt.wantBody)
}
})
}
}
// TestParseErrors checks that every malformed header names the file and the line.
func TestParseErrors(t *testing.T) {
tests := []struct {
name string
src string
want string
}{
{"missing terminator", "Title: No end\nAuthor: x\n", "file.md:3"},
{"line without colon", "Title: Ok\nnonsense\n-\nBody\n", "file.md:2"},
{"empty key", "Title: Ok\n: value\n-\nBody\n", "file.md:2"},
{"duplicate key", "Title: One\nTitle: Two\n-\nBody\n", "file.md:2"},
{"empty file", "", "file.md"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_, _, err := Parse("file.md", []byte(tt.src))
if err == nil {
t.Fatal("Parse() error = nil")
}
if !strings.Contains(err.Error(), tt.want) {
t.Errorf("error %q does not contain %q", err, tt.want)
}
})
}
}
// TestRequire checks that a missing required key is reported by name.
func TestRequire(t *testing.T) {
h, _, err := Parse("file.md", []byte("Title: T\n-\n"))
if err != nil {
t.Fatal(err)
}
if err := h.Require("file.md", "Title"); err != nil {
t.Errorf("Require(Title) = %v", err)
}
err = h.Require("file.md", "Title", "Date")
if err == nil || !strings.Contains(err.Error(), "Date") || !strings.Contains(err.Error(), "file.md") {
t.Errorf("Require(Title, Date) = %v, want an error naming file.md and Date", err)
}
}
// TestOnly checks that a key outside the allowed list is rejected.
func TestOnly(t *testing.T) {
h, _, err := Parse("file.md", []byte("Title: T\nTitolo: T\n-\n"))
if err != nil {
t.Fatal(err)
}
err = h.Only("file.md", "Title", "Date")
if err == nil || !strings.Contains(err.Error(), "Titolo") {
t.Errorf("Only() = %v, want an error naming Titolo", err)
}
if err := h.Only("file.md", "Title", "Titolo"); err != nil {
t.Errorf("Only(all allowed) = %v", err)
}
}