# vim: set fileencoding=utf8 :
"""Some hooks that might be useful."""

from StringIO import StringIO
import logging

from wok.exceptions import DependencyException
from wok.util import slugify

try:
    from lxml import etree
except ImportError:
    etree = None


class HeadingAnchors(object):
    """
    Put some paragraph heading anchors.

    Serves as a 'page.template.post' wok hook.
    """

    def __init__(self, max_heading=3):
        if not etree:
            raise DependencyException('To use the HeadingAnchors hook, you must '
                                      'install the library lxml.')
        self.max_heading = max_heading
        logging.debug('Loaded hook HeadingAnchors')

    def __call__(self, page):
        logging.debug('Called hook HeadingAnchors on {0}'.format(page))
        parser = etree.HTMLParser()
        sio_source = StringIO(page.rendered)
        tree = etree.parse(sio_source, parser)

        for lvl in range(1, self.max_heading+1):
            headings = tree.iterfind('//h{0}'.format(lvl))
            for heading in headings:
                if not heading.text:
                    continue
                logging.debug('[HeadingAnchors] {0} {1}'.format(heading, heading.text))
                anchor = etree.Element('a')
                anchor.set('class', 'heading_anchor')
                anchor.set('href', '#heading-{0}'.format(slugify(heading.text)))
                anchor.set('name', 'heading-{0}'.format(slugify(heading.text)))
                anchor.text = u'¶'
                heading.append(anchor)

        sio_destination = StringIO()
        tree.write(sio_destination)
        page.rendered = sio_destination.getvalue()

@@ -1224,16 +1224,84 @@
 g.text))
+%0A%0A                name = 'heading-%7B0%7D'.format(slugify(heading.text))
 %0A       
@@ -1432,50 +1432,16 @@
 , '#
-heading-%7B0%7D'.format(slugify(heading.text))
+' + name
 )%0A  
@@ -1470,58 +1470,44 @@
 et('
-nam
+titl
 e', '
-heading-%7B0%7D'.format(slugify(heading.text))
+Permalink to this section.'
 )%0A  
@@ -1575,24 +1575,65 @@
 nd(anchor)%0A%0A
+                heading.set('id', name)%0A%0A
         sio_

Make wok.contrib.hooks.HeadingAnchors work right.