Пример #1
0
    def test_multiple_roots_zero_level(self):
        '''Test complex usage of heading where root level nodes
        may be of varying levels until an H1 is used.
        '''
        test_string = self.read_test_file(self.processor_name, 'multiple_roots_zero_level.md')
        blocks = self.to_blocks(test_string)

        self.assertListEqual([True, True, True, True, True, True, True], [HeadingBlockProcessor(self.ext, self.md.parser).test(blocks, block) for block in blocks], msg='"{}"'.format(test_string))

        html_template = self.read_test_file(self.processor_name, 'doc_example_override_html_template.html', strip=True)
        verto_extension = VertoExtension([self.processor_name], html_templates={self.processor_name: html_template})

        converted_test_string = markdown.markdown(test_string, extensions=[verto_extension])
        expected_string = self.read_test_file(self.processor_name, 'multiple_roots_zero_level_expected.html', strip=True)
        self.assertEqual(expected_string, converted_test_string)

        tree = verto_extension.get_heading_tree()
        expected_tree = (HeadingNode(title='This is an H4',
                                    title_slug='this-is-an-h4',
                                    level=4,
                                    children=()
                        ),
                        HeadingNode(title='This is an H2',
                                    title_slug='this-is-an-h2',
                                    level=2,
                                    children=(
                                        HeadingNode(
                                            title='This is an H3',
                                            title_slug='this-is-an-h3',
                                            level=3,
                                            children=()
                                        ),
                                    )
                        ),
                        HeadingNode(title='This is an H1',
                                    title_slug='this-is-an-h1',
                                    level=1,
                                    children=(
                                        HeadingNode(
                                            title='This is an H3',
                                            title_slug='this-is-an-h3-2',
                                            level=3,
                                            children=()
                                        ),
                                        HeadingNode(title='This is an H2',
                                                    title_slug='this-is-an-h2-2',
                                                    level=2,
                                                    children=(
                                                        HeadingNode(title='This is an H4',
                                                                    title_slug='this-is-an-h4-2',
                                                                    level=4,
                                                                    children=()
                                                        ),
                                                    )
                                        ),
                                    )
                        ),
                    )
        self.assertTupleEqual(tree, expected_tree)
Пример #2
0
 def create_converter(self):
     '''Create the Verto extension and converter for future use.'''
     self.verto_extension = VertoExtension(
         processors=self.processors,
         html_templates=self.html_templates,
         extensions=self.extensions,
         custom_argument_rules=self.custom_argument_rules,
     )
     all_extensions = self.extensions + [self.verto_extension]
     self.converter = markdown.Markdown(extensions=all_extensions)
Пример #3
0
    def test_example_standard_markdown_block(self):
        '''Tests that even without extensions it behaves as expected.
        '''
        verto_extension = VertoExtension([self.processor_name], {}, [])
        test_string = self.read_test_file(
            self.processor_name, 'example_standard_markdown_block.md')

        converted_test_string = markdown.markdown(test_string,
                                                  extensions=[verto_extension])
        expected_string = self.read_test_file(
            self.processor_name,
            'example_standard_markdown_block_expected.html',
            strip=True)
        self.assertEqual(expected_string, converted_test_string)

        actual_scratch_images = verto_extension.required_files[
            'scratch_images']
        expected_scratch_images = {
            ScratchImageMetaData(
                hash=
                'a0f8fcad796864abfacac8bda6e0719813833fd1fca348700abbd040557c1576',
                text=
                'when flag clicked\nclear\nforever\npen down\nif <<mouse down?> and <touching [mouse-pointer v]?>> then\nswitch costume to [button v]\nelse\nadd (x position) to [list v]\nend\nmove (foo) steps\nturn ccw (9) degrees'
            ),
        }
        self.assertSetEqual(actual_scratch_images, expected_scratch_images)
Пример #4
0
    def test_doc_example_override_html(self):
        '''A example showing how to override the html template.
        '''
        test_string = self.read_test_file(self.processor_name,
                                          'doc_example_override_html.md')
        blocks = self.to_blocks(test_string)

        self.assertListEqual(
            [True],
            [self.block_processor.test(blocks, block) for block in blocks],
            msg='"{}"'.format(test_string))

        html_template = self.read_test_file(
            self.processor_name,
            'doc_example_override_html_template.html',
            strip=True)
        verto_extension = VertoExtension(
            [self.processor_name],
            html_templates={self.processor_name: html_template})

        converted_test_string = markdown.markdown(test_string,
                                                  extensions=[verto_extension])
        expected_string = self.read_test_file(
            self.processor_name,
            'doc_example_override_html_expected.html',
            strip=True)
        self.assertEqual(expected_string, converted_test_string)
Пример #5
0
    def test_doc_example_override_html(self):
        '''An example showing how to override the html-template.'''
        test_string = self.read_test_file(self.processor_name,
                                          'doc_example_override_html.md')

        html_template = self.read_test_file(
            self.processor_name,
            'doc_example_override_html_template.html',
            strip=True)
        verto_extension = VertoExtension(
            [self.processor_name],
            html_templates={self.processor_name: html_template},
            extensions=['markdown.extensions.fenced_code'])

        converted_test_string = markdown.markdown(
            test_string,
            extensions=['markdown.extensions.fenced_code', verto_extension])
        expected_string = self.read_test_file(
            self.processor_name,
            'doc_example_override_html_expected.html',
            strip=True)
        self.assertEqual(expected_string, converted_test_string)

        actual_scratch_images = verto_extension.required_files[
            'scratch_images']
        expected_scratch_images = {
            ScratchImageMetaData(
                hash=
                '2f3ea223b778227287b8935bc5d209e25d3e8a25ef46ff85f6c44818159601d7',
                text='when flag clicked'),
        }
        self.assertSetEqual(actual_scratch_images, expected_scratch_images)
