def Render(self, results, context): value = context.get(self.path) if value == None: return values = bm_extract.coerce_list(value, separator = ", ") if not values: return count = -1 for v in values: count += 1 context.Push() context[self.variable] = v context["forloop.counter"] = count + 1 # iteration of the loop (1-indexed) context["forloop.counter0"] = count # iteration of the loop (0-indexed) context["forloop.revcounter"] = len(values) - count # iterations from the end of the loop (1-indexed) context["forloop.revcounter0"] = len(values) - count - 1 # iterations from the end of the loop (0-indexed) context["forloop.first"] = bool(count == 0) # True if this is the first time through the loop context["forloop.last"] = bool(count == len(values) - 1) # True if this is the last time through the loop djolt_base.Node.Render(self, results, context) context.Pop()
def _IterItemsOnPage(self, page): if self._item_path == None: raise StopIteration if not self._item_path: for item in bm_extract.coerce_list(page): yield item else: for item in bm_extract.as_list(page, self._item_path, otherwise = []): yield item
def PopExpecting(self, parents = None): if parents: found = False for parent in bm_extract.coerce_list(parents): if parent == self.__class__: found = True if not found: raise DjoltSyntaxError, "expected one of: %s got: %s" % ( ", ".join(map(lambda c: c.__name__, parents)), self.__class__, ) return self.parent
def Filter(self, name, argument, value): """ Example: {{ value|first }} If value is the list ['a', 'b', 'c'], the output will be 'a'. """ value = bm_extract.coerce_list(value, separator = None) if value: return value[0]
def Filter(self, name, argument, value): """ Returns a random item from the given list. For example: {{ value|random }} If value is the list ['a', 'b', 'c', 'd'], the output could be "b". """ value = bm_extract.coerce_list(value, separator = None) if value: return random.choice(value)
def Filter(self, name, argument, value): """ Returns the last item in a list. For example: {{ value|last }} If value is the list ['a', 'b', 'c', 'd'], the output will be the string "d". """ value = bm_extract.coerce_list(value, separator = None) if value: return value[-1]
def Filter(self, name, argument, value): """ Example: {{ value|atindex:1 }} If value is the list ['a', 'b', 'c'], the output will be 'b'. If called without an argument, we assume 0. """ value = bm_extract.coerce_list(value, separator = None) index = bm_extract.coerce_int(argument) if index < len(value): return value[index]
def Filter(self, name, argument, value): """ Joins a list with a string, like Python's str.join(list) For example: {{ value|join:" // " }} If value is the list ['a', 'b', 'c'], the output will be the string "a // b // c". """ value = bm_extract.coerce_list(value, separator = None) value = map(bm_extract.coerce_string, value) argument = bm_extract.coerce_string(argument, otherwise = "") if value: return argument.join(value)
def ScrubItem(self, itemd): """Note: *not* CustomizeAtomItem""" if not self.AtomLike(): return itemd itemd = dict(itemd) # # Atom title # try: if not itemd.get("title"): itemd["title"] = itemd.pop("text") except KeyError: pass # # Atom datetimes # try: created = itemd.pop("created") itemd["created"] = bm_extract.coerce_datetime(created, otherwise = created, atom = True) except KeyError: pass # # Atom categories # try: tags = itemd.pop("tags") tags = bm_extract.coerce_list(tags, separator = ",", strip = True) itemd["category"] = [ { "term" : tag } for tag in tags ] except KeyError: pass return itemd
def as_list(self, path, **ad): return bm_extract.coerce_list(self.get(path), **ad)
def ScrubEntry(self, itemd): if bm_extract.is_dict(itemd): nd = {} seen_html = False seen_rss = False seen_url = False for key, value in itemd.iteritems(): if self.AtomLike(): if key == "link": key = "htmlUrl" elif key == "feeds": key = "rssUrl" elif key == "content": key = "description" elif key == "title": key = "text" elif key == "category": key = "tags" value = ", ".join(map(lambda d: d["term"], value)) elif key == "links": for ld in bm_extract.coerce_list(value): if bm_extract.as_string(ld, "rel") == "alternate": key = "rssUrl" value = bm_extract.as_string(ld, "href") # # datetimes (?) # try: created = itemd.pop("created") itemd["created"] = bm_extract.coerce_datetime(created, otherwise = created, rfc822 = True) except KeyError: pass if key == "rssUrl": value = self.FirstInListLikeObject(value, value) if value == None: continue seen_rss = True elif key == "htmlUrl": value = self.FirstInListLikeObject(value, value) if value == None: continue seen_html = True elif key == "url": seen_url = True if key in [ "items", "outline" ]: nd["outline"] = self.ScrubEntry(value) elif value == None: pass elif bm_extract.is_atomic(value): nd['@%s' % key] = value if seen_rss: nd.setdefault("@type", "rss") elif seen_html: nd.setdefault("@type", "link") elif seen_url: nd.setdefault("@type", "link") nd.setdefault("@text", "") return nd elif bm_extract.is_atomic(itemd): return { "@title" : bm_extract.coerce_string(itemd) } elif bm_extract.is_list(itemd) or bm_extract.is_list_like(itemd): return map(self.ScrubEntry, itemd) return itemd