Skip to content

Commit

Permalink
- RSS feed generation for all directories
Browse files Browse the repository at this point in the history
- make all variables in site section of configuration available to templates
- renamed title template var in indexes to directory to avoid confusion
  • Loading branch information
yaph committed Mar 30, 2012
1 parent 9cdb883 commit aa3abe3
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 10 deletions.
1 change: 0 additions & 1 deletion README.markdown
Expand Up @@ -30,7 +30,6 @@ Please see the [documentation](http://yaph.github.com/logya/) for more informati

## Version 2.3

* RSS feed generation from indexes as extension
* XML and HTML sitemap generation as extension

## Version 2.4
Expand Down
61 changes: 59 additions & 2 deletions logya/__init__.py
@@ -1,5 +1,7 @@
# -*- coding: utf-8 -*-
import os
import datetime
import PyRSS2Gen
from operator import itemgetter
from config import Config
from docreader import DocReader
Expand All @@ -25,6 +27,8 @@ def __init__(self, **kwargs):

self.index_filename = 'index.html'

self.feed_limit = 10

def init_env(self):
"""Initialize the environment for generating the Web site to deploy.
Expand All @@ -37,13 +41,19 @@ def init_env(self):

dir_templates = self.get_path('templates', required=True)
self.template = Template(dir_templates)
self.template.add_var('base_path', self.config.get('site', 'base_path'))

# make all settings in site section available to templates
for key, val in self.config.items('site'):
self.template.add_var(key, val)

# optional directory with static files like style sheets, scripts and images
self.dir_static = self.get_path('static')

self.dir_dst = self.get_path('deploy')

# feeds are only generated, if base_url is set in site section of config
self.base_url = self.config.get('site', 'base_url')

def info(self, msg):
"""Print message if in verbose mode."""

Expand Down Expand Up @@ -119,6 +129,42 @@ def build_indexes(self):
# make indexes available to templates
self.template.add_var('indexes', self.indexes)

def write_rss(self, directory, docs):
"""Write RSS 2.0 XML file in target directory"""

items = []
for d in docs[0:self.feed_limit]:
# omit start page
if '/' == d['url']:
continue

url = self.base_url + d['url']
title = d['title']

description = title
# TODO make sure description is set in Parser class
if 'description' in d:
description = d['description']

items.append(PyRSS2Gen.RSSItem(
title = title,
link = url,
description = description,
guid = url,
pubDate = d['created']))

rss = PyRSS2Gen.RSS2(
title = directory,
link = self.base_url + os.path.join('/', directory, 'rss.xml'),
description = directory,
lastBuildDate = datetime.datetime.now(),
items = items)

rss_file_name = os.path.join(self.dir_dst, directory, 'rss.xml')
rss_file = open(rss_file_name, 'w')
rss.write_xml(rss_file)
rss_file.close()

def write_index(self, filewriter, directory, template):
"""Write an auto-generated index.html file."""

Expand All @@ -130,13 +176,17 @@ def write_index(self, filewriter, directory, template):
reverse=True)

self.template.add_var('index', docs)
self.template.add_var('title', directory)
self.template.add_var('directory', directory)

page = self.template.get_env().get_template(template)
filewriter.write(filewriter.getfile(self.dir_dst, directory),
page.render(self.template.get_vars())
.encode('utf-8'))

# write directory RSS file
if self.base_url:
self.write_rss(directory, docs)

def write_indexes(self):
"""Write index.html files to deploy directories where non exists.
Expand All @@ -149,3 +199,10 @@ def write_indexes(self):

for directory in self.indexes.keys():
self.write_index(FileWriter(), directory, template)

# write root RSS file
if self.base_url:
docs = sorted(self.docs_parsed.values(),
key=itemgetter('created'),
reverse=True)
self.write_rss('', docs)
3 changes: 3 additions & 0 deletions logya/config.py
Expand Up @@ -21,3 +21,6 @@ def get(self, section, var, required=False):
return False
else:
return val

def items(self, section):
return self.config.items(section)
9 changes: 7 additions & 2 deletions logya/sites/docs/content/index.html
Expand Up @@ -23,13 +23,15 @@ <h2>Features</h2>
<li>Automatic generation of document indexes in created directories</li>
<li>Site configuration</li>
<li>No server side scripting security issues</li>
<li>RSS feed generation for directories</li>
</ul>

<h2>Requirements</h2>
<ul>
<li><a href="http://python.org/">Python 2.7</a></li>
<li><a href="http://jinja.pocoo.org/docs">Jinja2</a></li>
<li><a href="http://pyyaml.org/">PyYAML</a></li>
<li><a href="http://www.dalkescientific.com/Python/PyRSS2Gen.html">PyRSS2Gen</a></li>
</ul>

<h2>Installation</h2>
Expand Down Expand Up @@ -84,15 +86,18 @@ <h2>Document Structure</h2>
</pre>
<p>The header is in <a href="http://yaml.org/">YAML</a> format. It starts and ends with 3 dashes. The remaining part of the document is treated as the content that goes in the body of the created HTML page. This content can be marked up with any HTML tags that can be in the body of an HTML document.</p>
<h3>Document Header</h3>
<p>Each document must specify the <code>url</code> attribute in the header, all other attributes are optional. Attribute values can range from simple strings to nested data structures that are automatically available in templates.</p>
<p>Each document must specify the <code>url</code> and <code>title</code> attributes in the header, all other attributes are optional. Attribute values can range from simple strings to nested data structures that are automatically available in templates.</p>
<p>The only exception with a pre-defined value format is the <code>created</code> attribute. If you set it, it must adhere to the format <code>YYYY-MM-DDTHH:MM:SS</code> without surrounding quotes as shown in the example above. If you don't set the creation time in a document header, the file modification time will be used for sorting documents in indexes.</p>
<h2>Configuration Settings</h2>
<p>Below you find configuration sections and settings:</p>

<h3>[site] section</h3>
<p>All settings in this section will be available to all templates, so names for configuration variables mustn't be used as names in document headers.</dd>
<dl>
<dt>base_path</dt>
<dd>If set will be available in all templates as base_path variable.</dd>
<dd>Use this if your site is located within a sub directory.</dd>
<dt>base_url</dt>
<dd>If set RSS feeds will be generated for each sub directory content directory.</dd>
</dl>

<h3>[templates] section</h3>
Expand Down
4 changes: 2 additions & 2 deletions logya/sites/docs/site.cfg
@@ -1,6 +1,6 @@
[site]
base_path=http://yaph.github.com/logya
#base_path=
#base_path=http://yaph.github.com/logya
base_path=

[templates]
index=index.html
Expand Down
4 changes: 2 additions & 2 deletions logya/sites/docs/templates/index.html
@@ -1,10 +1,10 @@
{% extends "base.html" %}

{% block content %}
<h1 class="index_heading">{{ title }}</h1>
<h1 class="index_heading">{{ directory }}</h1>
<ul class="index_docs">
{% for doc in index %}
<li><a href="{{ base_path }}{{ doc.url }}">{{ doc.title }}</a></li>
{% endfor %}
</ul>
{% endblock %}
{% endblock %}
3 changes: 2 additions & 1 deletion setup.py
Expand Up @@ -32,7 +32,8 @@ def get_package_data():
#test_suite='tests.all_tests',
install_requires=(
'Jinja2',
'PyYAML'
'PyYAML',
'PyRSS2Gen'
),
classifiers=[
'Development Status :: 5 - Production/Stable',
Expand Down

0 comments on commit aa3abe3

Please sign in to comment.