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)
def test_alt_hover_caption(self): '''Tests that multiple arguments are rendered correctly when caption argument is included and expected images are updated. ''' test_string = self.read_test_file(self.processor_name, 'alt_hover_caption.md') blocks = self.to_blocks(test_string) self.assertListEqual( [False, True, False, True, 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=[self.verto_extension]) expected_string = self.read_test_file( self.processor_name, 'alt_hover_caption_expected.html', strip=True) self.assertEqual(expected_string, converted_test_string) images = self.verto_extension.required_files['images'] expected_images = { 'finite-state-automata-no-trap-example.png', 'finite-state-automata-trap-added-example.png' } self.assertSetEqual(expected_images, images)
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)
def test_contains_multiple_images_some_captions(self): '''Tests to ensure that multiple internally reference images ar rendered correctly and expected images is updated and that images without captions are ignored. ''' test_string = self.read_test_file( self.processor_name, 'contains_multiple_images_some_captions.md') blocks = self.to_blocks(test_string) self.assertListEqual( [False, True, False, True, False, True, False, True, False, 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=[self.verto_extension]) expected_string = self.read_test_file( self.processor_name, 'contains_multiple_images_some_captions_expected.html', strip=True) self.assertEqual(expected_string, converted_test_string) images = self.verto_extension.required_files['images'] expected_images = { 'finite-state-automata-no-trap-example.png', 'finite-state-automata-trap-added-example.png' } self.assertSetEqual(expected_images, images)
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)
def test_multiple_images_captions_true(self): '''Tests to ensure that multiple internally reference images ar rendered correctly and expected images is updated. ''' test_string = self.read_test_file(self.processor_name, 'multiple_images_captions_true.md') blocks = self.to_blocks(test_string) self.assertListEqual([ False, True, False, True, True, False, True, 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=[self.verto_extension]) expected_string = self.read_test_file( self.processor_name, 'multiple_images_captions_true_expected.html', strip=True) self.assertEqual(expected_string, converted_test_string) images = self.verto_extension.required_files['images'] expected_images = { 'the-first-image.png', 'Lipsum.png', 'pixel-diamond.png' } self.assertSetEqual(expected_images, images)
def test_align_undefined_error(self): '''Tests that ArgumentValueError is raised when undefined align value is given. ''' test_string = self.read_test_file(self.processor_name, 'align_undefined_error.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( ArgumentValueError, lambda x: markdown.markdown(x, extensions=[self.verto_extension]), test_string)
def test_image_in_image_tag(self): '''Test that ImageCaptionContainsImageError is raised when the first line in an image container block is another image container block. ''' test_string = self.read_test_file(self.processor_name, 'image_in_image_tag.md') blocks = self.to_blocks(test_string) self.assertListEqual([True, True, False, True, False, True], [ ImageContainerBlockProcessor(self.ext, self.md.parser).test( blocks, block) for block in blocks ], msg='"{}"'.format(test_string)) self.assertRaises( ImageCaptionContainsImageError, lambda x: markdown.markdown(x, extensions=[self.verto_extension]), test_string)
def test_missing_alt_parameter(self): '''Tests that missing alt argument produces correct error. ''' test_string = self.read_test_file(self.processor_name, 'missing_alt_parameter.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=[self.verto_extension]), test_string)
def test_caption_true_not_provided_numbered_list(self): '''Tests that ImageMissingCaptionError is thrown when caption argument is true but not provided in numbered list. ''' test_string = self.read_test_file( self.processor_name, 'caption_true_not_provided_numbered_list.md') blocks = self.to_blocks(test_string) self.assertListEqual([False, False, False, True, True, False], [ ImageContainerBlockProcessor(self.ext, self.md.parser).test( blocks, block) for block in blocks ], msg='"{}"'.format(test_string)) self.assertRaises( ImageMissingCaptionError, lambda x: markdown.markdown(x, extensions=[self.verto_extension]), test_string)
def test_caption_true_missing_end_tag(self): # throw error '''Tests that TagNotMatchedError is thrown when image tag is missing end tag. ''' test_string = self.read_test_file(self.processor_name, 'caption_true_missing_end_tag.md') blocks = self.to_blocks(test_string) self.assertListEqual([False, True, False, False], [ ImageContainerBlockProcessor(self.ext, self.md.parser).test( blocks, block) for block in blocks ], msg='"{}"'.format(test_string)) self.assertRaises( TagNotMatchedError, lambda x: markdown.markdown(x, extensions=[self.verto_extension]), test_string)
def test_no_caption(self): '''Tests processor does not match image tag when caption argument is not included. ''' test_string = self.read_test_file(self.processor_name, 'no_caption.md') blocks = self.to_blocks(test_string) self.assertListEqual([False, False, 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=[self.verto_extension]) expected_string = self.read_test_file(self.processor_name, 'no_caption_expected.html', strip=True) self.assertEqual(expected_string, converted_test_string)
def test_external_image(self): '''Tests that external images are processed and that the expected images are unchanged. ''' test_string = self.read_test_file(self.processor_name, 'external_image.md') blocks = self.to_blocks(test_string) self.assertListEqual([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=[self.verto_extension]) expected_string = self.read_test_file(self.processor_name, 'external_image_expected.html', strip=True) self.assertEqual(expected_string, converted_test_string)
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)
def test_align_left(self): '''Tests that argument for align produces expected output when set to left and expected images are updated. ''' test_string = self.read_test_file(self.processor_name, 'align_left.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)) converted_test_string = markdown.markdown( test_string, extensions=[self.verto_extension]) expected_string = self.read_test_file(self.processor_name, 'align_left_expected.html', strip=True) self.assertEqual(expected_string, converted_test_string) images = self.verto_extension.required_files['images'] expected_images = {'computer-studying-turing-test.png'} self.assertSetEqual(expected_images, images)
def test_caption_true(self): '''Tests to ensure that caption is rendered correctly and expected images are updated. ''' test_string = self.read_test_file(self.processor_name, 'caption_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=[self.verto_extension]) expected_string = self.read_test_file(self.processor_name, 'caption_true_expected.html', strip=True) self.assertEqual(expected_string, converted_test_string) images = self.verto_extension.required_files['images'] expected_images = {'cats.png'} self.assertSetEqual(expected_images, images)
def test_doc_example_basic(self): '''Basic example of common usage. ''' test_string = self.read_test_file(self.processor_name, 'doc_example_basic_usage.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)) converted_test_string = markdown.markdown( test_string, extensions=[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) images = self.verto_extension.required_files['images'] expected_images = set() self.assertSetEqual(expected_images, images)
def buildProcessors(self, md, md_globals): ''' Populates internal variables for processors. This should not be called externally, this is used by the extendMarkdown method. Args: md: An instance of the markdown object being extended. md_globals: Global variables in the markdown module namespace. ''' self.preprocessors = [ ['comment', CommentPreprocessor(self, md), '_begin'], ['save-title', SaveTitlePreprocessor(self, md), '_end'], ['remove-title', RemoveTitlePreprocessor(self, md), '_end'], ] self.blockprocessors = [ # Markdown overrides ['heading', HeadingBlockProcessor(self, md.parser), '<hashheader'], # Single line (in increasing complexity) [ 'interactive-tag', InteractiveTagBlockProcessor(self, md.parser), '<paragraph' ], [ 'interactive-container', InteractiveContainerBlockProcessor(self, md.parser), '<paragraph' ], [ 'image-container', ImageContainerBlockProcessor(self, md.parser), '<paragraph' ], [ 'image-tag', ImageTagBlockProcessor(self, md.parser), '<paragraph' ], ['video', VideoBlockProcessor(self, md.parser), '<paragraph'], [ 'conditional', ConditionalProcessor(self, md.parser), '<paragraph' ], ['panel', PanelBlockProcessor(self, md.parser), '<paragraph'], [ 'blockquote', BlockquoteBlockProcessor(self, md.parser), '<paragraph' ], # Multiline ] self.inlinepatterns = [ # A special treeprocessor ['relative-link', RelativeLinkPattern(self, md), '_begin'], ['glossary-link', GlossaryLinkPattern(self, md), '_begin'], ['image-inline', ImageInlinePattern(self, md), '_begin'] ] scratch_ordering = '>inline' if 'hilite' not in self.compatibility else '<hilite' self.treeprocessors = [ ['scratch', ScratchTreeprocessor(self, md), scratch_ordering], [ 'scratch-inline', ScratchInlineTreeprocessor(self, md), '>inline' ], ] self.postprocessors = [] self.buildGenericProcessors(md, md_globals)