From aa3abe3f57c4196243e8d7055c2755485410c295 Mon Sep 17 00:00:00 2001 From: Ramiro Gomez Date: Fri, 30 Mar 2012 22:59:47 +0200 Subject: [PATCH] - RSS feed generation for all directories - make all variables in site section of configuration available to templates - renamed title template var in indexes to directory to avoid confusion --- README.markdown | 1 - logya/__init__.py | 61 ++++++++++++++++++++++++++- logya/config.py | 3 ++ logya/sites/docs/content/index.html | 9 +++- logya/sites/docs/site.cfg | 4 +- logya/sites/docs/templates/index.html | 4 +- setup.py | 3 +- 7 files changed, 75 insertions(+), 10 deletions(-) diff --git a/README.markdown b/README.markdown index e144f22..619a663 100644 --- a/README.markdown +++ b/README.markdown @@ -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 diff --git a/logya/__init__.py b/logya/__init__.py index 897e056..ac6e779 100644 --- a/logya/__init__.py +++ b/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 @@ -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. @@ -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.""" @@ -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.""" @@ -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. @@ -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) diff --git a/logya/config.py b/logya/config.py index 270e940..11fe67e 100644 --- a/logya/config.py +++ b/logya/config.py @@ -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) diff --git a/logya/sites/docs/content/index.html b/logya/sites/docs/content/index.html index 5ef5672..dd3f9a6 100644 --- a/logya/sites/docs/content/index.html +++ b/logya/sites/docs/content/index.html @@ -23,6 +23,7 @@

Features

  • Automatic generation of document indexes in created directories
  • Site configuration
  • No server side scripting security issues
  • +
  • RSS feed generation for directories
  • Requirements

    @@ -30,6 +31,7 @@

    Requirements

  • Python 2.7
  • Jinja2
  • PyYAML
  • +
  • PyRSS2Gen
  • Installation

    @@ -84,15 +86,18 @@

    Document Structure

    The header is in YAML 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.

    Document Header

    -

    Each document must specify the url 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.

    +

    Each document must specify the url and title 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.

    The only exception with a pre-defined value format is the created attribute. If you set it, it must adhere to the format YYYY-MM-DDTHH:MM:SS 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.

    Configuration Settings

    Below you find configuration sections and settings:

    [site] section

    +

    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.

    base_path
    -
    If set will be available in all templates as base_path variable.
    +
    Use this if your site is located within a sub directory.
    +
    base_url
    +
    If set RSS feeds will be generated for each sub directory content directory.

    [templates] section

    diff --git a/logya/sites/docs/site.cfg b/logya/sites/docs/site.cfg index 7c9cef3..3e1d9e2 100644 --- a/logya/sites/docs/site.cfg +++ b/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 diff --git a/logya/sites/docs/templates/index.html b/logya/sites/docs/templates/index.html index 6abf377..baa4792 100644 --- a/logya/sites/docs/templates/index.html +++ b/logya/sites/docs/templates/index.html @@ -1,10 +1,10 @@ {% extends "base.html" %} {% block content %} -

    {{ title }}

    +

    {{ directory }}

    -{% endblock %} \ No newline at end of file +{% endblock %} diff --git a/setup.py b/setup.py index 034cfab..7eff8b1 100644 --- a/setup.py +++ b/setup.py @@ -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',