def paginate(self): X = type('X', (str, ), {'modified': True}) refs.load() res = ['1', 'asd', 'asd123', 'egg', 'spam', 'ham', '3.14', '42'] res = [X(val) for val in res] # default stuff assert list(helpers.paginate(res, 4)) == \ [((None, 1, 2), res[:4], True), ((1, 2, None), res[4:], True)] assert list(helpers.paginate(res, 7)) == \ [((None, 1, 2), res[:7], True), ((1, 2, None), res[7:], True)] # with orphans assert list(helpers.paginate(res, 7, orphans=1)) == \ [((None, 1, None), res, True)] assert list(helpers.paginate(res, 6, orphans=1)) == \ [((None, 1, 2), res[:6], True), ((1, 2, None), res[6:], True)] # a real world example which has previously failed res = [X(_) for _ in range(20)] assert list(helpers.paginate(res, 10)) == \ [((None, 1, 2), res[:10], True), ((1, 2, None), res[10:], True)] res = [X(_) for _ in range(21)] assert list(helpers.paginate(res, 10)) == \ [((None, 1, 2), res[:10], True), ((1, 2, 3), res[10:20], True), ((2, 3, None), res[20:], True)] # edge cases assert list(helpers.paginate([], 2)) == [] assert list(helpers.paginate([], 2, orphans=7)) == [] assert list(helpers.paginate([X('1'), X('2'), X('3')], 3, orphans=1)) == \ [((None, 1, None), [X('1'), X('2'), X('3')], True)]
def paginate(self): X = type('X', (str, ), {'modified': True}); refs.load() res = ['1', 'asd', 'asd123', 'egg', 'spam', 'ham', '3.14', '42'] res = [X(val) for val in res] # default stuff assert list(helpers.paginate(res, 4)) == \ [((None, 1, 2), res[:4], True), ((1, 2, None), res[4:], True)] assert list(helpers.paginate(res, 7)) == \ [((None, 1, 2), res[:7], True), ((1, 2, None), res[7:], True)] # with orphans assert list(helpers.paginate(res, 7, orphans=1)) == \ [((None, 1, None), res, True)] assert list(helpers.paginate(res, 6, orphans=1)) == \ [((None, 1, 2), res[:6], True), ((1, 2, None), res[6:], True)] # a real world example which has previously failed res = [X(_) for _ in range(20)] assert list(helpers.paginate(res, 10)) == \ [((None, 1, 2), res[:10], True), ((1, 2, None), res[10:], True)] res = [X(_) for _ in range(21)] assert list(helpers.paginate(res, 10)) == \ [((None, 1, 2), res[:10], True), ((1, 2, 3), res[10:20], True), ((2, 3, None), res[20:], True)] # edge cases assert list(helpers.paginate([], 2)) == [] assert list(helpers.paginate([], 2, orphans=7)) == [] assert list(helpers.paginate([X('1'), X('2'), X('3')], 3, orphans=1)) == \ [((None, 1, None), [X('1'), X('2'), X('3')], True)]
def generate(self, request): """Creates nicely paged listing of your posts. First page is the index.hml used to have this nice url: http://yourblog.com/ with a recent list of your (e.g. summarized) Posts. Other pages are enumerated to /page/n+1 """ ipp = self.items_per_page tt = self.env.jinja2.get_template('main.html') entrylist = [entry for entry in request['entrylist'] if not entry.draft] paginator = paginate(entrylist, ipp, orphans=self.conf['default_orphans']) for (next, curr, prev), entries, has_changed in paginator: # curr = current page, next = newer pages, prev = older pages if next is not None: next = self.path.rstrip('/') if next == 1 \ else expand(self.pagination, {'num': next}) curr = self.path if curr == 1 else expand(self.pagination, {'num': curr}) prev = None if prev is None else expand(self.pagination, {'num': prev}) path = joinurl(self.conf['output_dir'], curr, 'index.html') if exists(path) and not has_changed and not tt.has_changed: event.skip(path) continue html = tt.render(conf=self.conf, env=union(self.env, entrylist=entries, type='index', prev=prev, curr=curr, next=next, items_per_page=ipp, num_entries=len(entrylist))) yield html, path
def generate(self, conf, env, data): ipp = self.items_per_page tt = env.engine.fromfile(self.template) entrylist = data['entrylist'] paginator = paginate(entrylist, ipp, self.path, conf.default_orphans) route = self.path for (next, curr, prev), entries, modified in paginator: # curr = current page, next = newer pages, prev = older pages next = None if next is None \ else link(u'« Next', self.path.rstrip('/')) if next == 1 \ else link(u'« Next', expand(self.pagination, {'num': next})) curr = link(curr, self.path) if curr == 1 \ else link(expand(self.pagination, {'num': curr})) prev = None if prev is None \ else link(u'Previous »', expand(self.pagination, {'num': prev})) path = joinurl(conf['output_dir'], curr.href, 'index.html') if isfile(path) and not (modified or tt.modified or env.modified or conf.modified): event.skip(path) continue html = tt.render(conf=conf, env=union(env, entrylist=entries, type='index', prev=prev, curr=curr, next=next, items_per_page=ipp, num_entries=len(entrylist), route=route)) yield html, path
def generate(self, request): ipp = self.items_per_page tt = self.env.engine.fromfile(self.template) entrylist = [entry for entry in request['entrylist'] if not entry.draft] paginator = paginate(entrylist, ipp, orphans=self.conf['default_orphans']) route = self.path for (next, curr, prev), entries, has_changed in paginator: # curr = current page, next = newer pages, prev = older pages next = None if next is None \ else link(u'« Next', self.path.rstrip('/')) if next == 1 \ else link(u'« Next', expand(self.pagination, {'num': next})) curr = link(curr, self.path) if curr == 1 \ else link(expand(self.pagination, {'num': curr})) prev = None if prev is None \ else link(u'Previous »', expand(self.pagination, {'num': prev})) path = joinurl(self.conf['output_dir'], curr.href, 'index.html') if exists(path) and not has_changed and not tt.has_changed: event.skip(path) continue html = tt.render(conf=self.conf, env=union(self.env, entrylist=entries, type='index', prev=prev, curr=curr, next=next, items_per_page=ipp, num_entries=len(entrylist), route=route)) yield html, path
def generate(self, conf, env, data, **kwargs): if self.pagination is None: self.items_per_page = 2**32 self.pagination = self.path ipp = self.items_per_page tt = env.engine.fromfile(env, self.template) route = expand(self.path, kwargs) entrylist = data['entrylist'] paginator = paginate(entrylist, ipp, route, conf.default_orphans) for (next, curr, prev), entrylist, modified in paginator: next = None if next is None \ else link(u'Next', expand(self.path, kwargs)) if next == 1 \ else link(u'Next', expand(self.pagination, union({'num': next}, kwargs))) curr = link(curr, expand(self.path, kwargs)) if curr == 1 \ else link(curr, expand(self.pagination, union({'num': curr}, kwargs))) prev = None if prev is None \ else link(u'Previous', expand(self.pagination, union({'num': prev}, kwargs))) path = joinurl(conf['output_dir'], curr.href) if isfile(path) and not (modified or tt.modified or env.modified or conf.modified): event.skip(self.__class__.__name__.lower(), path) continue html = self.render(conf, env, union(locals(), kwargs)) yield html, path
def generate(self, request): """Creates paged listing by tag.""" ipp = self.items_per_page tt = self.env.engine.fromfile(self.template) for tag in self.tags: entrylist = [entry for entry in self.tags[tag]] paginator = paginate(entrylist, ipp, salt=tag, orphans=self.conf["default_orphans"]) route = expand(self.path, {"name": tag}).rstrip("/") for (next, curr, prev), entries, has_changed in paginator: # e.g.: curr = /page/3, next = /page/2, prev = /page/4 next = ( None if next is None else link(u"« Next", expand(self.path, {"name": tag}).rstrip("/")) if next == 1 else link(u"« Next", expand(self.pagination, {"name": tag, "num": next})) ) curr = ( link(curr, expand(self.path, {"name": tag})) if curr == 1 else link(expand(self.pagination, {"num": curr, "name": tag})) ) prev = ( None if prev is None else link(u"Previous »", expand(self.pagination, {"name": tag, "num": prev})) ) path = joinurl(self.conf["output_dir"], curr, "index.html") if exists(path) and not has_changed and not tt.has_changed: event.skip(path) continue html = tt.render( conf=self.conf, env=union( self.env, entrylist=entries, type="tag", prev=prev, curr=curr, next=next, tag=tag, items_per_page=ipp, num_entries=len(entrylist), route=route, ), ) yield html, path
def generate(self, request): """Creates paged listing by tag.""" ipp = self.items_per_page tt = self.env.engine.fromfile(self.template) entrylist = [ entry for entry in request['entrylist'] if not entry.draft ] for tag in self.tags: entrylist = [entry for entry in self.tags[tag]] paginator = paginate(entrylist, ipp, salt=tag, orphans=self.conf['default_orphans']) route = expand(self.path, {'name': tag}).rstrip('/') for (next, curr, prev), entries, has_changed in paginator: # e.g.: curr = /page/3, next = /page/2, prev = /page/4 next = None if next is None \ else link(u'« Next', expand(self.path, {'name': tag}).rstrip('/')) if next == 1 \ else link(u'« Next', expand(self.pagination, {'name': tag, 'num': next})) curr = link(curr, expand(self.path, {'name': tag})) if curr == 1 \ else link(expand(self.pagination, {'num': curr, 'name': tag})) prev = None if prev is None \ else link(u'Previous »', expand(self.pagination, {'name': tag, 'num': prev})) path = joinurl(self.conf['output_dir'], curr, 'index.html') if exists(path) and not has_changed and not tt.has_changed: event.skip(path) continue html = tt.render(conf=self.conf, env=union(self.env, entrylist=entries, type='tag', prev=prev, curr=curr, next=next, tag=tag, items_per_page=ipp, num_entries=len(entrylist), route=route)) yield html, path
def test_paginate(self): class X(str): # dummy class has_changed = True md5 = property(lambda x: str(hash(x))) res = ['1', 'asd', 'asd123', 'egg', 'spam', 'ham', '3.14', '42'] res = [X(val) for val in res] # default stuff self.assertEqual(list(helpers.paginate(res, 4)), [((None, 1, 2), res[:4], True), ((1, 2, None), res[4:], True)]) self.assertEqual(list(helpers.paginate(res, 4, lambda x: x.isdigit())), [((None, 1, None), [X('1'), X('42')], True), ]) self.assertEqual(list(helpers.paginate(res, 7)), [((None, 1, 2), res[:7], True), ((1, 2, None), res[7:], True)]) # with orphans self.assertEqual(list(helpers.paginate(res, 7, orphans=1)), [((None, 1, None), res, True)]) self.assertEqual(list(helpers.paginate(res, 6, orphans=1)), [((None, 1, 2), res[:6], True), ((1, 2, None), res[6:], True)]) # a real world example which has previously failed res = [X(_) for _ in range(20)] self.assertEqual(list(helpers.paginate(res, 10)), [((None, 1, 2), res[:10], True), ((1, 2, None), res[10:], True)]) res = [X(_) for _ in range(21)] self.assertEqual(list(helpers.paginate(res, 10)), [((None, 1, 2), res[:10], True), ((1, 2, 3), res[10:20], True), ((2, 3, None), res[20:], True)]) # edge cases self.assertEqual(list(helpers.paginate([], 2)), []) self.assertEqual(list(helpers.paginate([], 2, orphans=7)), []) self.assertEqual(list(helpers.paginate([X('1'), X('2'), X('3')], 3, orphans=1)), [((None, 1, None), [X('1'), X('2'), X('3')], True)])
def generate(self, conf, env, data): for lang in env.langs: ipp = self.items_per_page tt = env.engine.fromfile(self.template) entrylist = [] for entry in data['entrylist']: try: e = entry_for_lang(data, lang, entry) entrylist.append(e) except TranslationNotFound: pass#entrylist.append(entry) paginator = paginate(entrylist, ipp, self.path, conf.default_orphans) route = '/blog/' for (next, curr, prev), entries, modified in paginator: # curr = current page, next = newer pages, prev = older pages if next is None: pass elif next == 1: next = link(u'« Next', expand(self.path.rstrip('/'), {'lang':lang})) else: next = link(u'« Next', expand(self.pagination, {'num': next,'lang': lang})) if next: next.href = strip_default_lang(next.href, self.conf) curr = link(curr, self.path) if curr == 1 \ else link(expand(self.pagination, {'num': curr,'lang': lang})) curr.href = strip_default_lang(curr.href, self.conf) prev = None if prev is None \ else link(u'Previous »', expand(self.pagination, {'num': prev,'lang': lang})) if prev: prev.href = strip_default_lang(prev.href, self.conf) path = joinurl(conf['output_dir'], expand(curr.href, {'lang': lang}), 'index.html') path = strip_default_lang(path, self.conf) env['lang'] = lang env['active_route'] = route if isfile(path) and not (modified or tt.modified or env.modified or conf.modified): event.skip(path) continue html = tt.render(conf=conf, env=union(env, entrylist=entries, type='index', prev=prev, curr=curr, next=next, items_per_page=ipp, num_entries=len(entrylist), route=route)) yield html, path
def test_paginate(self): class X(str): # dummy class has_changed = True md5 = property(lambda x: str(hash(x))) res = ['1', 'asd', 'asd123', 'egg', 'spam', 'ham', '3.14', '42'] res = [X(val) for val in res] # default stuff assert list(helpers.paginate(res, 4)) == \ [((None, 1, 2), res[:4], True), ((1, 2, None), res[4:], True)] assert list(helpers.paginate(res, 4, lambda x: x.isdigit())) == \ [((None, 1, None), [X('1'), X('42')], True), ] assert list(helpers.paginate(res, 7)) == \ [((None, 1, 2), res[:7], True), ((1, 2, None), res[7:], True)] # with orphans assert list(helpers.paginate(res, 7, orphans=1)) == \ [((None, 1, None), res, True)] assert list(helpers.paginate(res, 6, orphans=1)) == \ [((None, 1, 2), res[:6], True), ((1, 2, None), res[6:], True)] # a real world example which has previously failed res = [X(_) for _ in range(20)] assert list(helpers.paginate(res, 10)) == \ [((None, 1, 2), res[:10], True), ((1, 2, None), res[10:], True)] res = [X(_) for _ in range(21)] assert list(helpers.paginate(res, 10)) == \ [((None, 1, 2), res[:10], True), ((1, 2, 3), res[10:20], True), ((2, 3, None), res[20:], True)] # edge cases assert list(helpers.paginate([], 2)) == [] assert list(helpers.paginate([], 2, orphans=7)) == [] assert list(helpers.paginate([X('1'), X('2'), X('3')], 3, orphans=1)) == \ [((None, 1, None), [X('1'), X('2'), X('3')], True)]
def test_paginate(self): class X(str): # dummy class has_changed = True md5 = property(lambda x: str(hash(x))) res = ["1", "asd", "asd123", "egg", "spam", "ham", "3.14", "42"] res = [X(val) for val in res] # default stuff assert list(helpers.paginate(res, 4)) == [((None, 1, 2), res[:4], True), ((1, 2, None), res[4:], True)] assert list(helpers.paginate(res, 4, lambda x: x.isdigit())) == [((None, 1, None), [X("1"), X("42")], True)] assert list(helpers.paginate(res, 7)) == [((None, 1, 2), res[:7], True), ((1, 2, None), res[7:], True)] # with orphans assert list(helpers.paginate(res, 7, orphans=1)) == [((None, 1, None), res, True)] assert list(helpers.paginate(res, 6, orphans=1)) == [ ((None, 1, 2), res[:6], True), ((1, 2, None), res[6:], True), ] # a real world example which has previously failed res = [X(_) for _ in range(20)] assert list(helpers.paginate(res, 10)) == [((None, 1, 2), res[:10], True), ((1, 2, None), res[10:], True)] res = [X(_) for _ in range(21)] assert list(helpers.paginate(res, 10)) == [ ((None, 1, 2), res[:10], True), ((1, 2, 3), res[10:20], True), ((2, 3, None), res[20:], True), ] # edge cases assert list(helpers.paginate([], 2)) == [] assert list(helpers.paginate([], 2, orphans=7)) == [] assert list(helpers.paginate([X("1"), X("2"), X("3")], 3, orphans=1)) == [ ((None, 1, None), [X("1"), X("2"), X("3")], True) ]
def generate(self, request): ipp = self.items_per_page tt = self.env.engine.fromfile(self.template) entrylist = [ entry for entry in request['entrylist'] if not entry.draft ] paginator = paginate(entrylist, ipp, orphans=self.conf['default_orphans']) route = self.path for (next, curr, prev), entries, has_changed in paginator: # curr = current page, next = newer pages, prev = older pages next = None if next is None \ else link(u'« Next', self.path.rstrip('/')) if next == 1 \ else link(u'« Next', expand(self.pagination, {'num': next})) curr = link(curr, self.path) if curr == 1 \ else link(expand(self.pagination, {'num': curr})) prev = None if prev is None \ else link(u'Previous »', expand(self.pagination, {'num': prev})) path = joinurl(self.conf['output_dir'], curr.href, 'index.html') if exists(path) and not has_changed and not tt.has_changed: event.skip(path) continue html = tt.render(conf=self.conf, env=union(self.env, entrylist=entries, type='index', prev=prev, curr=curr, next=next, items_per_page=ipp, num_entries=len(entrylist), route=route)) yield html, path
def generate(self, request): """Creates paged listing by tag.""" ipp = self.items_per_page tt = self.env.jinja2.get_template('main.html') entrylist = [entry for entry in request['entrylist'] if not entry.draft] for tag in self.tags: entrylist = [entry for entry in self.tags[tag]] paginator = paginate(entrylist, ipp, salt=tag, orphans=self.conf['default_orphans']) for (next, curr, prev), entries, has_changed in paginator: # e.g.: curr = /page/3, next = /page/2, prev = /page/4 if next == 1: next = expand(self.path, {'name': tag}).rstrip('/') elif next > 1: next = expand(self.pagination, {'name': tag, 'num': next}) if curr == 1: curr = expand(self.path, {'name': tag}) elif curr > 1: curr = expand(self.pagination, {'num': curr, 'name': tag}) if prev is not None: prev = expand(self.pagination, {'name': tag, 'num': prev}) path = joinurl(self.conf['output_dir'], curr, 'index.html') if exists(path) and not has_changed and not tt.has_changed: event.skip(path) continue html = tt.render(conf=self.conf, env=union(self.env, entrylist=entries, type='tag', prev=prev, curr=curr, next=next, items_per_page=ipp, num_entries=len(entrylist))) yield html, path
def generate(self, request): """Creates paged listing by tag.""" ipp = self.items_per_page tt = self.env.engine.fromfile(self.template) entrylist = [entry for entry in request['entrylist'] if not entry.draft] for tag in self.tags: entrylist = [entry for entry in self.tags[tag]] paginator = paginate(entrylist, ipp, salt=tag, orphans=self.conf['default_orphans']) route = expand(self.path, {'name': tag}).rstrip('/') for (next, curr, prev), entries, has_changed in paginator: # e.g.: curr = /page/3, next = /page/2, prev = /page/4 next = None if next is None \ else link(u'« Next', expand(self.path, {'name': tag}).rstrip('/')) if next == 1 \ else link(u'« Next', expand(self.pagination, {'name': tag, 'num': next})) curr = link(curr, expand(self.path, {'name': tag})) if curr == 1 \ else link(expand(self.pagination, {'num': curr, 'name': tag})) prev = None if prev is None \ else link(u'Previous »', expand(self.pagination, {'name': tag, 'num': prev})) path = joinurl(self.conf['output_dir'], curr, 'index.html') if exists(path) and not has_changed and not tt.has_changed: event.skip(path) continue html = tt.render(conf=self.conf, env=union(self.env, entrylist=entries, type='tag', prev=prev, curr=curr, next=next, tag=tag, items_per_page=ipp, num_entries=len(entrylist), route=route)) yield html, path
def generate(self, conf, env, data): """Creates paged listing by tag.""" ipp = self.items_per_page tt = env.engine.fromfile(self.template) for tag in self.tags: route = expand(self.path, {'name': tag}).rstrip('/') entrylist = [entry for entry in self.tags[tag]] paginator = paginate(entrylist, ipp, route, conf['default_orphans']) for (next, curr, prev), entries, modified in paginator: # e.g.: curr = /page/3, next = /page/2, prev = /page/4 next = None if next is None \ else link(u'Next', expand(self.path, {'name': tag}).rstrip('/')) if next == 1 \ else link(u'Next', expand(self.pagination, {'name': tag, 'num': next})) curr = link(curr, expand(self.path, {'name': tag})) if curr == 1 \ else link(expand(self.pagination, {'num': curr, 'name': tag})) prev = None if prev is None \ else link(u'Previous', expand(self.pagination, {'name': tag, 'num': prev})) path = joinurl(conf['output_dir'], curr) if isfile(path) and not (modified or tt.modified or env.modified or conf.modified): event.skip('tag', path) continue html = tt.render(conf=conf, env=union(env, entrylist=entries, type='tag', prev=prev, curr=curr, next=next, tag=tag, items_per_page=ipp, num_entries=len(entrylist), route=route)) yield html, path