collections via config

This commit is contained in:
Sebastian Frank 2019-03-10 13:26:26 +01:00
parent 7f9910244b
commit e3dfbcb591
Signed by: apairon
GPG Key ID: 7270D06DDA7FE8C3
6 changed files with 77 additions and 17 deletions

View File

@ -6,11 +6,20 @@ import (
"github.com/imdario/mergo"
)
// CollectionConfig describes a collection
type CollectionConfig struct {
Name *string `yaml:"Name"`
URL *string `yaml:"URL"`
GoToTemplate *string `yaml:"GoToTemplate"`
RegisterNav *bool `yaml:"RegisterNav"`
}
// ThisPathConfig is struct for This in paths yaml
type ThisPathConfig struct {
Navname *string `yaml:"Navname"`
GoTo *string `yaml:"GoTo"`
Data interface{} `yaml:"Data"`
Navname *string `yaml:"Navname"`
GoTo *string `yaml:"GoTo"`
Collections []*CollectionConfig `yaml:"Collections"`
Data interface{} `yaml:"Data"`
}
// IndexConfig describes index input and output file
@ -78,6 +87,8 @@ type PathConfigTree struct {
OutputPath string
Hidden bool // for collections which are not part of the navigation
ColMap map[string]interface{}
InputFiles []string
OtherFiles []string

View File

@ -84,6 +84,23 @@ func fillNodeConfig(node *config.PathConfigTree, inBase, outBase, dir string, co
Log.Infof("calculated output directory: %s", outPath)
node.OutputPath = outPath
// handle collections
for _, colConfig := range newConfig.This.Collections {
if colConfig != nil {
if colConfig.Name == nil || *colConfig.Name == "" {
Log.Panicf("missing Name in collection config in '%s'", inPath)
}
if colConfig.URL == nil || *colConfig.URL == "" {
Log.Panicf("missing URL in collection config in '%s'", inPath)
}
}
if node.ColMap == nil {
node.ColMap = make(map[string]interface{})
}
node.ColMap[*colConfig.Name] = jsonWebRequest(*colConfig.URL)
}
}
// ReadContentDir walks through content directory and builds the tree of configurations
@ -291,6 +308,7 @@ RewriteRule ^$ %{REQUEST_URI}`+goToFixed+`/ [R,L]
ctx["This"] = newConfig.This
ctx["Meta"] = newConfig.Meta
ctx["Data"] = newConfig.Data
ctx["ColMap"] = rootConf.ColMap // root as NavMap and NavSlice, for sub go to NavElement.ColMap
ctx["NavMap"] = navMap
ctx["NavSlice"] = navSlice
ctx["NavActive"] = navActive
@ -298,6 +316,21 @@ RewriteRule ^$ %{REQUEST_URI}`+goToFixed+`/ [R,L]
ctx["BodyParts"] = htmlParts
ctx["AssetsPath"] = config.Config.Assets.ToPath
ctx["CurrentPath"] = curNavPath
// set active nav element
if len(navActive) > 0 {
ctx["NavElement"] = navActive[len(navActive)-1]
} else {
// if no active path to content, we are in root dir
ctx["NavElement"] = &NavElement{
GoTo: BackToRoot(curNavPath),
Active: true,
ColMap: rootConf.ColMap,
Data: rootConf.Config.Data,
This: rootConf.Config.This,
SubMap: &navMap,
SubSlice: &navSlice,
}
}
// register functions
ctx["fnRequest"] = RequestFn

View File

@ -14,6 +14,8 @@ type NavElement struct {
GoTo string
Active bool
ColMap map[string]interface{}
Data interface{}
This config.ThisPathConfig
@ -51,6 +53,7 @@ func BuildNavigation(conf *config.PathConfigTree, curNavMap *map[string]*NavElem
navEl := NavElement{
Active: strings.HasPrefix(activeNav, elPath),
Data: el.Config.Data,
ColMap: el.ColMap,
SubMap: &subMap,
SubSlice: &subSlice,
}

View File

@ -10,26 +10,24 @@ import (
"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()
Log.Noticef("requesting url via GET %s", u)
func jsonWebRequest(url string) map[string]interface{} {
Log.Noticef("requesting url via GET %s", url)
resp, err := http.Get(u)
resp, err := http.Get(url)
if err != nil {
Log.Panicf("could not get url '%s': %s", u, err)
Log.Panicf("could not get url '%s': %s", url, err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
Log.Panicf("could not read body from url '%s': %s", u, err)
Log.Panicf("could not read body from url '%s': %s", url, err)
}
Log.Debugf("output from url '%s':\n%s", u, string(body))
Log.Debugf("output from url '%s':\n%s", url, string(body))
if resp.StatusCode >= 400 {
Log.Panicf("bad status '%d - %s' from url '%s'", resp.StatusCode, resp.Status, u)
Log.Panicf("bad status '%d - %s' from url '%s'", resp.StatusCode, resp.Status, url)
}
contentType := resp.Header.Get("Content-Type")
@ -37,16 +35,22 @@ func RequestFn(url *pongo2.Value, args ...*pongo2.Value) *pongo2.Value {
if strings.Contains(contentType, "json") {
} else {
Log.Panicf("is not json '%s' from url '%s'", contentType, u)
Log.Panicf("is not json '%s' from url '%s'", contentType, url)
}
jsonMap := make(map[string]interface{})
err = json.Unmarshal(body, &jsonMap)
if err != nil {
Log.Panicf("could not read json from '%s': %s", u, err)
Log.Panicf("could not read json from '%s': %s", url, err)
}
return pongo2.AsValue(jsonMap)
return jsonMap
}
// 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()
return pongo2.AsValue(jsonWebRequest(u))
}
// RenderFn renders a pongo2 template with additional context

View File

@ -0,0 +1,9 @@
This:
Collections:
- Name: blog1st
URL: https://mark2web.basiscms.de/api/collections/get/mark2webBlog?token=89ff216524093123bf7a0a10f7b273&filter[published]=true&sort[date]=-1&skip=0&limit=1
GoToTemplate: '{{ date|add:"-"|add:title|slugify }}'
- Name: blog1skip
URL: https://mark2web.basiscms.de/api/collections/get/mark2webBlog?token=89ff216524093123bf7a0a10f7b273&filter[published]=true&sort[date]=-1&skip=1&limit=100
GoToTemplate: '{{ date|add:"-"|add:title|slugify }}'

View File

@ -2,7 +2,7 @@
{% block part0 %}
{{ Body }}
{% for e in fnRequest("https://mark2web.basiscms.de/api/collections/get/mark2webBlog?token="|add:Data.token|add:"&filter[published]=true&sort[date]=-1&limit=1").entries %}
{% for e in NavElement.ColMap.blog1st.entries %}
<h2>
{{ e.title }}
<div class="datum">{{ e.date|datum }}</div>
@ -19,7 +19,7 @@
{% comment %}
limit wird für skip in Query gebraucht
{% endcomment %}
{% for e in fnRequest("https://mark2web.basiscms.de/api/collections/get/mark2webBlog?token="|add:Data.token|add:"&filter[published]=true&sort[date]=-1&skip=1&limit=100").entries %}
{% for e in NavElement.ColMap.blog1skip.entries %}
<h2>
{{ e.title }}
<div class="datum">{{ e.date|datum }}</div>