diff --git a/weasyprint/__init__.py b/weasyprint/__init__.py index e475e6e3..8dd3ea18 100644 --- a/weasyprint/__init__.py +++ b/weasyprint/__init__.py @@ -110,7 +110,8 @@ class HTML(object): def _get_metadata(self): return get_html_metadata(self.root_element) - def render(self, stylesheets=None, enable_hinting=False): + def render(self, stylesheets=None, enable_hinting=False, + presentational_hints=False): """Lay out and paginate the document, but do not (yet) export it to PDF or another format. @@ -129,10 +130,14 @@ class HTML(object): 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). + :type presentational_hints: bool + :param presentational_hints: Whether HTML presentational hints are + followed. :returns: A :class:`~document.Document` object. """ - return Document._render(self, stylesheets, enable_hinting) + return Document._render( + self, stylesheets, enable_hinting, presentational_hints) def write_pdf(self, target=None, stylesheets=None, zoom=1, attachments=None): diff --git a/weasyprint/__main__.py b/weasyprint/__main__.py index 326ab7db..a5fabe2d 100644 --- a/weasyprint/__main__.py +++ b/weasyprint/__main__.py @@ -66,6 +66,10 @@ def main(argv=None, stdout=None, stdin=None): Adds an attachment to the document which is included in the PDF output. This option can be added multiple times to attach more files. + .. option:: -p, --presentational-hints + + Follow HTML presentational hints. + .. option:: --version Show the version number. Other options and arguments are ignored. @@ -100,6 +104,8 @@ def main(argv=None, stdout=None, stdin=None): parser.add_argument('-a', '--attachment', action='append', help='URL or filename of a file ' 'to attach to the PDF document') + parser.add_argument('-p', '--presentational-hints', action='store_true', + help='Follow HTML presentational hints.') parser.add_argument( 'input', help='URL or filename of the HTML input, or - for stdin') parser.add_argument( @@ -152,7 +158,8 @@ def main(argv=None, stdout=None, stdin=None): parser.error('--attachment only applies for the PDF format.') html = HTML(source, base_url=args.base_url, encoding=args.encoding, - media_type=args.media_type) + media_type=args.media_type, + presentational_hints=args.presentational_hints) getattr(html, 'write_' + format_)(output, **kwargs) diff --git a/weasyprint/css/__init__.py b/weasyprint/css/__init__.py index 4f68d80e..9e641883 100644 --- a/weasyprint/css/__init__.py +++ b/weasyprint/css/__init__.py @@ -798,9 +798,9 @@ def preprocess_stylesheet(device_media_type, base_url, rules, url_fetcher): yield margin_rule, selector_list, declarations -def get_all_computed_styles(html, user_stylesheets=None): - """Compute all the computed styles of all elements - in the given ``html`` document. +def get_all_computed_styles(html, user_stylesheets=None, + presentational_hints=False): + """Compute all the computed styles of all elements in ``html`` document. Do everything from finding author stylesheets to parsing and applying them. @@ -848,14 +848,15 @@ def get_all_computed_styles(html, user_stylesheets=None): cascaded_styles, name, values, weight, element, pseudo_type) - specificity = (0, 0, 0, 0) - for element, declarations, base_url in find_presentational_hints( - element_tree): - for name, values, importance in preprocess_declarations( - base_url, declarations): - precedence = declaration_precedence('author', importance) - weight = (precedence, specificity) - add_declaration(cascaded_styles, name, values, weight, element) + if presentational_hints: + specificity = (0, 0, 0, 0) + for element, declarations, base_url in find_presentational_hints( + element_tree): + for name, values, importance in preprocess_declarations( + base_url, declarations): + precedence = declaration_precedence('author', importance) + weight = (precedence, specificity) + add_declaration(cascaded_styles, name, values, weight, element) specificity = (1, 0, 0, 0) for element, declarations, base_url in find_style_attributes(element_tree): diff --git a/weasyprint/document.py b/weasyprint/document.py index a337b193..12df8598 100644 --- a/weasyprint/document.py +++ b/weasyprint/document.py @@ -313,11 +313,13 @@ class Document(object): """ @classmethod - def _render(cls, html, stylesheets, enable_hinting): - style_for = get_all_computed_styles(html, user_stylesheets=[ - css if hasattr(css, 'rules') - else CSS(guess=css, media_type=html.media_type) - for css in stylesheets or []]) + def _render(cls, html, stylesheets, enable_hinting, + presentational_hints=False): + style_for = get_all_computed_styles( + html, presentational_hints=presentational_hints, user_stylesheets=[ + css if hasattr(css, 'rules') + else CSS(guess=css, media_type=html.media_type) + for css in stylesheets or []]) get_image_from_uri = functools.partial( images.get_image_from_uri, {}, html.url_fetcher) page_boxes = layout_document(