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" "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 // ThisPathConfig is struct for This in paths yaml
type ThisPathConfig struct { type ThisPathConfig struct {
Navname *string `yaml:"Navname"` Navname *string `yaml:"Navname"`
GoTo *string `yaml:"GoTo"` GoTo *string `yaml:"GoTo"`
Data interface{} `yaml:"Data"` Collections []*CollectionConfig `yaml:"Collections"`
Data interface{} `yaml:"Data"`
} }
// IndexConfig describes index input and output file // IndexConfig describes index input and output file
@ -78,6 +87,8 @@ type PathConfigTree struct {
OutputPath string OutputPath string
Hidden bool // for collections which are not part of the navigation Hidden bool // for collections which are not part of the navigation
ColMap map[string]interface{}
InputFiles []string InputFiles []string
OtherFiles []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) Log.Infof("calculated output directory: %s", outPath)
node.OutputPath = 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 // 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["This"] = newConfig.This
ctx["Meta"] = newConfig.Meta ctx["Meta"] = newConfig.Meta
ctx["Data"] = newConfig.Data ctx["Data"] = newConfig.Data
ctx["ColMap"] = rootConf.ColMap // root as NavMap and NavSlice, for sub go to NavElement.ColMap
ctx["NavMap"] = navMap ctx["NavMap"] = navMap
ctx["NavSlice"] = navSlice ctx["NavSlice"] = navSlice
ctx["NavActive"] = navActive ctx["NavActive"] = navActive
@ -298,6 +316,21 @@ RewriteRule ^$ %{REQUEST_URI}`+goToFixed+`/ [R,L]
ctx["BodyParts"] = htmlParts ctx["BodyParts"] = htmlParts
ctx["AssetsPath"] = config.Config.Assets.ToPath ctx["AssetsPath"] = config.Config.Assets.ToPath
ctx["CurrentPath"] = curNavPath 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 // register functions
ctx["fnRequest"] = RequestFn ctx["fnRequest"] = RequestFn

View File

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

View File

@ -10,26 +10,24 @@ import (
"github.com/flosch/pongo2" "github.com/flosch/pongo2"
) )
// RequestFn will make a web request and returns map[string]interface form pongo2 func jsonWebRequest(url string) map[string]interface{} {
func RequestFn(url *pongo2.Value, args ...*pongo2.Value) *pongo2.Value { Log.Noticef("requesting url via GET %s", url)
u := url.String()
Log.Noticef("requesting url via GET %s", u)
resp, err := http.Get(u) resp, err := http.Get(url)
if err != nil { 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() defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body) body, err := ioutil.ReadAll(resp.Body)
if err != nil { 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 { 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") 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") { if strings.Contains(contentType, "json") {
} else { } 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{}) jsonMap := make(map[string]interface{})
err = json.Unmarshal(body, &jsonMap) err = json.Unmarshal(body, &jsonMap)
if err != nil { 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 // 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 %} {% block part0 %}
{{ Body }} {{ 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> <h2>
{{ e.title }} {{ e.title }}
<div class="datum">{{ e.date|datum }}</div> <div class="datum">{{ e.date|datum }}</div>
@ -19,7 +19,7 @@
{% comment %} {% comment %}
limit wird für skip in Query gebraucht limit wird für skip in Query gebraucht
{% endcomment %} {% 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> <h2>
{{ e.title }} {{ e.title }}
<div class="datum">{{ e.date|datum }}</div> <div class="datum">{{ e.date|datum }}</div>