/
templates.go
167 lines (152 loc) · 5.07 KB
/
templates.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
// Copyright 2018 NDP Systèmes. All Rights Reserved.
// See LICENSE file for full licensing details.
package templates
import (
"encoding/xml"
"strconv"
"sync"
"github.com/beevik/etree"
"github.com/hexya-erp/hexya/src/i18n"
"github.com/hexya-erp/hexya/src/tools/hweb"
"github.com/hexya-erp/hexya/src/tools/xmlutils"
)
// A Collection of templates
type Collection struct {
sync.RWMutex
templates map[string]*Template
rawInheritedTemplates []*TemplateXML
}
// newCollection returns a pointer to a new
// Collection instance
func newCollection() *Collection {
res := Collection{
templates: make(map[string]*Template),
}
return &res
}
// Add the given template to the Collection
func (tc *Collection) Add(t *Template) {
tc.Lock()
defer tc.Unlock()
tc.templates[t.ID] = t
}
// GetByID returns the Template with the given id
func (tc *Collection) GetByID(id string) *Template {
return tc.templates[id]
}
// LoadFromEtree loads the given template given as Element
// into this collection.
func (tc *Collection) LoadFromEtree(element *etree.Element) {
xmlBytes, err := xmlutils.ElementToXMLNoIndent(element)
if err != nil {
log.Panic("Unable to convert element to XML", "error", err)
}
var templateXML TemplateXML
if err = xml.Unmarshal(xmlBytes, &templateXML); err != nil {
log.Panic("Unable to unmarshal element", "error", err, "bytes", string(xmlBytes))
}
if templateXML.InheritID != "" {
// Update an existing template.
// Put in raw inherited templates for now, as the base template may not exist yet.
tc.rawInheritedTemplates = append(tc.rawInheritedTemplates, &templateXML)
return
}
// Create a new template
tc.createNewTemplateFromXML(&templateXML)
}
// createNewViewFromXML creates and register a new template with the given XML
func (tc *Collection) createNewTemplateFromXML(templateXML *TemplateXML) {
priority := uint8(16)
if templateXML.Priority != 0 {
priority = templateXML.Priority
}
optional := templateXML.Optional != ""
optionalDefault := templateXML.Optional == "enabled"
page, _ := strconv.ParseBool(templateXML.Page)
tmpl := Template{
ID: templateXML.ID,
Priority: priority,
Page: page,
Optional: optional,
OptionalDefault: optionalDefault,
hWebContent: templateXML.Content,
p2Contents: make(map[string][]byte),
}
tc.Add(&tmpl)
}
// A Template holds information of a HWeb template
type Template struct {
*hweb.Template
ID string
Priority uint8
Page bool
Optional bool
OptionalDefault bool
hWebContent []byte
p2Content []byte
p2Contents map[string][]byte
}
// Content returns the template data for the given language.
// Call with empty string to get the default language's data.
func (t *Template) Content(lang string) []byte {
res, ok := t.p2Contents[lang]
if !ok || lang == "" {
res = t.p2Content
}
return res
}
// updateFromXML updates this template with the given XML
// templateXML must have an InheritID
func (t *Template) updateFromXML(templateXML *TemplateXML) {
specDoc, err := xmlutils.XMLToDocument(string(templateXML.Content))
if err != nil {
log.Panic("Unable to read inheritance specs", "error", err, "arch", string(templateXML.Content))
}
content, err := xmlutils.XMLToDocument(string(t.hWebContent))
if err != nil {
log.Panic("Error while reading base template content", "error", err, "template", t.ID, "content", string(t.hWebContent))
}
newContent, err := xmlutils.ApplyExtensions(content, specDoc)
if err != nil {
log.Panic("Error while applying template extension specs", "error", err, "specTmpl", templateXML.ID,
"specs", string(templateXML.Content), "template", t.ID, "content", string(t.hWebContent))
}
ncs, err := xmlutils.DocumentToXMLNoIndent(newContent)
if err != nil {
log.Panic("Error while converting back to XML", "error", err, "content", newContent,
"specTmpl", templateXML.ID, "template", t.ID)
}
t.hWebContent = ncs
}
// postProcess the template by
//
// - populating the p2Content (Pongo2)
// - populating the p2Contents map with all the translations
func (t *Template) postProcess() {
p2Content, err := hweb.ToPongo(t.hWebContent)
if err != nil {
log.Panic("Error while transpiling to Pongo2", "error", err, "template", t.ID)
}
t.p2Content = p2Content
for _, lang := range i18n.Langs {
p2Content, err = hweb.ToPongo([]byte(i18n.TranslateResourceItem(lang, t.ID, string(t.hWebContent))))
if err != nil {
log.Panic("Error while transpiling translation to Pongo2", "error", err, "template", t.ID)
}
t.p2Contents[lang] = p2Content
}
}
// TemplateXML is used to unmarshal the XML definition of a template
type TemplateXML struct {
ID string `xml:"id,attr"`
InheritID string `xml:"inherit_id,attr"`
Content []byte `xml:",innerxml"`
Priority uint8 `xml:"priority,attr"`
Page string `xml:"page,attr"`
Optional string `xml:"optional,attr"`
}
// LoadFromEtree reads the view given etree.Element, creates or updates the template
// and adds it to the template registry if it not already.
func LoadFromEtree(element *etree.Element) {
Registry.collection.LoadFromEtree(element)
}