Пример #6
0
    def test_image_width_value_external_image(self):
        '''Test image rendered correctly with width value.
        '''
        test_string = self.read_test_file(
            self.processor_name, 'file_width_value_external_image.md')
        blocks = self.to_blocks(test_string)

        self.assertListEqual([True, False, True], [
            ImageContainerBlockProcessor(self.ext, self.md.parser).test(
                blocks, block) for block in blocks
        ],
                             msg='"{}"'.format(test_string))

        html_template = self.read_test_file(
            self.processor_name,
            'file_width_value_external_image_html_template.html',
            strip=True)
        verto_extension = VertoExtension(
            [self.processor_name],
            html_templates={self.tag_argument: html_template})
        converted_test_string = markdown.markdown(test_string,
                                                  extensions=[verto_extension])
        expected_string = self.read_test_file(
            self.processor_name,
            'file_width_value_external_image_expected.html',
            strip=True)
        self.assertEqual(expected_string, converted_test_string)

        images = self.verto_extension.required_files['images']
        expected_images = set()
        self.assertSetEqual(expected_images, images)
Пример #7
0
    def test_custom_arguments_alt_false_source_true(self):
        '''Tests to ensure that image tag is rendered correctly when alt argument is not required and source argument is true and expected images are updated.
        '''
        custom_argument_rules = {
            "image-container": {
                "alt": False,
                "source": True
            }
        }
        verto_extension_custom_rules = VertoExtension(
            processors=[self.processor_name],
            custom_argument_rules=custom_argument_rules)

        test_string = self.read_test_file(self.processor_name,
                                          'alt_false_source_true.md')
        blocks = self.to_blocks(test_string)

        self.assertListEqual([False, True, False, True, False], [
            ImageContainerBlockProcessor(self.ext, self.md.parser).test(
                blocks, block) for block in blocks
        ],
                             msg='"{}"'.format(test_string))

        converted_test_string = markdown.markdown(
            test_string, extensions=[verto_extension_custom_rules])
        expected_string = self.read_test_file(
            self.processor_name,
            'alt_false_source_true_expected.html',
            strip=True)
        self.assertEqual(expected_string, converted_test_string)

        images = verto_extension_custom_rules.required_files['images']
        expected_images = {'cats.png'}
        self.assertSetEqual(expected_images, images)
Пример #8
0
    def test_doc_example_override_html(self):
        '''An example showing how to override the html-template.
        '''
        test_string = self.read_test_file(self.processor_name,
                                          'doc_example_override_html.md')

        html_template = self.read_test_file(
            self.processor_name,
            'doc_example_override_html_template.html',
            strip=True)
        verto_extension = VertoExtension(
            [self.processor_name],
            html_templates={self.processor_name: html_template},
            extensions=['markdown.extensions.fenced_code'])

        converted_test_string = markdown.markdown(
            test_string,
            extensions=['markdown.extensions.fenced_code', verto_extension])
        expected_string = self.read_test_file(
            self.processor_name,
            'doc_example_override_html_expected.html',
            strip=True)
        self.assertEqual(expected_string, converted_test_string)

        actual_scratch_images = verto_extension.required_files[
            'scratch_images']
        expected_scratch_images = {
            ScratchImageMetaData(
                hash=
                'a3b77ed3c3fa57e43c830e338dc39d292c7def676e0e8f7545972b7da20275da',
                text='when flag clicked\nsay [Hi]'),
        }
        self.assertSetEqual(actual_scratch_images, expected_scratch_images)
Пример #9
0
    def test_doc_example_2_override_html(self):  # TODO check docstring
        '''Basic example showing how to override the html-template for relative files in a specific file only.
        '''
        test_string = self.read_test_file(self.processor_name,
                                          'doc_example_2_override_html.md')
        blocks = self.to_blocks(test_string)

        self.assertListEqual([True, False, True], [
            ImageContainerBlockProcessor(self.ext, self.md.parser).test(
                blocks, block) for block in blocks
        ],
                             msg='"{}"'.format(test_string))

        html_template = self.read_test_file(
            self.processor_name,
            'doc_example_2_override_html_template.html',
            strip=True)
        verto_extension = VertoExtension(
            [self.processor_name],
            html_templates={self.tag_argument: html_template})

        converted_test_string = markdown.markdown(test_string,
                                                  extensions=[verto_extension])
        expected_string = self.read_test_file(
            self.processor_name,
            'doc_example_2_override_html_expected.html',
            strip=True)
        self.assertEqual(expected_string, converted_test_string)

        images = self.verto_extension.required_files['images']
        expected_images = set()
        self.assertSetEqual(expected_images, images)
Пример #10
0
    def test_custom_arguments_alt_false_source_true(self):
        '''Tests to ensure that image tag is rendered correctly when alt argument is not required and source argument is required and expected images are updated.
        '''
        custom_argument_rules = {
            "image-inline": {
                "alt": False,
                "source": True
            }
        }
        verto_extension_custom_rules = VertoExtension(
            processors=[self.processor_name],
            custom_argument_rules=custom_argument_rules)

        test_string = self.read_test_file(self.processor_name,
                                          'alt_false_source_true.md')
        processor = ImageInlinePattern(self.ext, self.md.parser)
        self.assertIsNotNone(re.search(processor.compiled_re, test_string))

        converted_test_string = markdown.markdown(
            test_string, extensions=[verto_extension_custom_rules])
        expected_string = self.read_test_file(
            self.processor_name,
            'alt_false_source_true_expected.html',
            strip=True)
        self.assertEqual(expected_string, converted_test_string)

        images = verto_extension_custom_rules.required_files['images']
        expected_images = {'img/example.png'}
        self.assertSetEqual(expected_images, images)
Пример #11
0
    def test_doc_example_override_html(self):
        '''Basic example showing how to override the html-template.'''
        test_string = self.read_test_file(self.processor_name,
                                          'doc_example_override_html.md')

        processor = ImageInlinePattern(self.ext, self.md.parser)
        self.assertIsNotNone(re.search(processor.compiled_re, test_string))

        html_template = self.read_test_file(
            self.processor_name,
            'doc_example_override_html_template.html',
            strip=True)
        verto_extension = VertoExtension(
            [self.processor_name],
            html_templates={self.processor_name: html_template})

        converted_test_string = markdown.markdown(test_string,
                                                  extensions=[verto_extension])
        expected_string = self.read_test_file(
            self.processor_name,
            'doc_example_override_html_expected.html',
            strip=True).strip()
        self.assertEqual(expected_string, converted_test_string)

        images = verto_extension.required_files['images']
        expected_images = set()
        self.assertSetEqual(expected_images, images)
