Skip to content

Commit

Permalink
Encode non-ASCII characters for HTML files
Browse files Browse the repository at this point in the history
  • Loading branch information
mdiep committed Apr 10, 2011
1 parent 9374005 commit 492caf4
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion fireproof.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,23 @@

VERSION = '0.0.0'

import codecs
from htmlentitydefs import codepoint2name

# register a codec to handle escaping non-ASCII characters
def named_entities(text):
if isinstance(text, (UnicodeEncodeError, UnicodeTranslateError)):
s = []
for c in text.object[text.start:text.end]:
if ord(c) in codepoint2name:
s.append(u'&%s;' % codepoint2name[ord(c)])
else:
s.append(u'&#%s;' % ord(c))
return ''.join(s), text.end
else:
raise TypeError("Can't handle %s" % text.__name__)
codecs.register_error('named_entities', named_entities)

class Page(object):
def __init__(self, site, path):
self.site = site
Expand All @@ -26,7 +43,7 @@ def __init__(self, site, path):

for key, value in yaml.load(data).items():
setattr(self, key, value)

self.text = markdown2.markdown(text)

def __str__(self):
Expand Down Expand Up @@ -123,6 +140,7 @@ def render_to_dir(self, output_dir):
env = jinja2.Environment(loader=loader)

for type in self.pages:
ext = self.page_exts[type]
for page in self.pages[type]:
template = type + self.page_exts[page.type]
template = env.get_template(template)
Expand All @@ -133,6 +151,8 @@ def render_to_dir(self, output_dir):
'site': self,
}
for line in template.stream(**context):
if ext == '.html':
line = line.encode('ascii', 'named_entities')
stream.write(line)

def main():
Expand Down

0 comments on commit 492caf4

Please sign in to comment.