Fix arguments of @param, @return etc translations to Python.

For the parameter documentation to be really taken as such, in its entirety,
by Sphinx, it must be indented relative to the :param: tag. Do this by
appending an extra indent after every line of the output and work around the
unnecessary indent of the last line by removing the trailing whitespace.

This required updating the existing tests and removing the expected but not
present any more whitespace from them, but as trailing whitespace in the
documentation is at best insignificant (and at worst harmful) anyhow, this is
not a big price to pay for simpler translator code.
This commit is contained in:
Vadim Zeitlin 2014-07-13 20:27:52 +02:00
commit 8b83976f4c
7 changed files with 135 additions and 39 deletions

View file

@ -57,7 +57,7 @@ commentVerifier.check(doxygen_basic_translate.function4.__doc__,
) )
commentVerifier.check(doxygen_basic_translate.function5.__doc__, commentVerifier.check(doxygen_basic_translate.function5.__doc__,
""" """
This is a post comment. This is a post comment.
""" """
) )
commentVerifier.check(doxygen_basic_translate.function6.__doc__, commentVerifier.check(doxygen_basic_translate.function6.__doc__,

View file

@ -23,7 +23,7 @@ commentVerifier.check(doxygen_misc_constructs.getAddress.__doc__,
commentVerifier.check(doxygen_misc_constructs.CConnectionConfig.__doc__, commentVerifier.check(doxygen_misc_constructs.CConnectionConfig.__doc__,
r""" r"""
This class contains information for connection to winIDEA. Its methods This class contains information for connection to winIDEA. Its methods
return reference to self, so we can use it like this: return reference to self, so we can use it like this:
CConnectionConfig config = new CConnectionConfig(); CConnectionConfig config = new CConnectionConfig();
@ -41,7 +41,7 @@ commentVerifier.check(doxygen_misc_constructs.CConnectionConfig.__doc__,
commentVerifier.check(doxygen_misc_constructs.waitTime.__doc__, commentVerifier.check(doxygen_misc_constructs.waitTime.__doc__,
r""" r"""
Determines how long the ``isystem.connect`` should wait for running Determines how long the ``isystem.connect`` should wait for running
instances to respond. Only one of ``lfWaitXXX`` flags from IConnect::ELaunchFlags instances to respond. Only one of ``lfWaitXXX`` flags from IConnect::ELaunchFlags
may be specified. may be specified.
""" """
@ -123,8 +123,8 @@ commentVerifier.check(doxygen_misc_constructs.backslashB.__doc__,
commentVerifier.check(doxygen_misc_constructs.backslashC.__doc__, commentVerifier.check(doxygen_misc_constructs.backslashC.__doc__,
r""" r"""
Backslash e at end of *line* froze SWIG Backslash e at end of *line* froze SWIG
*with* old comment parser. *with* old comment parser.
See also: MyClass::fun(char, See also: MyClass::fun(char,
float) float)
@ -146,15 +146,15 @@ commentVerifier.check(doxygen_misc_constructs.cycle.__doc__,
Spaces at the start of line should be taken into account: Spaces at the start of line should be taken into account:
:type id: int :type id: int
:param id: used as prefix in log :param id: used as prefix in log
statements. The default value is empty string, which is OK if statements. The default value is empty string, which is OK if
there is only one app. instance. Example: there is only one app. instance. Example:
ctrl.setBP("func1"); ctrl.setBP("func1");
If we set the id to ``main_``, we get: If we set the id to ``main_``, we get:
main_ctrl.setBP("func1"); main_ctrl.setBP("func1");
:type fileName: string :type fileName: string
:param fileName: name of the log file :param fileName: name of the log file

View file

@ -21,7 +21,7 @@ commentVerifier.check(doxygen_parsing.SomeAnotherClass.classMethod.__doc__,
r""" r"""
The class method comment. The class method comment.
SomeAnotherClass#classMethodExtended(int, int) a link text SomeAnotherClass#classMethodExtended(int, int) a link text
""") """)
commentVerifier.check(doxygen_parsing.SomeAnotherClass.classMethodExtended.__doc__, commentVerifier.check(doxygen_parsing.SomeAnotherClass.classMethodExtended.__doc__,
r""" r"""
@ -33,7 +33,7 @@ commentVerifier.check(doxygen_parsing.SomeAnotherClass.classMethodExtended.__doc
:param b: Parameter b :param b: Parameter b
""" """
) )
commentVerifier.check(doxygen_parsing.SomeAnotherClass.classMethodExtended2.__doc__, commentVerifier.check(doxygen_parsing.SomeAnotherClass.classMethodExtended2.__doc__,
r""" r"""
The class method with parameter The class method with parameter

View file

@ -36,7 +36,7 @@ r"""
'citationword' 'citationword'
some test code some test code
""") """)
commentVerifier.check(doxygen_translate_all_tags.func02.__doc__, commentVerifier.check(doxygen_translate_all_tags.func02.__doc__,
@ -95,6 +95,7 @@ r"""
:math:`\sqrt{(x_2-x_1)^2+(y_2-y_1)^2}` :math:`\sqrt{(x_2-x_1)^2+(y_2-y_1)^2}`
.. math:: .. math::
\sqrt{(x_2-x_1)^2+(y_2-y_1)^2} \sqrt{(x_2-x_1)^2+(y_2-y_1)^2}
@ -116,7 +117,6 @@ r"""
This will only appear in hmtl This will only appear in hmtl
""") """)
@ -176,7 +176,7 @@ r"""
someMember Some description follows someMember Some description follows

View file

@ -22,7 +22,7 @@ r"""
'citationword' 'citationword'
some test code some test code
Conditional comment: SOMECONDITION Conditional comment: SOMECONDITION
Some conditional comment Some conditional comment
@ -67,7 +67,7 @@ r"""
someMember Some description follows someMember Some description follows
@ -171,7 +171,7 @@ r"""
*Starts a piece of text displayed in an italic font.* *Starts a piece of text displayed in an italic font.*
Input tag. Input tag.
Image: src="slika.png" Image: src="slika.png"
Meta tag. Meta tag.
Multicol is ignored by doxygen. Multicol is ignored by doxygen.
@ -227,16 +227,16 @@ r"""
:type byFlags: int :type byFlags: int
:param byFlags: bits marking required items: :param byFlags: bits marking required items:
| Size in bits| Items Required | | Size in bits| Items Required |
-------------------------------- --------------------------------
| 1 - 8 | 1 | | 1 - 8 | 1 |
| 9 - 16 | 2 | | 9 - 16 | 2 |
| 17 - 32 | 4 | | 17 - 32 | 4 |
Almost all combinations of above flags are supported by Almost all combinations of above flags are supported by
``htmlTable...`` functions. ``htmlTable...`` functions.
""") """)
commentVerifier.check(doxygen_translate.htmlEntitiesFunction.__doc__, commentVerifier.check(doxygen_translate.htmlEntitiesFunction.__doc__,
@ -253,7 +253,7 @@ r"""
" "
- -
-- --
x x
- -
. .

View file

@ -24,6 +24,72 @@ std::map<std::string, std::string> PyDocConverter::sectionTitles;
using std::string; using std::string;
// Helper class increasing the provided indent string in its ctor and decreasing
// it in its dtor.
class IndentGuard
{
public:
// One indent level.
static const char* Level() { return " "; }
// Ctor takes the output to determine the current indent and to remove the
// extra indent added to it in the dtor and the variable containing the indent
// to use, which must be used after every new line by the code actually
// updating the output.
explicit IndentGuard(string& output, string& indent) :
m_output(output),
m_indent(indent)
{
const size_t lastNonSpace = m_output.find_last_not_of(' ');
if (lastNonSpace == string::npos) {
m_firstLineIndent = m_output.length();
} else if (m_output[lastNonSpace] == '\n') {
m_firstLineIndent = m_output.length() - (lastNonSpace + 1);
} else {
m_firstLineIndent = 0;
}
// Notice that the indent doesn't include the first line indent because it's
// implicit, i.e. it is present in the input and so is copied into the
// output anyhow.
m_indent = Level();
}
// Get the indent for the first line of the paragraph, which is smaller than
// the indent for the subsequent lines.
string getFirstLineIndent() const { return string(m_firstLineIndent, ' '); }
~IndentGuard()
{
m_indent.clear();
// Get rid of possible remaining extra indent, e.g. if there were any trailing
// new lines: we shouldn't add the extra indent level to whatever follows
// this paragraph.
static const size_t lenIndentLevel = strlen(Level());
if (m_output.length() > lenIndentLevel) {
const size_t start = m_output.length() - lenIndentLevel;
if (m_output.compare(start, string::npos, Level()) == 0)
m_output.erase(start);
}
}
private:
string& m_output;
string& m_indent;
unsigned m_firstLineIndent;
IndentGuard(const IndentGuard&);
IndentGuard& operator=(const IndentGuard&);
};
static void trimWhitespace(string& s)
{
const size_t lastNonSpace = s.find_last_not_of(' ');
if (lastNonSpace != string::npos)
s.erase(lastNonSpace + 1);
}
/* static */ /* static */
PyDocConverter::TagHandlersMap::mapped_type PyDocConverter::TagHandlersMap::mapped_type
PyDocConverter::make_handler(tagHandler handler) PyDocConverter::make_handler(tagHandler handler)
@ -311,13 +377,27 @@ void PyDocConverter::handleMath(DoxygenEntity &tag,
std::string &translatedComment, std::string &translatedComment,
const std::string& arg) const std::string& arg)
{ {
IndentGuard indent(translatedComment, m_indent);
// Only \f$ is translated to inline formulae, \f[ and \f{ are for the block ones. // Only \f$ is translated to inline formulae, \f[ and \f{ are for the block ones.
const bool inlineFormula = tag.typeOfEntity == "f$"; const bool inlineFormula = tag.typeOfEntity == "f$";
string formulaNL;
if (inlineFormula) { if (inlineFormula) {
translatedComment += ":math:`"; translatedComment += ":math:`";
} else { } else {
translatedComment += ".. math::\n\n "; trimWhitespace(translatedComment);
translatedComment += '\n';
const string formulaIndent = indent.getFirstLineIndent();
translatedComment += formulaIndent;
translatedComment += ".. math::\n";
formulaNL = '\n';
formulaNL += formulaIndent;
formulaNL += m_indent;
translatedComment += formulaNL;
} }
std::string formula; std::string formula;
@ -330,10 +410,9 @@ void PyDocConverter::handleMath(DoxygenEntity &tag,
if (start != std::string::npos) { if (start != std::string::npos) {
for (size_t n = start; n <= end; n++) { for (size_t n = start; n <= end; n++) {
if (formula[n] == '\n') { if (formula[n] == '\n') {
// New lines must be suppressed in inline maths and indented in the // New lines must be suppressed in inline maths and indented in the block ones.
// block ones.
if (!inlineFormula) if (!inlineFormula)
translatedComment += "\n "; translatedComment += formulaNL;
} else { } else {
// Just copy everything else. // Just copy everything else.
translatedComment += formula[n]; translatedComment += formula[n];
@ -344,7 +423,7 @@ void PyDocConverter::handleMath(DoxygenEntity &tag,
if (inlineFormula) { if (inlineFormula) {
translatedComment += "`"; translatedComment += "`";
} else { } else {
translatedComment += "\n\n"; translatedComment += '\n';
} }
} }
@ -427,16 +506,21 @@ void PyDocConverter::handleTagParam(DoxygenEntity& tag,
if (tag.entityList.size() < 2) if (tag.entityList.size() < 2)
return; return;
IndentGuard indent(translatedComment, m_indent);
DoxygenEntity paramNameEntity = *tag.entityList.begin(); DoxygenEntity paramNameEntity = *tag.entityList.begin();
tag.entityList.pop_front(); tag.entityList.pop_front();
const std::string& paramName = paramNameEntity.data; const std::string& paramName = paramNameEntity.data;
const std::string paramType = getParamType(paramName); const std::string paramType = getParamType(paramName);
if (!paramType.empty()) if (!paramType.empty()) {
translatedComment += ":type " + paramName + ": " + paramType + "\n"; translatedComment += ":type " + paramName + ": " + paramType + "\n";
translatedComment += indent.getFirstLineIndent();
}
translatedComment += ":param " + paramName + ":"; translatedComment += ":param " + paramName + ":";
handleParagraph(tag, translatedComment); handleParagraph(tag, translatedComment);
} }
@ -445,6 +529,8 @@ void PyDocConverter::handleTagReturn(DoxygenEntity &tag,
std::string &translatedComment, std::string &translatedComment,
const std::string &) const std::string &)
{ {
IndentGuard indent(translatedComment, m_indent);
translatedComment += ":return: "; translatedComment += ":return: ";
handleParagraph(tag, translatedComment); handleParagraph(tag, translatedComment);
} }
@ -454,6 +540,8 @@ void PyDocConverter::handleTagException(DoxygenEntity &tag,
std::string &translatedComment, std::string &translatedComment,
const std::string &) const std::string &)
{ {
IndentGuard indent(translatedComment, m_indent);
translatedComment += ":raises: "; translatedComment += ":raises: ";
handleParagraph(tag, translatedComment); handleParagraph(tag, translatedComment);
} }
@ -618,7 +706,11 @@ void PyDocConverter::handleNewLine(DoxygenEntity&,
std::string& translatedComment, std::string& translatedComment,
const std::string&) const std::string&)
{ {
trimWhitespace(translatedComment);
translatedComment += "\n"; translatedComment += "\n";
if (!m_indent.empty())
translatedComment += m_indent;
} }
String *PyDocConverter::makeDocumentation(Node *n) String *PyDocConverter::makeDocumentation(Node *n)

View file

@ -174,6 +174,10 @@ private:
// temporary thing, should be refactored somehow // temporary thing, should be refactored somehow
Node *currentNode; Node *currentNode;
// Extra indent for the current paragraph, must be output after each new line.
std::string m_indent;
// this contains the handler pointer and one string argument // this contains the handler pointer and one string argument
typedef std::map<std::string, std::pair<tagHandler, std::string> > TagHandlersMap; typedef std::map<std::string, std::pair<tagHandler, std::string> > TagHandlersMap;
static TagHandlersMap tagHandlers; static TagHandlersMap tagHandlers;