From ecd1edf23b263e5a88107179489b433742fb17c9 Mon Sep 17 00:00:00 2001 From: Holger Weiss Date: Mon, 14 Oct 2013 01:46:55 +0200 Subject: Add an actual News page Initial support for News pages had been committed already, this commit completes that support and adds an actual News page. News pages (with up to ten articles) are auto-created from any pages that have the "date" attribute set. --- web/macros.py | 107 ++++++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 90 insertions(+), 17 deletions(-) (limited to 'web/macros.py') diff --git a/web/macros.py b/web/macros.py index f3b6ec6..5918195 100644 --- a/web/macros.py +++ b/web/macros.py @@ -8,9 +8,13 @@ page = { "description": "Standard monitoring plugins for Nagios and compatible monitoring solutions.", "keywords": "Nagios, Icinga, Shinken, Monitoring, Official, Plugins, Open, Source, Free, Software" } -release_notes = 'doc/release-notes/' + plugins_release.replace('.', '-') + '.html' +release_notes = 'news/release-%s.html' % plugins_release.replace('.', '-') site_url = 'https://www.nagios-plugins.org/' +# +# RSS Feed +# + _RSS = """ @@ -40,15 +44,14 @@ _RSS_ITEM = """ def hook_postconvert_rss(): items = [] - posts = [p for p in pages if 'post' in p] + posts = [p for p in pages if 'date' in p] posts.sort(key=lambda p: p.date, reverse=True) for p in posts: - title = p.post link = '%s/%s' % (site_url.rstrip('/'), p.url) desc = hx(p.html) date = time.mktime(time.strptime('%s 12' % p.date, '%Y-%m-%d %H')) date = email.utils.formatdate(date) - items.append(_RSS_ITEM % (title, link, desc, link, date)) + items.append(_RSS_ITEM % (p.title, link, desc, date, link)) items = ''.join(items) title = 'Nagios Plugins' link = '%s/news/index.html' % site_url.rstrip('/') @@ -59,23 +62,84 @@ def hook_postconvert_rss(): fp.write(rss) fp.close() -def list_posts(max_posts=-1): - posts = [p for p in pages if 'post' in p] +# +# News +# + +def hook_preconvert_news(): + posts_per_page = 10 + posts = [p for p in pages if 'date' in p] posts.sort(key=lambda p: p.date, reverse=True) - if max_posts == -1: - max_posts = len(posts) - for p in posts[:max_posts]: - date = time.strftime('%B %d, %Y', time.strptime(p['date'], '%Y-%m-%d')) - print '* **[%s](%s)** (%s)' % (p.post, p.url, date) + n_news_pages = len(posts) / posts_per_page + if len(posts) % posts_per_page > 0: + n_news_pages += 1 + for i, chunk in enumerate(next_news_chunk(posts, posts_per_page)): + content = make_news_page(chunk, i) + make_news_footer(n_news_pages, i) + if i == 0: + p = Page('news/index.md', + virtual=content, + menu=2, + title='News', + parent='Home') + else: + p = Page('news/%d.md' % (i + 1), + virtual=content, + title='News Page %d' % (i + 1), + parent='News') + pages.append(p) -def list_kids(): - kids = [(p.url, p.title) for p in pages if p.get('parent') == page.title] - for kid in sorted(kids): - print('* [%s](%s)' % (kid[1], kid[0])) +def make_news_page(posts, current_index): + marker = '' + source = list() + if current_index == 0: + title = 'News' + else: + title = 'News Page %d' % (current_index + 1) + abstract = ['# ' + title] + for p in posts: + timestamp = time.strptime(p.date, '%Y-%m-%d') + date = time.strftime('%A, %B %-e, %Y', timestamp) + abstract.append('## %s' % p.title) + abstract.append('*%s*' % date) + abstract.append('%s' % p.source.split(marker, 1)[0]) + abstract.append('[Read more]' % p.url) + source.append('# %s' % p.title) + source.append('*%s*' % date) + source.append(p.source.replace(marker, '', 1)) + p.source = '\n'.join(source) + p['parent'] = title + return '\n'.join(abstract) + '\n\n' + +def make_news_footer(n_news_pages, current_index): + footer = list() + if current_index != 0: + previous = 'index' if current_index == 1 else str(current_index) + footer.append('[First](news/index.html)') + footer.append('[Previous](news/%s.html)' % previous) + if n_news_pages <= 20: + for i in range(n_news_pages): + if i == current_index: + footer.append('%d' % (i + 1)) + else: + footer.append('[%d](news/%d.html)' % (i + 1, i + 1)) + if current_index != n_news_pages - 1: + footer.append('[Next](news/%d.html)' % (current_index + 2)) + footer.append('[Last](news/%d.html)' % n_news_pages) + return ' '.join(footer) + '\n{: #news-footer }\n' + +def next_news_chunk(posts, posts_per_page): + index = 0 + while len(posts[index:]) > 0: + yield posts[index:index + posts_per_page] + index += posts_per_page + +# +# Menu and Breadcrumb Navigation +# def menu(): - menu_pages = [p for p in pages if 'menu-position' in p] - menu_pages.sort(key=lambda p: int(p['menu-position'])) + menu_pages = [p for p in pages if 'menu' in p] + menu_pages.sort(key=lambda p: int(p['menu'])) for p in menu_pages: if p.title == page.title: print('%s' % hx(p.title)) @@ -96,6 +160,15 @@ def breadcrumb(): crumbs = ' ' + stable return crumbs +# +# Miscellaneous +# + +def list_kids(): + kids = [(p.url, p.title) for p in pages if p.get('parent') == page.title] + for kid in sorted(kids): + print('* [%s](%s)' % (kid[1], kid[0])) + def copyright_years(since=None): this_year = time.gmtime().tm_year if since is not None and int(since) != this_year: -- cgit v1.2.3-74-g34f1