2017-09-23 00:56:58 -05:00
|
|
|
// Copyright 2015 Light Code Labs, LLC
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2016-06-05 23:39:23 -05:00
|
|
|
package markdown
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"io/ioutil"
|
2017-11-04 12:36:59 -05:00
|
|
|
"os"
|
|
|
|
"sync"
|
2016-06-05 23:39:23 -05:00
|
|
|
"text/template"
|
|
|
|
|
|
|
|
"github.com/mholt/caddy/caddyhttp/httpserver"
|
|
|
|
"github.com/mholt/caddy/caddyhttp/markdown/metadata"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Data represents a markdown document.
|
|
|
|
type Data struct {
|
|
|
|
httpserver.Context
|
2016-12-03 01:35:33 -05:00
|
|
|
Doc map[string]interface{}
|
|
|
|
Styles []string
|
|
|
|
Scripts []string
|
2017-01-22 21:16:38 -05:00
|
|
|
Meta map[string]string
|
2016-12-03 01:35:33 -05:00
|
|
|
Files []FileInfo
|
2016-06-05 23:39:23 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Include "overrides" the embedded httpserver.Context's Include()
|
|
|
|
// method so that included files have access to d's fields.
|
|
|
|
// Note: using {{template 'template-name' .}} instead might be better.
|
2017-05-08 11:32:14 -05:00
|
|
|
func (d Data) Include(filename string, args ...interface{}) (string, error) {
|
|
|
|
d.Args = args
|
2016-06-05 23:39:23 -05:00
|
|
|
return httpserver.ContextInclude(filename, d, d.Root)
|
|
|
|
}
|
|
|
|
|
2017-11-04 12:36:59 -05:00
|
|
|
var templateUpdateMu sync.RWMutex
|
|
|
|
|
2016-06-05 23:39:23 -05:00
|
|
|
// execTemplate executes a template given a requestPath, template, and metadata
|
2017-01-22 21:16:38 -05:00
|
|
|
func execTemplate(c *Config, mdata metadata.Metadata, meta map[string]string, files []FileInfo, ctx httpserver.Context) ([]byte, error) {
|
2016-06-05 23:39:23 -05:00
|
|
|
mdData := Data{
|
2016-12-03 01:35:33 -05:00
|
|
|
Context: ctx,
|
|
|
|
Doc: mdata.Variables,
|
|
|
|
Styles: c.Styles,
|
|
|
|
Scripts: c.Scripts,
|
2017-01-22 21:16:38 -05:00
|
|
|
Meta: meta,
|
2016-12-03 01:35:33 -05:00
|
|
|
Files: files,
|
2016-06-05 23:39:23 -05:00
|
|
|
}
|
2017-06-25 06:31:12 -05:00
|
|
|
templateName := mdata.Template
|
2017-11-04 12:36:59 -05:00
|
|
|
|
|
|
|
updateTemplate := func() error {
|
|
|
|
templateUpdateMu.Lock()
|
|
|
|
defer templateUpdateMu.Unlock()
|
|
|
|
|
|
|
|
templateFile, ok := c.TemplateFiles[templateName]
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
currentFileInfo, err := os.Lstat(templateFile.path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !fileChanged(currentFileInfo, templateFile.fi) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// update template due to file changes
|
|
|
|
err = SetTemplate(c.Template, templateName, templateFile.path)
|
2017-06-25 06:31:12 -05:00
|
|
|
if err != nil {
|
2017-11-04 12:36:59 -05:00
|
|
|
return err
|
2017-06-25 06:31:12 -05:00
|
|
|
}
|
2017-11-04 12:36:59 -05:00
|
|
|
|
|
|
|
templateFile.fi = currentFileInfo
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := updateTemplate(); err != nil {
|
|
|
|
return nil, err
|
2017-06-25 06:31:12 -05:00
|
|
|
}
|
|
|
|
|
2016-06-05 23:39:23 -05:00
|
|
|
b := new(bytes.Buffer)
|
2017-11-04 12:36:59 -05:00
|
|
|
templateUpdateMu.RLock()
|
|
|
|
defer templateUpdateMu.RUnlock()
|
2017-06-25 06:31:12 -05:00
|
|
|
if err := c.Template.ExecuteTemplate(b, templateName, mdData); err != nil {
|
2016-06-05 23:39:23 -05:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return b.Bytes(), nil
|
|
|
|
}
|
|
|
|
|
2017-11-04 12:36:59 -05:00
|
|
|
func fileChanged(new, old os.FileInfo) bool {
|
|
|
|
// never checked before
|
|
|
|
if old == nil {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
if new.Size() != old.Size() ||
|
|
|
|
new.Mode() != old.Mode() ||
|
|
|
|
new.ModTime() != old.ModTime() {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2016-06-20 18:11:29 -05:00
|
|
|
// SetTemplate reads in the template with the filename provided. If the file does not exist or is not parsable, it will return an error.
|
2016-06-05 23:39:23 -05:00
|
|
|
func SetTemplate(t *template.Template, name, filename string) error {
|
|
|
|
|
|
|
|
// Read template
|
|
|
|
buf, err := ioutil.ReadFile(filename)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update if exists
|
|
|
|
if tt := t.Lookup(name); tt != nil {
|
|
|
|
_, err = tt.Parse(string(buf))
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Allocate new name if not
|
|
|
|
_, err = t.New(name).Parse(string(buf))
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-06-20 18:11:29 -05:00
|
|
|
// GetDefaultTemplate returns the default template.
|
2016-06-05 23:39:23 -05:00
|
|
|
func GetDefaultTemplate() *template.Template {
|
|
|
|
return template.Must(template.New("").Parse(defaultTemplate))
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
defaultTemplate = `<!DOCTYPE html>
|
|
|
|
<html>
|
|
|
|
<head>
|
|
|
|
<title>{{.Doc.title}}</title>
|
|
|
|
<meta charset="utf-8">
|
2017-01-22 21:16:38 -05:00
|
|
|
{{range $key, $val := .Meta}}
|
|
|
|
<meta name="{{$key}}" content="{{$val}}">
|
|
|
|
{{end}}
|
2016-06-05 23:39:23 -05:00
|
|
|
{{- range .Styles}}
|
|
|
|
<link rel="stylesheet" href="{{.}}">
|
|
|
|
{{- end}}
|
|
|
|
{{- range .Scripts}}
|
|
|
|
<script src="{{.}}"></script>
|
|
|
|
{{- end}}
|
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
{{.Doc.body}}
|
|
|
|
</body>
|
|
|
|
</html>`
|
|
|
|
)
|