summaryrefslogtreecommitdiffstats
path: root/utils/html.go
blob: 8e1a788751525dafcf355b4e0f0e97efea0851b9 (plain)
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
// Copyright (c) 2016-present Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.

package utils

import (
	"bytes"
	"errors"
	"fmt"
	"html/template"
	"io"
	"path/filepath"
	"reflect"
	"strings"
	"sync/atomic"

	"github.com/fsnotify/fsnotify"
	"github.com/mattermost/mattermost-server/mlog"
	"github.com/nicksnyder/go-i18n/i18n"
)

type HTMLTemplateWatcher struct {
	templates atomic.Value
	stop      chan struct{}
	stopped   chan struct{}
}

func NewHTMLTemplateWatcher(directory string) (*HTMLTemplateWatcher, error) {
	templatesDir, _ := FindDir(directory)
	mlog.Debug(fmt.Sprintf("Parsing server templates at %v", templatesDir))

	ret := &HTMLTemplateWatcher{
		stop:    make(chan struct{}),
		stopped: make(chan struct{}),
	}

	watcher, err := fsnotify.NewWatcher()
	if err != nil {
		return nil, err
	}

	if err = watcher.Add(templatesDir); err != nil {
		return nil, err
	}

	if htmlTemplates, err := template.ParseGlob(filepath.Join(templatesDir, "*.html")); err != nil {
		return nil, err
	} else {
		ret.templates.Store(htmlTemplates)
	}

	go func() {
		defer close(ret.stopped)
		defer watcher.Close()

		for {
			select {
			case <-ret.stop:
				return
			case event := <-watcher.Events:
				if event.Op&fsnotify.Write == fsnotify.Write {
					mlog.Info(fmt.Sprintf("Re-parsing templates because of modified file %v", event.Name))
					if htmlTemplates, err := template.ParseGlob(filepath.Join(templatesDir, "*.html")); err != nil {
						mlog.Error(fmt.Sprintf("Failed to parse templates %v", err))
					} else {
						ret.templates.Store(htmlTemplates)
					}
				}
			case err := <-watcher.Errors:
				mlog.Error(fmt.Sprintf("Failed in directory watcher %s", err))
			}
		}
	}()

	return ret, nil
}

func (w *HTMLTemplateWatcher) Templates() *template.Template {
	return w.templates.Load().(*template.Template)
}

func (w *HTMLTemplateWatcher) Close() {
	close(w.stop)
	<-w.stopped
}

type HTMLTemplate struct {
	Templates    *template.Template
	TemplateName string
	Props        map[string]interface{}
	Html         map[string]template.HTML
}

func NewHTMLTemplate(templates *template.Template, templateName string) *HTMLTemplate {
	return &HTMLTemplate{
		Templates:    templates,
		TemplateName: templateName,
		Props:        make(map[string]interface{}),
		Html:         make(map[string]template.HTML),
	}
}

func (t *HTMLTemplate) Render() string {
	var text bytes.Buffer
	t.RenderToWriter(&text)
	return text.String()
}

func (t *HTMLTemplate) RenderToWriter(w io.Writer) error {
	if t.Templates == nil {
		return errors.New("no html templates")
	}

	if err := t.Templates.ExecuteTemplate(w, t.TemplateName, t); err != nil {
		mlog.Error(fmt.Sprintf("Error rendering template %v err=%v", t.TemplateName, err))
		return err
	}

	return nil
}

func TranslateAsHtml(t i18n.TranslateFunc, translationID string, args map[string]interface{}) template.HTML {
	message := t(translationID, escapeForHtml(args))
	message = strings.Replace(message, "[[", "<strong>", -1)
	message = strings.Replace(message, "]]", "</strong>", -1)
	return template.HTML(message)
}

func escapeForHtml(arg interface{}) interface{} {
	switch typedArg := arg.(type) {
	case string:
		return template.HTMLEscapeString(typedArg)
	case *string:
		return template.HTMLEscapeString(*typedArg)
	case map[string]interface{}:
		safeArg := make(map[string]interface{}, len(typedArg))
		for key, value := range typedArg {
			safeArg[key] = escapeForHtml(value)
		}
		return safeArg
	default:
		mlog.Warn(fmt.Sprintf("Unable to escape value for HTML template %v of type %v", arg, reflect.ValueOf(arg).Type()))
		return ""
	}
}