Skip to content

Commit 4bcc9bd

Browse files
committed
Tidy: 1) Fixed handling docs with comments. 2) Refactored splitting multiline docs.
Update issue 1289 Status: Done Refactored. Not exactly "refrigerator code" but good enough.
1 parent ecc7c95 commit 4bcc9bd

7 files changed

Lines changed: 71 additions & 30 deletions

File tree

atest/testdata/tidy/documentation.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Documentation Hello
55

66
*** Test Cases ***
77
Multiline
8-
[ DocuMentation ] Hillo
8+
[ DocuMentation ] Hillo # I can has comment!
99
... | table |
1010
... | world |
1111
Multiline with manual line separators
@@ -16,6 +16,10 @@ Empty
1616
[Documentation]
1717
None
1818
No Operation
19+
Comments
20+
[Documentation] First line # First comment
21+
... Middle line # Middle comment
22+
... Last line # Last comment
1923

2024
*** Keywords ***
2125
Keyword doc

atest/testdata/tidy/documentation_expected.html

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,11 @@ <h1>Documentation</h1>
7474
<tr>
7575
<td class="name"><a name="test_Multiline">Multiline</a></td>
7676
<td>[Documentation]</td>
77-
<td class="colspan3" colspan="3">Hillo\n<br>
77+
<td>Hillo\n<br>
7878
| table |\n<br>
7979
| world |</td>
80+
<td># I can has comment!</td>
81+
<td></td>
8082
</tr>
8183
<tr>
8284
<td class="name"></td>
@@ -139,6 +141,29 @@ <h1>Documentation</h1>
139141
<td></td>
140142
<td></td>
141143
</tr>
144+
<tr>
145+
<td class="name"><a name="test_Comments">Comments</a></td>
146+
<td>[Documentation]</td>
147+
<td>First line\n<br>
148+
Middle line\n<br>
149+
Last line</td>
150+
<td># First comment</td>
151+
<td>Middle comment</td>
152+
</tr>
153+
<tr>
154+
<td class="name"></td>
155+
<td>...</td>
156+
<td>#Last comment</td>
157+
<td></td>
158+
<td></td>
159+
</tr>
160+
<tr>
161+
<td class="name"></td>
162+
<td></td>
163+
<td></td>
164+
<td></td>
165+
<td></td>
166+
</tr>
142167
</table>
143168
<table border="1" id="keyword">
144169
<tr>

atest/testdata/tidy/documentation_expected.tsv

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Documentation Hello
44
... - <world>
55

66
*Test Cases*
7-
Multiline [Documentation] Hillo
7+
Multiline [Documentation] Hillo # I can has comment!
88
... | table |
99
... | world |
1010

@@ -18,6 +18,10 @@ Empty
1818

1919
None No Operation
2020

21+
Comments [Documentation] First line # First comment Middle comment Last comment
22+
... Middle line
23+
... Last line
24+
2125
*Keywords*
2226
Keyword doc [Documentation] Multi
2327
... line

atest/testdata/tidy/documentation_expected.txt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Documentation Hello
55

66
*** Test Cases ***
77
Multiline
8-
[Documentation] Hillo
8+
[Documentation] Hillo # I can has comment!
99
... | table |
1010
... | world |
1111

@@ -22,6 +22,11 @@ Empty
2222
None
2323
No Operation
2424

25+
Comments
26+
[Documentation] First line # First comment Middle comment Last comment
27+
... Middle line
28+
... Last line
29+
2530
*** Keywords ***
2631
Keyword doc
2732
[Documentation] Multi

src/robot/writer/formatters.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@
2121

2222
class _DataFileFormatter(object):
2323
_whitespace = re.compile('\s{2,}')
24-
_split_doc = True
24+
_split_multiline_doc = True
2525

2626
def __init__(self, column_count):
27-
self._splitter = RowSplitter(column_count, split_doc=self._split_doc)
27+
self._splitter = RowSplitter(column_count, self._split_multiline_doc)
2828
self._column_count = column_count
2929
self._extractor = DataExtractor(self._want_names_on_first_content_row)
3030

