Skip to content

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also .

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also .
...
  • 4 commits
  • 5 files changed
  • 0 commit comments
  • 2 contributors
Commits on Jun 23, 2012
@SimonSapin Move the Navigator startup out of exception handling.
Otherwise, any other traceback would be prefixed with:

Traceback (most recent call last):
  File "weasyprint/navigator.py", line 160, in run
    from werkzeug.serving import run_simple
ImportError: No module named werkzeug.serving

During handling of the above exception, another exception occurred:
18cc295
@SimonSapin Test the Navigator 78461b8
@liZe liZe Remove useless import 6c4f05f
@liZe liZe Merge branch 'master' of github.com:Kozea/WeasyPrint 1c9a910
Showing with 58 additions and 13 deletions.
  1. +1 −1 weasyprint/images.py
  2. +0 −2 weasyprint/layout/inlines.py
  3. +9 −6 weasyprint/navigator.py
  4. +4 −4 weasyprint/pdf.py
  5. +44 −0 weasyprint/tests/test_api.py
View
2 weasyprint/images.py
@@ -123,7 +123,7 @@ def get_image_from_uri(cache, uri, type_=None):
finally:
try:
file_like.close()
- except Exception:
+ except Exception: # pragma: no cover
# May already be closed or something.
# This is just cleanup anyway.
pass
View
2 weasyprint/layout/inlines.py
@@ -13,8 +13,6 @@
from __future__ import division, unicode_literals
import functools
-import cairo
-
from .absolute import absolute_layout, AbsolutePlaceholder
from .markers import image_marker_layout
from .percentages import resolve_percentages, resolve_one_percentage
View
15 weasyprint/navigator.py
@@ -148,7 +148,7 @@ def app(environ, start_response):
body = '<!doctype html><title>Not Found</title><h1>Not Found</h1>'
start_response(status, [
- ('Content-type', content_type),
+ ('Content-Type', content_type),
('Content-Length', str(len(body))),
])
return [body]
@@ -159,13 +159,16 @@ def run(port=5000):
try:
from werkzeug.serving import run_simple
except ImportError:
- print('Could not import Werkzeug, running without the reloader '
- 'or debugger.')
- from wsgiref.simple_server import make_server
- print('Listening on http://%s:%s/ ...' % (host, port))
- make_server(host, port, app).serve_forever()
+ pass
else:
run_simple(host, port, app, use_reloader=True, use_debugger=True)
+ return
+ print('Could not import Werkzeug, running without the reloader '
+ 'or debugger.')
+ from wsgiref.simple_server import make_server
+ server = make_server(host, port, app)
+ print('Listening on http://%s:%s/ ...' % (host, port))
+ server.serve_forever()
if __name__ == '__main__':
View
8 weasyprint/pdf.py
@@ -436,16 +436,16 @@ def write_pdf_metadata(document, fileobj):
'/Outlines {0} 0 R /PageMode /UseOutlines', bookmark_root))
for bookmark in bookmarks:
content = [pdf_format('<< /Title {0!P}\n', bookmark['label'])]
+ content.append(pdf_format(
+ '/A << /Type /Action /S /GoTo /D [{0} /XYZ {1:f} {2:f} 0] >>',
+ *bookmark['destination']))
if bookmark['Count']:
content.append(pdf_format('/Count {0}\n', bookmark['Count']))
for key in ['Parent', 'Prev', 'Next', 'First', 'Last']:
if bookmark[key]:
content.append(pdf_format(
'/{0} {1} 0 R\n', key, bookmark[key] + bookmark_root))
- content.append(pdf_format(
- '/A << /Type /Action /S /GoTo '
- '/D [{0} /XYZ {1:f} {2:f} 0] >>\n>>',
- *bookmark['destination']))
+ content.append(b'>>')
pdf.write_new_object(b''.join(content))
for page, page_links in zip(pdf.pages, links):
View
44 weasyprint/tests/test_api.py
@@ -22,12 +22,14 @@
import pystacia
import lxml.html
+import pytest
from .testing_utils import (
resource_filename, assert_no_logs, TEST_UA_STYLESHEET)
from ..compat import urljoin
from .. import HTML, CSS
from .. import __main__
+from .. import navigator
CHDIR_LOCK = threading.Lock()
@@ -100,6 +102,8 @@ def _test_resource(class_, basename, check, **kwargs):
encoding = kwargs.get('encoding') or 'utf8'
check(class_(string=content.decode(encoding), # unicode
base_url=relative_filename, **kwargs))
+ with pytest.raises(TypeError):
+ class_(filename='foo', url='bar')
@assert_no_logs
@@ -367,3 +371,43 @@ def test_unicode_filenames():
TestHTML(string=html).write_png(bytes_filename)
assert read_file(unicode_filename) == png_bytes
+
+
+def wsgi_client(path_info):
+ start_response_calls = []
+ def start_response(status, headers):
+ start_response_calls.append((status, headers))
+ environ = {'PATH_INFO': path_info}
+ response = b''.join(navigator.app(environ, start_response))
+ assert len(start_response_calls) == 1
+ status, headers = start_response_calls[0]
+ return status, dict(headers), response
+
+
+@assert_no_logs
+def test_navigator():
+ with temp_directory() as temp:
+ filename = os.path.join(temp, 'test.html')
+ write_file(filename, b'''
+ <h1 id=foo><a href="http://weasyprint.org">Lorem ipsum</a></h1>
+ <h2><a href="#foo">bar</a></h2>
+ ''')
+ status, headers, body = wsgi_client('/view/file://' + filename)
+ body = body.decode('utf8')
+ assert status == '200 OK'
+ assert headers['Content-Type'].startswith('text/html;')
+ assert '<title>WeasyPrint Navigator</title>' in body
+ assert '<img src="data:image/png;base64,' in body
+ assert ' name="foo"></a>' in body
+ assert ' href="#foo"></a>' in body
+ assert ' href="/view/http://weasyprint.org"></a>' in body
+
+ status, headers, body = wsgi_client('/pdf/file://' + filename)
+ assert status == '200 OK'
+ assert headers['Content-Type'] == 'application/pdf'
+ assert body.startswith(b'%PDF')
+ assert (b'/A << /Type /Action /S /URI /URI '
+ b'(http://weasyprint.org) >>') in body
+ lipsum = '\ufeffLorem ipsum'.encode('utf-16-be')
+ assert (b'<< /Title (' + lipsum +
+ b')\n/A << /Type /Action /S /GoTo') in body

No commit comments for this range

Something went wrong with that request. Please try again.