Skip to content
Browse files

ContentNode instances now responsible for loading their own content

…giving the chance for subclasses to override the get_content method for their own means. 

Speaking of subclasses, the visitor pattern implementation for the content hierarchy now find the most specific visitXXX() method based on the entire class hierarchy rather than just looking at the leaf class name. This means that it no longer falls back to the visit() method when subclassing a ContentNode or DirectoryNode, which would break generation.
  • Loading branch information...
1 parent f0a82a6 commit 5a8f96f14145ee486b900debd250ec253cc438a7 Steve Webster committed Nov 27, 2011
Showing with 20 additions and 9 deletions.
  1. +1 −4 lanyon/__init__.py
  2. +19 −5 lanyon/structure.py
View
5 lanyon/__init__.py
@@ -158,10 +158,7 @@ def visitContentNode( self, node ):
if node_content_processors:
print "# Processing: /" + node.path
try:
- # Read content from content file
- content_file = codecs.open( content_path, 'r', 'utf-8' )
- content = content_file.read()
- content_file.close()
+ content = node.get_content( self.config['content_path'] )
# Apply registered processors to content
for processor in node_content_processors:
View
24 lanyon/structure.py
@@ -1,4 +1,4 @@
-import os
+import os, codecs, inspect
import lanyon.glob
class SiteNode( object ):
@@ -14,10 +14,13 @@ def relative_path_to( self, descendant ):
return path
def visit( self, visitor ):
- method = 'visit' + self.__class__.__name__;
- if not hasattr( visitor, method ) or not callable( getattr( visitor, method ) ):
- method = 'visit'
- getattr( visitor, method )( self )
+ class_hierarchy = inspect.getmro(self.__class__)
+ visit_methods = ['visit' + x.__name__ for x in class_hierarchy]
+ for method in visit_methods:
+ if hasattr( visitor, method ) and callable( getattr( visitor, method ) ):
+ return getattr( visitor, method )( self )
+
+ return visitor.visit(self);
class DirectoryNode( SiteNode ):
@@ -74,6 +77,17 @@ def _set_output_path( self, path ):
output_path = property( _get_output_path, _set_output_path )
+ #TODO: Find a cleaner way for content nodes to know how to find their
+ # underlying files.
+ def get_content( self, content_path ):
+ # Read content from content file
+ file_path = os.path.abspath(os.path.join( content_path, self.path ))
+ content_file = codecs.open( file_path, 'r', 'utf-8' )
+ content = content_file.read()
+ content_file.close()
+
+ return content
+
class SiteNodeVisitor( object ):
def __init__( self, config ):

0 comments on commit 5a8f96f

Please sign in to comment.
Something went wrong with that request. Please try again.