@ -17,7 +17,6 @@ package templates
import (
import (
"bytes"
"bytes"
"fmt"
"fmt"
"html/template"
"io"
"io"
"net"
"net"
"net/http"
"net/http"
@ -25,6 +24,7 @@ import (
"strconv"
"strconv"
"strings"
"strings"
"sync"
"sync"
"text/template"
"github.com/Masterminds/sprig/v3"
"github.com/Masterminds/sprig/v3"
"github.com/alecthomas/chroma/formatters/html"
"github.com/alecthomas/chroma/formatters/html"
@ -57,7 +57,7 @@ func (c templateContext) OriginalReq() http.Request {
// Note that included files are NOT escaped, so you should only include
// Note that included files are NOT escaped, so you should only include
// trusted files. If it is not trusted, be sure to use escaping functions
// trusted files. If it is not trusted, be sure to use escaping functions
// in your template.
// in your template.
func ( c templateContext ) funcInclude ( filename string , args ... interface { } ) ( template . HTML , error ) {
func ( c templateContext ) funcInclude ( filename string , args ... interface { } ) ( string , error ) {
if c . Root == nil {
if c . Root == nil {
return "" , fmt . Errorf ( "root file system not specified" )
return "" , fmt . Errorf ( "root file system not specified" )
}
}
@ -84,14 +84,14 @@ func (c templateContext) funcInclude(filename string, args ...interface{}) (temp
return "" , err
return "" , err
}
}
return template . HTML ( bodyBuf . String ( ) ) , nil
return bodyBuf . String ( ) , nil
}
}
// funcHTTPInclude returns the body of a virtual (lightweight) request
// funcHTTPInclude returns the body of a virtual (lightweight) request
// to the given URI on the same server. Note that included bodies
// to the given URI on the same server. Note that included bodies
// are NOT escaped, so you should only include trusted resources.
// are NOT escaped, so you should only include trusted resources.
// If it is not trusted, be sure to use escaping functions yourself.
// If it is not trusted, be sure to use escaping functions yourself.
func ( c templateContext ) funcHTTPInclude ( uri string ) ( template . HTML , error ) {
func ( c templateContext ) funcHTTPInclude ( uri string ) ( string , error ) {
// prevent virtual request loops by counting how many levels
// prevent virtual request loops by counting how many levels
// deep we are; and if we get too deep, return an error
// deep we are; and if we get too deep, return an error
recursionCount := 1
recursionCount := 1
@ -132,7 +132,7 @@ func (c templateContext) funcHTTPInclude(uri string) (template.HTML, error) {
return "" , err
return "" , err
}
}
return template . HTML ( buf . String ( ) ) , nil
return buf . String ( ) , nil
}
}
func ( c templateContext ) executeTemplateInBuffer ( tplName string , buf * bytes . Buffer ) error {
func ( c templateContext ) executeTemplateInBuffer ( tplName string , buf * bytes . Buffer ) error {
@ -231,7 +231,7 @@ func (c templateContext) funcStripHTML(s string) string {
// funcMarkdown renders the markdown body as HTML. The resulting
// funcMarkdown renders the markdown body as HTML. The resulting
// HTML is NOT escaped so that it can be rendered as HTML.
// HTML is NOT escaped so that it can be rendered as HTML.
func ( c templateContext ) funcMarkdown ( input interface { } ) ( template . HTML , error ) {
func ( c templateContext ) funcMarkdown ( input interface { } ) ( string , error ) {
inputStr := toString ( input )
inputStr := toString ( input )
md := goldmark . New (
md := goldmark . New (
@ -259,7 +259,7 @@ func (c templateContext) funcMarkdown(input interface{}) (template.HTML, error)
md . Convert ( [ ] byte ( inputStr ) , buf )
md . Convert ( [ ] byte ( inputStr ) , buf )
return template . HTML ( buf . String ( ) ) , nil
return buf . String ( ) , nil
}
}
// splitFrontMatter parses front matter out from the beginning of input,
// splitFrontMatter parses front matter out from the beginning of input,
@ -338,14 +338,12 @@ func toString(input interface{}) string {
switch v := input . ( type ) {
switch v := input . ( type ) {
case string :
case string :
return v
return v
case template . HTML :
return string ( v )
case fmt . Stringer :
case fmt . Stringer :
return v . String ( )
return v . String ( )
case error :
case error :
return v . Error ( )
return v . Error ( )
default :
default :
return fmt . Sprintf ( "%s " , input )
return fmt . Sprintf ( "%v " , input )
}
}
}
}
@ -357,6 +355,6 @@ var bufPool = sync.Pool{
// at time of writing, sprig.FuncMap() makes a copy, thus
// at time of writing, sprig.FuncMap() makes a copy, thus
// involves iterating the whole map, so do it just once
// involves iterating the whole map, so do it just once
var sprigFuncMap = sprig . FuncMap ( )
var sprigFuncMap = sprig . Txt FuncMap( )
const recursionPreventionHeader = "Caddy-Templates-Include"
const recursionPreventionHeader = "Caddy-Templates-Include"