This commit is contained in:
parent
5acc4083aa
commit
cb55dcd42b
@ -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...)
|
||||
}
|
||||
|
@ -1 +0,0 @@
|
||||
package helper
|
@ -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
|
||||
}
|
||||
|
@ -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)
|
||||
}
|
||||
|
@ -9,8 +9,8 @@
|
||||
</h2>
|
||||
{{ e.teaser|markdown }}
|
||||
{% if e.body %}
|
||||
<a href="{{ e.title|slugify }}" class="btn">mehr lesen</a>
|
||||
{{ fnRender("base_blog_details.html", e.title, e, "details", e.body) }}
|
||||
<a href="{{ e.date|add:"-"|add:e.title|slugify }}" class="btn">mehr lesen ></a>
|
||||
{{ fnRender("base_blog_details.html", e.date|add:"-"|add:e.title, e, "details", e.body) }}
|
||||
{% endif %}
|
||||
{% endfor %}
|
||||
{% endblock part0 %}
|
||||
@ -26,8 +26,8 @@
|
||||
</h2>
|
||||
{{ e.teaser|markdown }}
|
||||
{% if e.body %}
|
||||
<a href="{{ e.title|slugify }}" class="btn">mehr lesen</a>
|
||||
{{ fnRender("base_blog_details.html", e.title, e, "details", e.body) }}
|
||||
<a href="{{ e.date|add:"-"|add:e.title|slugify }}" class="btn">mehr lesen ></a>
|
||||
{{ fnRender("base_blog_details.html", e.date|add:"-"|add:e.title, e, "details", e.body) }}
|
||||
{% endif %}
|
||||
{% endfor %}
|
||||
{% endblock part1 %}
|
||||
|
@ -1,10 +1,15 @@
|
||||
{% extends 'base.html' %}
|
||||
|
||||
{% block part0 %}
|
||||
<h1>{{ Data.title }}</h1>
|
||||
<h1>
|
||||
<small>{{ Data.details.date }}</small>
|
||||
{{ Data.details.title }}
|
||||
</h1>
|
||||
{{ Data.details.teaser|markdown }}
|
||||
{% endblock part0 %}
|
||||
|
||||
{% block part1 %}
|
||||
{{ Body }}
|
||||
{% endblock part1 %}
|
||||
|
||||
<a href="../" class="btn">< zurück</a>
|
||||
{% endblock part1 %}
|
||||
|
Loading…
Reference in New Issue
Block a user