Hi you all, 

I am willing to create an Sphinx Extension that recognizes the directive 
*    .. checkbox::  some_text_here_for_the_item * 

in order to create a html checkbox preceding the text in the page.

I followed this tutorial (
https://www.sphinx-doc.org/en/master/development/tutorials/todo.html) and 
managed to have this (code .py attached): 


4.  Checkboxes
----------------------
.. checkbox:: item 1
.. checkbox:: item 2
.. checkbox:: item 3


[image: Capture.PNG] <about:invalid#zClosurez>













The thing that I would like to have is every element in a line in a single 
line, and not that 

Could someone tell me if there would be a way of modifying the way sphinx 
represents the nodes of each directive ?
Do you have any other workaround ? 

Thank you and have a great day !

Carlos





-- 
You received this message because you are subscribed to the Google Groups 
"sphinx-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sphinx-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sphinx-users/b1f6dc0b-08ae-428e-b8fb-fe4754c9e0fd%40googlegroups.com.
from docutils import nodes
from docutils.parsers.rst import Directive

from sphinx.locale import _
from sphinx.util.docutils import SphinxDirective

class checkbox(nodes.Admonition, nodes.Element):
    pass

def visit_checkbox_node(self, node):
    self.body.append("<div><input type=\"checkbox\" ><label>")


def depart_checkbox_node(self, node):
    self.body.append("</label></div>")


class CheckboxDirective(SphinxDirective):

    has_content = True

    def run(self):
        print(">> CheckboxDirective !!\n")
        targetid = 'checkbox-%d' % self.env.new_serialno('checkbox')
        targetnode = nodes.target('', '', ids=[targetid])

        checkbox_node = checkbox(self.content)

        print(self.content_offset)
        self.state.nested_parse(self.content, self.content_offset, checkbox_node)

        return [targetnode, checkbox_node]


def purge_cbs(app, env, docname):
    if not hasattr(env, 'checkbox_all_checkboxes'):
        return

    env.checkbox_all_checkboxes = [cb for cb in env.checkbox_all_checkboxes
                                    if cb['docname'] != docname]


def process_checkbox_nodes(app, doctree, fromdocname):
    if not app.config.include_checkbox:
        for node in doctree.traverse(checkbox):
            node.parent.remove(checkbox)

    env = app.builder.env

    for node in doctree.traverse(checkbox):
        if not app.config.include_checkbox:
            node.replace_self([])
            continue

        content = []

        para = nodes.paragraph()
        content.append(node)
        content.append(para)

        node.replace_self(content)


def setup(app):
    app.add_config_value('include_checkbox', False, 'html')

    app.add_node(checkbox,
                html=(visit_checkbox_node, depart_checkbox_node),
                text=(visit_checkbox_node, depart_checkbox_node),
                latex=(visit_checkbox_node, depart_checkbox_node))
    app.add_directive('checkbox', CheckboxDirective)
    app.connect('doctree-resolved', process_checkbox_nodes)
    app.connect('env-purge-doc', purge_cbs)

    return {'version' : '0.1'}  # identifies the version of our extension

Reply via email to