-
Notifications
You must be signed in to change notification settings - Fork 0
/
merge.go
50 lines (41 loc) · 1.04 KB
/
merge.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
/**
* snowmark - HTML templates for Go.
*
* MIT License.
* Copyright (c) 2022, Sandeep Gupta.
* https://github.com/sangupta/snowmark
*
* Use of this source code is governed by a MIT style license
* that can be found in LICENSE file in the code repository:
*/
package snowmark
import (
"strings"
"github.com/sangupta/lhtml"
)
//
// Merge given HTML string with the given model.
//
func (pageProcessor *HtmlPageProcessor) MergeHtml(html string, model *Model) (string, error) {
elements, err := lhtml.ParseHtmlString(html)
if err != nil {
return "", err
}
return pageProcessor.Merge(elements, model)
}
//
// Merge given parsed HTML document with the given model.
//
func (pageProcessor *HtmlPageProcessor) Merge(elements *lhtml.HtmlElements, model *Model) (string, error) {
if elements.IsEmpty() {
return "", nil
}
// create evaluator
builder := strings.Builder{}
evaluator := &Evaluator{
builder: &builder,
processor: pageProcessor,
}
evaluator.EvaluateNodes(elements.Nodes(), model)
return evaluator.builder.String(), nil
}