full header in asciidoc formatter

This commit is contained in:
Erik Winter 2022-06-11 08:50:20 +02:00
parent 315c1405e8
commit f744089b62
3 changed files with 28 additions and 1 deletions

View File

@ -10,7 +10,6 @@ type ADoc struct {
Title string
Attributes map[string]string
Author string
Path string
Date time.Time
Content []element.Element
}

View File

@ -13,6 +13,12 @@ func AsciiDoc(doc *adoc.ADoc) string {
func AsciiDocHeader(doc *adoc.ADoc) string {
header := fmt.Sprintf("= %s\n", doc.Title)
if doc.Author != "" {
header += fmt.Sprintf("%s\n", doc.Author)
}
if !doc.Date.IsZero() {
header += fmt.Sprintf("%s\n", doc.Date.Format("2006-01-02"))
}
for k, v := range doc.Attributes {
header += fmt.Sprintf(":%s: %s\n", k, v)
}

View File

@ -3,7 +3,9 @@ package format_test
import (
"strings"
"testing"
"time"
"ewintr.nl/adoc"
"ewintr.nl/adoc/element"
"ewintr.nl/adoc/format"
"ewintr.nl/adoc/parser"
@ -23,6 +25,26 @@ With some text.
test.Equals(t, input, format.AsciiDoc(doc))
}
func TestAsciiDocHeader(t *testing.T) {
input := &adoc.ADoc{
Title: "title",
Author: "author",
Date: time.Date(2022, time.Month(6), 11, 12, 0, 0, 0, time.UTC),
Attributes: map[string]string{
"key1": "value 1",
"key2": "value 2",
},
}
exp := `= title
author
2022-06-11
:key1: value 1
:key2: value 2
`
test.Equals(t, exp, format.AsciiDocHeader(input))
}
func TestAsciiDocFragment(t *testing.T) {
for _, tc := range []struct {
name string