Skip to content

Commit

Permalink
Generate the mimimal atom feed with no entries.
Browse files Browse the repository at this point in the history
  • Loading branch information
mblayman committed Jun 24, 2015
1 parent 71248b0 commit 999716d
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 8 deletions.
13 changes: 6 additions & 7 deletions handroll/director.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ def __init__(self, config, site, extensions):
self.catalog = catalog.TemplateCatalog(site.path)
self.composers = Composers()

def lookup_outdir(self):
@property
def outdir(self):
"""Look up the output directory based on what configuration is
available.
"""
Expand All @@ -62,8 +63,7 @@ def process_file(self, filepath):

signals.pre_composition.send(self)
dirname = os.path.dirname(filepath)
output_dirpath = self._get_output_dirpath(
dirname, self.lookup_outdir())
output_dirpath = self._get_output_dirpath(dirname, self.outdir)
self._process_file(filepath, output_dirpath, self.config.timing)
signals.post_composition.send(self)

Expand All @@ -81,8 +81,7 @@ def process_directory(self, directory):

signals.pre_composition.send(self)
dirname, basedir = os.path.split(directory)
output_dirpath = self._get_output_dirpath(
dirname, self.lookup_outdir())
output_dirpath = self._get_output_dirpath(dirname, self.outdir)
os.mkdir(os.path.join(output_dirpath, basedir))
signals.post_composition.send(self)

Expand All @@ -95,7 +94,7 @@ def is_in_output(self, path):
"""
# Because all paths should be absolute, it should be simple to find out
# if this path is in the output directory.
outdir = self.lookup_outdir()
outdir = self.outdir
source_is_in_outdir = self.site.path.startswith(outdir)
if source_is_in_outdir and path.startswith(self.site.path):
return False
Expand All @@ -105,7 +104,7 @@ def is_in_output(self, path):
def produce(self):
"""Walk the site tree and generate the output."""
signals.pre_composition.send(self)
self._generate_output(self.lookup_outdir(), self.config.timing)
self._generate_output(self.outdir, self.config.timing)
signals.post_composition.send(self)

def prune_skip_directories(self, dirnames):
Expand Down
12 changes: 12 additions & 0 deletions handroll/extensions/blog.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
# Copyright (c) 2015, Matt Layman

import os

try:
import ConfigParser as configparser
except ImportError: # pragma: no cover
import configparser

from werkzeug.contrib.atom import AtomFeed

from handroll.exceptions import AbortError
from handroll.extensions.base import Extension
from handroll.i18n import _
Expand Down Expand Up @@ -60,6 +64,14 @@ def on_frontmatter_loaded(self, source_file, frontmatter):
source_file=source_file,
))

def on_post_composition(self, director):
"""Generate the atom feed."""
feed = AtomFeed(**self.atom_metadata)
output_file = os.path.join(director.outdir, self.atom_output)
with open(output_file, 'wb') as out:
out.write(feed.to_string().encode('utf-8'))
out.write(b'<!-- handrolled for excellence -->\n')

def _add_atom_metadata(self, name, option):
"""Add atom metadata from the config parser."""
self.atom_metadata[name] = self._get_option(option)
Expand Down
2 changes: 1 addition & 1 deletion handroll/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def serve(site, director):

# The simple HTTP server is pretty dumb and does not even take a path to
# serve. The only way to serve the right path is to change the directory.
outdir = director.lookup_outdir()
outdir = director.outdir
os.chdir(outdir)

httpd = socketserver.TCPServer(('', PORT), SimpleHTTPRequestHandler)
Expand Down
16 changes: 16 additions & 0 deletions handroll/tests/test_extensions.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Copyright (c) 2015, Matt Layman
import os

import mock

Expand Down Expand Up @@ -106,6 +107,13 @@ def _add_blog_section(self, parser, exclude=None):
continue
parser.set('blog', option, value)

def _make_preprocessed_one(self, director):
"""Make an instance that has all default metadata already parsed."""
self._add_blog_section(director.config.parser)
extension = BlogExtension(director.config)
extension.on_pre_composition(director)
return extension

def test_handles_frontmatter_loaded(self):
extension = BlogExtension(None)
self.assertTrue(extension.handle_frontmatter_loaded)
Expand Down Expand Up @@ -228,3 +236,11 @@ def test_has_atom_output_in_metadata(self):
extension = BlogExtension(director.config)
extension.on_pre_composition(director)
self.assertEqual('feed.xml', extension.atom_output)

def test_post_composition_generates_feed(self):
director = self.factory.make_director()
os.mkdir(director.outdir)
extension = self._make_preprocessed_one(director)
extension.on_post_composition(director)
feed = os.path.join(director.outdir, extension.atom_output)
self.assertTrue(os.path.exists(feed))

0 comments on commit 999716d

Please sign in to comment.