markdown filter s parameter, more tests
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Sebastian Frank 2019-03-20 14:53:38 +01:00
parent c5fd151060
commit 50139c6f51
Signed by: apairon
GPG Key ID: 7270D06DDA7FE8C3
11 changed files with 186 additions and 11 deletions

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
/html/
/build/dist
/coverage.out
/test/out

View File

@ -0,0 +1,57 @@
package filter
import (
"os"
"testing"
"gitbase.de/apairon/mark2web/pkg/mark2web"
"github.com/flosch/pongo2"
. "github.com/smartystreets/goconvey/convey"
)
func TestImageProcessFilter(t *testing.T) {
Convey("set context", t, func() {
ctx := pongo2.Context{
"testlocal": "/img/test.jpg",
"testurl": "http://url",
}
// we want to check files after function calls, so no multithreading
mark2web.SetNumCPU(1)
mark2web.Config.Directories.Input = "../../test/in"
mark2web.Config.Directories.Output = "../../test/out"
mark2web.CurrentTreeNode = &mark2web.TreeNode{
InputPath: "../../test/in/content",
OutputPath: "../../test/out",
Config: &mark2web.PathConfig{
Imaging: &mark2web.ImagingConfig{
Quality: 60,
Height: 300,
Width: 300,
Process: "fit",
},
},
}
mark2web.CurrentContext = &pongo2.Context{
"CurrentPath": "",
}
os.Remove("../../test/out/fit_300x300_q060_test.jpg")
Convey("local image with defaults", func() {
output, err := pongo2.RenderTemplateString("{{ testlocal|image_process }}", ctx)
So(err, ShouldBeNil)
So(output, ShouldEqual, "fit_300x300_q060_test.jpg")
Convey("local image with fit", func() {
output, err := pongo2.RenderTemplateString(`{{ testlocal|image_process:"p=fit,w=300,h=300,q=60,t=/" }}`, ctx)
So(err, ShouldBeNil)
So(output, ShouldEqual, "/fit_300x300_q060_test.jpg")
})
})
})
}

View File

