#! /usr/bin/env python3 # $Id$ # Author: reggie dugard # Copyright: This module has been placed in the public domain. """ Test for fragment code in HTML writer. Note: the 'body' and 'whole' entries have been removed from the parts dictionaries (redundant), along with 'meta' and 'stylesheet' entries with standard values, and any entries with empty values. """ from pathlib import Path import sys import unittest if __name__ == '__main__': # prepend the "docutils root" to the Python library path # so we import the local `docutils` package. sys.path.insert(0, str(Path(__file__).resolve().parents[2])) import docutils import docutils.core class Html5WriterPublishPartsTestCase(unittest.TestCase): """Test case for HTML writer via the publish_parts interface.""" # maxDiff = None def test_publish(self): writer_name = 'html5' for name, (settings_overrides, cases) in totest.items(): for casenum, (case_input, case_expected) in enumerate(cases): with self.subTest(id=f'totest[{name!r}][{casenum}]'): parts = docutils.core.publish_parts( source=case_input, writer_name=writer_name, settings_overrides={ '_disable_config': True, 'strict_visitor': True, 'stylesheet': '', 'section_self_link': True, **settings_overrides, } ) self.assertEqual(self.format_output(parts), case_expected) standard_content_type_template = '\n' standard_generator_template = '\n' standard_viewport_template = '\n' standard_html_meta_value = (standard_content_type_template + standard_generator_template + standard_viewport_template) standard_meta_value = standard_html_meta_value % 'utf-8' standard_html_prolog = '\n' def format_output(self, parts): """Minimize & standardize the output.""" # remove redundant parts & uninteresting parts: del parts['whole'] assert parts['body'] == parts['fragment'] del parts['body'] del parts['body_pre_docinfo'] del parts['body_prefix'] del parts['body_suffix'] del parts['head'] del parts['head_prefix'] del parts['encoding'] del parts['errors'] del parts['version'] # remove standard portions: parts['meta'] = parts['meta'].replace(self.standard_meta_value, '') parts['html_head'] = parts['html_head'].replace( self.standard_html_meta_value, '...') parts['html_prolog'] = parts['html_prolog'].replace( self.standard_html_prolog, '') return {k: v for k, v in parts.items() if v} totest = {} totest['standard'] = ({'stylesheet_path': '', 'embed_stylesheet': False}, [ ["""\ Simple String """, {'fragment': '''

Simple String

\n''', 'html_body': '''

Simple String

\n''', 'html_head': '''...<string>\n''' }], ["""\ Simple String with *markup* """, {'fragment': '''

Simple String with markup

\n''', 'html_body': '''

Simple String with markup

\n''', 'html_head': '''...<string>\n''' }], ["""\ Simple String with an even simpler ``inline literal`` """, {'fragment': '''

Simple String with an even simpler inline literal

\n''', 'html_body': '''

Simple String with an even simpler inline literal

\n''', 'html_head': '''...<string>\n''' }], ["""\ A simple `anonymous reference`__ __ http://www.test.com/test_url """, {'fragment': '''

A simple anonymous reference

\n''', 'html_body': '''

A simple anonymous reference

\n''', 'html_head': '''...<string>\n''' }], ["""\ One paragraph. Two paragraphs. """, {'fragment': '''

One paragraph.

Two paragraphs.

\n''', 'html_body': '''

One paragraph.

Two paragraphs.

\n''', 'html_head': '''...<string>\n''' }], ["""\ A simple `named reference`_ with stuff in between the reference and the target. .. _`named reference`: http://www.test.com/test_url """, {'fragment': '''

A simple named reference with stuff in between the reference and the target.

\n''', 'html_body': '''

A simple named reference with stuff in between the reference and the target.

\n''', 'html_head': '''...<string>\n''' }], [""".. [CIT2022] A citation.""", {'fragment': '''
[CIT2022]

A citation.

\n''', 'html_body': '''
[CIT2022]

A citation.

\n''', 'html_head': '''...<string>\n''' }], ["""\ +++++ Title +++++ Subtitle ======== Some stuff Section ------- Some more stuff Another Section ............... And even more stuff """, {'fragment': '''

Some stuff

Section

Some more stuff

Another Section

And even more stuff

\n''', 'html_body': '''

Title

Subtitle

Some stuff

Section

Some more stuff

Another Section

And even more stuff

\n''', 'html_head': '''...Title\n''', 'html_subtitle': '''

Subtitle

\n''', 'html_title': '''

Title

\n''', 'subtitle': '''Subtitle''', 'title': '''Title''' }], ["""\ +++++ Title +++++ :author: me Some stuff """, {'docinfo': '''
Author:

me

\n''', 'fragment': '''

Some stuff

\n''', 'html_body': '''

Title

Author:

me

Some stuff

\n''', 'html_head': '''... Title\n''', 'html_title': '''

Title

\n''', 'meta': '''\n''', 'title': '''Title''' }] ]) totest['no_title_promotion'] = ({'doctitle_xform': False, 'stylesheet_path': '', 'embed_stylesheet': False}, [ ["""\ Simple String """, {'fragment': '''

Simple String

\n''', 'html_body': '''

Simple String

\n''', 'html_head': '''...<string>\n''' }], ["""\ Simple String with *markup* """, {'fragment': '''

Simple String with markup

\n''', 'html_body': '''

Simple String with markup

\n''', 'html_head': '''...<string>\n''' }], ["""\ Simple String with an even simpler ``inline literal`` """, {'fragment': '''

Simple String with an even simpler inline literal

\n''', 'html_body': '''

Simple String with an even simpler inline literal

