Translate Doxygen code blocks to Sphinx code blocks.

This ensures they are formatted correctly and syntax highlighted (currently
always as C++ code).
This commit is contained in:
Vadim Zeitlin 2014-08-06 18:18:20 +02:00
commit d678891e43
5 changed files with 65 additions and 4 deletions

View file

@ -50,7 +50,10 @@ commentVerifier.check(doxygen_basic_translate.function4.__doc__,
Warning: This may not work as expected
int main() { while(true); }
.. code-block:: c++
int main() { while(true); }
}
"""

View file

@ -36,7 +36,11 @@ r"""
'citationword'
some test code
.. code-block:: c++
some test code
""")
commentVerifier.check(doxygen_translate_all_tags.func02.__doc__,

View file

@ -22,7 +22,11 @@ r"""
'citationword'
some test code
.. code-block:: c++
some test code
Conditional comment: SOMECONDITION
Some conditional comment

View file

@ -153,7 +153,7 @@ void PyDocConverter::fillStaticTables()
tagHandlers["authors"] = make_handler(&PyDocConverter::handleParagraph);
tagHandlers["brief"] = make_handler(&PyDocConverter::handleParagraph);
tagHandlers["bug"] = make_handler(&PyDocConverter::handleParagraph);
tagHandlers["code"] = make_handler(&PyDocConverter::handleParagraph);
tagHandlers["code"] = make_handler(&PyDocConverter::handleCode);
tagHandlers["copyright"] = make_handler(&PyDocConverter::handleParagraph);
tagHandlers["date"] = make_handler(&PyDocConverter::handleParagraph);
tagHandlers["deprecated"] = make_handler(&PyDocConverter::handleParagraph);
@ -441,6 +441,51 @@ void PyDocConverter::handleMath(DoxygenEntity &tag,
}
}
void PyDocConverter::handleCode(DoxygenEntity &tag,
std::string &translatedComment,
const std::string &arg)
{
IndentGuard indent(translatedComment, m_indent);
trimWhitespace(translatedComment);
translatedComment += '\n';
// Use the current indent for the code-block line itself.
string codeIndent = indent.getFirstLineIndent();
translatedComment += codeIndent;
// Go out on a limb and assume that examples in the C or C++ sources use C++.
// In the worst case, we'll highlight C code using C++ syntax which is not a
// big deal (TODO: handle Doxygen code command language argument).
translatedComment += ".. code-block:: c++\n\n";
// For now on, use extra indent level for all the subsequent lines.
codeIndent += m_indent;
std::string code;
handleTagVerbatim(tag, code, arg);
translatedComment += codeIndent;
for (size_t n = 0; n < code.length(); n++) {
if (code[n] == '\n') {
// Don't leave trailing white space, this results in PEP8 validation
// errors in Python code (which are performed by our own unit tests).
trimWhitespace(translatedComment);
translatedComment += '\n';
// Ensure that we indent all the lines by the code indent.
translatedComment += codeIndent;
} else {
// Just copy everything else.
translatedComment += code[n];
}
}
trimWhitespace(translatedComment);
if (*translatedComment.rbegin() != '\n')
translatedComment += '\n';
}
void PyDocConverter::handlePlainString(DoxygenEntity& tag,
std::string& translatedComment,
const std::string&)

View file

@ -81,6 +81,11 @@ protected:
*/
void handleMath(DoxygenEntity &tag, std::string &translatedComment, const std::string &arg);
/*
* Handle a code snippet.
*/
void handleCode(DoxygenEntity &tag, std::string &translatedComment, const std::string &arg);
/*
* Print only data part of code
*/