2015-11-25 10:38:01 +03:00
|
|
|
|
# coding: utf-8
|
2012-03-22 02:19:27 +04:00
|
|
|
|
"""
|
|
|
|
|
WeasyPrint
|
|
|
|
|
==========
|
2011-04-28 21:15:30 +04:00
|
|
|
|
|
2012-03-22 02:19:27 +04:00
|
|
|
|
WeasyPrint converts web documents to PDF.
|
2011-08-09 14:45:51 +04:00
|
|
|
|
|
2012-03-22 02:19:27 +04:00
|
|
|
|
The public API is what is accessible from this "root" packages
|
|
|
|
|
without importing sub-modules.
|
2011-08-19 13:22:31 +04:00
|
|
|
|
|
2014-01-10 18:27:02 +04:00
|
|
|
|
:copyright: Copyright 2011-2014 Simon Sapin and contributors, see AUTHORS.
|
2012-03-22 02:19:27 +04:00
|
|
|
|
:license: BSD, see LICENSE for details.
|
2011-08-19 13:22:31 +04:00
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
2012-02-17 21:49:58 +04:00
|
|
|
|
from __future__ import division, unicode_literals
|
|
|
|
|
|
|
|
|
|
|
2015-08-04 17:33:05 +03:00
|
|
|
|
VERSION = '0.24'
|
2012-02-07 19:11:38 +04:00
|
|
|
|
__version__ = VERSION
|
2012-02-15 21:49:37 +04:00
|
|
|
|
|
2012-05-15 15:40:36 +04:00
|
|
|
|
# Used for 'User-Agent' in HTTP and 'Creator' in PDF
|
|
|
|
|
VERSION_STRING = 'WeasyPrint %s (http://weasyprint.org/)' % VERSION
|
|
|
|
|
|
2014-04-27 21:16:14 +04:00
|
|
|
|
__all__ = ['HTML', 'CSS', 'Attachment', 'Document', 'Page',
|
|
|
|
|
'default_url_fetcher', 'VERSION']
|
2012-05-15 15:40:36 +04:00
|
|
|
|
|
2012-07-13 14:24:55 +04:00
|
|
|
|
|
2013-06-20 15:58:24 +04:00
|
|
|
|
import contextlib
|
2013-07-22 03:10:25 +04:00
|
|
|
|
import html5lib
|
2013-02-25 18:21:25 +04:00
|
|
|
|
|
2013-06-20 15:17:03 +04:00
|
|
|
|
from .urls import (fetch, default_url_fetcher, path2url, ensure_url,
|
2014-04-22 22:40:46 +04:00
|
|
|
|
url_is_absolute)
|
2013-07-22 03:10:25 +04:00
|
|
|
|
from .compat import unicode
|
2013-02-25 18:21:25 +04:00
|
|
|
|
from .logger import LOGGER
|
|
|
|
|
# Some import are at the end of the file (after the CSS class) is defined
|
|
|
|
|
# to work around circular imports.
|
2012-07-13 14:24:55 +04:00
|
|
|
|
|
2012-02-15 21:49:37 +04:00
|
|
|
|
|
2012-09-19 19:37:52 +04:00
|
|
|
|
class HTML(object):
|
|
|
|
|
"""Represents an HTML document parsed by `lxml <http://lxml.de/>`_.
|
2012-02-27 18:07:41 +04:00
|
|
|
|
|
2012-09-19 19:37:52 +04:00
|
|
|
|
You can just create an instance with a positional argument:
|
|
|
|
|
``doc = HTML(something)``
|
|
|
|
|
The class will try to guess if the input is a filename, an absolute URL,
|
|
|
|
|
or a file-like object.
|
2012-02-27 18:07:41 +04:00
|
|
|
|
|
2012-09-19 19:37:52 +04:00
|
|
|
|
Alternatively, use **one** named argument so that no guessing is involved:
|
2012-02-27 18:07:41 +04:00
|
|
|
|
|
2012-09-19 19:37:52 +04:00
|
|
|
|
:param filename: A filename, relative to the current directory or absolute.
|
|
|
|
|
:param url: An absolute, fully qualified URL.
|
|
|
|
|
:param file_obj: a file-like: any object with a :meth:`~file.read` method.
|
|
|
|
|
:param string: a string of HTML source. (This argument must be named.)
|
|
|
|
|
:param tree: a parsed lxml tree. (This argument must be named.)
|
2012-02-27 18:07:41 +04:00
|
|
|
|
|
|
|
|
|
Specifying multiple inputs is an error: ``HTML(filename=foo, url=bar)``
|
2012-09-19 19:37:52 +04:00
|
|
|
|
will raise.
|
2012-02-27 18:07:41 +04:00
|
|
|
|
|
2012-09-19 19:37:52 +04:00
|
|
|
|
You can also pass optional named arguments:
|
2012-02-27 18:07:41 +04:00
|
|
|
|
|
2012-09-19 19:37:52 +04:00
|
|
|
|
:param encoding: Force the source character encoding.
|
|
|
|
|
:param base_url: The base used to resolve relative URLs
|
|
|
|
|
(eg. in ``<img src="../foo.png">``). If not provided, try to use
|
2012-10-05 20:50:40 +04:00
|
|
|
|
the input filename, URL, or ``name`` attribute of file-like objects.
|
2012-10-08 21:51:18 +04:00
|
|
|
|
:param url_fetcher: a function or other callable
|
|
|
|
|
with the same signature as :func:`default_url_fetcher` called to
|
|
|
|
|
fetch external resources such as stylesheets and images.
|
|
|
|
|
(See :ref:`url-fetchers`.)
|
2012-09-19 19:37:52 +04:00
|
|
|
|
:param media_type: The media type to use for ``@media``.
|
|
|
|
|
Defaults to ``'print'``. **Note:** In some cases like
|
|
|
|
|
``HTML(string=foo)`` relative URLs will be invalid if ``base_url``
|
|
|
|
|
is not provided.
|
2012-02-27 18:07:41 +04:00
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
def __init__(self, guess=None, filename=None, url=None, file_obj=None,
|
2012-07-13 14:24:55 +04:00
|
|
|
|
string=None, tree=None, encoding=None, base_url=None,
|
2014-04-22 22:40:46 +04:00
|
|
|
|
url_fetcher=default_url_fetcher, media_type='print'):
|
2013-06-20 15:58:24 +04:00
|
|
|
|
result = _select_source(
|
2012-07-13 14:24:55 +04:00
|
|
|
|
guess, filename, url, file_obj, string, tree, base_url,
|
|
|
|
|
url_fetcher)
|
2013-06-20 15:58:24 +04:00
|
|
|
|
with result as (source_type, source, base_url, protocol_encoding):
|
|
|
|
|
if source_type == 'tree':
|
|
|
|
|
result = source
|
2012-05-24 18:47:40 +04:00
|
|
|
|
else:
|
2013-06-20 15:58:24 +04:00
|
|
|
|
if not encoding:
|
|
|
|
|
encoding = protocol_encoding
|
2013-07-22 03:10:25 +04:00
|
|
|
|
if isinstance(source, unicode):
|
|
|
|
|
encoding = None
|
|
|
|
|
result = html5lib.parse(
|
|
|
|
|
source, treebuilder='lxml', encoding=encoding,
|
|
|
|
|
namespaceHTMLElements=False)
|
|
|
|
|
assert result
|
2012-08-03 17:20:22 +04:00
|
|
|
|
base_url = find_base_url(result, base_url)
|
2012-05-24 18:47:40 +04:00
|
|
|
|
if hasattr(result, 'getroot'):
|
|
|
|
|
result.docinfo.URL = base_url
|
2012-02-16 21:27:30 +04:00
|
|
|
|
result = result.getroot()
|
2012-05-24 18:47:40 +04:00
|
|
|
|
else:
|
|
|
|
|
result.getroottree().docinfo.URL = base_url
|
2012-02-16 21:27:30 +04:00
|
|
|
|
self.root_element = result
|
2012-06-15 00:08:34 +04:00
|
|
|
|
self.base_url = base_url
|
2012-07-13 14:24:55 +04:00
|
|
|
|
self.url_fetcher = url_fetcher
|
2012-08-02 15:04:31 +04:00
|
|
|
|
self.media_type = media_type
|
2012-02-15 21:49:37 +04:00
|
|
|
|
|
2012-09-25 18:01:47 +04:00
|
|
|
|
def _ua_stylesheets(self):
|
2012-02-16 21:52:36 +04:00
|
|
|
|
return [HTML5_UA_STYLESHEET]
|
|
|
|
|
|
2013-07-14 15:08:02 +04:00
|
|
|
|
def _get_metadata(self):
|
2014-04-22 22:40:46 +04:00
|
|
|
|
return get_html_metadata(self.root_element)
|
2013-07-14 15:08:02 +04:00
|
|
|
|
|
2012-10-05 22:12:05 +04:00
|
|
|
|
def render(self, stylesheets=None, enable_hinting=False):
|
2012-10-05 20:50:40 +04:00
|
|
|
|
"""Lay out and paginate the document, but do not (yet) export it
|
|
|
|
|
to PDF or another format.
|
2012-09-12 21:33:16 +04:00
|
|
|
|
|
2012-10-08 21:51:18 +04:00
|
|
|
|
This returns a :class:`~document.Document` object which provides
|
|
|
|
|
access to individual pages and various meta-data.
|
|
|
|
|
See :meth:`write_pdf` to get a PDF directly.
|
|
|
|
|
|
|
|
|
|
.. versionadded:: 0.15
|
2012-09-12 21:33:16 +04:00
|
|
|
|
|
2012-09-25 18:01:47 +04:00
|
|
|
|
:param stylesheets:
|
|
|
|
|
An optional list of user stylesheets. (See
|
|
|
|
|
:ref:`stylesheet-origins`\.) List elements are :class:`CSS`
|
|
|
|
|
objects, filenames, URLs, or file-like objects.
|
2012-09-20 19:21:44 +04:00
|
|
|
|
:type enable_hinting: bool
|
|
|
|
|
:param enable_hinting:
|
|
|
|
|
Whether text, borders and background should be *hinted* to fall
|
|
|
|
|
at device pixel boundaries. Should be enabled for pixel-based
|
|
|
|
|
output (like PNG) but not vector based output (like PDF).
|
2012-10-08 21:51:18 +04:00
|
|
|
|
:returns: A :class:`~document.Document` object.
|
2012-09-20 19:21:44 +04:00
|
|
|
|
|
2012-09-12 21:33:16 +04:00
|
|
|
|
"""
|
2012-10-05 22:12:05 +04:00
|
|
|
|
return Document._render(self, stylesheets, enable_hinting)
|
2012-10-02 20:59:02 +04:00
|
|
|
|
|
2014-04-22 22:40:46 +04:00
|
|
|
|
def write_pdf(self, target=None, stylesheets=None, zoom=1,
|
2014-04-27 21:16:14 +04:00
|
|
|
|
attachments=None):
|
2012-10-08 21:51:18 +04:00
|
|
|
|
"""Render the document to a PDF file.
|
|
|
|
|
|
|
|
|
|
This is a shortcut for calling :meth:`render`, then
|
|
|
|
|
:meth:`Document.write_pdf() <document.Document.write_pdf>`.
|
2012-02-15 21:49:37 +04:00
|
|
|
|
|
|
|
|
|
:param target:
|
2012-10-08 21:51:18 +04:00
|
|
|
|
A filename, file-like object, or :obj:`None`.
|
2012-02-15 21:49:37 +04:00
|
|
|
|
:param stylesheets:
|
2012-09-19 19:37:52 +04:00
|
|
|
|
An optional list of user stylesheets. (See
|
|
|
|
|
:ref:`stylesheet-origins`\.) The list’s elements are
|
|
|
|
|
:class:`CSS` objects, filenames, URLs, or file-like objects.
|
2012-11-23 01:27:34 +04:00
|
|
|
|
:type zoom: float
|
|
|
|
|
:param zoom:
|
|
|
|
|
The zoom factor in PDF units per CSS units.
|
|
|
|
|
**Warning**: All CSS units (even physical, like ``cm``)
|
|
|
|
|
are affected.
|
|
|
|
|
For values other than 1, physical CSS units will thus be “wrong”.
|
|
|
|
|
Page size declarations are affected too, even with keyword values
|
|
|
|
|
like ``@page { size: A3 landscape; }``
|
2014-04-22 22:40:46 +04:00
|
|
|
|
:param attachments: A list of additional file attachments for the
|
2014-04-26 01:35:43 +04:00
|
|
|
|
generated PDF document or :obj:`None`. The list's elements are
|
|
|
|
|
:class:`Attachment` objects, filenames, URLs or file-like objects.
|
2012-02-15 21:49:37 +04:00
|
|
|
|
:returns:
|
2012-10-08 21:51:18 +04:00
|
|
|
|
The PDF as byte string if :obj:`target` is not provided or
|
|
|
|
|
:obj:`None`, otherwise :obj:`None` (the PDF is written to
|
|
|
|
|
:obj:`target`.)
|
2012-09-19 19:37:52 +04:00
|
|
|
|
|
2012-02-15 21:49:37 +04:00
|
|
|
|
"""
|
2014-04-22 22:40:46 +04:00
|
|
|
|
return self.render(stylesheets).write_pdf(target, zoom, attachments)
|
2012-02-15 21:49:37 +04:00
|
|
|
|
|
2012-12-29 04:00:30 +04:00
|
|
|
|
def write_image_surface(self, stylesheets=None, resolution=96):
|
|
|
|
|
surface, _width, _height = (
|
|
|
|
|
self.render(stylesheets, enable_hinting=True)
|
|
|
|
|
.write_image_surface(resolution))
|
|
|
|
|
return surface
|
|
|
|
|
|
2012-09-20 21:24:58 +04:00
|
|
|
|
def write_png(self, target=None, stylesheets=None, resolution=96):
|
2012-10-05 20:50:40 +04:00
|
|
|
|
"""Paint the pages vertically to a single PNG image.
|
2012-02-15 21:49:37 +04:00
|
|
|
|
|
2012-10-05 20:50:40 +04:00
|
|
|
|
There is no decoration around pages other than those specified in CSS
|
|
|
|
|
with ``@page`` rules. The final image is as wide as the widest page.
|
|
|
|
|
Each page is below the previous one, centered horizontally.
|
2012-09-19 19:37:52 +04:00
|
|
|
|
|
2012-10-08 21:51:18 +04:00
|
|
|
|
This is a shortcut for calling :meth:`render`, then
|
|
|
|
|
:meth:`Document.write_png() <document.Document.write_png>`.
|
|
|
|
|
|
2012-02-15 21:49:37 +04:00
|
|
|
|
:param target:
|
2012-10-08 21:51:18 +04:00
|
|
|
|
A filename, file-like object, or :obj:`None`.
|
2012-02-15 21:49:37 +04:00
|
|
|
|
:param stylesheets:
|
2012-09-19 19:37:52 +04:00
|
|
|
|
An optional list of user stylesheets. (See
|
|
|
|
|
:ref:`stylesheet-origins`\.) The list’s elements are
|
|
|
|
|
:class:`CSS` objects, filenames, URLs, or file-like objects.
|
2012-09-20 19:21:44 +04:00
|
|
|
|
:type resolution: float
|
2012-09-19 19:37:52 +04:00
|
|
|
|
:param resolution:
|
|
|
|
|
The output resolution in PNG pixels per CSS inch. At 96 dpi
|
|
|
|
|
(the default), PNG pixels match the CSS ``px`` unit.
|
2012-02-15 21:49:37 +04:00
|
|
|
|
:returns:
|
2012-10-08 21:51:18 +04:00
|
|
|
|
The image as byte string if :obj:`target` is not provided or
|
|
|
|
|
:obj:`None`, otherwise :obj:`None` (the image is written to
|
|
|
|
|
:obj:`target`.)
|
2012-09-19 19:37:52 +04:00
|
|
|
|
|
2012-02-15 21:49:37 +04:00
|
|
|
|
"""
|
2012-10-05 20:19:17 +04:00
|
|
|
|
png_bytes, _width, _height = (
|
2012-10-05 22:12:05 +04:00
|
|
|
|
self.render(stylesheets, enable_hinting=True)
|
|
|
|
|
.write_png(target, resolution))
|
2012-10-05 20:19:17 +04:00
|
|
|
|
return png_bytes
|
2012-02-15 21:49:37 +04:00
|
|
|
|
|
|
|
|
|
|
2012-09-19 19:37:52 +04:00
|
|
|
|
class CSS(object):
|
|
|
|
|
"""Represents a CSS stylesheet parsed by tinycss.
|
|
|
|
|
|
|
|
|
|
An instance is created in the same way as :class:`HTML`, except that
|
|
|
|
|
the ``tree`` parameter is not available. All other parameters are the same.
|
2012-02-15 21:49:37 +04:00
|
|
|
|
|
2012-09-19 19:37:52 +04:00
|
|
|
|
``CSS`` objects have no public attribute or method. They are only meant to
|
2012-10-05 20:50:40 +04:00
|
|
|
|
be used in the :meth:`~HTML.write_pdf`, :meth:`~HTML.write_png` and
|
|
|
|
|
:meth:`~HTML.render` methods of :class:`HTML` objects.
|
2012-02-15 21:49:37 +04:00
|
|
|
|
|
2012-02-27 18:07:41 +04:00
|
|
|
|
"""
|
|
|
|
|
def __init__(self, guess=None, filename=None, url=None, file_obj=None,
|
2012-03-24 16:39:31 +04:00
|
|
|
|
string=None, encoding=None, base_url=None,
|
2012-08-02 15:04:31 +04:00
|
|
|
|
url_fetcher=default_url_fetcher, _check_mime_type=False,
|
|
|
|
|
media_type='print'):
|
2013-06-20 15:58:24 +04:00
|
|
|
|
result = _select_source(
|
2012-05-24 18:47:40 +04:00
|
|
|
|
guess, filename, url, file_obj, string, tree=None,
|
2012-07-13 14:24:55 +04:00
|
|
|
|
base_url=base_url, url_fetcher=url_fetcher,
|
|
|
|
|
check_css_mime_type=_check_mime_type,)
|
2013-06-20 15:58:24 +04:00
|
|
|
|
with result as (source_type, source, base_url, protocol_encoding):
|
|
|
|
|
if source_type == 'string' and not isinstance(source, bytes):
|
2012-03-24 16:39:31 +04:00
|
|
|
|
# unicode, no encoding
|
2013-06-20 15:58:24 +04:00
|
|
|
|
stylesheet = PARSER.parse_stylesheet(source)
|
|
|
|
|
else:
|
|
|
|
|
if source_type == 'file_obj':
|
|
|
|
|
source = source.read()
|
|
|
|
|
stylesheet = PARSER.parse_stylesheet_bytes(
|
|
|
|
|
source, linking_encoding=encoding,
|
|
|
|
|
protocol_encoding=protocol_encoding)
|
2012-03-24 16:39:31 +04:00
|
|
|
|
self.base_url = base_url
|
2012-03-25 04:41:02 +04:00
|
|
|
|
self.rules = list(preprocess_stylesheet(
|
2013-06-20 15:58:24 +04:00
|
|
|
|
media_type, base_url, stylesheet.rules, url_fetcher))
|
|
|
|
|
# TODO: do not keep this self.stylesheet around?
|
|
|
|
|
self.stylesheet = stylesheet
|
2012-03-24 16:39:31 +04:00
|
|
|
|
for error in self.stylesheet.errors:
|
2013-08-19 16:38:09 +04:00
|
|
|
|
LOGGER.warning(error)
|
2012-03-24 16:39:31 +04:00
|
|
|
|
|
2014-04-27 21:16:14 +04:00
|
|
|
|
|
2014-04-26 01:35:43 +04:00
|
|
|
|
class Attachment(object):
|
|
|
|
|
"""Represents a file attachment for a PDF document.
|
|
|
|
|
|
|
|
|
|
An instance is created in the same way as :class:`HTML`, except that
|
|
|
|
|
the HTML specific parameters are not supported. An optional description can
|
|
|
|
|
be provided with the ``description`` parameter.
|
|
|
|
|
|
|
|
|
|
:param description: A description of the attachment to be included in the
|
|
|
|
|
PDF document. May be :obj:`None`
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
def __init__(self, guess=None, filename=None, url=None, file_obj=None,
|
|
|
|
|
string=None, base_url=None, url_fetcher=default_url_fetcher,
|
|
|
|
|
description=None):
|
|
|
|
|
self.source = _select_source(
|
|
|
|
|
guess, filename, url, file_obj, string, tree=None,
|
|
|
|
|
base_url=base_url, url_fetcher=url_fetcher)
|
|
|
|
|
self.description = description
|
|
|
|
|
|
2012-02-15 21:49:37 +04:00
|
|
|
|
|
2013-06-20 15:58:24 +04:00
|
|
|
|
@contextlib.contextmanager
|
2012-03-24 16:39:31 +04:00
|
|
|
|
def _select_source(guess=None, filename=None, url=None, file_obj=None,
|
2012-06-15 00:08:34 +04:00
|
|
|
|
string=None, tree=None, base_url=None,
|
2012-07-13 14:24:55 +04:00
|
|
|
|
url_fetcher=default_url_fetcher, check_css_mime_type=False):
|
2012-02-27 18:07:41 +04:00
|
|
|
|
"""
|
|
|
|
|
Check that only one input is not None, and return it with the
|
|
|
|
|
normalized ``base_url``.
|
2012-05-31 16:38:34 +04:00
|
|
|
|
|
2012-02-27 18:07:41 +04:00
|
|
|
|
"""
|
2012-02-15 21:49:37 +04:00
|
|
|
|
if base_url is not None:
|
|
|
|
|
base_url = ensure_url(base_url)
|
|
|
|
|
|
2012-02-27 18:07:41 +04:00
|
|
|
|
nones = [guess is None, filename is None, url is None,
|
2012-05-24 18:47:40 +04:00
|
|
|
|
file_obj is None, string is None, tree is None]
|
|
|
|
|
if nones == [False, True, True, True, True, True]:
|
2012-02-27 18:07:41 +04:00
|
|
|
|
if hasattr(guess, 'read'):
|
|
|
|
|
type_ = 'file_obj'
|
2012-05-31 16:38:34 +04:00
|
|
|
|
elif url_is_absolute(guess):
|
2012-02-27 18:07:41 +04:00
|
|
|
|
type_ = 'url'
|
2012-02-15 21:49:37 +04:00
|
|
|
|
else:
|
2012-02-27 18:07:41 +04:00
|
|
|
|
type_ = 'filename'
|
2013-06-20 15:58:24 +04:00
|
|
|
|
result = _select_source(
|
2012-07-13 14:24:55 +04:00
|
|
|
|
base_url=base_url, url_fetcher=url_fetcher,
|
|
|
|
|
check_css_mime_type=check_css_mime_type,
|
2013-07-11 21:43:11 +04:00
|
|
|
|
# Use str() to work around http://bugs.python.org/issue4978
|
|
|
|
|
# See https://github.com/Kozea/WeasyPrint/issues/97
|
|
|
|
|
**{str(type_): guess})
|
2013-06-20 15:58:24 +04:00
|
|
|
|
with result as result:
|
|
|
|
|
yield result
|
|
|
|
|
elif nones == [True, False, True, True, True, True]:
|
2012-03-24 16:39:31 +04:00
|
|
|
|
if base_url is None:
|
|
|
|
|
base_url = path2url(filename)
|
2013-06-20 15:58:24 +04:00
|
|
|
|
with open(filename, 'rb') as file_obj:
|
|
|
|
|
yield 'file_obj', file_obj, base_url, None
|
|
|
|
|
elif nones == [True, True, False, True, True, True]:
|
|
|
|
|
with fetch(url_fetcher, url) as result:
|
|
|
|
|
if check_css_mime_type and result['mime_type'] != 'text/css':
|
2013-08-19 16:38:09 +04:00
|
|
|
|
LOGGER.warning(
|
2013-06-20 15:58:24 +04:00
|
|
|
|
'Unsupported stylesheet type %s for %s',
|
|
|
|
|
result['mime_type'], result['redirected_url'])
|
|
|
|
|
yield 'string', '', base_url, None
|
|
|
|
|
else:
|
2013-12-13 17:12:31 +04:00
|
|
|
|
proto_encoding = result.get('encoding')
|
|
|
|
|
if base_url is None:
|
|
|
|
|
base_url = result.get('redirected_url', url)
|
|
|
|
|
if 'string' in result:
|
|
|
|
|
yield 'string', result['string'], base_url, proto_encoding
|
|
|
|
|
else:
|
|
|
|
|
yield (
|
|
|
|
|
'file_obj', result['file_obj'], base_url,
|
|
|
|
|
proto_encoding)
|
2013-06-20 15:58:24 +04:00
|
|
|
|
elif nones == [True, True, True, False, True, True]:
|
2012-02-16 16:40:29 +04:00
|
|
|
|
if base_url is None:
|
2012-10-05 20:50:40 +04:00
|
|
|
|
# filesystem file-like objects have a 'name' attribute.
|
2012-02-16 16:40:29 +04:00
|
|
|
|
name = getattr(file_obj, 'name', None)
|
2012-07-18 16:31:55 +04:00
|
|
|
|
# Some streams have a .name like '<stdin>', not a filename.
|
|
|
|
|
if name and not name.startswith('<'):
|
2012-02-16 16:40:29 +04:00
|
|
|
|
base_url = ensure_url(name)
|
2013-06-20 15:58:24 +04:00
|
|
|
|
yield 'file_obj', file_obj, base_url, None
|
|
|
|
|
elif nones == [True, True, True, True, False, True]:
|
|
|
|
|
yield 'string', string, base_url, None
|
|
|
|
|
elif nones == [True, True, True, True, True, False]:
|
|
|
|
|
yield 'tree', tree, base_url, None
|
|
|
|
|
else:
|
|
|
|
|
raise TypeError('Expected exactly one source, got ' + (
|
|
|
|
|
', '.join(
|
|
|
|
|
name for i, name in enumerate(
|
|
|
|
|
'guess filename url file_obj string tree'.split())
|
|
|
|
|
if not nones[i]
|
|
|
|
|
) or 'nothing'
|
|
|
|
|
))
|
2013-02-25 18:21:25 +04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Work around circular imports.
|
|
|
|
|
from .css import PARSER, preprocess_stylesheet
|
2013-07-14 15:08:02 +04:00
|
|
|
|
from .html import find_base_url, HTML5_UA_STYLESHEET, get_html_metadata
|
2013-02-25 18:21:25 +04:00
|
|
|
|
from .document import Document, Page
|