Skip to content

Commit

Permalink
Handle the admonitions
Browse files Browse the repository at this point in the history
  • Loading branch information
liZe committed Mar 16, 2012
1 parent d760010 commit 2df7dc6
Showing 1 changed file with 21 additions and 9 deletions.
30 changes: 21 additions & 9 deletions docutils_html5/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
#! -*- coding: utf-8 -*-
# $Id: __init__.py 6153 2009-10-05 13:37:10Z milde $
# Author: James H. Fisher <jameshfisher@gmail.com>
# Copyright: This module has been placed in the public domain.

"""
Expand Down Expand Up @@ -66,6 +64,7 @@ def date_string_parse(s):

import docutils
from docutils import nodes, writers
from docutils.transforms import writer_aux

text_content = etree.XPath("string()")

Expand All @@ -80,6 +79,9 @@ def __init__(self):
writers.Writer.__init__(self)
self.translator_class = HTML5Translator

def get_transforms(self):
return writers.Writer.get_transforms(self) + [writer_aux.Admonitions]

def translate(self):
visitor = self.translator_class(self.document)
self.document.walkabout(visitor)
Expand Down Expand Up @@ -129,6 +131,7 @@ def __init__(self, document):
self.settings.cloak_email_addresses = getattr(self.settings,
'cloak_email_addresses', False)
self._in_topic = False
self._in_admonition = False

def astext(self):
compact(self.html)
Expand Down Expand Up @@ -262,10 +265,8 @@ def add(self, name, **attrs):
def local_header(self):
# Get the appropriate header for attaching titles or docinfo
tmp = self.cur_el()
if self._in_topic:
return tmp
while True:
if tmp.tag in ("section", "article"):
if tmp.tag in ("section", "article", "aside"):
headers = tmp.xpath('header')
if len(headers) > 0:
header = headers[0]
Expand All @@ -284,7 +285,7 @@ def local_header(self):
def local_footer(self):
tmp = self.cur_el()
while True:
if tmp.tag in ("section", "article"):
if tmp.tag in ("section", "article", "aside"):
footers = tmp.xpath('footer')
if len(footers) > 0:
footer = footers[0]
Expand All @@ -306,7 +307,11 @@ def add_meta(self, attr, val):

def visit_title(self, node):
self.level += 1
title = etree.SubElement(self.local_header(), "h" + str(self.level))
if self._in_topic or self._in_admonition:
title = self.local_header()
else:
title = etree.SubElement(
self.local_header(), "h" + str(self.level))
if self.section.tag == 'article':
self.in_document_title = True
self.title_node = title
Expand Down Expand Up @@ -494,11 +499,9 @@ def visit_label(self, node):
self.visit("a", node,
id=node.parent.attributes['ids'][0],
href="#" + node.parent.attributes['backrefs'][0])
self.visit('sup', node)

def depart_label(self, node):
self.depart()
self.depart()

def wrap_in_section(self, node):
"""Wrap top level paragraphs in a section element."""
Expand Down Expand Up @@ -561,6 +564,15 @@ def depart_topic(self, node):
self.level -= 1
self.depart()

def visit_admonition(self, node):
self._in_admonition = True
self.visit('aside', node)

def depart_admonition(self, node=None):
self._in_admonition = False
self.level -= 1
self.depart()


class Tag:
def __init__(self, html_tag_name, classes=None, attribute_map={}):
Expand Down

0 comments on commit 2df7dc6

Please sign in to comment.