Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use the only directive inline by using role in python-sphinx?

In python-sphinx there is the only directive, which can be used to conditionally influence the document according to its output. For instance text appears in html or latex only.

It is use like this:

.. only:: not latex

   Here there is some Text, that does not appear in latex output.

.. only:: html

   Here there is some Text, that only appears in html output.

How can I use the role directive in the right way to use the only-class inline, let's say like this:

Here there is some Text, that :only-notlatex:`does not appear in latex`
:only-html:`only appears in html`.

I saw something similar for the raw directive. Is this also possible for the only directive? I tried:


.. role:: only-html(only)
   :format: html

.. role:: only-notlatex(only)
   :format: not latex 

But this does not work.

like image 499
Andreas Gschossmann Avatar asked Apr 12 '15 12:04

Andreas Gschossmann


People also ask

What is a role in Sphinx?

Sphinx uses interpreted text roles to insert semantic markup into documents. They are written as :rolename:`content` . Note. The default role ( `content` ) has no special meaning by default.

What is a directive Sphinx?

A directive (ref) is a generic block of explicit markup. Along with roles, it is one of the extension mechanisms of reST, and Sphinx makes heavy use of it. Docutils supports the following directives: Admonitions: attention, caution, danger, error, hint, important, note, tip, warning and the generic admonition.


1 Answers

Directives work on blocks of text (whole paragraphs); roles are for inline text (within paragraphs).

You mentioned raw, and there is indeed both a directive and a role with that name, for "raw data pass-through".

But there is no built-in role that is the inline equivalent of the only directive. You will have to create your own custom role for this purpose. I can't provide any detailed instructions, but here is an article that can help you get started: http://doughellmann.com/2010/05/09/defining-custom-roles-in-sphinx.html.

like image 176
mzjn Avatar answered Oct 08 '22 22:10

mzjn