#! /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 Html4WriterPublishPartsTestCase(unittest.TestCase): """ Test case for HTML writer via the publish_parts interface. """ def test_publish(self): writer_name = 'html4' 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': '', **settings_overrides, } ) self.assertEqual(self.format_output(parts), case_expected) standard_content_type_template = ('\n') standard_generator_template = ( '\n') standard_html_meta_value = ( standard_content_type_template + standard_generator_template) standard_meta_value = standard_html_meta_value % 'utf-8' standard_html_prolog = ( '\n' '\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['title_promotion'] = ({'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''' }], ["""\ Simple ``inline\xA0literal`` with NBSP """, {'fragment': '''

Simple inline literal with NBSP

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

Simple inline literal with NBSP

\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''' }], ["""\ +++++ 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''' }], ]) if __name__ == '__main__': unittest.main()