diff mbox series

[v4,05/18] scripts/qapi: Move doc-comment whitespace stripping to doc.py

Message ID 20200309154405.13548-6-peter.maydell@linaro.org
State Superseded
Headers show
Series Convert QAPI doc comments to generate rST instead of texinfo | expand

Commit Message

Peter Maydell March 9, 2020, 3:43 p.m. UTC
As we accumulate lines from doc comments when parsing the JSON, the
QAPIDoc class generally strips leading and trailing whitespace using
line.strip() when it calls _append_freeform().  This is fine for
texinfo, but for rST leading whitespace is significant.  We'd like to
move to having the text in doc comments be rST format rather than a
custom syntax, so move the removal of leading whitespace from the
QAPIDoc class to the texinfo-specific processing code in
texi_format() in qapi/doc.py.

(Trailing whitespace will always be stripped by the rstrip() in
Section::append regardless.)

In a followup commit we will make the whitespace in the lines of doc
comment sections more consistently follow the input source.

There is no change to the generated .texi files before and after this
commit.

Because the qapi-schema test checks the exact values of the
documentation comments against a reference, we need to update that
reference to match the new whitespace.  In the first four places this
is now correctly checking that we did put in the amount of whitespace
to pass a rST-formatted list to the backend; in the last two places
the extra whitespace is 'wrong' and will go away again in the
following commit.

Signed-off-by: Peter Maydell <peter.maydell@linaro.org>

---
New in v2: update doc-good.out as noted in final para of commit msg
---
 scripts/qapi/doc.py            |  1 +
 scripts/qapi/parser.py         | 12 ++++--------
 tests/qapi-schema/doc-good.out | 12 ++++++------
 3 files changed, 11 insertions(+), 14 deletions(-)

-- 
2.20.1

Comments

Richard Henderson March 11, 2020, 6:07 a.m. UTC | #1
On 3/9/20 8:43 AM, Peter Maydell wrote:
> As we accumulate lines from doc comments when parsing the JSON, the

> QAPIDoc class generally strips leading and trailing whitespace using

> line.strip() when it calls _append_freeform().  This is fine for

> texinfo, but for rST leading whitespace is significant.  We'd like to

> move to having the text in doc comments be rST format rather than a

> custom syntax, so move the removal of leading whitespace from the

> QAPIDoc class to the texinfo-specific processing code in

> texi_format() in qapi/doc.py.

> 

> (Trailing whitespace will always be stripped by the rstrip() in

> Section::append regardless.)

> 

> In a followup commit we will make the whitespace in the lines of doc

> comment sections more consistently follow the input source.

> 

> There is no change to the generated .texi files before and after this

> commit.

> 

> Because the qapi-schema test checks the exact values of the

> documentation comments against a reference, we need to update that

> reference to match the new whitespace.  In the first four places this

> is now correctly checking that we did put in the amount of whitespace

> to pass a rST-formatted list to the backend; in the last two places

> the extra whitespace is 'wrong' and will go away again in the

> following commit.

> 

> Signed-off-by: Peter Maydell <peter.maydell@linaro.org>

> ---

> New in v2: update doc-good.out as noted in final para of commit msg

> ---

>  scripts/qapi/doc.py            |  1 +

>  scripts/qapi/parser.py         | 12 ++++--------

>  tests/qapi-schema/doc-good.out | 12 ++++++------

>  3 files changed, 11 insertions(+), 14 deletions(-)


Reviewed-by: Richard Henderson <richard.henderson@linaro.org>



r~
Markus Armbruster Aug. 6, 2020, 3:06 p.m. UTC | #2
Peter Maydell <peter.maydell@linaro.org> writes:

> As we accumulate lines from doc comments when parsing the JSON, the

> QAPIDoc class generally strips leading and trailing whitespace using

> line.strip() when it calls _append_freeform().  This is fine for

> texinfo,


Texinfo

>          but for rST leading whitespace is significant.  We'd like to

> move to having the text in doc comments be rST format rather than a

