Inline Code & Textroles

ヒント

Too much inline code can make the information on a page highly unreadable. If this is the case, consider using Codeblocks with Syntax Highlighting.

How to Semantically Markup Specific Text

There are several ways to semantically mark specific parts of the text. The main goal is to be able to use a consistent style for specific parts of the text, for example code fragments, file names and GUI elements.

Use Textroles

  1. Preferred: Use Sphinx interpreted text roles to explicitly specify what kind of text / code (= textrole) it is. This shows the semantics and in the output there may be a a special coloring or highlighting:

    Role Source Output Note
    (default) `result = (1 + x) * 32` result = (1 + x) * 32 This works because in Includes.txt we set the default role to :code:`...`
    aspect :aspect:`Description:` Description: For better optics
    html :html:`<a href="#">` <a href="#">  
    issue :issue:`12345` :issue:`12345` To link to a TYPO3 issue.
    js :js:`var f = function () {return 1;}` var f = function () {return 1;}  
    php :php:`$result = $a + 23;` $result = $a + 23;  
    sep :sep:`|` | To give the separator '|' a special style in some contexts like Definition Lists
    ts :ts:`lib.hello.value = Hello World!` lib.hello.value = Hello World!  
    typoscript :typoscript:`lib.hello.value = Hello World!` lib.hello.value = Hello World!  
    yaml :yaml:`- {name: John Smith, age: 33}` - {name: John Smith, age: 33}  

    Standard Sphinx interpreted text roles:

    Role Source Output Note
    abbr :abbr:`LIFO (last-in, first-out)` LIFO An abbreviation. If the role content contains a parenthesized explanation, it will be treated specially: it will be shown in a tool-tip in HTML, and output only once in LaTeX.
    code :code:`result = (1 + x) * 32` result = (1 + x) * 32  
    command :command:`rm` rm The name of an OS-level command, such as rm.
    dfn :dfn:`something` something Mark the defining instance of a term in the text. (No index entries are generated.)
    file :file:`/etc/passwd` /etc/passwd  
    guilabel :guilabel:`&Cancel`, :guilabel:`O&k`, :guilabel:`&Reset`, :guilabel:`F&&Q` Cancel, Ok, Reset, F&Q Labels presented as part of an interactive user interface should be marked using guilabel. This includes labels from text-based interfaces such as those created using curses or other text-based libraries. Any label used in the interface should be marked with this role, including button labels, window titles, field names, menu and menu selection names, and even values in selection lists.
    kbd Press :kbd:`ctrl` + :kbd:`s` Press ctrl + s Mark a sequence of keystrokes. What form the key sequence takes may depend on platform- or application-specific conventions. When there are no relevant conventions, the names of modifier keys should be spelled out, to improve accessibility for new users and non-native speakers. For example, an xemacs key sequence may be marked like C + :kbd`x`, C + f, but without reference to a specific application or platform, the same sequence should be marked as ctrl + x, ctrl + f.
    mailheader :mailheader:`Content-Type` Content-Type The name of an RFC 822-style mail header. This markup does not imply that the header is being used in an email message, but can be used to refer to any header of the same “style.” This is also used for headers defined by the various MIME specifications. The header name should be entered in the same way it would normally be found in practice, with the camel-casing conventions being preferred where there is more than one common usage.
    ref :ref:`Inline-Code` Inline Code & Textroles Sphinx cross-referencing

    Standard Docutils interpreted text roles:

    ちなみに

    For more information about the ref directive, see Hyperlinks & Cross-referencing.

    Role Source Output Note
    emphasis :emphasis:`text`, *text* text, text  
    literal :literal:`\ \ abc` abc  
    literal :literal:`text`, ''text'' (backticks!) text, text  
    math :math:`A_\text{c} = (\pi/4) d^2` \(A_\text{c} = (\pi/4) d^2\) The math role marks its content as mathematical notation (inline formula). The input format is LaTeX math syntax without the “math delimiters“ ($ $).
    rfc, rfc-reference :RFC:`2822` RFC 2822  
    strong :strong:`text`, **text** text, text Implements strong emphasis.
    subscript :subscript:`subscripted` subscripted  
    superscript :superscript:`superscripted` superscripted  
    t, title-reference :t:`Design Patterns` Design Patterns The :title-reference: role is used to describe the titles of books, periodicals, and other materials.
  2. As an alternative, you can use the default text role for small inline code snippets, but it is better to use specific textroles. However, if no textrole exists, you may use this to mark the text.

    Surround the code by single backticks and don't start or end the code with whitespace. Example: Type `2 + 2 = 4` to get 2 + 2 = 4 as result.

  3. Just write the code as it is. This may make the text more difficult to read. Use your common sense.

When to Use Literal Code ``...``

Things get tricky if your inline code already contains single backquotes (backticks).

  1. In many cases you can still use the interpreted textrole as described in 1. to 3. For example we can write :code:`:html:`<br>`` to get :html:`<br>`

    This is possible if (a) your code doesn't start with a backtick and (b) if no backtick in your code has a trailing whitespace.

  2. But: To be really free to include inline any code containing backticks you will want to use inline literals. Again: Don't escape or double anything, whitespace is maintained. Example:

    Write

    SQL-example code: ``SELECT  `tt_content` . `bodytext`  AS  `t1` . `text`;``
    

    to get:

    SQL-example code: SELECT  `tt_content` . `bodytext`  AS  `t1` . `text`;

    The drawbacks of literal inline code notation are:

    • there is no way to semantically classify the kind of code
    • there is no special coloring or highlighting
    • the raw reST code looks less beautiful and is less readable

Inline Code versus Code-Blocks

The name for - very - small code snippets that occur within normal text flow within sentences is inline code.

inline code

  • is styled somewhat differently,
  • has no syntax highlighting,
  • does not need to be syntactically correct,
  • can be compared to <span>...</span> tags in html,
  • and is made up by self-defined names. For example, look at the ../../Includes.txt file to see how :php:, :ts: are defined.

In contrast, code-blocks

  • appear "as a box",
  • can have syntax highlighting,
  • need to be syntactically correct in order to have highlighting,
  • can be compared to <pre>...</pre> blocks in html,
  • use predefined names for the different languages that come with Pygments, the syntax highlighter.

Definition of Textroles

For all officials TYPO3 manuals php is set as default highlight language with the exception of the TypoScript manuals, where typoscript is the default.

Use 'Includes.txt' File

In general, the manual you are working on will already contain an Includes.txt file. In that file, the textroles are defined.

You Need Another Textrole for Code Markup?

You need another 'language' to mark up inline?

You are free to define additional textroles in your project or even on an individual page as you like. Make use of the role directive. Example: You want 'haskell'? Define that role as derivative of 'code':

.. role:: haskell(code)

You may then write:

Here is some :haskell:`haskell inline code` in the sentence.

The immediate advantage will be that you can explicitly markup your source code semantically and declare snippets to be 'Haskell'. The visual appearance will be that of 'code' until a special css class has been defined. Feel free to open a request here. Look at this html to understand the technical background:

<code class="code haskell docutils literal">
   <span class="pre">haskell inline code</span>
</code>

A default styling for class="code" exists and is in effect until overridden by a special styling class="code.haskell" that needs to be defined.