func fillInTemplate(scope *goquery.Selection, cont *context) error { // filling in the template happens children first so that the closure of // available data is readily available (if the most senior elements were // done first there'd be no way to know the reflection path within data) // NOTE: handleGatsRepeatOvers is mutually recursive with fillInTemplate for sel := scope.Find("[gatsrepeatover]"); sel.Length() > 0; sel = scope.Find("[gatsrepeatover]") { handleGatsRepeatOvers(sel.First(), cont) } // do the actual work of filling in the template e := handleGatsIf(scope, cont) if e != nil { return e } e = handleGatsContent(scope, cont) if e != nil { return e } e = handleGatsText(scope, cont) if e != nil { return e } e = handleGatsAttributes(scope, cont) if e != nil { return e } e = handleGatsAttribute(scope, cont) if e != nil { return e } return nil }
func handleGats(t *goquery.Selection, selector string, meat func(string, *goquery.Selection)) { attribName := selector[1 : len(selector)-1] t.Find(selector).Each(func(_ int, sel *goquery.Selection) { fieldName, _ := sel.Attr(attribName) meat(fieldName, sel) sel.RemoveAttr(attribName) }) return }
func handleGatsTranscludes(scope *goquery.Selection) (result error) { for scope.Find("[gatstransclude]").Length() > 0 && result == nil { handleGats(scope, "[gatstransclude]", func(ts string, sel *goquery.Selection) { if result != nil { return } filename, selector, res := splitString(ts) if res != nil { result = res return } rootNode, res := parseFile(filename) if res != nil { result = res return } newKids := goquery.NewDocumentFromNode(rootNode).Find(selector) sel.Empty().Append(newKids) }) } return result }
func handleGatsRepeatOvers(sel *goquery.Selection, cont *context) { fieldName, _ := sel.Attr("gatsrepeatover") length, err := getLength(fieldName, cont) if err == nil { for i := 0; i < length; i++ { c, e := getItem(fieldName, i, cont) if e == nil { instance := sel.Clone().InsertBefore(sel) e1 := fillInTemplate(instance, c) // TODO: stop ignoring the returned errors here if e1 != nil { panic(e1) // is this really panic worthy? } instance.RemoveAttr("gatsrepeatover") } } } sel.Remove() }
func handleGatsRemoves(t *goquery.Selection) { t.Find("[gatsremove]").Remove() }
func handleGatsOmitTag(t *goquery.Selection) { t.Find("[gatsomittag]").Each(func(_ int, parent *goquery.Selection) { parent.Contents().Remove().InsertBefore(parent) parent.Remove() }) }