Пример #12
0
    def test_image_width_value_no_units(self):
        '''Test image rendered correctly with width value with no units.
        '''
        test_string = self.read_test_file(self.processor_name,
                                          'file_width_value_no_units.md')

        processor = ImageInlinePattern(self.ext, self.md.parser)
        self.assertIsNotNone(re.search(processor.compiled_re, test_string))

        html_template = self.read_test_file(
            self.processor_name,
            'file_width_value_no_units_html_template.html',
            strip=True)
        verto_extension = VertoExtension(
            [self.processor_name],
            html_templates={self.processor_name: html_template})

        converted_test_string = markdown.markdown(test_string,
                                                  extensions=[verto_extension])
        expected_string = self.read_test_file(
            self.processor_name,
            'file_width_value_no_units_expected.html',
            strip=True).strip()
        self.assertEqual(expected_string, converted_test_string)

        images = verto_extension.required_files['images']
        expected_images = {'path/to/[email protected]'}
        self.assertSetEqual(expected_images, images)
Пример #13
0
    def test_custom_arguments_hover_true(self):
        '''Tests to ensure that image tag is rendered correctly when hover argument is required and expected images are updated.
        '''
        custom_argument_rules = {"image-tag": {"hover-text": True}}
        verto_extension_custom_rules = VertoExtension(
            processors=[self.processor_name],
            custom_argument_rules=custom_argument_rules)

        test_string = self.read_test_file(self.processor_name, 'hover_true.md')
        blocks = self.to_blocks(test_string)

        self.assertListEqual([True], [
            ImageTagBlockProcessor(self.ext, self.md.parser).test(
                blocks, block) for block in blocks
        ],
                             msg='"{}"'.format(test_string))

        converted_test_string = markdown.markdown(
            test_string, extensions=[verto_extension_custom_rules])
        expected_string = self.read_test_file(self.processor_name,
                                              'hover_true_expected.html',
                                              strip=True)
        self.assertEqual(expected_string, converted_test_string)

        images = verto_extension_custom_rules.required_files['images']
        expected_images = {'computer-studying-turing-test.png'}
        self.assertSetEqual(expected_images, images)
Пример #14
0
    def test_custom_arguments_parameters_and_thumbnail_true(self):
        '''Tests to ensure that interactive tag is rendered correctly when type argument is not required and parameters argument is required.
        '''
        custom_argument_rules = {
            "interactive-tag": {
                "parameters": True,
                "thumbnail": True
            }
        }
        verto_extension_custom_rules = VertoExtension(
            processors=[self.processor_name],
            custom_argument_rules=custom_argument_rules)

        test_string = self.read_test_file(self.processor_name,
                                          'parameters_and_thumbnail_true.md')
        blocks = self.to_blocks(test_string)

        self.assertListEqual([False, True, False], [
            InteractiveTagBlockProcessor(self.ext, self.md.parser).test(
                blocks, block) for block in blocks
        ],
                             msg='"{}"'.format(test_string))

        converted_test_string = markdown.markdown(
            test_string, extensions=[verto_extension_custom_rules])
        expected_string = self.read_test_file(
            self.processor_name,
            'parameters_and_thumbnail_true_expected.html',
            strip=True)
        self.assertEqual(expected_string, converted_test_string)
Пример #15
0
    def test_codeblocks_compatibility(self):
        '''Test the codehilite and fenced_code do not causes any issues.'''
        extensions = [
            'markdown.extensions.codehilite', 'markdown.extensions.fenced_code'
        ]
        verto_extension = VertoExtension([self.processor_name], {}, extensions)
        test_string = self.read_test_file(self.processor_name,
                                          'multiple_codeblocks.md')

        converted_test_string = markdown.markdown(test_string,
                                                  extensions=extensions +
                                                  [verto_extension])
        expected_string = self.read_test_file(
            self.processor_name,
            'multiple_codeblocks_expected.html',
            strip=True)
        self.assertEqual(expected_string, converted_test_string)

        actual_scratch_images = verto_extension.required_files[
            'scratch_images']
        expected_scratch_images = {
            ScratchImageMetaData(
                hash=
                '3dfa73663a21d295e1e5c1e5583d8d01edd68ec53ad3050597de126076c44ea5',
                text='say [Hello] for (2) secs'),
            ScratchImageMetaData(
                hash=
                '2f3ea223b778227287b8935bc5d209e25d3e8a25ef46ff85f6c44818159601d7',
                text='when flag clicked'),
            ScratchImageMetaData(
                hash=
                '1c95862744e873cc87e4cadf6174257ce6e8a237b29b5c41f241e98e0d78eb14',
                text='turn ccw (9) degrees'),
        }
        self.assertSetEqual(actual_scratch_images, expected_scratch_images)
Пример #16
0
 def create_converter(self):
     '''Create the Verto extension and converter for future use.'''
     self.verto_extension = VertoExtension(
         processors=self.processors,
         html_templates=self.html_templates,
         extensions=self.extensions,
         custom_argument_rules=self.custom_argument_rules,
     )
     all_extensions = self.extensions + [self.verto_extension]
     self.converter = markdown.Markdown(extensions=all_extensions)
Пример #17
0
    def test_processor_off(self):
        '''Tests that disabling the processor, leaves the document
        unchanged.
        '''
        # Create Verto extension without processor enabled (off by default)
        verto_extension = VertoExtension()
        test_string = self.read_test_file(self.processor_name, 'processor_off.md')

        converted_test_string = markdown.markdown(test_string, extensions=[verto_extension])
        expected_string = self.read_test_file(self.processor_name, 'processor_off_expected.html', strip=True)
        self.assertEqual(expected_string, converted_test_string)
Пример #18
0
    def test_contains_inner_image(self):
        '''Tests that other processors within a panel still renders correctly.'''
        verto_extension = VertoExtension([self.processor_name, 'image-container'], {})
        test_string = self.read_test_file(self.processor_name, 'contains_inner_image.md')
        blocks = self.to_blocks(test_string)

        self.assertListEqual([True, False, False, False, False, False, False, False, True], [self.block_processor.test(blocks, block) for block in blocks], msg='"{}"'.format(test_string))

        converted_test_string = markdown.markdown(test_string, extensions=[verto_extension])
        expected_string = self.read_test_file(self.processor_name, 'contains_inner_image_expected.html', strip=True)
        self.assertEqual(expected_string, converted_test_string)
