1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
|
import sphinx.locale
import docutils.statemachine
sphinx.locale.admonitionlabels['embed'] = u'' #u'Default Embedded Stage'
sphinx.locale.admonitionlabels['plugin'] = u''# u'Non-default Dynamic Plugin Stage'
sphinx.locale.admonitionlabels['streamable'] = u''# u'Streamable Stage'
def setup(app):
app.add_node(embed,
html=(visit_embed_node, depart_node),
latex=(visit_admonition, depart_node),
text=(visit_admonition, depart_node))
app.add_node(plugin,
html=(visit_plugin_node, depart_node),
latex=(visit_admonition, depart_node),
text=(visit_admonition, depart_node))
app.add_node(streamable,
html=(visit_streamable_node, depart_node),
latex=(visit_admonition, depart_node),
text=(visit_admonition, depart_node))
app.add_directive('embed', EmbedDirective)
app.add_directive('plugin', PluginDirective)
app.add_directive('streamable', StreamableDirective)
app.connect('env-purge-doc', purge_embeds)
return {'version': '0.1'} # identifies the version of our extension
from docutils import nodes
class embed(nodes.Admonition, nodes.Element):
pass
class plugin(nodes.Admonition, nodes.Element):
pass
class streamable(nodes.Admonition, nodes.Element):
pass
def visit_admonition(self, node):
self.visit_admonition(node)
def visit_embed_node(self, node):
self.body.append(self.starttag(
node, 'div', CLASS=('admonition embed')))
# self.set_first_last(node)
def visit_plugin_node(self, node):
self.body.append(self.starttag(
node, 'div', CLASS=('admonition plugin')))
# self.set_first_last(node)
def visit_streamable_node(self, node):
self.body.append(self.starttag(
node, 'div', CLASS=('admonition streamable')))
# self.set_first_last(node)
def depart_node(self, node):
self.depart_admonition(node)
from docutils.parsers.rst import Directive
from sphinx.locale import _
class EmbedDirective(Directive):
# this enables content in the directive
has_content = True
def run(self):
env = self.state.document.settings.env
targetid = "embed-%d" % env.new_serialno('embed')
targetnode = nodes.target('', '', ids=[targetid])
# self.content = 'This stage is enabled by default'
self.content = docutils.statemachine.StringList(['This stage is enabled by default'])
embed_node = embed('\n'.join(self.content))
embed_node += nodes.title(_('Default Embedded Stage'), _('Default Embedded Stage '))
self.state.nested_parse(self.content, self.content_offset, embed_node)
if not hasattr(env, 'embed_all_embeds'):
env.embed_all_embeds = []
env.embed_all_embeds.append({
'docname': env.docname,
'lineno': self.lineno,
'embed': embed_node.deepcopy(),
'target': targetnode,
})
return [targetnode, embed_node]
class PluginDirective(Directive):
# this enables content in the directive
has_content = True
def run(self):
env = self.state.document.settings.env
targetid = "plugin-%d" % env.new_serialno('plugin')
targetnode = nodes.target('', '', ids=[targetid])
# self.content = 'This stage requires a dynamic plugin to operate'
self.content = docutils.statemachine.StringList(['This stage requires a dynamic plugin to operate'])
plugin_node = plugin('\n'.join(self.content))
plugin_node += nodes.title(_('Dynamic Plugin'), _('Dynamic Plugin'))
self.state.nested_parse(self.content, self.content_offset, plugin_node)
if not hasattr(env, 'plugin_all_plugins'):
env.plugin_all_plugins = []
env.plugin_all_plugins.append({
'docname': env.docname,
'lineno': self.lineno,
'plugin': plugin_node.deepcopy(),
'target': targetnode,
})
return [targetnode, plugin_node]
class StreamableDirective(Directive):
# this enables content in the directive
has_content = True
def run(self):
env = self.state.document.settings.env
targetid = "streamable-%d" % env.new_serialno('streamable')
targetnode = nodes.target('', '', ids=[targetid])
# self.content = 'This stage supports streaming operations'
self.content = docutils.statemachine.StringList(['This stage supports streaming operations'])
streamable_node = streamable('\n'.join(self.content))
streamable_node += nodes.title(_('Streamable Stage'), _('Streamable Stage'))
self.state.nested_parse(self.content, self.content_offset, streamable_node)
if not hasattr(env, 'streamable_all_streamable'):
env.streamable_all_streamable = []
env.streamable_all_streamable.append({
'docname': env.docname,
'lineno': self.lineno,
'plugin': streamable_node.deepcopy(),
'target': targetnode,
})
return [targetnode, streamable_node]
def purge_embeds(app, env, docname):
if not hasattr(env, 'embed_all_embeds'):
return
env.embed_all_embeds = [embed for embed in env.embed_all_embeds
if embed['docname'] != docname]
if not hasattr(env, 'plugin_all_plugins'):
return
env.plugin_all_plugins = [embed for embed in env.plugin_all_plugins
if embed['docname'] != docname]
if not hasattr(env, 'streamable_all_streamable'):
return
env.streamable_all_streamable = [embed for embed in env.streamable_all_streamable
if embed['docname'] != docname]
|