summaryrefslogtreecommitdiffstats
path: root/web/macros.py
blob: c950a0441725ac06ab0925345412636467d90453 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import email.utils
import os.path
import time

plugins_release = '1.5'
mib_release = '1.0.1'
release_notes = 'news/release-%s.html' % plugins_release.replace('.', '-')
site_url = 'https://www.nagios-plugins.org/'
feeds = """These project news articles are also available as an
[RSS feed][rss]{: rel="alternate" type="application/rss+xml" }.  Additional
stuff is posted to [Twitter][twitter], so you might want to [follow us][follow]
there as well.

[rss]: rss.xml "RSS Feed"
[twitter]: https://twitter.com/ "Twitter"
[follow]: https://twitter.com/intent/follow?screen_name=monitorplugins "Follow Us"
"""

page = {
    "description": "Standard monitoring plugins for Nagios and compatible monitoring solutions.",
    "keywords": "Nagios, Icinga, Shinken, Naemon, Monitoring, Official, Plugins, Open, Source, Free, Software"
}

#
# RSS Feed
#

_RSS = """<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <title>%s</title>
    <link>%s</link>
    <description>%s</description>
    <language>en-us</language>
    <webMaster>webmaster@nagios-plugins.org</webMaster>
    <pubDate>%s</pubDate>
    <lastBuildDate>%s</lastBuildDate>
    <generator>Poole</generator>
    <docs>http://blogs.law.harvard.edu/tech/rss</docs>
    %s
  </channel>
</rss>
"""

_RSS_ITEM = """
    <item>
      <title>%s</title>
      <link>%s</link>
      <description>%s</description>
      <guid>%s</guid>
      <pubDate>%s</pubDate>
    </item>
"""

def hook_postconvert_rss():
    items = []
    posts = [p for p in pages if 'date' in p]
    posts.sort(key=lambda p: p.date, reverse=True)
    for p in posts:
        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 % (p.title, link, desc, link, date))
    items = ''.join(items)
    title = 'Nagios Plugins'
    link = '%s/news/' % site_url.rstrip('/')
    desc = 'Announcements published by the Nagios Plugins Development Team.'
    date = email.utils.formatdate()
    rss = _RSS % (title, link, desc, date, date, items)
    fp = open(os.path.join(output, 'rss.xml'), 'w')
    fp.write(rss)
    fp.close()

#
# 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)
    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 make_news_page(posts, current_index):
    marker = '<end-of-teaser>'
    if current_index == 0:
        title = 'News'
        teaser = ['# ' + title, feeds]
    else:
        title = 'News Page %d' % (current_index + 1)
        teaser = ['# ' + title]
    for p in posts:
        source = list()
        author = p.author.encode('ascii', 'xmlcharrefreplace')
        timestamp = time.strptime(p.date, '%Y-%m-%d')
        author_date = author + ', ' + time.strftime('%B %-e, %Y', timestamp)
        teaser.append('## %s' % p.title)
        teaser.append('*%s*' % author_date)
        teaser.append('%s' % p.source.split(marker, 1)[0].rstrip())
        teaser.append('<a href="%s">Read&nbsp;more&nbsp;&raquo;</a>' % p.url)
        source.append('# %s' % p.title)
        source.append('*%s*' % author_date)
        source.append(p.source.replace(marker, '', 1))
        p.source = '\n'.join(source)
        p['parent'] = title
    return '\n'.join(teaser) + '\n\n'

def make_news_footer(n_news_pages, current_index):
    if n_news_pages == 1:
        return ''
    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 '&emsp;'.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' in p]
    menu_pages.sort(key=lambda p: int(p['menu']))
    for p in menu_pages:
        if p.title == page.title:
            print('<span id="current">%s</span>' % hx(p.title))
        else:
            print('<span><a href="%s">%s</a></span>' % (p.url, hx(p.title)))

def breadcrumb():
    stable = '<span id="release">Stable release: <a href="%s">%s</a></span>' \
           % (release_notes, plugins_release)
    parents = {p.title: (p.url, p.get('parent')) for p in pages}
    title = page.title
    crumbs = hx(title)
    while parents[title][1] is not None:
        title = parents[title][1]
        url = parents[title][0]
        crumbs = '<a href="%s">%s</a> &raquo; %s' % (url, hx(title), crumbs)
    if crumbs == hx(page.title):
        crumbs = '&nbsp;' + 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:
        return str(since) + '&ndash;' + str(this_year)
    else:
        return str(this_year)

# vim:set expandtab softtabstop=4 shiftwidth=4 textwidth=80: