forked from robotframework/robotframework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhtmlutils.py
More file actions
144 lines (122 loc) · 4.91 KB
/
htmlutils.py
File metadata and controls
144 lines (122 loc) · 4.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# Copyright 2008-2015 Nokia Networks
# Copyright 2016- Robot Framework Foundation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import re
try:
from urllib import quote
except ImportError:
from urllib.parse import quote
from robot.errors import DataError
from robot.utils import html_escape, html_format, NormalizedDict
from robot.utils.htmlformatters import HeaderFormatter
class DocFormatter:
_header_regexp = re.compile(r'<h([234])>(.+?)</h\1>')
_name_regexp = re.compile('`(.+?)`')
def __init__(self, keywords, data_types, introduction, doc_format='ROBOT'):
self._doc_to_html = DocToHtml(doc_format)
self._targets = self._get_targets(keywords, data_types, introduction,
robot_format=doc_format == 'ROBOT')
def _get_targets(self, keywords, data_types, introduction, robot_format):
targets = {
'introduction': 'Introduction',
'library introduction': 'Introduction',
'importing': 'Importing',
'library importing': 'Importing',
'keywords': 'Keywords',
'data types': 'Data types'
}
for kw in keywords:
targets[kw.name] = kw.name
for dt in data_types:
targets[dt.name] = dt.name
if robot_format:
for header in self._yield_header_targets(introduction):
targets[header] = header
return self._escape_and_encode_targets(targets)
def _yield_header_targets(self, introduction):
headers = HeaderFormatter()
for line in introduction.splitlines():
match = headers.match(line.strip())
if match:
yield match.group(2)
def _escape_and_encode_targets(self, targets):
return NormalizedDict((html_escape(key), self._encode_uri_component(value))
for key, value in targets.items())
def _encode_uri_component(self, value):
# Emulates encodeURIComponent javascript function
return quote(value.encode('UTF-8'), safe="-_.!~*'()")
def html(self, doc, intro=False):
doc = self._doc_to_html(doc)
if intro:
doc = self._header_regexp.sub(r'<h\1 id="\2">\2</h\1>', doc)
return self._name_regexp.sub(self._link_keywords, doc)
def _link_keywords(self, match):
name = match.group(1)
if name in self._targets:
return '<a href="#%s" class="name">%s</a>' % (self._targets[name], name)
return '<span class="name">%s</span>' % name
class DocToHtml:
def __init__(self, doc_format):
self._formatter = self._get_formatter(doc_format)
def _get_formatter(self, doc_format):
try:
return {'ROBOT': html_format,
'TEXT': self._format_text,
'HTML': lambda doc: doc,
'REST': self._format_rest}[doc_format]
except KeyError:
raise DataError("Invalid documentation format '%s'." % doc_format)
def _format_text(self, doc):
return '<p style="white-space: pre-wrap">%s</p>' % html_escape(doc)
def _format_rest(self, doc):
try:
from docutils.core import publish_parts
except ImportError:
raise DataError("reST format requires 'docutils' module to be installed.")
parts = publish_parts(doc, writer_name='html',
settings_overrides={'syntax_highlight': 'short'})
return parts['html_body']
def __call__(self, doc):
return self._formatter(doc)
class HtmlToText:
html_tags = {
'b': '*',
'i': '_',
'strong': '*',
'em': '_',
'code': '``',
'div.*?': ''
}
html_chars = {
'<br */?>': '\n',
'&': '&',
'<': '<',
'>': '>',
'"': '"',
''': "'"
}
def get_shortdoc_from_html(self, doc):
match = re.search(r'<p.*?>(.*?)</?p>', doc, re.DOTALL)
if match:
doc = match.group(1)
doc = self.html_to_plain_text(doc)
return doc
def html_to_plain_text(self, doc):
for tag, repl in self.html_tags.items():
doc = re.sub(r'<%(tag)s>(.*?)</%(tag)s>' % {'tag': tag},
r'%(repl)s\1%(repl)s' % {'repl': repl}, doc,
flags=re.DOTALL)
for html, text in self.html_chars.items():
doc = re.sub(html, text, doc)
return doc