Permalink
Browse files

Add anchors on headlines

  • Loading branch information...
1 parent 2964e77 commit 0e0075ef82d887cc928db5dcc5bf0d0576abbf18 @emesik emesik committed Jan 14, 2011
Showing with 8 additions and 2 deletions.
  1. +5 −2 djiki/parser.py
  2. +3 −0 djiki/utils.py
View
@@ -10,8 +10,11 @@ class DjikiHtmlEmitter(HtmlEmitter):
image_params_re = re.compile(r'^(?:(?P<size>[0-9]+x[0-9]+)(?:\||$))?(?P<title>.*)$')
def header_emit(self, node):
- return u'<h%d>%s</h%d>\n' % (
- node.level + 1, self.html_escape(node.content), node.level)
+ return u'<a name="%s"></a><h%d>%s</h%d>\n' % (
+ utils.anchorize(node.content),
+ node.level + 1,
+ self.html_escape(node.content),
+ node.level)
def link_emit(self, node):
target = node.content
View
@@ -10,3 +10,6 @@ def deurlize_title(title):
if settings.DJIKI_SPACES_AS_UNDERSCORES:
return re.sub(r'[_\s]+', ' ', title)
return title
+
+def anchorize(txt):
+ return re.compile(r'[^\w_,\.-]+', re.UNICODE).sub('_', txt).strip('_')

0 comments on commit 0e0075e

Please sign in to comment.