2015-07-21 00:58:34 -05:00
|
|
|
package markdown
|
|
|
|
|
|
|
|
import (
|
2015-08-04 17:35:09 -05:00
|
|
|
"bufio"
|
2016-04-30 22:09:25 -05:00
|
|
|
"io/ioutil"
|
2015-07-21 00:58:34 -05:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2015-07-29 11:00:08 -05:00
|
|
|
"os"
|
2016-04-11 01:17:01 -05:00
|
|
|
"path/filepath"
|
2015-07-21 10:45:32 -05:00
|
|
|
"strings"
|
2015-07-21 00:58:34 -05:00
|
|
|
"testing"
|
2016-04-30 22:09:25 -05:00
|
|
|
"text/template"
|
2015-07-29 11:00:08 -05:00
|
|
|
"time"
|
2015-07-21 00:58:34 -05:00
|
|
|
|
2015-07-23 02:35:46 -05:00
|
|
|
"github.com/mholt/caddy/middleware"
|
2015-07-21 00:58:34 -05:00
|
|
|
"github.com/russross/blackfriday"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestMarkdown(t *testing.T) {
|
2016-04-11 01:17:01 -05:00
|
|
|
rootDir := "./testdata"
|
|
|
|
|
|
|
|
f := func(filename string) string {
|
|
|
|
return filepath.ToSlash(rootDir + string(filepath.Separator) + filename)
|
|
|
|
}
|
|
|
|
|
2015-07-21 00:58:34 -05:00
|
|
|
md := Markdown{
|
2016-04-11 01:17:01 -05:00
|
|
|
Root: rootDir,
|
|
|
|
FileSys: http.Dir(rootDir),
|
2015-09-10 17:12:46 -05:00
|
|
|
Configs: []*Config{
|
2015-10-09 17:35:34 -05:00
|
|
|
{
|
2016-04-10 19:20:09 -05:00
|
|
|
Renderer: blackfriday.HtmlRenderer(0, "", ""),
|
|
|
|
PathScope: "/blog",
|
|
|
|
Extensions: map[string]struct{}{
|
2016-04-16 19:07:06 -05:00
|
|
|
".md": {},
|
2016-04-10 19:20:09 -05:00
|
|
|
},
|
2016-04-11 01:17:01 -05:00
|
|
|
Styles: []string{},
|
|
|
|
Scripts: []string{},
|
|
|
|
Template: setDefaultTemplate(f("markdown_tpl.html")),
|
2015-07-21 00:58:34 -05:00
|
|
|
},
|
2016-02-22 05:53:47 -05:00
|
|
|
{
|
2016-04-10 19:20:09 -05:00
|
|
|
Renderer: blackfriday.HtmlRenderer(0, "", ""),
|
|
|
|
PathScope: "/docflags",
|
|
|
|
Extensions: map[string]struct{}{
|
2016-04-16 19:07:06 -05:00
|
|
|
".md": {},
|
2016-04-10 19:20:09 -05:00
|
|
|
},
|
2016-04-11 01:17:01 -05:00
|
|
|
Styles: []string{},
|
|
|
|
Scripts: []string{},
|
|
|
|
Template: setDefaultTemplate(f("docflags/template.txt")),
|
2016-02-22 05:53:47 -05:00
|
|
|
},
|
2015-10-09 17:35:34 -05:00
|
|
|
{
|
2016-04-10 19:20:09 -05:00
|
|
|
Renderer: blackfriday.HtmlRenderer(0, "", ""),
|
|
|
|
PathScope: "/log",
|
|
|
|
Extensions: map[string]struct{}{
|
2016-04-16 19:07:06 -05:00
|
|
|
".md": {},
|
2016-04-10 19:20:09 -05:00
|
|
|
},
|
2016-04-11 01:17:01 -05:00
|
|
|
Styles: []string{"/resources/css/log.css", "/resources/css/default.css"},
|
|
|
|
Scripts: []string{"/resources/js/log.js", "/resources/js/default.js"},
|
|
|
|
Template: GetDefaultTemplate(),
|
2015-07-21 10:45:32 -05:00
|
|
|
},
|
2015-10-09 17:35:34 -05:00
|
|
|
{
|
2016-04-10 19:20:09 -05:00
|
|
|
Renderer: blackfriday.HtmlRenderer(0, "", ""),
|
|
|
|
PathScope: "/og",
|
|
|
|
Extensions: map[string]struct{}{
|
2016-04-16 19:07:06 -05:00
|
|
|
".md": {},
|
2015-07-29 16:43:26 -05:00
|
|
|
},
|
2016-04-11 01:17:01 -05:00
|
|
|
Styles: []string{},
|
|
|
|
Scripts: []string{},
|
|
|
|
Template: setDefaultTemplate(f("markdown_tpl.html")),
|
2015-07-29 09:21:35 -05:00
|
|
|
},
|
2015-07-21 00:58:34 -05:00
|
|
|
},
|
|
|
|
IndexFiles: []string{"index.html"},
|
2015-07-23 02:35:46 -05:00
|
|
|
Next: middleware.HandlerFunc(func(w http.ResponseWriter, r *http.Request) (int, error) {
|
|
|
|
t.Fatalf("Next shouldn't be called")
|
|
|
|
return 0, nil
|
|
|
|
}),
|
2015-07-21 00:58:34 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
req, err := http.NewRequest("GET", "/blog/test.md", nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Could not create HTTP request: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
rec := httptest.NewRecorder()
|
|
|
|
|
|
|
|
md.ServeHTTP(rec, req)
|
|
|
|
if rec.Code != http.StatusOK {
|
|
|
|
t.Fatalf("Wrong status, expected: %d and got %d", http.StatusOK, rec.Code)
|
|
|
|
}
|
|
|
|
|
|
|
|
respBody := rec.Body.String()
|
|
|
|
expectedBody := `<!DOCTYPE html>
|
|
|
|
<html>
|
|
|
|
<head>
|
2015-10-29 10:59:14 -05:00
|
|
|
<title>Markdown test 1</title>
|
2015-07-21 00:58:34 -05:00
|
|
|
</head>
|
|
|
|
<body>
|
2016-03-07 17:32:07 -05:00
|
|
|
<h1>Header for: Markdown test 1</h1>
|
2015-07-21 00:58:34 -05:00
|
|
|
|
|
|
|
Welcome to A Caddy website!
|
|
|
|
<h2>Welcome on the blog</h2>
|
|
|
|
|
|
|
|
<p>Body</p>
|
|
|
|
|
2015-10-29 10:59:14 -05:00
|
|
|
<pre><code class="language-go">func getTrue() bool {
|
2015-07-21 00:58:34 -05:00
|
|
|
return true
|
|
|
|
}
|
2015-10-29 10:59:14 -05:00
|
|
|
</code></pre>
|
2015-07-21 00:58:34 -05:00
|
|
|
|
|
|
|
</body>
|
|
|
|
</html>
|
|
|
|
`
|
2015-08-04 17:35:09 -05:00
|
|
|
if !equalStrings(respBody, expectedBody) {
|
2015-07-21 10:45:32 -05:00
|
|
|
t.Fatalf("Expected body: %v got: %v", expectedBody, respBody)
|
|
|
|
}
|
|
|
|
|
2016-02-22 05:53:47 -05:00
|
|
|
req, err = http.NewRequest("GET", "/docflags/test.md", nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Could not create HTTP request: %v", err)
|
|
|
|
}
|
|
|
|
rec = httptest.NewRecorder()
|
|
|
|
|
|
|
|
md.ServeHTTP(rec, req)
|
|
|
|
if rec.Code != http.StatusOK {
|
|
|
|
t.Fatalf("Wrong status, expected: %d and got %d", http.StatusOK, rec.Code)
|
|
|
|
}
|
|
|
|
respBody = rec.Body.String()
|
|
|
|
expectedBody = `Doc.var_string hello
|
|
|
|
Doc.var_bool <no value>
|
|
|
|
DocFlags.var_string <no value>
|
|
|
|
DocFlags.var_bool true`
|
|
|
|
|
|
|
|
if !equalStrings(respBody, expectedBody) {
|
|
|
|
t.Fatalf("Expected body: %v got: %v", expectedBody, respBody)
|
|
|
|
}
|
|
|
|
|
2015-07-21 10:45:32 -05:00
|
|
|
req, err = http.NewRequest("GET", "/log/test.md", nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Could not create HTTP request: %v", err)
|
|
|
|
}
|
|
|
|
rec = httptest.NewRecorder()
|
|
|
|
|
|
|
|
md.ServeHTTP(rec, req)
|
|
|
|
if rec.Code != http.StatusOK {
|
|
|
|
t.Fatalf("Wrong status, expected: %d and got %d", http.StatusOK, rec.Code)
|
|
|
|
}
|
|
|
|
respBody = rec.Body.String()
|
|
|
|
expectedBody = `<!DOCTYPE html>
|
|
|
|
<html>
|
|
|
|
<head>
|
2015-10-29 10:59:14 -05:00
|
|
|
<title>Markdown test 2</title>
|
2015-07-21 10:45:32 -05:00
|
|
|
<meta charset="utf-8">
|
|
|
|
<link rel="stylesheet" href="/resources/css/log.css">
|
2016-04-11 01:17:01 -05:00
|
|
|
<link rel="stylesheet" href="/resources/css/default.css">
|
2015-07-21 10:45:32 -05:00
|
|
|
<script src="/resources/js/log.js"></script>
|
2016-04-11 01:17:01 -05:00
|
|
|
<script src="/resources/js/default.js"></script>
|
2016-04-12 20:53:15 -05:00
|
|
|
</head>
|
2015-07-21 10:45:32 -05:00
|
|
|
<body>
|
|
|
|
<h2>Welcome on the blog</h2>
|
|
|
|
|
|
|
|
<p>Body</p>
|
|
|
|
|
2015-10-29 10:59:14 -05:00
|
|
|
<pre><code class="language-go">func getTrue() bool {
|
2015-07-21 10:45:32 -05:00
|
|
|
return true
|
|
|
|
}
|
2015-10-29 10:59:14 -05:00
|
|
|
</code></pre>
|
2015-07-21 10:45:32 -05:00
|
|
|
|
|
|
|
</body>
|
|
|
|
</html>`
|
2015-07-21 00:58:34 -05:00
|
|
|
|
2015-08-04 17:35:09 -05:00
|
|
|
if !equalStrings(respBody, expectedBody) {
|
2015-07-21 00:58:34 -05:00
|
|
|
t.Fatalf("Expected body: %v got: %v", expectedBody, respBody)
|
|
|
|
}
|
2015-07-29 09:21:35 -05:00
|
|
|
|
|
|
|
req, err = http.NewRequest("GET", "/og/first.md", nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Could not create HTTP request: %v", err)
|
|
|
|
}
|
|
|
|
rec = httptest.NewRecorder()
|
2015-07-29 11:00:08 -05:00
|
|
|
currenttime := time.Now().Local().Add(-time.Second)
|
2016-04-16 19:07:06 -05:00
|
|
|
_ = os.Chtimes("testdata/og/first.md", currenttime, currenttime)
|
2015-07-29 11:00:08 -05:00
|
|
|
currenttime = time.Now().Local()
|
2016-04-16 19:07:06 -05:00
|
|
|
_ = os.Chtimes("testdata/og_static/og/first.md/index.html", currenttime, currenttime)
|
2015-08-05 03:55:04 -05:00
|
|
|
time.Sleep(time.Millisecond * 200)
|
2015-07-29 09:21:35 -05:00
|
|
|
|
|
|
|
md.ServeHTTP(rec, req)
|
|
|
|
if rec.Code != http.StatusOK {
|
|
|
|
t.Fatalf("Wrong status, expected: %d and got %d", http.StatusOK, rec.Code)
|
|
|
|
}
|
|
|
|
respBody = rec.Body.String()
|
|
|
|
expectedBody = `<!DOCTYPE html>
|
|
|
|
<html>
|
|
|
|
<head>
|
|
|
|
<title>first_post</title>
|
|
|
|
</head>
|
|
|
|
<body>
|
2016-03-07 17:32:07 -05:00
|
|
|
<h1>Header for: first_post</h1>
|
2015-07-29 09:21:35 -05:00
|
|
|
|
2015-08-05 03:55:04 -05:00
|
|
|
Welcome to title!
|
2015-07-29 09:21:35 -05:00
|
|
|
<h1>Test h1</h1>
|
|
|
|
|
|
|
|
</body>
|
|
|
|
</html>`
|
2015-08-04 17:35:09 -05:00
|
|
|
|
|
|
|
if !equalStrings(respBody, expectedBody) {
|
2015-07-29 09:21:35 -05:00
|
|
|
t.Fatalf("Expected body: %v got: %v", expectedBody, respBody)
|
|
|
|
}
|
2015-07-21 00:58:34 -05:00
|
|
|
}
|
2015-08-04 17:35:09 -05:00
|
|
|
|
|
|
|
func equalStrings(s1, s2 string) bool {
|
|
|
|
s1 = strings.TrimSpace(s1)
|
|
|
|
s2 = strings.TrimSpace(s2)
|
|
|
|
in := bufio.NewScanner(strings.NewReader(s1))
|
|
|
|
for in.Scan() {
|
|
|
|
txt := strings.TrimSpace(in.Text())
|
|
|
|
if !strings.HasPrefix(strings.TrimSpace(s2), txt) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
s2 = strings.Replace(s2, txt, "", 1)
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
2016-04-30 22:09:25 -05:00
|
|
|
|
|
|
|
func setDefaultTemplate(filename string) *template.Template {
|
|
|
|
buf, err := ioutil.ReadFile(filename)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return template.Must(GetDefaultTemplate().Parse(string(buf)))
|
|
|
|
}
|