def _build(self, mo, element_store, environ): heading_tag = self.tags[len(mo.group(1))-1] heading_text = mo.group(2) slug = _slugify(heading_text, default='heading') if environ is not None: headings = environ.setdefault('headings', []) i = 1 original_slug = slug while True: if slug not in headings: headings.append(slug) break slug = '%s_%d' % (original_slug, i) i += 1 toc = environ.setdefault('toc', []) toc.append((heading_tag, escape(heading_text), slug)) # The heading itself. text = creoleparser.core.fragmentize(heading_text, self.child_elements, element_store, environ) # The anchor and link to the anchor. anchor = genshi.builder.tag.span(id=slug) anchor_link = genshi.builder.tag.a(genshi.core.Markup('¶'), class_='headerlink', href='#' + slug, title=escape(_('Permalink to this headline'))) heading = genshi.builder.tag.__getattr__(heading_tag) return heading([text, anchor, anchor_link], class_='heading')
def get_toc(data): """A very rudimentar TOC creator. No nested lists. Simple.""" if len(data) <= 1: return '' res = u'<ul class="toc">' for level, title, anchor in data: res += u'<li class="toc-%s"><a href="#%s">%s</a></li>' % (level, anchor, escape(title)) return res + u'</ul>'