@@ -48,9 +48,8 @@ def _should_split_rows(self, table):
4848
return not self._should_align_columns(table)
4949

5050
def _split_rows(self, original_rows, table):
51-
indented = self._is_indented_table(table)
5251
for original in original_rows:
53-
for split in self._splitter.split(original, indented):
52+
for split in self._splitter.split(original, table.type):
5453
yield split
5554

5655
def _should_align_columns(self, table):

src/robot/writer/htmlformatter.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121

2222
class HtmlFormatter(_DataFileFormatter):
23-
_split_doc = False
23+
_split_multiline_doc = False
2424

2525
def _format_row(self, row, table=None):
2626
row = self._pad(self._escape_consecutive_whitespace(row), table)

src/robot/writer/rowsplitter.py

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -19,46 +19,50 @@ class RowSplitter(object):
1919
_comment_mark = '#'
2020
_empty_cell_escape = '${EMPTY}'
2121
_line_continuation = '...'
22+
_setting_table = 'setting'
23+
_tc_table = 'test case'
24+
_kw_table = 'keyword'
2225

23-
def __init__(self, cols=8, split_doc=True):
26+
def __init__(self, cols=8, split_multiline_doc=True):
2427
self._cols = cols
25-
self._split_doc = split_doc
28+
self._split_multiline_doc = split_multiline_doc
2629

27-
def split(self, row, indented_table=False): # TODO: pass table type instead
30+
def split(self, row, table_type):
2831
if not row:
2932
return self._split_empty_row()
30-
indent = self._get_indent(row, indented_table)
31-
if self._split_doc and self._is_doc_row(row, indented_table):
33+
indent = self._get_indent(row, table_type)
34+
if self._split_multiline_doc and self._is_doc_row(row, table_type):
3235
return self._split_doc_row(row, indent)
3336
return self._split_row(row, indent)
3437

3538
def _split_empty_row(self):
3639
yield []
3740

38-
def _is_doc_row(self, row, tc_or_kw_table):
39-
if tc_or_kw_table:
41+
def _get_indent(self, row, table_type):
42+
indent = len(list(itertools.takewhile(lambda x: x == '', row)))
43+
min_indent = 1 if table_type in [self._tc_table, self._kw_table] else 0
44+
return max(indent, min_indent)
45+
46+
def _is_doc_row(self, row, table_type):
47+
if table_type == self._setting_table:
48+
return len(row) > 1 and row[0] == 'Documentation'
49+
if table_type in [self._tc_table, self._kw_table]:
4050
return len(row) > 2 and row[1] == '[Documentation]'
41-
return len(row) > 1 and row[0] == 'Documentation'
51+
return False
4252

4353
def _split_doc_row(self, row, indent):
44-
first, rest = self._split_row_from_doc(row[indent+1])
45-
yield row[:indent+1] + [first]
54+
first, rest = self._split_doc(row[indent+1])
55+
yield row[:indent+1] + [first] + row[indent+2:]
4656
while rest:
47-
current, rest = self._split_row_from_doc(rest)
57+
current, rest = self._split_doc(rest)
4858
yield self._indent([self._line_continuation, current], indent)
4959

50-
def _split_row_from_doc(self, doc):
60+
def _split_doc(self, doc):
5161
if '\\n' not in doc:
5262
return doc, ''
53-
first, rest = doc.split('\\n', 1)
54-
if rest.startswith(' '):
55-
rest = rest[1:]
56-
return first, rest
57-
58-
def _get_indent(self, row, indented_table):
59-
indent = len(list(itertools.takewhile(lambda x: x == '', row)))
60-
min_indent = 1 if indented_table else 0
61-
return max(indent, min_indent)
63+
if '\\n ' in doc:
64+
doc = doc.replace('\\n ', '\\n')
65+
return doc.split('\\n', 1)
6266

6367
def _split_row(self, row, indent):
6468
while row:

0 commit comments

Comments
 (0)