Пример #19
0
    def test_comment_within_block_container(self):
        '''Tests that comments are removed from containers.
        '''
        verto_extension = VertoExtension([self.processor_name, 'panel'], {})
        test_string = self.read_test_file(self.processor_name, 'comment_within_block_container.md')

        self.assertTrue(CommentPreprocessor(self.ext, self.md.parser).test(test_string), msg='"{}"'.format(test_string))

        converted_test_string = markdown.markdown(test_string, extensions=[verto_extension])
        expected_string = self.read_test_file(self.processor_name, 'comment_within_block_container_expected.html', strip=True)
        self.assertEqual(expected_string, converted_test_string)
Пример #20
0
    def test_no_result_processor_off(self):
        '''Tests that disabling the processor correctly
        functions.
        '''
        # Create Verto extension without processor enabled
        verto_extension = VertoExtension()
        test_string = self.read_test_file(self.processor_name,
                                          'doc_example_basic_usage.md')

        converted_test_string = markdown.markdown(test_string,
                                                  extensions=[verto_extension])
        self.assertIsNone(verto_extension.title)
Пример #21
0
    def test_doc_example_override_html(self):
        '''Complex example using all features and checking that it
        works with custom templates.
        '''
        test_string = self.read_test_file(self.processor_name, 'doc_example_override_html.md')
        blocks = self.to_blocks(test_string)

        self.assertListEqual([True, False, True, False, True, False, True], [ConditionalProcessor(self.ext, self.md.parser).test(blocks, block) for block in blocks], msg='"{}"'.format(test_string))

        html_template = self.read_test_file(self.processor_name, 'doc_example_override_html_template.html', strip=True)
        verto_extension = VertoExtension([self.processor_name], html_templates={self.processor_name: html_template})

        converted_test_string = markdown.markdown(test_string, extensions=[verto_extension])
        expected_string = self.read_test_file(self.processor_name, 'doc_example_override_html_expected.html', strip=True)
        self.assertEqual(expected_string, converted_test_string)
Пример #22
0
    def test_example_elif_no_else(self):
        '''Complex example showing multiple elif statements with
        no else statement.
        '''
        test_string = self.read_test_file(self.processor_name, 'example_elif_no_else.md')
        blocks = self.to_blocks(test_string)

        self.assertListEqual([True, False, True, False, True, False, True], [ConditionalProcessor(self.ext, self.md.parser).test(blocks, block) for block in blocks], msg='"{}"'.format(test_string))

        html_template = self.read_test_file(self.processor_name, 'example_elif_no_else_template.html', strip=True)
        verto_extension = VertoExtension([self.processor_name], html_templates={self.processor_name: html_template})

        converted_test_string = markdown.markdown(test_string, extensions=[verto_extension])
        expected_string = self.read_test_file(self.processor_name, 'example_elif_no_else_expected.html', strip=True)
        self.assertEqual(expected_string, converted_test_string)
Пример #23
0
    def test_custom_arguments_hover_true_not_provided(self):
        '''Tests to ensure that correct error is raised when hover text is required and not provided.
        '''
        custom_argument_rules = {"image-inline": {"hover-text": True}}
        verto_extension_custom_rules = VertoExtension(
            processors=[self.processor_name],
            custom_argument_rules=custom_argument_rules)

        test_string = self.read_test_file(self.processor_name,
                                          'hover_true_not_provided.md')
        processor = ImageInlinePattern(self.ext, self.md.parser)
        self.assertIsNotNone(re.search(processor.compiled_re, test_string))

        self.assertRaises(
            ArgumentMissingError, lambda x: markdown.markdown(
                x, extensions=[verto_extension_custom_rules]), test_string)
Пример #24
0
    def test_custom_argument_rules_incorrect_processor_argument_error(self):
        '''Checks that error is raised when a processor given in custom argument rules does not exist.
        '''
        custom_argument_rules = {
            "panl": {
                "subtitle": True
            },
            "image-tag": {
                "alt": False
            }
        }
        processors = {'image-tag', 'panel', 'comment'}

        self.assertRaises(
            CustomArgumentRulesError, lambda: VertoExtension(
                processors=processors,
                custom_argument_rules=custom_argument_rules))
Пример #25
0
    def test_custom_arguments_subtitle_true_not_provided(self):
        '''Tests to ensure that correct error is raised when subtitle is required and not provided.
        '''
        custom_argument_rules = {
            "panel": {
                "subtitle": True
            }
        }
        verto_extension_custom_rules = VertoExtension(
            processors=[self.processor_name],
            custom_argument_rules=custom_argument_rules
        )

        test_string = self.read_test_file(self.processor_name, 'subtitle_true_not_provided.md')
        blocks = self.to_blocks(test_string)

        self.assertListEqual([True, False, False, False, True], [self.block_processor.test(blocks, block) for block in blocks], msg='"{}"'.format(test_string))

        self.assertRaises(ArgumentMissingError, lambda x: markdown.markdown(x, extensions=[verto_extension_custom_rules]), test_string)
Пример #26
0
    def test_custom_arguments_indented_required_not_provided(self):
        '''Tests to ensure that error is raised when indented argument is required and not given.
        '''
        custom_argument_rules = {
            "boxed-text": {
                "indented": True
            }
        }
        verto_extension_custom_rules = VertoExtension(
            processors=[self.processor_name],
            custom_argument_rules=custom_argument_rules
        )

        test_string = self.read_test_file(self.processor_name, 'indented_required_not_provided.md')
        blocks = self.to_blocks(test_string)

        self.assertListEqual([True, False, True], [GenericContainerBlockProcessor(self.processor_name, self.ext, Mock()).test(blocks, block) for block in blocks], msg='"{}"'.format(test_string))

        self.assertRaises(ArgumentMissingError, lambda x: markdown.markdown(x, extensions=[verto_extension_custom_rules]), test_string)
