Create a gist now

Instantly share code, notes, and snippets.

sphinxcontrib_fancybulletlist
Source
=======
* item 1
* item 1-1
* item 1-2
* item 2
Before: without ext.
=====================
<bullet_list bullet="*">
<list_item>
<definition_list>
<definition_list_item>
<term>
item 1</term>
<definition>
<bullet_list bullet="*">
<list_item>
<paragraph>
item 1-1</paragraph>
</list_item>
<list_item>
<paragraph>
item 1-2</paragraph>
</list_item>
</bullet_list>
</definition>
</definition_list_item>
</definition_list>
</list_item>
<list_item>
<paragraph>
item 2</paragraph>
</list_item>
</bullet_list>
After: with ext.
=================
<bullet_list bullet="*">
<list_item>
<paragraph>
item 1</paragraph>
<bullet_list bullet="*">
<list_item>
<paragraph>
item 1-1</paragraph>
</list_item>
<list_item>
<paragraph>
item 1-2</paragraph>
</list_item>
</bullet_list>
</list_item>
<list_item>
<paragraph>
item 2</paragraph>
</list_item>
</bullet_list>
Note
=====
The generated doctree using the extension is same as well-formed reST bullet list::
* item 1
* item 1-1
* item 1-2
* item 2
# -*- coding: utf-8 -*-
"""
sphinxcontrib_fancybulletlist
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Convert nested bullet list without blank line to correct one.
:copyright: Copyright 2012 by Takeshi Komiya.
:license: BSDL.
"""
from docutils import nodes, transforms
class FancyBulletListTransform(transforms.Transform):
default_priority = 500
def apply(self):
for target in self.document.traverse(nodes.list_item):
if self.is_definition_bullet_list(target):
string = target[0][0][0][0]
bullet_list = target[0][0][1][0]
target.clear()
target.append(nodes.paragraph(text=string))
target.append(bullet_list)
def is_definition_bullet_list(self, node):
try:
if (len(node) == 1 and
isinstance(node[0], nodes.definition_list) and
isinstance(node[0][0], nodes.definition_list_item) and
len(node[0][0]) == 2 and
isinstance(node[0][0][0], nodes.term) and
isinstance(node[0][0][1], nodes.definition) and
isinstance(node[0][0][1][0], nodes.bullet_list)):
return True
except:
pass
return False
def setup(app):
app.add_transform(FancyBulletListTransform)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment