| 1 |
|
| 2 |
Code Blocks |
| 3 |
=========== |
| 4 |
|
| 5 |
Breathe supports rendering code blocks with syntax highlighting provided by the |
| 6 |
`Pygments <https://pygments.org/>`_ library. By default, Breathe will assume |
| 7 |
that code blocks match the language of the source file, but you can also specify |
| 8 |
the language of the code blocks using |
| 9 |
`Doxygen's code command <https://www.doxygen.nl/manual/commands.html#cmdcode>`_ |
| 10 |
or `MarkDown's fenced code blocks <https://www.doxygen.nl/manual/markdown.html#md_fenced>`_. |
| 11 |
|
| 12 |
.. note:: |
| 13 |
Any hyperlinked text found within the code blocks rendered with Doxygen's HTML output |
| 14 |
will not be hyperlinked in any Sphinx output due to the use of the Pygments library. |
| 15 |
As a benefit, a code-block's syntax highlighting can be any syntax supported by |
| 16 |
Pygments (which is much more than only the languages supported by Doxygen's parsers). |
| 17 |
|
| 18 |
The Doxygen syntax for code blocks supports specifying the language as follows: |
| 19 |
|
| 20 |
.. code-block:: |
| 21 |
|
| 22 |
\code{.py} |
| 23 |
class Python: |
| 24 |
pass |
| 25 |
\endcode |
| 26 |
|
| 27 |
@code{.cpp} |
| 28 |
class Cpp {}; |
| 29 |
@endcode |
| 30 |
|
| 31 |
This technique can also be utilized from MarkDown syntax/files |
| 32 |
|
| 33 |
.. code-block:: markdown |
| 34 |
|
| 35 |
```py |
| 36 |
class Python: |
| 37 |
pass |
| 38 |
``` |
| 39 |
|
| 40 |
```cpp |
| 41 |
class Cpp {}; |
| 42 |
``` |
| 43 |
|
| 44 |
Breathe will pass the language specified to Pygments to get accurate |
| 45 |
highlighting. If no language is explicitly provided (either from ``\code`` |
| 46 |
command or via Doxygen's XML output about the language documented), then |
| 47 |
Pygments will try to guess what syntax the code block is using (based on |
| 48 |
the code block's contents). |
| 49 |
|
| 50 |
Examples |
| 51 |
-------- |
| 52 |
|
| 53 |
The following should render with standard C/C++ highlighting. Notice, the |
| 54 |
syntax is automatically highlighted as C++ because the documented function |
| 55 |
exists in a C++ source file. |
| 56 |
|
| 57 |
---- |
| 58 |
|
| 59 |
.. code-block:: cpp |
| 60 |
|
| 61 |
/** A function with an unannotated code block with C/C++ code. |
| 62 |
* |
| 63 |
* @code |
| 64 |
* char *buffer = new char[42]; |
| 65 |
* int charsAdded = sprintf(buffer, "Tabs are normally %d spaces\n", 8); |
| 66 |
* @endcode |
| 67 |
*/ |
| 68 |
void with_standard_code_block(); |
| 69 |
|
| 70 |
---- |
| 71 |
|
| 72 |
.. doxygenfunction:: with_standard_code_block |
| 73 |
:path: ../../examples/specific/code_blocks/xml |
| 74 |
:no-link: |
| 75 |
|
| 76 |
---- |
| 77 |
|
| 78 |
The following should render with no detected highlighting. |
| 79 |
Notice, there is no syntax highlighting because Pygments does not |
| 80 |
recognize the code block's contained syntax as a C++ snippet. |
| 81 |
|
| 82 |
---- |
| 83 |
|
| 84 |
.. code-block:: cpp |
| 85 |
|
| 86 |
/** A function with an unannotated code block with non-C/C++ code. |
| 87 |
* |
| 88 |
* @code |
| 89 |
* set(user_list A B C) |
| 90 |
* foreach(element ${user_list}) |
| 91 |
* message(STATUS "Element is ${element}") |
| 92 |
* endforeach() |
| 93 |
* @endcode |
| 94 |
* |
| 95 |
* Another code-block that explicitly remains not highlighted. |
| 96 |
* @code{.unparsed} |
| 97 |
* Show this as is. |
| 98 |
* @endcode |
| 99 |
*/ |
| 100 |
void with_unannotated_cmake_code_block(); |
| 101 |
|
| 102 |
---- |
| 103 |
|
| 104 |
.. doxygenfunction:: with_unannotated_cmake_code_block |
| 105 |
:path: ../../examples/specific/code_blocks/xml |
| 106 |
:no-link: |
| 107 |
|
| 108 |
---- |
| 109 |
|
| 110 |
The following should render with specified CMake highlighting. Here, the syntax |
| 111 |
highlighting is explicitly recognized as a CMake script snippet which overrides |
| 112 |
the inherent C++ context. |
| 113 |
|
| 114 |
---- |
| 115 |
|
| 116 |
.. code-block:: cpp |
| 117 |
|
| 118 |
/** A function with an annotated cmake code block. |
| 119 |
* |
| 120 |
* @code{.cmake} |
| 121 |
* set(user_list A B C) |
| 122 |
* foreach(element ${user_list}) |
| 123 |
* message(STATUS "Element is ${element}") |
| 124 |
* endforeach() |
| 125 |
* @endcode |
| 126 |
*/ |
| 127 |
void with_annotated_cmake_code_block(); |
| 128 |
|
| 129 |
---- |
| 130 |
|
| 131 |
.. doxygenfunction:: with_annotated_cmake_code_block |
| 132 |
:path: ../../examples/specific/code_blocks/xml |
| 133 |
:no-link: |
| 134 |
|
| 135 |
.. warning:: |
| 136 |
Pygments will raise a warning in the Sphinx build logs if |
| 137 |
the specified syntax does conform the specified syntax's convention(s). |