def render_paginated(self, template, path, **ctx): """Render a template, but break the content into multiple pages""" nctx = self.ctx.copy() nctx.update(ctx) ctx = nctx ctx.update({ 'articles': self.ctx['articles'], }) pl = settings.PAGINATE_N articles = ctx["a"][:] if self.lang is not None: articles = filter(lambda a: a.hard_language in (self.lang, None), articles) path = path.lstrip("/") dirname = os.path.dirname(path) baseurl = Url(path).switch_language(self.lang) if len(articles) > pl: if "pag" not in ctx: ctx["pag"] = {} if "base" not in ctx["pag"]: ctx["pag"]["base"] = dirname + "/" + ctx["_"]("page_%s") pages = (len(articles) - 1) // pl + 1 ctx["pag"]["first"] = baseurl.get_path() ctx["pag"]["pages"] = pages for p in range(1, pages): ctx["pag"]["cur"] = p + 1 ctx["a"] = articles[p * pl:(p + 1) * pl] self.render_template( "_templates/%s.mako" % template, Url(ctx["pag"]["base"] % (p + 1) + ".html").switch_language(self.lang), **ctx) ctx["pag"]["cur"] = 1 ctx["a"] = articles[:pl] self.render_template("_templates/%s.mako" % template, baseurl, **ctx)
def aa(path): """Make a path absolute""" if isinstance(path, Url): return path.get() elif re.match(r"[a-z0-9\-]+:", path) or path.startswith("//"): return path return Url(path).get()
def render_template(self, template, path, **ctx): """Render a template within the given context ctx""" nctx = self.ctx.copy() nctx.update(ctx) ctx = nctx ctx.update({ 'articles': self.ctx['articles'], }) if self.lang is not None and "a" in ctx: ctx["a"] = filter(lambda a: a.hard_language in (self.lang, None), ctx["a"]) if ctx.get("filter_langs", False) and "a" in ctx: ctx["a"] = filter( lambda a: a.headers.language in (self.lang, None), ctx["a"]) if not isinstance(path, Url): path = Url(path).switch_language(self.lang) else: path.switch_language(self.lang) ctx['url'] = path tpl = self.lookup.get_template(template) template_engine.write_to(ctx['url'].get_path(), tpl.render_unicode(**ctx), ctx.get("date", settings.now)) sitemap = [ ctx["url"].copy(), ctx.get("date", settings.now), "monthly", 0.5 ] self.sitemap.append(sitemap)
def _laa(path): if isinstance(path, Article): if path.headers.translation: for t in path.headers.translation: if t.headers.language.startswith(lang): return t.url.get() return path.url.copy().switch_language(lang).get() elif isinstance(path, Url): return path.copy().switch_language(lang).get() else: return Url(path).switch_language(lang).get()
def __init__(self, path): """Initialize with path to article source""" path = path.lstrip("/") self.lexers.update(settings.get('LEXERS', {})) self.processed = False self.category = os.path.dirname(path).strip("/") self.url = Url(settings.get("ARTICLE_PATH", "") + path) l = filter(lambda s: s in settings.languages, self.url.get_extensions()) self.hard_language = None if len(l) == 1: self.hard_language = l[0] self.url.fix_language() f = open("_articles/%s" % path, 'r') head, content = f.read().replace("\r\n", "\n").split("\n\n", 1) f.close() self.headers = ArticleHeaders(head) self.raw_content = content.decode("utf-8") self.soup = BeautifulSoup(self.raw_content, fromEncoding="utf-8") self.complete_headers() self.process_content()