Пример #27
0
    def test_custom_arguments_alignment_true_missing_argument(self):
        '''Tests to ensure that blockquote tag raises errors when alignment argument is required and not given.
        '''
        custom_argument_rules = {"blockquote": {"alignment": True}}
        verto_extension_custom_rules = VertoExtension(
            processors=[self.processor_name],
            custom_argument_rules=custom_argument_rules)

        test_string = self.read_test_file(
            self.processor_name, 'alignment_true_missing_argument.md')
        blocks = self.to_blocks(test_string)

        self.assertListEqual(
            [True, False, True],
            [self.block_processor.test(blocks, block) for block in blocks],
            msg='"{}"'.format(test_string))

        self.assertRaises(
            ArgumentMissingError, lambda x: markdown.markdown(
                x, extensions=[verto_extension_custom_rules]), test_string)
Пример #28
0
    def test_custom_argument_rules_for_multiple_tags_error(self):
        '''Checks that error is raised when a tag's custom argument rules are not followed.
        '''
        custom_argument_rules = {
            "panel": {
                "subtitle": True
            },
            "image-tag": {
                "alt": False
            }
        }
        processors = {'image-tag', 'panel', 'comment'}
        verto = VertoExtension(processors=processors,
                               custom_argument_rules=custom_argument_rules)

        test_string = self.read_test_file(
            self.test_name, 'custom_argument_rules_multiple_tags_error.md')
        self.assertRaises(ArgumentMissingError,
                          lambda x: markdown.markdown(x, extensions=[verto]),
                          test_string)
Пример #29
0
    def test_example_multiple_codeblocks_2(self):
        '''Tests that enabling the codehilite extension does not effect
        the functionality. (Loads the compatiability processor).
        '''
        extensions = [
            'markdown.extensions.codehilite', 'markdown.extensions.fenced_code'
        ]
        verto_extension = VertoExtension([self.processor_name], {}, extensions)
        test_string = self.read_test_file(self.processor_name,
                                          'example_multiple_codeblocks_2.md')

        converted_test_string = markdown.markdown(test_string,
                                                  extensions=extensions +
                                                  [verto_extension])
        expected_string = self.read_test_file(
            self.processor_name,
            'example_multiple_codeblocks_expected_2.html',
            strip=True)
        self.assertEqual(expected_string, converted_test_string)

        actual_scratch_images = verto_extension.required_files[
            'scratch_images']
        expected_scratch_images = {
            ScratchImageMetaData(
                hash=
                'a3b77ed3c3fa57e43c830e338dc39d292c7def676e0e8f7545972b7da20275da',
                text='when flag clicked\nsay [Hi]'),
            ScratchImageMetaData(
                hash=
                'cd6d9a0d464bb8f5eec1e6fc9a4e33378a64ebfce7c6198794ead614962f38e5',
                text=
                'when flag clicked\nsay [Hi]\nmove (foo) steps\nturn ccw (9) degrees'
            ),
            ScratchImageMetaData(
                hash=
                '8e8a2129c3cecf32101248439961735fc1d45793fadc56e2575673f63d42b9fb',
                text=
                'when flag clicked\nclear\nforever\npen down\n\nif <<mouse down?> and <touching [mouse-pointer v]?>> then\nswitch costume to [button v]\nelse\nadd (x position) to [list v]\nend\n\nmove (foo) steps\nturn ccw (9) degrees'
            ),
        }
        self.assertSetEqual(actual_scratch_images, expected_scratch_images)
Пример #30
0
    def test_custom_arguments_expanded_true(self):
        '''Tests to ensure that panel tag is rendered correctly when expanded argument is required.
        '''
        custom_argument_rules = {
            "panel": {
                "expanded": True
            }
        }
        verto_extension_custom_rules = VertoExtension(
            processors=[self.processor_name],
            custom_argument_rules=custom_argument_rules
        )

        test_string = self.read_test_file(self.processor_name, 'expanded_true.md')
        blocks = self.to_blocks(test_string)

        self.assertListEqual([True, False, False, False, True], [self.block_processor.test(blocks, block) for block in blocks], msg='"{}"'.format(test_string))

        converted_test_string = markdown.markdown(test_string, extensions=[verto_extension_custom_rules])
        expected_string = self.read_test_file(self.processor_name, 'expanded_true_expected.html', strip=True)
        self.assertEqual(expected_string, converted_test_string)
Пример #31
0
    def test_custom_arguments_hover_true_not_provided(self):
        '''Tests to ensure that correct error is raised when hover text is required and not provided.
        '''
        custom_argument_rules = {"image-container": {"hover-text": True}}
        verto_extension_custom_rules = VertoExtension(
            processors=[self.processor_name],
            custom_argument_rules=custom_argument_rules)

        test_string = self.read_test_file(self.processor_name,
                                          'hover_true_not_provided.md')
        blocks = self.to_blocks(test_string)

        self.assertListEqual([True, False, True], [
            ImageContainerBlockProcessor(self.ext, self.md.parser).test(
                blocks, block) for block in blocks
        ],
                             msg='"{}"'.format(test_string))

        self.assertRaises(
            ArgumentMissingError, lambda x: markdown.markdown(
                x, extensions=[verto_extension_custom_rules]), test_string)
Пример #32
0
 def setUp(self):
     '''Overrides the generic setup to load the fenced_code
     extension by default (as this is the desired usecase).
     '''
     self.verto_extension = VertoExtension([self.processor_name], {}, ['markdown.extensions.fenced_code'])