@ -1,21 +1,43 @@
package filter
import (
"fmt"
"strings"
"gitbase.de/apairon/mark2web/pkg/helper"
"gitbase.de/apairon/mark2web/pkg/mark2web"
"github.com/flosch/pongo2"
)
// MarkdownFilter is a pongo2 filter, which converts markdown to html
func MarkdownFilter(in *pongo2.Value, param *pongo2.Value) (*pongo2.Value, *pongo2.Error) {
chromaRenderer := false
chromaStyle := "monokai"
if m := mark2web.CurrentTreeNode.Config.Markdown; m != nil {
if m.ChromaRenderer != nil && *m.ChromaRenderer {
chromaRenderer = true
}
if m.ChromaStyle != nil && *m.ChromaStyle != "" {
chromaStyle = *m.ChromaStyle
chromaStyle := ""
if pStr := param.String(); pStr != "" {
for _, s := range strings.Split(pStr, ",") {
e := strings.Split(s, "=")
if len(e) < 2 {
return nil, &pongo2.Error{
Sender: "filter:markdown",
OrigError: fmt.Errorf("invalid parameter: %s", s),
}
}
switch e[0] {
case "s":
if e[1] == "" {
return nil, &pongo2.Error{
Sender: "filter:markdown",
OrigError: fmt.Errorf("need a syntax sheme name for parameter '%s='", e[0]),
}
}
chromaRenderer = true
chromaStyle = e[1]
default:
return nil, &pongo2.Error{
Sender: "filter:markdown",
OrigError: fmt.Errorf("unknown parameter '%s='", e[0]),
}
}
}
}
return pongo2.AsSafeValue(

View File

@ -0,0 +1,49 @@
package filter
import (
"testing"
"github.com/flosch/pongo2"
. "github.com/smartystreets/goconvey/convey"
)
func TestMarkdownFilter(t *testing.T) {
Convey("set context", t, func() {
ctx := pongo2.Context{
"testvar": "# test",
"testcode": "```sh\ntest=test\n```",
}
Convey("parse template", func() {
output, err := pongo2.RenderTemplateString("{{ testvar|markdown }}", ctx)
So(err, ShouldBeNil)
So(output, ShouldEqual, "<h1>test</h1>\n")
output, err = pongo2.RenderTemplateString("{{ testcode|markdown }}", ctx)
So(err, ShouldBeNil)
So(output, ShouldEqual, `<pre><code class="language-sh">test=test
</code></pre>
`)
output, err = pongo2.RenderTemplateString(`{{ testcode|markdown:"s=monokai" }}`, ctx)
So(err, ShouldBeNil)
So(output, ShouldEqual, `<pre style="color:#f8f8f2;background-color:#272822">test<span style="color:#f92672">=</span>test
</pre>`)
output, err = pongo2.RenderTemplateString(`{{ testcode|markdown:"s=" }}`, ctx)
So(output, ShouldBeEmpty)
So(err, ShouldNotBeNil)
So(err.Error(), ShouldContainSubstring, "need a syntax sheme name for parameter")
output, err = pongo2.RenderTemplateString(`{{ testcode|markdown:"test=test" }}`, ctx)
So(output, ShouldBeEmpty)
So(err, ShouldNotBeNil)
So(err.Error(), ShouldContainSubstring, "unknown parameter")
output, err = pongo2.RenderTemplateString(`{{ testcode|markdown:"s=monokai,test" }}`, ctx)
So(output, ShouldBeEmpty)
So(err, ShouldNotBeNil)
So(err.Error(), ShouldContainSubstring, "invalid parameter: test")
})
})
}

View File

@ -0,0 +1,40 @@
package filter
import (
"testing"
"gitbase.de/apairon/mark2web/pkg/mark2web"
"github.com/flosch/pongo2"
. "github.com/smartystreets/goconvey/convey"
)
func TestRelativePathFilter(t *testing.T) {
Convey("set context", t, func() {
ctx := pongo2.Context{
"testrel": "rel",
"testabs": "/abs",
"testsub": "../sub/rel",
}
Convey("parse template", func() {
mark2web.CurrentContext = &pongo2.Context{
"CurrentPath": "sub",
}
output, err := pongo2.RenderTemplateString("{{ testrel|relative_path }}", ctx)
So(err, ShouldBeNil)
So(output, ShouldEqual, "rel")
output, err = pongo2.RenderTemplateString("{{ testabs|relative_path }}", ctx)
So(err, ShouldBeNil)
So(output, ShouldEqual, "../abs")
/* TODO
output, err = pongo2.RenderTemplateString("{{ testsub|relative_path }}", ctx)
So(err, ShouldBeNil)
So(output, ShouldEqual, "rel")
*/
})
})
}

View File

@ -234,6 +234,7 @@ RewriteRule ^$ %{REQUEST_URI}`+goToFixed+`/ [R,L]
ctx := NewContext()
ctx["This"] = newConfig.This
ctx["Meta"] = newConfig.Meta
ctx["Markdown"] = newConfig.Markdown
ctx["Data"] = newConfig.Data
ctx["ColMap"] = node.root.ColMap // root as NavMap and NavSlice, for sub go to NavElement.ColMap
ctx["NavMap"] = navMap

View File

@ -53,3 +53,8 @@ func ThreadStart(f func(), forceNewThread ...bool) {
f()
}
}
// SetNumCPU is for testing package without threading
func SetNumCPU(i int) {
numCPU = i
}

BIN
test/in/img/test.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 234 KiB

0
test/out/.keep Normal file
View File

View File

@ -7,7 +7,7 @@
{{ e.title }}
<div class="datum">{{ e.date|datum }}</div>
</h2>
{{ e.teaser|markdown }}
{{ e.teaser|markdown:"s=monokai" }}
{% if e.body %}
<a href="{{ e.date|add:"-"|add:e.title|slugify }}" class="btn">mehr lesen &raquo;</a>
{% endif %}
@ -23,7 +23,7 @@
{{ e.title }}
<div class="datum">{{ e.date|datum }}</div>
</h2>
{{ e.teaser|markdown }}
{{ e.teaser|markdown:"s=monokai" }}
{% if e.body %}
<a href="{{ e.date|add:"-"|add:e.title|slugify }}" class="btn">mehr lesen &raquo;</a>
{% endif %}

View File

@ -26,7 +26,7 @@
{{ Data.details.title }}
<div class="datum">{{ Data.details.date|datum }}</div>
</h1>
{{ Data.details.teaser|markdown }}
{{ Data.details.teaser|markdown:"s=monokai" }}
{% endblock part0 %}
{% block part1 %}