Help ! : Checkbox Extension

73 views
Skip to first unread message

Carlos R

unread,
Sep 20, 2019, 11:33:45 AM9/20/19
to sphinx-users
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

Capture.PNG













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





checkbox.py

Carlos R

unread,
Sep 23, 2019, 7:29:50 AM9/23/19
to sphinx-users
Well, I found  a solution here

class CheckboxDirective(SphinxDirective):

    has_content = True

    def run(self):
        targetid = 'checkbox-%d' % self.env.new_serialno('checkbox')
        targetnode = nodes.target('', '', ids=[targetid])

        checkbox_node = checkbox(self.content)

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

        return [targetnode, checkbox_node]


def html_checkbox(self, node):
    template= """
        <div class=\"checkbox\"><input type=\"checkbox\" ><label>
        %(content)s
        </label></div>
    """
    print(str(node))
    self.body.append(template%{'content':str(node)})
    raise nodes.SkipNode

[...]

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

    app.add_node(checkbox,
                html=(html_checkbox, 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

Hope it helps someone :) 

Carlos

Komiya Takeshi

unread,
Oct 7, 2019, 12:46:14 PM10/7/19
to sphinx...@googlegroups.com
Just FYI, 

nested_parse() usually generates paragraph node on parsing content body.
It means the checkbox directive generates such document tree:

   <checkbox><paragraph>item 1</paragraph></checkbox>

And it will be converted <p> tag in HTML output. This is why the lines are folded.

2019年9月21日(土) 0:33 Carlos R <alien31...@gmail.com>:
--
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...@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.
Reply all
Reply to author
Forward
0 new messages