\n''', 'html_head': '''...<string>\n''' }], ["""\ A simple `anonymous reference`__ __ http://www.test.com/test_url """, {'fragment': '''

A simple anonymous reference

\n''', 'html_body': '''

A simple anonymous reference

\n''', 'html_head': '''...<string>\n''' }], ["""\ A simple `named reference`_ with stuff in between the reference and the target. .. _`named reference`: http://www.test.com/test_url """, {'fragment': '''

A simple named reference with stuff in between the reference and the target.

\n''', 'html_body': '''

A simple named reference with stuff in between the reference and the target.

\n''', 'html_head': '''...<string>\n''' }], ["""\ +++++ Title +++++ Not A Subtitle ============== Some stuff Section ------- Some more stuff Another Section ............... And even more stuff """, {'fragment': '''

Title

Not A Subtitle

Some stuff

Section

Some more stuff

Another Section

And even more stuff

\n''', 'html_body': '''

Title

Not A Subtitle

Some stuff

Section

Some more stuff

Another Section

And even more stuff

\n''', 'html_head': '''...<string>\n''' }], ["""\ * bullet * list """, {'fragment': '''\n''', 'html_body': '''
\n''', 'html_head': '''...<string>\n''' }], ["""\ .. table:: :align: right +-----+-----+ | 1 | 2 | +-----+-----+ | 3 | 4 | +-----+-----+ """, {'fragment': '''

1

2

3

4

\n''', 'html_body': '''

1

2

3

4

\n''', 'html_head': '''...<string>\n''' }], ["""\ Not a docinfo. :This: .. _target: is :a: :simple: :field: list """, {'fragment': '''

Not a docinfo.

This:

is

a:

simple:

field:

list

\n''', 'html_body': '''

Not a docinfo.

This:

is

a:

simple:

field:

list

\n''', 'html_head': '''...<string>\n''' }], ["""\ Not a docinfo. :This is: a :simple field list with loooong field: names """, {'fragment': '''\

Not a docinfo.

This is:

a

simple field list with loooong field:

names

\n''', 'html_body': '''\

Not a docinfo.

This is:

a

simple field list with loooong field:

names

\n''', 'html_head': '''...<string>\n''' }], ["""\ Not a docinfo. .. class:: field-indent-200 :This: is a :simple: field list with custom indent. """, {'fragment': '''

Not a docinfo.

This:

is a

simple:

field list with custom indent.

\n''', 'html_body': '''

Not a docinfo.

This:

is a

simple:

field list with custom indent.

\n''', 'html_head': '''...<string>\n''' }], ["""\ Not a docinfo. .. class:: field-indent-200uf :This: is a :simple: field list without custom indent, because the unit "uf" is invalid. """, {'fragment': '''

Not a docinfo.

This:

is a

simple:

field list without custom indent, because the unit "uf" is invalid.

\n''', 'html_body': '''

Not a docinfo.

This:

is a

simple:

field list without custom indent, because the unit "uf" is invalid.

\n''', 'html_head': '''...<string>\n''' }], ["""\ .. figure:: dummy.png The figure's caption. A legend. The legend's second paragraph. """, {'fragment': '''\
dummy.png

The figure's caption.

A legend.

The legend's second paragraph.

\n''', 'html_body': '''\
dummy.png

The figure's caption.

A legend.

The legend's second paragraph.

\n''', 'html_head': '''...<string>\n''' }], ["""\ .. figure:: dummy.png The figure's caption, no legend. """, {'fragment': '''\
dummy.png

The figure's caption, no legend.

\n''', 'html_body': '''\
dummy.png

The figure's caption, no legend.

\n''', 'html_head': '''...<string>\n''' }], ["""\ .. figure:: dummy.png .. A legend without caption. """, {'fragment': '''\
dummy.png

A legend without caption.

\n''', 'html_body': '''\
dummy.png

A legend without caption.

\n''', 'html_head': '''...<string>\n''' }], ["""\ .. figure:: dummy.png No caption nor legend. """, {'fragment': '''\
dummy.png

No caption nor legend.

\n''', 'html_body': '''\
dummy.png

No caption nor legend.

\n''', 'html_head': '''...<string>\n''' }], ]) totest['lazy_loading'] = ({'image_loading': 'lazy', 'stylesheet_path': '', 'embed_stylesheet': False}, [ ["""\ .. image:: dummy.png """, {'fragment': '''\ dummy.png\n''', 'html_body': '''\
dummy.png
\n''', 'html_head': '''...<string>\n''' }], ["""\ .. figure:: dummy.png """, {'fragment': '''\
dummy.png
\n''', 'html_body': '''\
dummy.png
\n''', 'html_head': '''...<string>\n''' }], ]) totest['no_backlinks'] = ({'footnote_backlinks': False, 'stylesheet_path': '', 'embed_stylesheet': False}, [ ["""\ Two footnotes [#f1]_ [#f2]_ and two citations [once]_ [twice]_. The latter are referenced a second time [#f2]_ [twice]_. .. [#f1] referenced once .. [#f2] referenced twice .. [once] citation referenced once .. [twice] citation referenced twice """, {'fragment': '''\

Two footnotes [1] [2] and two citations [once] [twice].

The latter are referenced a second time [2] [twice].

[once]

citation referenced once

[twice]

citation referenced twice

\n''', 'html_body': '''\

Two footnotes [1] [2] and two citations [once] [twice].

The latter are referenced a second time [2] [twice].

[once]

citation referenced once

[twice]

citation referenced twice

\n''', 'html_head': '''...<string>\n''' }], ]) if __name__ == '__main__': unittest.main()