@@ -2,8 +2,10 @@ package ui
22
33import (
44 "bytes"
5+ "errors"
56 "fmt"
6- "html/template"
7+ html_template "html/template"
8+ text_template "text/template"
79 "io"
810 "log"
911 "net/http"
@@ -61,10 +63,25 @@ func (h *devHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
6163// Renders a templated asset in dev-mode. This simply embeds external script tags
6264// for the source elements.
6365func renderForDev (w io.Writer , root string , c * content , cfg * config.Config , r * http.Request ) error {
64- t , err := template .ParseFiles (
65- filepath .Join (root , c .template ))
66- if err != nil {
67- return err
66+ var err error
67+ // For more context, see: https://github.com/etsy/hound/issues/239
68+ switch c .tplType {
69+ case "html" :
70+ // Use html/template to parse the html template
71+ c .tpl , err = html_template .ParseFiles (filepath .Join (root , c .template ))
72+ if err != nil {
73+ return err
74+ }
75+ case "xml" , "text" :
76+ // Use text/template to parse the xml or text templates
77+ // We are using text/template here for parsing xml to keep things
78+ // consistent with html/template parsing.
79+ c .tpl , err = text_template .ParseFiles (filepath .Join (root , c .template ))
80+ if err != nil {
81+ return err
82+ }
83+ default :
84+ return errors .New ("invalid tplType for content" )
6885 }
6986
7087 json , err := cfg .ToJsonString ()
@@ -84,11 +101,11 @@ func renderForDev(w io.Writer, root string, c *content, cfg *config.Config, r *h
84101 path )
85102 }
86103
87- return t .Execute (w , map [string ]interface {}{
104+ return c . tpl .Execute (w , map [string ]interface {}{
88105 "ReactVersion" : ReactVersion ,
89106 "jQueryVersion" : JQueryVersion ,
90107 "ReposAsJson" : json ,
91- "Source" : template .HTML (buf .String ()),
108+ "Source" : html_template .HTML (buf .String ()),
92109 "Host" : r .Host ,
93110 })
94111}
@@ -133,7 +150,6 @@ func (h *prdHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
133150// Renders a templated asset in prd-mode. This strategy will embed
134151// the sources directly in a script tag on the templated page.
135152func renderForPrd (w io.Writer , c * content , cfgJson string , r * http.Request ) error {
136-
137153 var buf bytes.Buffer
138154 buf .WriteString ("<script>" )
139155 for _ , src := range c .sources {
@@ -149,7 +165,7 @@ func renderForPrd(w io.Writer, c *content, cfgJson string, r *http.Request) erro
149165 "ReactVersion" : ReactVersion ,
150166 "jQueryVersion" : JQueryVersion ,
151167 "ReposAsJson" : cfgJson ,
152- "Source" : template .HTML (buf .String ()),
168+ "Source" : html_template .HTML (buf .String ()),
153169 "Host" : r .Host ,
154170 })
155171}
@@ -185,9 +201,24 @@ func newPrdHandler(cfg *config.Config) (http.Handler, error) {
185201 return nil , err
186202 }
187203
188- cnt .tpl , err = template .New (cnt .template ).Parse (string (a ))
189- if err != nil {
190- return nil , err
204+ // For more context, see: https://github.com/etsy/hound/issues/239
205+ switch cnt .tplType {
206+ case "html" :
207+ // Use html/template to parse the html template
208+ cnt .tpl , err = html_template .New (cnt .template ).Parse (string (a ))
209+ if err != nil {
210+ return nil , err
211+ }
212+ case "xml" , "text" :
213+ // Use text/template to parse the xml or text templates
214+ // We are using text/template here for parsing xml to keep things
215+ // consistent with html/template parsing.
216+ cnt .tpl , err = text_template .New (cnt .template ).Parse (string (a ))
217+ if err != nil {
218+ return nil , err
219+ }
220+ default :
221+ return nil , errors .New ("invalid tplType for content" )
191222 }
192223 }
193224
0 commit comments