-
Notifications
You must be signed in to change notification settings - Fork 0
/
markup_providers.go
91 lines (74 loc) · 2.07 KB
/
markup_providers.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
package compton
import (
"embed"
"errors"
"github.com/boggydigital/compton/consts/compton_atoms"
"golang.org/x/net/html/atom"
"strings"
)
const (
tacMarkupTemplate = "<{tag} {{.Attributes}}>{{.Content}}</{tag}>"
voidTacMarkupTemplate = "<{tag} {{.Attributes}}/>"
transparentContentMarkup = "{{.Content}}"
)
type atomsEmbedMarkupProvider struct {
efs embed.FS
fn string
}
func (caemp *atomsEmbedMarkupProvider) GetMarkup() ([]byte, error) {
bts, err := caemp.efs.ReadFile(caemp.fn)
if err != nil {
return nil, err
}
return bts, nil
}
func atomsEmbedMarkup(ca atom.Atom, efs embed.FS) (atom.Atom, MarkupProvider) {
return ca, &atomsEmbedMarkupProvider{
efs: efs,
fn: compton_atoms.MarkupName(ca),
}
}
type tacMarkupProvider struct {
a atom.Atom
}
func (tmp *tacMarkupProvider) GetMarkup() ([]byte, error) {
if an := compton_atoms.Atos(tmp.a); an != "" {
tacm := strings.Replace(tacMarkupTemplate, "{tag}", an, -1)
return []byte(tacm), nil
} else {
return nil, errors.New("no tag-attribute-content markup for atom")
}
}
func tacMarkup(a atom.Atom) (atom.Atom, MarkupProvider) {
return a, &tacMarkupProvider{a: a}
}
type voidTacMarkupProvider struct {
a atom.Atom
}
func (vtmp *voidTacMarkupProvider) GetMarkup() ([]byte, error) {
if an := compton_atoms.Atos(vtmp.a); an != "" {
vtacm := strings.Replace(voidTacMarkupTemplate, "{tag}", an, -1)
return []byte(vtacm), nil
} else {
return nil, errors.New("no void-tag-attribute-content markup for atom")
}
}
func voidTacMarkup(a atom.Atom) (atom.Atom, MarkupProvider) {
return a, &voidTacMarkupProvider{a: a}
}
type contentMarkupProvider struct{}
func (cmp *contentMarkupProvider) GetMarkup() ([]byte, error) {
return []byte(transparentContentMarkup), nil
}
func contentMarkup(a atom.Atom) (atom.Atom, MarkupProvider) {
return a, &contentMarkupProvider{}
}
type bytesMarkupProvider struct {
bts []byte
}
func (cmp *bytesMarkupProvider) GetMarkup() ([]byte, error) {
return cmp.bts, nil
}
func BytesMarkup(a atom.Atom, bts []byte) (atom.Atom, MarkupProvider) {
return a, &bytesMarkupProvider{bts: bts}
}