> custom syntax, so move the removal of leading whitespace from the

> QAPIDoc class to the texinfo-specific processing code in

> texi_format() in qapi/doc.py.

>

> (Trailing whitespace will always be stripped by the rstrip() in

> Section::append regardless.)


We strip to keep the Texinfo source tidier.  Stripping less as we move
towards its replacement is fine.

> In a followup commit we will make the whitespace in the lines of doc

> comment sections more consistently follow the input source.

>

> There is no change to the generated .texi files before and after this

> commit.

>

> Because the qapi-schema test checks the exact values of the

> documentation comments against a reference, we need to update that

> reference to match the new whitespace.  In the first four places this

> is now correctly checking that we did put in the amount of whitespace

> to pass a rST-formatted list to the backend; in the last two places

> the extra whitespace is 'wrong' and will go away again in the

> following commit.

>

> Signed-off-by: Peter Maydell <peter.maydell@linaro.org>


The thorough explanation in the commit message made review easier.
Thanks!

Reviewed-by: Markus Armbruster <armbru@redhat.com>
diff mbox series

Patch

diff --git a/scripts/qapi/doc.py b/scripts/qapi/doc.py
index 1787a53d91a..26cd1a1751e 100644
--- a/scripts/qapi/doc.py
+++ b/scripts/qapi/doc.py
@@ -79,6 +79,7 @@  def texi_format(doc):
     inlist = ''
     lastempty = False
     for line in doc.split('\n'):
+        line = line.strip()
         empty = line == ''
 
         # FIXME: Doing this in a single if / elif chain is
diff --git a/scripts/qapi/parser.py b/scripts/qapi/parser.py
index abadacbb0e8..7fae4478d34 100644
--- a/scripts/qapi/parser.py
+++ b/scripts/qapi/parser.py
@@ -417,10 +417,10 @@  class QAPIDoc:
                 self._append_line = self._append_various_line
                 self._append_various_line(line)
             else:
-                self._append_freeform(line.strip())
+                self._append_freeform(line)
         else:
             # This is a free-form documentation block
-            self._append_freeform(line.strip())
+            self._append_freeform(line)
 
     def _append_args_line(self, line):
         """
@@ -453,7 +453,7 @@  class QAPIDoc:
                 self._append_various_line(line)
             return
 
-        self._append_freeform(line.strip())
+        self._append_freeform(line)
 
     def _append_features_line(self, line):
         name = line.split(' ', 1)[0]
@@ -472,7 +472,7 @@  class QAPIDoc:
             self._append_various_line(line)
             return
 
-        self._append_freeform(line.strip())
+        self._append_freeform(line)
 
     def _append_various_line(self, line):
         """
@@ -495,10 +495,6 @@  class QAPIDoc:
             line = line[len(name)+1:]
             self._start_section(name[:-1])
 
-        if (not self._section.name or
-                not self._section.name.startswith('Example')):
-            line = line.strip()
-
         self._append_freeform(line)
 
     def _start_symbol_section(self, symbols_dict, name):
diff --git a/tests/qapi-schema/doc-good.out b/tests/qapi-schema/doc-good.out
index 9503a3a3178..a65bce639ff 100644
--- a/tests/qapi-schema/doc-good.out
+++ b/tests/qapi-schema/doc-good.out
@@ -71,20 +71,20 @@  doc freeform
 
 * List item one
 * Two, multiple
-lines
+  lines
 
 * Three
-Still in list
+  Still in list
 
 Not in list
 
 - Second list
-Note: still in list
+  Note: still in list
 
 Note: not in list
 
 1. Third list
-is numbered
+   is numbered
 
 2. another item
 
@@ -144,7 +144,7 @@  doc symbol=Alternate
 
     arg=i
 an integer
-@b is undocumented
+    @b is undocumented
     arg=b
 
 doc freeform
@@ -157,7 +157,7 @@  doc symbol=cmd
 the first argument
     arg=arg2
 the second
-argument
+       argument
     arg=arg3
 
     feature=cmd-feat1