-
Notifications
You must be signed in to change notification settings - Fork 2
/
filehtml.go
96 lines (80 loc) · 1.83 KB
/
filehtml.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
// SPDX-FileCopyrightText: 2019 Shulhan <[email protected]>
// SPDX-License-Identifier: GPL-3.0-or-later
package ciigo
import (
"fmt"
"html/template"
"strings"
"git.sr.ht/~shulhan/asciidoctor-go"
)
const (
metadataStylesheet = `stylesheet`
metadataTitle = `title`
)
// fileHTML represent an HTML metadata for header and its body.
type fileHTML struct {
Title string
EmbeddedCSS *template.CSS
Styles []string
Body template.HTML
Metadata map[string]string
rawBody strings.Builder
}
func newFileHTML() (fhtml *fileHTML) {
fhtml = &fileHTML{
Metadata: map[string]string{},
}
return fhtml
}
func (fhtml *fileHTML) unpackAdocMetadata(doc *asciidoctor.Document) {
var (
k string
v string
)
fhtml.Title = doc.Title.String()
fhtml.Styles = fhtml.Styles[:0]
for k, v = range doc.Attributes {
switch k {
case metadataStylesheet:
fhtml.Styles = append(fhtml.Styles, v)
case asciidoctor.MetaNameAuthorNames:
fhtml.Metadata[asciidoctor.MetaNameAuthor] = v
case asciidoctor.MetaNameDescription,
asciidoctor.MetaNameGenerator,
asciidoctor.MetaNameKeywords:
fhtml.Metadata[k] = v
}
}
if len(fhtml.Styles) == 0 {
fhtml.EmbeddedCSS = embeddedCSS()
}
}
func (fhtml *fileHTML) unpackMarkdownMetadata(metadata map[string]any) {
var (
key string
val any
vstr string
ok bool
)
fhtml.Styles = fhtml.Styles[:0]
for key, val = range metadata {
vstr, ok = val.(string)
if !ok {
vstr = fmt.Sprintf(`%s`, val)
}
key = strings.ToLower(key)
switch key {
case metadataStylesheet:
fhtml.Styles = append(fhtml.Styles, vstr)
case metadataTitle:
fhtml.Title = vstr
default:
// Metadata `author_names`, `description`,
// `generator`, and `keywords` goes here.
fhtml.Metadata[key] = vstr
}
}
if len(fhtml.Styles) == 0 {
fhtml.EmbeddedCSS = embeddedCSS()
}
}