Last active
August 29, 2015 14:03
-
-
Save shidarin/575d93c80cf60af68758 to your computer and use it in GitHub Desktop.
Sphinx Docutils Bootstrap Directives - Directives for some common Sphinx paragraph types
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from docutils import nodes | |
from docutils.parsers.rst import directives, Directive | |
PARAGRAPH_CODE = """ | |
<div class="panel {panel_type}"> | |
<div class="panel-heading"> | |
<h3 class="panel-title">{panel_title}</h3> | |
</div> | |
<div class="panel-body"> | |
<p class="panel-text">{panel_content}</p> | |
</div> | |
</div>""" | |
class Note(Directive): | |
""" Block for aside notes.""" | |
required_arguments = 0 | |
optional_arguments = 0 | |
final_argument_whitespace = True | |
has_content = True | |
def run(self): | |
self.assert_has_content() | |
content_text = u'\n'.join(self.content) | |
if u'\n\n' in content_text: | |
content_text = content_text.replace( | |
u'\n\n', u'\n</p><p class="panel-text">\n' | |
) | |
code = PARAGRAPH_CODE.format( | |
panel_type='panel-primary', | |
panel_title='Note', | |
panel_content=content_text | |
) | |
return [nodes.raw('', code, format='html')] | |
class Warning(Directive): | |
""" Block for aside warnings.""" | |
required_arguments = 0 | |
optional_arguments = 0 | |
final_argument_whitespace = True | |
has_content = True | |
def run(self): | |
self.assert_has_content() | |
content_text = u'\n'.join(self.content) | |
if u'\n\n' in content_text: | |
content_text = content_text.replace( | |
u'\n\n', u'\n</p><p class="panel-text">\n' | |
) | |
code = PARAGRAPH_CODE.format( | |
panel_type='panel-danger', | |
panel_title='Warning', | |
panel_content=content_text | |
) | |
return [nodes.raw('', code, format='html')] | |
class VersionAdded(Directive): | |
""" Block for version added info.""" | |
required_arguments = 1 | |
optional_arguments = 0 | |
final_argument_whitespace = False | |
has_content = True | |
def run(self): | |
self.assert_has_content() | |
content_text = u'\n'.join(self.content) | |
if u'\n\n' in content_text: | |
content_text = content_text.replace( | |
u'\n\n', u'\n</p><p class="panel-text">\n' | |
) | |
code = PARAGRAPH_CODE.format( | |
panel_type='panel-success', | |
panel_title='New in Version: <i>{ver}</i>'.format(ver=self.arguments[0]), | |
panel_content=content_text | |
) | |
return [nodes.raw('', code, format='html')] | |
class VersionChanged(Directive): | |
""" Block for version changed notes.""" | |
required_arguments = 1 | |
optional_arguments = 0 | |
final_argument_whitespace = False | |
has_content = True | |
def run(self): | |
self.assert_has_content() | |
content_text = u'\n'.join(self.content) | |
if u'\n\n' in content_text: | |
content_text = content_text.replace( | |
u'\n\n', u'\n</p><p class="panel-text">\n' | |
) | |
code = PARAGRAPH_CODE.format( | |
panel_type='panel-info', | |
panel_title='Changed in Version: <i>{ver}</i>'.format(ver=self.arguments[0]), | |
panel_content=content_text | |
) | |
return [nodes.raw('', code, format='html')] | |
class Deprecated(Directive): | |
""" Block for deprecated warnings.""" | |
required_arguments = 1 | |
optional_arguments = 0 | |
final_argument_whitespace = False | |
has_content = True | |
def run(self): | |
self.assert_has_content() | |
content_text = u'\n'.join(self.content) | |
if u'\n\n' in content_text: | |
content_text = content_text.replace( | |
u'\n\n', u'\n</p><p class="panel-text">\n' | |
) | |
code = PARAGRAPH_CODE.format( | |
panel_type='panel-warning', | |
panel_title='Deprecated in Version: <i>{ver}</i>'.format(ver=self.arguments[0]), | |
panel_content=content_text | |
) | |
return [nodes.raw('', code, format='html')] | |
directives.register_directive('note', Note) | |
directives.register_directive('warning', Warning) | |
directives.register_directive('versionadded', VersionAdded) | |
directives.register_directive('versionchanged', VersionChanged) | |
directives.register_directive('deprecated', Deprecated) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment