13

Using rsStructuredText to generate HTML, I am trying to wrap a

paragraph with an extra div element. The must contain an "id" attribute with a value I assign. Also, the

must have a "class" attribute with "editable" value.

This is what I have so far:

.. raw:: html 

   <div id="an_identifier">

.. class:: editable                                                                 

   paragraph content

.. raw:: html

   </div>

This is the output:

<div id="an_identifier">
    <p class="editable">paragraph content</p>
</div>

I already have got the results I was looking for, although I don't like having raw HTML embedded. My question is whether is there any directive or other method in reStructuredText to achieve the same results unobtrusively?

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
nabucosound
  • 1,283
  • 1
  • 12
  • 23

4 Answers4

24

Since release 0.8 (2011-07-07), you can use the container directive with a name option:

 .. container:: test
    :name: my-id

    a paragraph

results in

  <div class="test container" id="my-id">
  a paragraph
  </div>
e9t
  • 15,534
  • 5
  • 23
  • 25
Günter Milde
  • 241
  • 2
  • 3
2

I've been just working on with something similar and I found the solution here. What you need to do is to use a custom directive and add it to an existing writer. You can simply add the directive (with small modifications) from the link to the rst2html.py script and you are all set. See also the documentation for creating directives.

Matti Pastell
  • 9,135
  • 3
  • 37
  • 44
2

If you find the added "container" class clashes with the bootstrap class of the same name (sigh), use this instead:

.. class:: class_name

    paragraph content
Gringo Suave
  • 29,931
  • 6
  • 88
  • 75
0

For those looking for an inline solution instead of the block-based ones above, you can define a new role as follows:

.. role:: my_role
   :class: my_css_class

Which can be used inline as:

Some text :my_role:`some more text with my_css_class applied`.
ThunderStruct
  • 1,504
  • 6
  • 23
  • 32