Robot Framework Integrated Development Environment (RIDE)
htmlformatter.py
Go to the documentation of this file.
1 # Copyright 2008-2015 Nokia Networks
2 # Copyright 2016- Robot Framework Foundation
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 
16 import re
17 
18 from robotide.lib.robot.utils import attribute_escape, html_escape
19 
20 from .formatters import _DataFileFormatter
21 
22 
24 
27  _split_multiline_doc = False
28 
29  def _format_row(self, row, table=None):
30  row = self._pad_pad(self._escape_consecutive_whitespace_escape_consecutive_whitespace(row), table)
31  if self._is_documentation_row_is_documentation_row(row):
32  return self._create_documentation_row_create_documentation_row(row)
33  first_cell = self._create_first_cell_create_first_cell(row[0], table)
34  if self._is_indented_documentation_row_is_indented_documentation_row(row[1:], table):
35  return self._create_indented_documentation_row_create_indented_documentation_row(first_cell, row[1:])
36  return [first_cell] + [HtmlCell(c) for c in row[1:]]
37 
38  def _is_documentation_row(self, row):
39  return row[0] == 'Documentation'
40 
41  def _create_documentation_row(self, row):
42  return [NameCell(row[0]), DocumentationCell(row[1], span=self._column_count_column_count-1)]
43 
44  def _is_indented_documentation_row(self, cells, table):
45  return self._is_indented_table_is_indented_table(table) and cells and \
46  cells[0] == '[Documentation]'
47 
48  def _create_indented_documentation_row(self, first_cell, cells):
49  start = [first_cell, HtmlCell(cells[0])]
50  if any(c.startswith('#') for c in cells):
51  return start + [HtmlCell(c) for c in cells[1:]]
52  return start + [DocumentationCell(cells[1], self._column_count_column_count-2)]
53 
54  def _create_first_cell(self, cell, table):
55  if self._is_indented_table_is_indented_table(table) and cell:
56  return AnchorNameCell(cell, 'keyword' if table.type == 'keyword'
57  else 'test')
58  return NameCell(cell)
59 
60  def format_header(self, table):
61  if not self._should_align_columns_should_align_columns(table) or len(table.header) == 1:
62  return [HeaderCell(table.header[0], self._column_count_column_count)]
63  headers = self._pad_header_pad_header(table)
64  return [HeaderCell(hdr) for hdr in headers]
65 
66  def _pad_header(self, table):
67  header = table.header
68  return header + [''] * (self._get_column_count_get_column_count(table) - len(header))
69 
70  def _pad(self, row, table):
71  return row + [''] * (self._get_column_count_get_column_count(table) - len(row))
72 
73  def _get_column_count(self, table):
74  if table is None or len(table.header) == 1 \
75  or not self._is_indented_table_is_indented_table(table):
76  return self._column_count_column_count
77  return max(self._max_column_count_max_column_count(table), len(table.header))
78 
79  def _max_column_count(self, table):
80  count = 0
81  for item in table:
82  for child in item:
83  count = max(count, len(child.as_list()) + 1)
84  return count
85 
86 
87 class HtmlCell():
88 
91  _backslash_matcher = re.compile(r'(\\+)n ?')
92 
93  def __init__(self, content='', attributes=None, tag='td', escape=True):
94  if escape:
95  content = html_escape(content)
96  self.contentcontent = self._replace_newlines_replace_newlines(content)
97  self.attributesattributes = attributes or {}
98  self.tagtag = tag
99 
100  def _replace_newlines(self, content):
101  def replacer(match):
102  backslash_count = len(match.group(1))
103  if backslash_count % 2 == 1:
104  return '%sn<br>\n' % match.group(1)
105  return match.group()
106  return self._backslash_matcher_backslash_matcher.sub(replacer, content)
107 
108 
110 
111  def __init__(self, name='', attributes=None):
112  HtmlCell.__init__(self, name, {'class': 'name'})
113 
114 
116 
117  def __init__(self, name, type_):
118  HtmlCell.__init__(self, self._link_from_name_link_from_name(name, type_),
119  {'class': 'name'}, escape=False)
120 
121  def _link_from_name(self, name, type_):
122  return '<a name="%s_%s">%s</a>' % (type_, attribute_escape(name),
123  html_escape(name))
124 
125 
127 
128  def __init__(self, content, span):
129  HtmlCell.__init__(self, content, {'class': 'colspan%d' % span,
130  'colspan': '%d' % span})
131 
132 
134 
135  def __init__(self, name, span=1):
136  HtmlCell.__init__(self, name, {'class': 'name', 'colspan': '%d' % span},
137  tag='th')
def __init__(self, content='', attributes=None, tag='td', escape=True)
def _create_indented_documentation_row(self, first_cell, cells)
def __init__(self, name='', attributes=None)
def html_escape(text, linkify=True)
Definition: markuputils.py:40