Пример #33
0
class ScratchTest(ProcessorTest):
    '''Scratch blocks are unique in that they override behaviour in
    markdown and behaviour in markdown extensions, while also retaining
    compatiability.
    '''
    def __init__(self, *args, **kwargs):
        '''Sets name for loading test assets.
        '''
        ProcessorTest.__init__(self, *args, **kwargs)
        self.processor_name = 'scratch'

    def setUp(self):
        '''Overrides the generic setup to load the fenced_code
        extension by default (as this is the desired usecase).
        '''
        self.verto_extension = VertoExtension([self.processor_name], {}, ['markdown.extensions.fenced_code'])

    # ~
    # Doc Tests
    # ~

    def test_doc_example_basic(self):
        '''An example of common useage.
        '''
        test_string = self.read_test_file(self.processor_name, 'doc_example_basic_usage.md')

        converted_test_string = markdown.markdown(test_string, extensions=['markdown.extensions.fenced_code', self.verto_extension])
        expected_string = self.read_test_file(self.processor_name, 'doc_example_basic_usage_expected.html', strip=True)
        self.assertEqual(expected_string, converted_test_string)

        actual_scratch_images = self.verto_extension.required_files['scratch_images']
        expected_scratch_images = {
                        ScratchImageMetaData(
                            hash='a0f8fcad796864abfacac8bda6e0719813833fd1fca348700abbd040557c1576',
                            text='when flag clicked\nclear\nforever\npen down\nif <<mouse down?> and <touching [mouse-pointer v]?>> then\nswitch costume to [button v]\nelse\nadd (x position) to [list v]\nend\nmove (foo) steps\nturn ccw (9) degrees'
                        ),
                    }
        self.assertSetEqual(actual_scratch_images, expected_scratch_images)

    def test_doc_example_override_html(self):
        '''An example showing how to override the html-template.
        '''
        test_string = self.read_test_file(self.processor_name, 'doc_example_override_html.md')

        html_template = self.read_test_file(self.processor_name, 'doc_example_override_html_template.html', strip=True)
        verto_extension = VertoExtension([self.processor_name], html_templates={self.processor_name: html_template}, extensions=['markdown.extensions.fenced_code'])

        converted_test_string = markdown.markdown(test_string, extensions=['markdown.extensions.fenced_code', verto_extension])
        expected_string = self.read_test_file(self.processor_name, 'doc_example_override_html_expected.html', strip=True)
        self.assertEqual(expected_string, converted_test_string)

        actual_scratch_images = verto_extension.required_files['scratch_images']
        expected_scratch_images = {
                        ScratchImageMetaData(
                            hash='a3b77ed3c3fa57e43c830e338dc39d292c7def676e0e8f7545972b7da20275da',
                            text='when flag clicked\nsay [Hi]'
                        ),
                    }
        self.assertSetEqual(actual_scratch_images, expected_scratch_images)

    #~
    # Other Tests
    #~
    def test_example_standard_markdown_block(self):
        '''Tests that even without extensions it behaves as expected.
        '''
        verto_extension = VertoExtension([self.processor_name], {}, [])
        test_string = self.read_test_file(self.processor_name, 'example_standard_markdown_block.md')

        converted_test_string = markdown.markdown(test_string, extensions=[verto_extension])
        expected_string = self.read_test_file(self.processor_name, 'example_standard_markdown_block_expected.html', strip=True)
        self.assertEqual(expected_string, converted_test_string)

        actual_scratch_images = verto_extension.required_files['scratch_images']
        expected_scratch_images = {
                        ScratchImageMetaData(
                            hash='a0f8fcad796864abfacac8bda6e0719813833fd1fca348700abbd040557c1576',
                            text='when flag clicked\nclear\nforever\npen down\nif <<mouse down?> and <touching [mouse-pointer v]?>> then\nswitch costume to [button v]\nelse\nadd (x position) to [list v]\nend\nmove (foo) steps\nturn ccw (9) degrees'
                        ),
                    }
        self.assertSetEqual(actual_scratch_images, expected_scratch_images)

    def test_example_separate_blocks(self):
        '''Tests that code separated by whitespace is still processed.
        '''
        test_string = self.read_test_file(self.processor_name, 'example_separate_blocks.md')

        converted_test_string = markdown.markdown(test_string, extensions=['markdown.extensions.fenced_code', self.verto_extension])
        expected_string = self.read_test_file(self.processor_name, 'example_separate_blocks_expected.html', strip=True)
        self.assertEqual(expected_string, converted_test_string)

        actual_scratch_images = self.verto_extension.required_files['scratch_images']
        expected_scratch_images = {
                        ScratchImageMetaData(
                            hash='8e8a2129c3cecf32101248439961735fc1d45793fadc56e2575673f63d42b9fb',
                            text='when flag clicked\nclear\nforever\npen down\n\nif <<mouse down?> and <touching [mouse-pointer v]?>> then\nswitch costume to [button v]\nelse\nadd (x position) to [list v]\nend\n\nmove (foo) steps\nturn ccw (9) degrees'
                        ),
                    }
        self.assertSetEqual(actual_scratch_images, expected_scratch_images)

    def test_example_multiple_codeblocks(self):
        '''Tests that multiple codeblocks are processed independently.
        '''
        test_string = self.read_test_file(self.processor_name, 'example_multiple_codeblocks.md')

        converted_test_string = markdown.markdown(test_string, extensions=['markdown.extensions.fenced_code', self.verto_extension])
        expected_string = self.read_test_file(self.processor_name, 'example_multiple_codeblocks_expected.html', strip=True)
        self.assertEqual(expected_string, converted_test_string)

        actual_scratch_images = self.verto_extension.required_files['scratch_images']
        expected_scratch_images = {
                        ScratchImageMetaData(
                            hash='a3b77ed3c3fa57e43c830e338dc39d292c7def676e0e8f7545972b7da20275da',
                            text='when flag clicked\nsay [Hi]'
                        ),
                        ScratchImageMetaData(
                            hash='cd6d9a0d464bb8f5eec1e6fc9a4e33378a64ebfce7c6198794ead614962f38e5',
                            text='when flag clicked\nsay [Hi]\nmove (foo) steps\nturn ccw (9) degrees'
                        ),
                        ScratchImageMetaData(
                            hash='8e8a2129c3cecf32101248439961735fc1d45793fadc56e2575673f63d42b9fb',
                            text='when flag clicked\nclear\nforever\npen down\n\nif <<mouse down?> and <touching [mouse-pointer v]?>> then\nswitch costume to [button v]\nelse\nadd (x position) to [list v]\nend\n\nmove (foo) steps\nturn ccw (9) degrees'
                        ),
                    }
        self.assertSetEqual(actual_scratch_images, expected_scratch_images)

    def test_example_multiple_codeblocks_2(self):
        '''Tests that enabling the codehilite extension does not effect
        the functionality. (Loads the compatiability processor).
        '''
        extensions = ['markdown.extensions.codehilite', 'markdown.extensions.fenced_code']
        verto_extension = VertoExtension([self.processor_name], {}, extensions)
        test_string = self.read_test_file(self.processor_name, 'example_multiple_codeblocks_2.md')

        converted_test_string = markdown.markdown(test_string, extensions=extensions + [verto_extension])
        expected_string = self.read_test_file(self.processor_name, 'example_multiple_codeblocks_expected_2.html', strip=True)
        self.assertEqual(expected_string, converted_test_string)

        actual_scratch_images = verto_extension.required_files['scratch_images']
        expected_scratch_images = {
                        ScratchImageMetaData(
                            hash='a3b77ed3c3fa57e43c830e338dc39d292c7def676e0e8f7545972b7da20275da',
                            text='when flag clicked\nsay [Hi]'
                        ),
                        ScratchImageMetaData(
                            hash='cd6d9a0d464bb8f5eec1e6fc9a4e33378a64ebfce7c6198794ead614962f38e5',
                            text='when flag clicked\nsay [Hi]\nmove (foo) steps\nturn ccw (9) degrees'
                        ),
                        ScratchImageMetaData(
                            hash='8e8a2129c3cecf32101248439961735fc1d45793fadc56e2575673f63d42b9fb',
                            text='when flag clicked\nclear\nforever\npen down\n\nif <<mouse down?> and <touching [mouse-pointer v]?>> then\nswitch costume to [button v]\nelse\nadd (x position) to [list v]\nend\n\nmove (foo) steps\nturn ccw (9) degrees'
                        ),
                    }
        self.assertSetEqual(actual_scratch_images, expected_scratch_images)

    def test_example_split_codeblocks(self):
        '''Tests that scratch images are split if the split option is
        given on the language.
        '''
        test_string = self.read_test_file(self.processor_name, 'example_split_codeblocks.md')

        converted_test_string = markdown.markdown(test_string, extensions=['markdown.extensions.fenced_code', self.verto_extension])
        expected_string = self.read_test_file(self.processor_name, 'example_split_codeblocks_expected.html', strip=True)
        self.assertEqual(expected_string, converted_test_string)

        actual_scratch_images = self.verto_extension.required_files['scratch_images']
        expected_scratch_images = {
                        ScratchImageMetaData(
                            hash='a3b77ed3c3fa57e43c830e338dc39d292c7def676e0e8f7545972b7da20275da',
                            text='when flag clicked\nsay [Hi]'
                        ),
                        ScratchImageMetaData(
                            hash='cd6d9a0d464bb8f5eec1e6fc9a4e33378a64ebfce7c6198794ead614962f38e5',
                            text='when flag clicked\nsay [Hi]\nmove (foo) steps\nturn ccw (9) degrees'
                        ),
                        ScratchImageMetaData(
                            hash='a0f8fcad796864abfacac8bda6e0719813833fd1fca348700abbd040557c1576',
                            text='when flag clicked\nclear\nforever\npen down\nif <<mouse down?> and <touching [mouse-pointer v]?>> then\nswitch costume to [button v]\nelse\nadd (x position) to [list v]\nend\nmove (foo) steps\nturn ccw (9) degrees'
                        ),
                    }
        self.assertSetEqual(actual_scratch_images, expected_scratch_images)

    def test_example_split_codeblocks_default(self):
        '''Tests that scratch images are split if the split option is
        given on the language.
        '''
        verto_extension = VertoExtension([self.processor_name], {}, [])
        test_string = self.read_test_file(self.processor_name, 'example_split_codeblocks_default.md')

        converted_test_string = markdown.markdown(test_string, extensions=[verto_extension])
        expected_string = self.read_test_file(self.processor_name, 'example_split_codeblocks_expected.html', strip=True)
        self.assertEqual(expected_string, converted_test_string)

        actual_scratch_images = verto_extension.required_files['scratch_images']
        expected_scratch_images = {
                        ScratchImageMetaData(
                            hash='a3b77ed3c3fa57e43c830e338dc39d292c7def676e0e8f7545972b7da20275da',
                            text='when flag clicked\nsay [Hi]'
                        ),
                        ScratchImageMetaData(
                            hash='cd6d9a0d464bb8f5eec1e6fc9a4e33378a64ebfce7c6198794ead614962f38e5',
                            text='when flag clicked\nsay [Hi]\nmove (foo) steps\nturn ccw (9) degrees'
                        ),
                        ScratchImageMetaData(
                            hash='a0f8fcad796864abfacac8bda6e0719813833fd1fca348700abbd040557c1576',
                            text='when flag clicked\nclear\nforever\npen down\nif <<mouse down?> and <touching [mouse-pointer v]?>> then\nswitch costume to [button v]\nelse\nadd (x position) to [list v]\nend\nmove (foo) steps\nturn ccw (9) degrees'
                        ),
                    }
        self.assertSetEqual(actual_scratch_images, expected_scratch_images)

    def test_example_split_codeblocks_compatibility(self):
        '''Tests that options are compatible with codehilite.
        '''
        extensions = ['markdown.extensions.codehilite', 'markdown.extensions.fenced_code']
        verto_extension = VertoExtension([self.processor_name], {}, extensions)
        test_string = self.read_test_file(self.processor_name, 'example_split_codeblocks.md')

        converted_test_string = markdown.markdown(test_string, extensions=extensions + [verto_extension])
        expected_string = self.read_test_file(self.processor_name, 'example_split_codeblocks_expected.html', strip=True)
        self.assertEqual(expected_string, converted_test_string)

        actual_scratch_images = verto_extension.required_files['scratch_images']
        expected_scratch_images = {
                        ScratchImageMetaData(
                            hash='a3b77ed3c3fa57e43c830e338dc39d292c7def676e0e8f7545972b7da20275da',
                            text='when flag clicked\nsay [Hi]'
                        ),
                        ScratchImageMetaData(
                            hash='cd6d9a0d464bb8f5eec1e6fc9a4e33378a64ebfce7c6198794ead614962f38e5',
                            text='when flag clicked\nsay [Hi]\nmove (foo) steps\nturn ccw (9) degrees'
                        ),
                        ScratchImageMetaData(
                            hash='a0f8fcad796864abfacac8bda6e0719813833fd1fca348700abbd040557c1576',
                            text='when flag clicked\nclear\nforever\npen down\nif <<mouse down?> and <touching [mouse-pointer v]?>> then\nswitch costume to [button v]\nelse\nadd (x position) to [list v]\nend\nmove (foo) steps\nturn ccw (9) degrees'
                        ),
                    }
        self.assertSetEqual(actual_scratch_images, expected_scratch_images)

    def test_example_random_codeblocks(self):
        '''Tests that scratch images are arranged randomly given
        the random option is given.
        '''
        test_string = self.read_test_file(self.processor_name, 'example_random_codeblocks.md')

        outputs = set()
        for i in range(6): #P(Outputs the Same) < 0.99 [3 Blocks]
            converted_test_string = markdown.markdown(test_string, extensions=['markdown.extensions.fenced_code', self.verto_extension])
            outputs.add(converted_test_string)
            self.assertEqual(len(self.verto_extension.required_files['scratch_images']), 1)
            self.verto_extension.clear_saved_data()
        self.assertFalse(len(outputs) == 1)

    def test_example_random_split_codeblocks(self):
        '''Tests that scratch images are arranged randomly given
        the random and split option is given.
        '''
        test_string = self.read_test_file(self.processor_name, 'example_random_split_codeblocks.md')

        outputs = set()
        for i in range(6): #P(Outputs the Same) < 0.99 [3 Blocks]
            converted_test_string = markdown.markdown(test_string, extensions=['markdown.extensions.fenced_code', self.verto_extension])
            outputs.add(converted_test_string)
            self.assertEqual(len(self.verto_extension.required_files['scratch_images']), 3)
            self.verto_extension.clear_saved_data()
        self.assertFalse(len(outputs) == 1)

    def test_example_other_code(self):
        '''Tests that other codeblocks that are not scratch codeblocks
        are not erroneously matched.
        '''
        test_string = self.read_test_file(self.processor_name, 'example_other_code.md')

        converted_test_string = markdown.markdown(test_string, extensions=['markdown.extensions.fenced_code', self.verto_extension])
        expected_string = self.read_test_file(self.processor_name, 'example_other_code_expected.html', strip=True)
        self.assertEqual(expected_string, converted_test_string)

        actual_scratch_images = self.verto_extension.required_files['scratch_images']
        expected_scratch_images = {
                        ScratchImageMetaData(
                            hash='a0f8fcad796864abfacac8bda6e0719813833fd1fca348700abbd040557c1576',
                            text='when flag clicked\nclear\nforever\npen down\nif <<mouse down?> and <touching [mouse-pointer v]?>> then\nswitch costume to [button v]\nelse\nadd (x position) to [list v]\nend\nmove (foo) steps\nturn ccw (9) degrees'
                        ),
                    }
        self.assertSetEqual(actual_scratch_images, expected_scratch_images)
