Source code for pyquickhelper.sphinxext.sphinx_bigger_extension

# -*- coding: utf-8 -*-
"""
Defines a :epkg:`sphinx` extension to display bigger text.


:githublink:`%|py|6`
"""
import html as cgiesc
import sphinx
from docutils import nodes
from sphinx.util.logging import getLogger


[docs]class bigger_node(nodes.Element): """ Defines *bigger* node. :githublink:`%|py|16` """ pass
[docs]def bigger_role(role, rawtext, text, lineno, inliner, options=None, content=None): """ Defines custom role *bigger*. The following instructions defines buttons of size 20 (:bigger:`text`): :: :bigger:`text` Or to specify a different :bigger:`::5:size` : :: :bigger:`::5:size` :param role: The role name used in the document. :param rawtext: The entire markup snippet, with role. :param text: The text marked with the role. :param lineno: The line number where rawtext appears in the input. :param inliner: The inliner instance that called us. :param options: Directive options for customization. :param content: The directive content for customization. :githublink:`%|py|42` """ if options is None: options = {} if content is None: content = [] if text.startswith("::"): size, text = text[2:].split(':') else: size = "4" node = bigger_node(text=text, size=size) node['classes'] += ["bigger"] node['bigger'] = node return [node], []
[docs]def depart_bigger_node_html(self, node): """ what to do when leaving a node *bigger* the function should have different behaviour, depending on the format, or the setup should specify a different function for each. It does only html for the time being. :githublink:`%|py|65` """ self.body.append( '<font size="{1}">{0}</font>'.format(cgiesc.escape(node["text"]), node["size"]))
[docs]def visit_bigger_node_rst(self, node): """ what to do when visiting a node *bigger* the function should have different behaviour, depending on the format, or the setup should specify a different function for each. :githublink:`%|py|76` """ self.add_text(':bigger:`') self.add_text(node["text"])
[docs]def depart_bigger_node_rst(self, node): """ depart bigger_node for rst :githublink:`%|py|84` """ self.add_text('`')
[docs]def visit_bigger_node_latex(self, node): """ what to do when visiting a node *bigger* the function should have different behaviour, depending on the format, or the setup should specify a different function for each. :githublink:`%|py|94` """ self.add_text('\\huge{') self.add_text(node["text"])
[docs]def depart_bigger_node_latex(self, node): """ depart bigger_node for latex :githublink:`%|py|102` """ self.add_text('}')
[docs]def visit_bigger_node(self, node): """ what to do when visiting a node *bigger* the function should have different behaviour, depending on the format, or the setup should specify a different function for each. :githublink:`%|py|112` """ pass
[docs]def depart_bigger_node(self, node): """ depart bigger_node for format other than html :githublink:`%|py|119` """ logger = getLogger("bigger") logger.warning("[depart_bigger_node] output only available for HTML not for '{0}'".format( type(self)))
[docs]def setup(app): """ setup for ``bigger`` (sphinx) :githublink:`%|py|128` """ if hasattr(app, "add_mapping"): app.add_mapping('bigger', bigger_node) app.add_node(bigger_node, html=(visit_bigger_node, depart_bigger_node_html), epub=(visit_bigger_node, depart_bigger_node_html), latex=(visit_bigger_node_latex, depart_bigger_node_latex), elatex=(visit_bigger_node_latex, depart_bigger_node_latex), text=(visit_bigger_node, depart_bigger_node), md=(visit_bigger_node, depart_bigger_node), rst=(visit_bigger_node_rst, depart_bigger_node_rst)) app.add_role('bigger', bigger_role) return {'version': sphinx.__display_version__, 'parallel_read_safe': True}