collections via config
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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,
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user