-
-
Notifications
You must be signed in to change notification settings - Fork 672
/
html.py
362 lines (285 loc) · 11 KB
/
html.py
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
# coding: utf8
"""
weasyprint.html
---------------
Specific handling for some HTML elements, especially replaced elements.
Replaced elements (eg. <img> elements) are rendered externally and
behave as an atomic opaque box in CSS. In general, they may or may not
have intrinsic dimensions. But the only replaced elements currently
supported in WeasyPrint are images with intrinsic dimensions.
:copyright: Copyright 2011-2014 Simon Sapin and contributors, see AUTHORS.
:license: BSD, see LICENSE for details.
"""
from __future__ import division, unicode_literals
import os.path
import logging
import re
from .css import get_child_text
from .formatting_structure import boxes
from .urls import get_url_attribute
from .compat import xrange, urljoin
from .logger import LOGGER
from . import CSS
# XXX temporarily disable logging for user-agent stylesheet
level = LOGGER.level
LOGGER.setLevel(logging.ERROR)
HTML5_UA_STYLESHEET = CSS(
filename=os.path.join(os.path.dirname(__file__), 'css', 'html5_ua.css'))
LOGGER.setLevel(level)
# http://whatwg.org/C#space-character
HTML_WHITESPACE = ' \t\n\f\r'
HTML_SPACE_SEPARATED_TOKENS_RE = re.compile('[^%s]+' % HTML_WHITESPACE)
def ascii_lower(string):
r"""Transform (only) ASCII letters to lower case: A-Z is mapped to a-z.
:param string: An Unicode string.
:returns: A new Unicode string.
This is used for `ASCII case-insensitive
<http://whatwg.org/C#ascii-case-insensitive>`_ matching.
This is different from the :meth:`~py:str.lower` method of Unicode strings
which also affect non-ASCII characters,
sometimes mapping them into the ASCII range:
>>> keyword = u'Bac\N{KELVIN SIGN}ground'
>>> assert keyword.lower() == u'background'
>>> assert ascii_lower(keyword) != keyword.lower()
>>> assert ascii_lower(keyword) == u'bac\N{KELVIN SIGN}ground'
"""
# This turns out to be faster than unicode.translate()
return string.encode('utf8').lower().decode('utf8')
def element_has_link_type(element, link_type):
"""
Return whether the given element has a ``rel`` attribute with the
given link type.
:param link_type: Must be a lower-case string.
"""
return any(ascii_lower(token) == link_type for token in
HTML_SPACE_SEPARATED_TOKENS_RE.findall(element.get('rel', '')))
# Maps HTML tag names to function taking an HTML element and returning a Box.
HTML_HANDLERS = {}
def handle_element(element, box, get_image_from_uri):
"""Handle HTML elements that need special care.
:returns: a (possibly empty) list of boxes.
"""
if box.element_tag in HTML_HANDLERS:
return HTML_HANDLERS[element.tag](element, box, get_image_from_uri)
else:
return [box]
def handler(tag):
"""Return a decorator registering a function handling ``tag`` elements."""
def decorator(function):
"""Decorator registering a function handling ``tag`` elements."""
HTML_HANDLERS[tag] = function
return function
return decorator
def make_replaced_box(element, box, image):
"""Wrap an image in a replaced box.
That box is either block-level or inline-level, depending on what the
element should be.
"""
if box.style.display in ('block', 'list-item', 'table'):
type_ = boxes.BlockReplacedBox
else:
# TODO: support images with 'display: table-cell'?
type_ = boxes.InlineReplacedBox
return type_(element.tag, element.sourceline, box.style, image)
@handler('img')
def handle_img(element, box, get_image_from_uri):
"""Handle ``<img>`` elements, return either an image or the alt-text.
See: http://www.w3.org/TR/html5/embedded-content-1.html#the-img-element
"""
src = get_url_attribute(element, 'src')
alt = element.get('alt')
if src:
image = get_image_from_uri(src)
if image is not None:
return [make_replaced_box(element, box, image)]
else:
# Invalid image, use the alt-text.
if alt:
return [box.copy_with_children(
[boxes.TextBox.anonymous_from(box, alt)])]
elif alt == '':
# The element represents nothing
return []
else:
assert alt is None
# TODO: find some indicator that an image is missing.
# For now, just remove the image.
return []
else:
if alt:
return [box.copy_with_children(
[boxes.TextBox.anonymous_from(box, alt)])]
else:
return []
@handler('embed')
def handle_embed(element, box, get_image_from_uri):
"""Handle ``<embed>`` elements, return either an image or nothing.
See: http://www.w3.org/TR/html5/the-iframe-element.html#the-embed-element
"""
src = get_url_attribute(element, 'src')
type_ = element.get('type', '').strip()
if src:
image = get_image_from_uri(src, type_)
if image is not None:
return [make_replaced_box(element, box, image)]
# No fallback.
return []
@handler('object')
def handle_object(element, box, get_image_from_uri):
"""Handle ``<object>`` elements, return either an image or the fallback
content.
See: http://www.w3.org/TR/html5/the-iframe-element.html#the-object-element
"""
data = get_url_attribute(element, 'data')
type_ = element.get('type', '').strip()
if data:
image = get_image_from_uri(data, type_)
if image is not None:
return [make_replaced_box(element, box, image)]
# The element’s children are the fallback.
return [box]
def integer_attribute(element, box, name, minimum=1):
"""Read an integer attribute from the HTML element and set it on the box.
"""
value = element.get(name, '').strip()
if value:
try:
value = int(value)
except ValueError:
pass
else:
if value >= minimum:
setattr(box, name, value)
@handler('colgroup')
def handle_colgroup(element, box, _get_image_from_uri):
"""Handle the ``span`` attribute."""
if isinstance(box, boxes.TableColumnGroupBox):
if any(child.tag == 'col' for child in element):
box.span = None # sum of the children’s spans
else:
integer_attribute(element, box, 'span')
box.children = (
boxes.TableColumnBox.anonymous_from(box, [])
for _i in xrange(box.span))
return [box]
@handler('col')
def handle_col(element, box, _get_image_from_uri):
"""Handle the ``span`` attribute."""
if isinstance(box, boxes.TableColumnBox):
integer_attribute(element, box, 'span')
if box.span > 1:
# Generate multiple boxes
# http://lists.w3.org/Archives/Public/www-style/2011Nov/0293.html
return [box.copy() for _i in xrange(box.span)]
return [box]
@handler('th')
@handler('td')
def handle_td(element, box, _get_image_from_uri):
"""Handle the ``colspan``, ``rowspan`` attributes."""
if isinstance(box, boxes.TableCellBox):
# HTML 4.01 gives special meaning to colspan=0
# http://www.w3.org/TR/html401/struct/tables.html#adef-rowspan
# but HTML 5 removed it
# http://www.w3.org/TR/html5/tabular-data.html#attr-tdth-colspan
# rowspan=0 is still there though.
integer_attribute(element, box, 'colspan')
integer_attribute(element, box, 'rowspan', minimum=0)
return [box]
def find_base_url(html_document, fallback_base_url):
"""Return the base URL for the document.
See http://www.w3.org/TR/html5/urls.html#document-base-url
"""
first_base_element = next(iter(html_document.iter('base')), None)
if first_base_element is not None:
href = first_base_element.get('href', '').strip()
if href:
return urljoin(fallback_base_url, href)
return fallback_base_url
def get_html_metadata(html_document):
"""
Relevant specs:
http://www.whatwg.org/html#the-title-element
http://www.whatwg.org/html#standard-metadata-names
http://wiki.whatwg.org/wiki/MetaExtensions
"""
title = None
description = None
generator = None
keywords = []
authors = []
created = None
modified = None
for element in html_document.iter('title', 'meta'):
if element.tag == 'title' and title is None:
title = get_child_text(element)
elif element.tag == 'meta':
name = ascii_lower(element.get('name', ''))
content = element.get('content', '')
if name == 'keywords':
for keyword in map(strip_whitespace, content.split(',')):
if keyword not in keywords:
keywords.append(keyword)
elif name == 'author':
authors.append(content)
elif name == 'description' and description is None:
description = content
elif name == 'generator' and generator is None:
generator = content
elif name == 'dcterms.created' and created is None:
created = parse_w3c_date(name, element.sourceline, content)
elif name == 'dcterms.modified' and modified is None:
modified = parse_w3c_date(name, element.sourceline, content)
return dict(title=title, description=description, generator=generator,
keywords=keywords, authors=authors,
created=created, modified=modified)
def ascii_lower(string):
"""Return :obj:`string` with ASCII letters in lower-case.
http://www.whatwg.org/html#ascii-case-insensitive
"""
return string.encode('utf8').lower().decode('utf8')
def strip_whitespace(string):
"""Use the HTML definition of "space character",
not all Unicode Whitespace.
http://www.whatwg.org/html#strip-leading-and-trailing-whitespace
http://www.whatwg.org/html#space-character
"""
return string.strip(' \t\n\f\r')
# YYYY (eg 1997)
# YYYY-MM (eg 1997-07)
# YYYY-MM-DD (eg 1997-07-16)
# YYYY-MM-DDThh:mmTZD (eg 1997-07-16T19:20+01:00)
# YYYY-MM-DDThh:mm:ssTZD (eg 1997-07-16T19:20:30+01:00)
# YYYY-MM-DDThh:mm:ss.sTZD (eg 1997-07-16T19:20:30.45+01:00)
W3C_DATE_RE = re.compile('''
^
[ \t\n\f\r]*
(?P<year>\d\d\d\d)
(?:
-(?P<month>0\d|1[012])
(?:
-(?P<day>[012]\d|3[01])
(?:
T(?P<hour>[01]\d|2[0-3])
:(?P<minute>[0-5]\d)
(?:
:(?P<second>[0-5]\d)
(?:\.\d+)? # Second fraction, ignored
)?
(?:
Z | # UTC
(?P<tz_hour>[+-](?:[01]\d|2[0-3]))
:(?P<tz_minute>[0-5]\d)
)
)?
)?
)?
[ \t\n\f\r]*
$
''', re.VERBOSE)
def parse_w3c_date(meta_name, source_line, string):
"""http://www.w3.org/TR/NOTE-datetime"""
if W3C_DATE_RE.match(string):
return string
else:
LOGGER.warning('Invalid date in <meta name="%s"> line %i: %r',
meta_name, source_line, string)