Пример #34
0
class Verto(object):
    '''A converter object for converting markdown with complex elements
    to HTML.
    '''

    def __init__(self, processors=DEFAULT_PROCESSORS, html_templates={}, extensions=[], custom_argument_rules={}):
        '''Creates a Verto object.

        Args:
            processors: A set of processor names given as strings for which
                their processors are enabled. If given, all other
                processors are skipped.
            html_templates: A dictionary of HTML templates to override
                existing HTML templates for processors. Dictionary contains
                processor names given as a string as keys mapping HTML strings
                as values.
                eg: {'image': '<img src={{ source }}>'}
            extensions: A list of extra extensions to run on the
                markdown package.
            custom_argument_rules: A dictionary of rules for the processors to
                override default processor rules.
        '''
        self.processors = set(processors)
        self.html_templates = dict(html_templates)
        self.extensions = list(extensions)
        self.custom_argument_rules = custom_argument_rules
        self.create_converter()

    def create_converter(self):
        '''Create the Verto extension and converter for future use.'''
        self.verto_extension = VertoExtension(
            processors=self.processors,
            html_templates=self.html_templates,
            extensions=self.extensions,
            custom_argument_rules=self.custom_argument_rules,
        )
        all_extensions = self.extensions + [self.verto_extension]
        self.converter = markdown.Markdown(extensions=all_extensions)

    def convert(self, text):
        '''Return a VertoResult object after converting
        the given markdown string.

        Args:
            text: A string of Markdown text to be converted.

        Returns:
            A VertoResult object.
        '''
        self.verto_extension.clear_document_data()
        html_string = self.converter.convert(text)
        result = VertoResult(
            html_string=html_string,
            title=self.verto_extension.title,
            required_files=self.verto_extension.required_files,
            heading_tree=self.verto_extension.get_heading_tree(),
            required_glossary_terms=self.verto_extension.glossary_terms
        )
        return result

    def clear_saved_data(self):
        '''Clears data that is saved between documents. This should be
        called between conversions on unrelated documents.
        '''
        self.verto_extension.clear_saved_data()

    def update_templates(self, html_templates):
        '''Update the template dictionary with the given dictionary
        of templates, while leaving all other HTML templates (including
        any custom set templates) untouched. The updated dictionary
        will be used for converting from this point onwards.

        Args:
            html_templates: A dictionary of HTML templates to override
                existing HTML templates for processors. Dictionary
                contains processor names given as a string as keys
                mapping HTML strings as values.
                eg: {'image': '<img src={{ source }}>'}
        '''
        self.html_templates.update(html_templates)
        self.create_converter()

    def clear_templates(self):
        '''Set the template dictionary to it's original values.
        '''
        self.html_templates = {}
        self.create_converter()

    @staticmethod
    def processor_defaults():
        '''Returns a copy of the default processor set.

        Returns:
            A set of default processor names as strings.
        '''
        return set(DEFAULT_PROCESSORS)

    def update_processors(self, processors=DEFAULT_PROCESSORS):
        '''Update the processors used for conversion with the given
        set. The updated set will be used for converting from this
        point onwards. If parameter is empty, default processors will
        be used.

        Args:
            processors: A set of processor names given as strings for
                which their processors are enabled. If given, all other
                processors are skipped.
        '''
        self.processors = set(processors)
        self.create_converter()