From cb55dcd42b2265f5e9c5e830394e68aa00a015d5 Mon Sep 17 00:00:00 2001 From: Sebastian Frank Date: Thu, 28 Feb 2019 15:40:06 +0100 Subject: [PATCH] cleaned spew output --- helper/content.go | 61 ++++++++++---------- helper/markdown.go | 1 - helper/template_filters.go | 10 +++- helper/template_functions.go | 73 +++--------------------- website/templates/base_blog.html | 8 +-- website/templates/base_blog_details.html | 9 ++- 6 files changed, 57 insertions(+), 105 deletions(-) delete mode 100644 helper/markdown.go diff --git a/helper/content.go b/helper/content.go index 7a4350a..4286103 100644 --- a/helper/content.go +++ b/helper/content.go @@ -188,7 +188,6 @@ RewriteRule ^$ %{REQUEST_URI}`+goToFixed+`/ [R,L] if inputString != nil { Log.Debugf("using input string instead of file") input = []byte(*inputString) - spew.Dump(string(input)) } } @@ -271,38 +270,14 @@ RewriteRule ^$ %{REQUEST_URI}`+goToFixed+`/ [R,L] outFile := conf.OutputPath + "/" + outputFilename Log.Debugf("using '%s' as output file", outFile) - var options []blackfriday.Option - - var chromaRenderer *bool - var chromaStyle *string - if m := newConfig.Markdown; m != nil { - chromaRenderer = m.ChromaRenderer - chromaStyle = m.ChromaStyle - } - if chromaStyle == nil { - style := "monokai" - chromaStyle = &style - } - if chromaRenderer != nil && *chromaRenderer { - options = []blackfriday.Option{ - blackfriday.WithRenderer( - bfchroma.NewRenderer( - bfchroma.Style(*chromaStyle), - ), - ), - } - } - - // fix \r from markdown for blackfriday - input = bytes.Replace(input, []byte("\r"), []byte(""), -1) - html := blackfriday.Run(input, options...) - // use --- for splitting document in markdown parts regex := regexp.MustCompile("\\r?\\n\\r?---\\r?\\n\\r?") inputParts := regex.Split(string(input), -1) htmlParts := make([]*pongo2.Value, 0) for _, iPart := range inputParts { - htmlParts = append(htmlParts, pongo2.AsSafeValue(string(blackfriday.Run([]byte(iPart), options...)))) + htmlParts = append(htmlParts, + pongo2.AsSafeValue( + string(renderMarkdown([]byte(iPart), newConfig.Markdown)))) } // build navigation @@ -319,7 +294,7 @@ RewriteRule ^$ %{REQUEST_URI}`+goToFixed+`/ [R,L] ctx["NavMap"] = navMap ctx["NavSlice"] = navSlice ctx["NavActive"] = navActive - ctx["Body"] = pongo2.AsSafeValue(string(html)) + ctx["Body"] = pongo2.AsSafeValue(string(renderMarkdown(input, newConfig.Markdown))) ctx["BodyParts"] = htmlParts ctx["AssetsPath"] = config.Config.Assets.ToPath ctx["CurrentPath"] = curNavPath @@ -367,3 +342,31 @@ RewriteRule ^$ %{REQUEST_URI}`+goToFixed+`/ [R,L] i++ } } + +func renderMarkdown(input []byte, markdownConf *config.MarkdownConfig) []byte { + var options []blackfriday.Option + + var chromaRenderer *bool + var chromaStyle *string + if m := markdownConf; m != nil { + chromaRenderer = m.ChromaRenderer + chromaStyle = m.ChromaStyle + } + if chromaStyle == nil { + style := "monokai" + chromaStyle = &style + } + if chromaRenderer != nil && *chromaRenderer { + options = []blackfriday.Option{ + blackfriday.WithRenderer( + bfchroma.NewRenderer( + bfchroma.Style(*chromaStyle), + ), + ), + } + } + + // fix \r from markdown for blackfriday + input = bytes.Replace(input, []byte("\r"), []byte(""), -1) + return blackfriday.Run(input, options...) +} diff --git a/helper/markdown.go b/helper/markdown.go deleted file mode 100644 index 947317d..0000000 --- a/helper/markdown.go +++ /dev/null @@ -1 +0,0 @@ -package helper diff --git a/helper/template_filters.go b/helper/template_filters.go index a4a87ae..ddd6656 100644 --- a/helper/template_filters.go +++ b/helper/template_filters.go @@ -3,7 +3,6 @@ package helper import ( "github.com/flosch/pongo2" _ "github.com/flosch/pongo2-addons" - "gopkg.in/russross/blackfriday.v2" ) func init() { @@ -12,6 +11,11 @@ func init() { // MarkdownFilter is a pongo2 filter, which converts markdown to html func MarkdownFilter(in *pongo2.Value, param *pongo2.Value) (*pongo2.Value, *pongo2.Error) { - html := blackfriday.Run([]byte(in.String())) - return pongo2.AsSafeValue(string(html)), nil + return pongo2.AsSafeValue( + string( + renderMarkdown( + []byte(in.String()), + currentPathConfig.Markdown, + ))), + nil } diff --git a/helper/template_functions.go b/helper/template_functions.go index 01ed2d7..0ea493c 100644 --- a/helper/template_functions.go +++ b/helper/template_functions.go @@ -2,36 +2,34 @@ package helper import ( "encoding/json" - "fmt" "io/ioutil" "net/http" "strings" "gitbase.de/apairon/mark2web/config" - "github.com/davecgh/go-spew/spew" "github.com/flosch/pongo2" ) // RequestFn will make a web request and returns map[string]interface form pongo2 func RequestFn(url *pongo2.Value, args ...*pongo2.Value) *pongo2.Value { u := url.String() - fmt.Printf("request GET %s\n", u) + Log.Notice("requesting url via GET %s", u) resp, err := http.Get(u) if err != nil { - panic(err) + Log.Panicf("could not get url '%s': %s", u, err) } defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) if err != nil { - panic(err) + Log.Panicf("could not read body from url '%s': %s", u, err) } - spew.Dump(string(body)) + Log.Debugf("output from url '%s':\n%s", u, string(body)) if resp.StatusCode >= 400 { - panic(resp.Status) + Log.Panicf("bad status '%d - %s' from url '%s'", resp.StatusCode, resp.Status, u) } contentType := resp.Header.Get("Content-Type") @@ -39,13 +37,13 @@ func RequestFn(url *pongo2.Value, args ...*pongo2.Value) *pongo2.Value { if strings.Contains(contentType, "json") { } else { - panic("invalid content-type") + Log.Panicf("is not json '%s' from url '%s'", contentType, u) } jsonMap := make(map[string]interface{}) err = json.Unmarshal(body, &jsonMap) if err != nil { - panic(err) + Log.Panicf("could not read json from '%s': %s", u, err) } return pongo2.AsValue(jsonMap) @@ -103,63 +101,6 @@ func RenderFn(templateFilename, outDir, ctx *pongo2.Value, param ...*pongo2.Valu newNodeConfig.Hidden = true currentTreeNodeConfig.Sub = append(currentTreeNodeConfig.Sub, newNodeConfig) - spew.Dump(currentTreeNodeConfig) - - /* - oDirSlug := slug.Slug(outDir.String()) - navPath := path.Clean((*currentContext)["CurrentPath"].(string) + "/" + oDirSlug) - - CreateDirectory(newNodeConfig.OutputPath) - - newContext := make(map[string]interface{}) - if err := config.Merge(&newContext, *currentContext); err != nil { - Log.Panicf("unable to merge context") - } - newContext[subCtxName.String()] = ctx - newContext["CurrentPath"] = navPath - - // remember old to set after recursion - oldContext := currentContext - oldNodeConfig := currentTreeNodeConfig - result, err := RenderTemplate( - templateFilename.String(), - newNodeConfig, - currentPathConfig, - &newContext, - ) - if err != nil { - panic(err) - } - currentContext = oldContext - currentTreeNodeConfig = oldNodeConfig - - result = FixAssetsPath( - result, - navPath, - ) - //spew.Dump(result) - - // build output filename - outputFilename := "index.html" - - var indexOutputFile *string - if i := currentPathConfig.Index; i != nil { - indexOutputFile = i.OutputFile - } - - if indexOutputFile != nil && *indexOutputFile != "" { - outputFilename = *indexOutputFile - } - - outFile := newNodeConfig.OutputPath + "/" + outputFilename - Log.Debugf("using '%s' as output file", outFile) - - Log.Noticef("writing to output file: %s", outFile) - err = ioutil.WriteFile(outFile, []byte(result), 0644) - if err != nil { - log.Panicf("could not write to output file '%s': %s", outFile, err) - } - */ return pongo2.AsValue(nil) } diff --git a/website/templates/base_blog.html b/website/templates/base_blog.html index 7783709..640b797 100644 --- a/website/templates/base_blog.html +++ b/website/templates/base_blog.html @@ -9,8 +9,8 @@ {{ e.teaser|markdown }} {% if e.body %} - mehr lesen - {{ fnRender("base_blog_details.html", e.title, e, "details", e.body) }} + mehr lesen > + {{ fnRender("base_blog_details.html", e.date|add:"-"|add:e.title, e, "details", e.body) }} {% endif %} {% endfor %} {% endblock part0 %} @@ -26,8 +26,8 @@ {{ e.teaser|markdown }} {% if e.body %} - mehr lesen - {{ fnRender("base_blog_details.html", e.title, e, "details", e.body) }} + mehr lesen > + {{ fnRender("base_blog_details.html", e.date|add:"-"|add:e.title, e, "details", e.body) }} {% endif %} {% endfor %} {% endblock part1 %} diff --git a/website/templates/base_blog_details.html b/website/templates/base_blog_details.html index 4e6b100..219e4af 100644 --- a/website/templates/base_blog_details.html +++ b/website/templates/base_blog_details.html @@ -1,10 +1,15 @@ {% extends 'base.html' %} {% block part0 %} -

{{ Data.title }}

+

+ {{ Data.details.date }} + {{ Data.details.title }} +

{{ Data.details.teaser|markdown }} {% endblock part0 %} {% block part1 %} {{ Body }} -{% endblock part1 %} \ No newline at end of file + + < zurück +{% endblock part1 %}