Skip to content

Commit

Permalink
posts can now be in markdown syntax
Browse files Browse the repository at this point in the history
drafts of posts can be put in _drafts and optionally rendered using the -d flag.
  • Loading branch information
EnigmaCurry committed Mar 9, 2009
1 parent 147cbe5 commit 2e6232c
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 18 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -4,3 +4,4 @@
/Blogofile.egg-info/*
/build/*
/dist/*
/blogofile/.ropeproject*
13 changes: 10 additions & 3 deletions blogofile/__init__.py
Expand Up @@ -21,8 +21,6 @@
"""

__author__ = "Ryan McGuire (ryan@enigmacurry.com)"
__date__ = "Tue Feb 3 12:52:52 2009"
__version__ = "0.1"

import ConfigParser
import os
Expand All @@ -43,6 +41,9 @@ def main():
parser.add_option("-b","--build",dest="do_build",
help="Build the blog again from source",
default=False, action="store_true")
parser.add_option("-d","--include-drafts",dest="include_drafts",
default=False, action="store_true",
help="Writes permapages for drafts (but not in feeds or chronlogical blog)")
(options, args) = parser.parse_args()

#load config
Expand All @@ -56,8 +57,14 @@ def main():
sys.exit(1)

posts = post.parse_posts("_posts", timezone=config.get("blogofile","timezone"))
if options.include_drafts:
drafts = post.parse_posts("_drafts", timezone=config.get("blogofile","timezone"))
for p in drafts:
p.draft = True
else:
drafts = None
writer = Writer(output_dir=os.path.join(config_dir,"_site"), config=config)
writer.write_blog(posts)
writer.write_blog(posts, drafts)

if __name__ == '__main__':
main()
26 changes: 16 additions & 10 deletions blogofile/post.py
Expand Up @@ -17,9 +17,13 @@
import pytz
import yaml
import textile
import markdown

date_format = "%Y/%m/%d %H:%M:%S"

class PostFormatException(Exception):
pass

class Post:
"""
Class to describe a blog post and associated metadata
Expand Down Expand Up @@ -57,10 +61,10 @@ def __init__(self, source, timezone):
self.tags = set()
self.permalink = None
self.content = u""
self.draft = False
self.format = "html"
self.author = ""
self.guid = None #Default guid is permalink
self.draft = False
self.__parse()

def __repr__(self):
Expand All @@ -81,9 +85,12 @@ def __parse(self):
#Convert post to HTML
if self.format == "textile":
self.content = textile.textile(post_src).decode("utf-8")
else:
#Assume it's raw html to begin with
elif self.format == "markdown":
self.content = markdown.markdown(post_src).decode("utf-8")
elif self.format == "html":
self.content = post_src.decode("utf-8")
else:
raise PostFormatException("Post format '%s' not recognized." % self.format)

def __parse_yaml(self, yaml_src):
y = yaml.load(yaml_src)
Expand Down Expand Up @@ -131,17 +138,16 @@ def parse_posts(directory, timezone):
Returns a list of the posts sorted in reverse by date."""
posts = []
textile_files = [f for f in os.listdir(directory) if f.endswith(".textile")]
for texi in textile_files:
src = open(os.path.join(directory,texi)).read()
post_filename_re = re.compile(".*((\.textile$)|(\.markdown$)|(\.html$))")
post_file_names = [f for f in os.listdir(directory) if post_filename_re.match(f)]
for post_fn in post_file_names:
src = open(os.path.join(directory,post_fn)).read()
p = Post(src, timezone)
#Exclude some posts
if not (p.draft == True or p.permalink == None):
if not (p.permalink == None):
posts.append(p)
posts.sort(key=operator.attrgetter('date'), reverse=True)
return posts


return posts

if __name__ == '__main__':
import doctest
Expand Down
15 changes: 15 additions & 0 deletions blogofile/util.py
@@ -0,0 +1,15 @@
html_escape_table = {
"&": "&",
'"': """,
"'": "'",
">": ">",
"<": "&lt;",
}

def html_escape(text):
"""Produce entities within text."""
L=[]
for c in text:
L.append(html_escape_table.get(c,c))
return "".join(L)

8 changes: 5 additions & 3 deletions blogofile/writer.py
Expand Up @@ -37,14 +37,16 @@ def __init__(self, output_dir, config):
self.files_exclude_regex = re.compile("(^_.*)|(^#.*)|(^.*~$)")
self.dirs_exclude_regex = re.compile("(^\.git)|(^\.hg)|(^\.bzr)|(^\.svn)|(^\CVS)")

def write_blog(self, posts):
def write_blog(self, posts, drafts=None):
self.archive_links = self.__get_archive_links(posts)
self.all_categories = self.__get_all_categories(posts)
self.category_link_names = self.__compute_category_link_names(self.all_categories)
self.__setup_output_dir()
self.__write_files(posts)
self.__write_blog_chron(posts)
self.__write_blog_permanent(posts)
self.__write_permapage(posts)
if drafts:
self.__write_permapage(drafts)
self.__write_monthly_archives(posts)
self.__write_blog_categories(posts)
self.__write_feed(posts, "/feed", "rss.mako")
Expand Down Expand Up @@ -224,7 +226,7 @@ def __write_monthly_archives(self, posts):
for link, posts in m.items():
self.__write_blog_chron(posts,root=link)

def __write_blog_permanent(self, posts):
def __write_permapage(self, posts):
"""Write blog posts to their permalink locations"""
perma_template = self.template_lookup.get_template("permapage.mako")
perma_template.output_encoding = "utf-8"
Expand Down
5 changes: 3 additions & 2 deletions setup.py
@@ -1,7 +1,7 @@
from setuptools import setup

setup(name='Blogofile',
version='0.3',
version='0.3.1',
description='A blog engine/compiler, inspired by Jekyll.',
author='Ryan McGuire',
author_email='ryan@enigmacurry.com',
Expand All @@ -13,7 +13,8 @@
'BeautifulSoup',
'pytz',
'pyyaml',
'textile'],
'textile',
'markdown'],
entry_points="""
[console_scripts]
blogofile = blogofile:main
Expand Down

0 comments on commit 2e6232c

Please sign in to comment.