forked from bytesparadise/libasciidoc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
document_details.go
101 lines (94 loc) · 3.1 KB
/
document_details.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package html5
import (
"bytes"
"fmt"
htmltemplate "html/template"
"strconv"
texttemplate "text/template"
"github.com/bytesparadise/libasciidoc/pkg/renderer"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
)
var documentDetailsTmpl texttemplate.Template
var documentAuthorDetailsTmpl texttemplate.Template
func init() {
documentDetailsTmpl = newTextTemplate("document details", `<div class="details">{{ if .Authors }}
{{.Authors}}{{ end }}{{ if .RevNumber }}
<span id="revnumber">version {{.RevNumber}},</span>{{ end }}{{ if .RevDate }}
<span id="revdate">{{.RevDate}}</span>{{ end }}{{ if .RevRemark }}
<br><span id="revremark">{{.RevRemark}}</span>{{ end }}
</div>`)
documentAuthorDetailsTmpl = newTextTemplate("author details", `{{ if .Name }}<span id="author{{.Index}}" class="author">{{.Name}}</span><br>{{ end }}{{ if .Email }}
<span id="email{{.Index}}" class="email"><a href="mailto:{{.Email}}">{{.Email}}</a></span><br>{{ end }}`)
}
func renderDocumentDetails(ctx *renderer.Context) (*htmltemplate.HTML, error) {
if ctx.Document.Attributes.HasAuthors() {
authors, err := renderDocumentAuthorsDetails(ctx)
if err != nil {
return nil, errors.Wrap(err, "error while rendering the document details")
}
documentDetailsBuff := bytes.NewBuffer(nil)
err = documentDetailsTmpl.Execute(documentDetailsBuff, struct {
Authors htmltemplate.HTML
RevNumber string
RevDate string
RevRemark string
}{
Authors: *authors,
RevNumber: ctx.Document.Attributes.GetAsString("revnumber"),
RevDate: ctx.Document.Attributes.GetAsString("revdate"),
RevRemark: ctx.Document.Attributes.GetAsString("revremark"),
})
if err != nil {
return nil, errors.Wrap(err, "error while rendering the document details")
}
documentDetails := htmltemplate.HTML(documentDetailsBuff.String())
return &documentDetails, nil
}
return nil, nil
}
func renderDocumentAuthorsDetails(ctx *renderer.Context) (*htmltemplate.HTML, error) {
authorsDetailsBuff := bytes.NewBuffer(nil)
i := 1
for {
var authorKey string
var emailKey string
var index string
if i == 1 {
authorKey = "author"
emailKey = "email"
index = ""
} else {
authorKey = fmt.Sprintf("author_%d", i)
emailKey = fmt.Sprintf("email_%d", i)
index = strconv.Itoa(i)
}
// having at least one author is the minimal requirement for document details
if author := ctx.Document.Attributes.GetAsString(authorKey); author != "" {
authorDetailsBuff := bytes.NewBuffer(nil)
err := documentAuthorDetailsTmpl.Execute(authorDetailsBuff, struct {
Index string
Name string
Email string
}{
Index: index,
Name: author,
Email: ctx.Document.Attributes.GetAsString(emailKey),
})
if err != nil {
return nil, errors.Wrap(err, "error while rendering the document author")
}
// if there were authors before, need to insert a `\n`
if i > 1 {
authorsDetailsBuff.WriteString("\n")
}
authorsDetailsBuff.Write(authorDetailsBuff.Bytes())
i++
} else {
log.Debugf("No match found for '%s'", authorKey)
break
}
}
result := htmltemplate.HTML(authorsDetailsBuff.String())
return &result, nil
}