-
-
Notifications
You must be signed in to change notification settings - Fork 202
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #760 from 2bndy5/fix-clode-block-highlighting
Fix code block highlighting
- Loading branch information
Showing
12 changed files
with
242 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
from typing import Optional | ||
import os.path | ||
|
||
import pygments # type: ignore | ||
|
||
|
||
def get_pygments_alias(filename: str) -> Optional[str]: | ||
"Find first pygments alias from filename" | ||
try: | ||
lexer_cls = pygments.lexers.get_lexer_for_filename(filename) | ||
return lexer_cls.aliases[0] | ||
except pygments.util.ClassNotFound: | ||
return None | ||
|
||
|
||
def get_extension(filename: str) -> str: | ||
"Get extension from filename" | ||
# If the filename is just '.ext' then we get ('.ext', '') so we fall back to first part if | ||
# the second isn't there | ||
(first, second) = os.path.splitext(filename) | ||
return (second or first).lstrip(".") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
|
||
Code Blocks | ||
=========== | ||
|
||
Breathe supports rendering code blocks with syntax highlighting provided by the | ||
`Pygments <https://pygments.org/>`_ library. By default, Breathe will assume | ||
that code blocks match the language of the source file, but you can also specify | ||
the language of the code blocks using | ||
`Doxygen's code command <https://www.doxygen.nl/manual/commands.html#cmdcode>`_ | ||
or `MarkDown's fenced code blocks <https://www.doxygen.nl/manual/markdown.html#md_fenced>`_. | ||
|
||
.. note:: | ||
Any hyperlinked text found within the code blocks rendered with Doxygen's HTML output | ||
will not be hyperlinked in any Sphinx output due to the use of the Pygments library. | ||
As a benefit, a code-block's syntax highlighting can be any syntax supported by | ||
Pygments (which is much more than only the languages supported by Doxygen's parsers). | ||
|
||
The Doxygen syntax for code blocks supports specifying the language as follows: | ||
|
||
.. code-block:: | ||
\code{.py} | ||
class Python: | ||
pass | ||
\endcode | ||
@code{.cpp} | ||
class Cpp {}; | ||
@endcode | ||
This technique can also be utilized from MarkDown syntax/files | ||
|
||
.. code-block:: markdown | ||
```py | ||
class Python: | ||
pass | ||
``` | ||
```cpp | ||
class Cpp {}; | ||
``` | ||
Breathe will pass the language specified to Pygments to get accurate | ||
highlighting. If no language is explicitly provided (either from ``\code`` | ||
command or via Doxygen's XML output about the language documented), then | ||
Pygments will try to guess what syntax the code block is using (based on | ||
the code block's contents). | ||
|
||
Examples | ||
-------- | ||
|
||
The following should render with standard C/C++ highlighting. Notice, the | ||
syntax is automatically highlighted as C++ because the documented function | ||
exists in a C++ source file. | ||
|
||
---- | ||
|
||
.. code-block:: cpp | ||
/** A function with an unannotated code block with C/C++ code. | ||
* | ||
* @code | ||
* char *buffer = new char[42]; | ||
* int charsAdded = sprintf(buffer, "Tabs are normally %d spaces\n", 8); | ||
* @endcode | ||
*/ | ||
void with_standard_code_block(); | ||
---- | ||
|
||
.. doxygenfunction:: with_standard_code_block | ||
:path: ../../examples/specific/code_blocks/xml | ||
:no-link: | ||
|
||
---- | ||
|
||
The following should render with no detected highlighting. | ||
Notice, there is no syntax highlighting because Pygments does not | ||
recognize the code block's contained syntax as a C++ snippet. | ||
|
||
---- | ||
|
||
.. code-block:: cpp | ||
/** A function with an unannotated code block with non-C/C++ code. | ||
* | ||
* @code | ||
* set(user_list A B C) | ||
* foreach(element ${user_list}) | ||
* message(STATUS "Element is ${element}") | ||
* endforeach() | ||
* @endcode | ||
*/ | ||
void with_unannotated_cmake_code_block(); | ||
---- | ||
|
||
.. doxygenfunction:: with_unannotated_cmake_code_block | ||
:path: ../../examples/specific/code_blocks/xml | ||
:no-link: | ||
|
||
---- | ||
|
||
The following should render with specified CMake highlighting. Here, the syntax | ||
highlighting is explicitly recognized as a CMake script snippet which overrides | ||
the inherent C++ context. | ||
|
||
---- | ||
|
||
.. code-block:: cpp | ||
/** A function with an annotated cmake code block. | ||
* | ||
* @code{.cmake} | ||
* set(user_list A B C) | ||
* foreach(element ${user_list}) | ||
* message(STATUS "Element is ${element}") | ||
* endforeach() | ||
* @endcode | ||
*/ | ||
void with_annotated_cmake_code_block(); | ||
---- | ||
|
||
.. doxygenfunction:: with_annotated_cmake_code_block | ||
:path: ../../examples/specific/code_blocks/xml | ||
:no-link: | ||
|
||
.. warning:: | ||
Pygments will raise a warning in the Sphinx build logs if | ||
the specified syntax does conform the specified syntax's convention(s). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
|
||
Examples: Code Blocks | ||
===================== | ||
|
||
The following should render with standard C/C++ highlighting. | ||
|
||
.. doxygenfunction:: with_standard_code_block | ||
:path: ../../examples/specific/code_blocks/xml | ||
:no-link: | ||
|
||
The following should render with no detected highlighting. | ||
|
||
.. doxygenfunction:: with_unannotated_cmake_code_block | ||
:path: ../../examples/specific/code_blocks/xml | ||
:no-link: | ||
|
||
The following should render with specified cmake highlighting. | ||
|
||
.. doxygenfunction:: with_annotated_cmake_code_block | ||
:path: ../../examples/specific/code_blocks/xml | ||
:no-link: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -65,6 +65,7 @@ Features | |
|
||
markups | ||
latexmath | ||
codeblocks | ||
domains | ||
customcss | ||
groups | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,4 +11,5 @@ Test Pages | |
embeddedrst | ||
inline | ||
members | ||
examples/codeblocks | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
PROJECT_NAME = "Code Blocks" | ||
OUTPUT_DIRECTORY = code_blocks | ||
GENERATE_LATEX = NO | ||
GENERATE_MAN = NO | ||
GENERATE_RTF = NO | ||
CASE_SENSE_NAMES = NO | ||
INPUT = code_blocks.h | ||
QUIET = YES | ||
JAVADOC_AUTOBRIEF = YES | ||
GENERATE_HTML = NO | ||
GENERATE_XML = YES |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
|
||
/** A function with an unannotated code block with C/C++ code. | ||
* | ||
* @code | ||
* char* buffer = new char[42]; | ||
* int charsAdded = sprintf(buffer, "Tabs are normally %d spaces\n", 8); | ||
* @endcode | ||
*/ | ||
void with_standard_code_block(); | ||
|
||
/** A function with an unannotated code block with non-C/C++ code. | ||
* | ||
* @code | ||
* set(user_list A B C) | ||
* foreach(element ${user_list}) | ||
* message(STATUS "Element is ${element}") | ||
* endforeach() | ||
* @endcode | ||
*/ | ||
void with_unannotated_cmake_code_block(); | ||
|
||
/** A function with an annotated cmake code block. | ||
* | ||
* @code{.cmake} | ||
* set(user_list A B C) | ||
* foreach(element ${user_list}) | ||
* message(STATUS "Element is ${element}") | ||
* endforeach() | ||
* @endcode | ||
*/ | ||
void with_annotated_cmake_code_block(); |