"""Test cases for Blocks (caption)."""
from ... import util
class TestBlocksCaption(util.MdCase):
"""Test Blocks caption cases with default configuration."""
extension = ['pymdownx.blocks.caption', 'md_in_html', 'pymdownx.blocks.html']
extension_configs = {
'pymdownx.blocks.caption': {
'auto': False
}
}
def test_caption(self):
"""Test basic caption."""
self.check_markdown(
R'''
A paragraph with a caption.
/// caption
This is the caption.
///
''',
R'''
A paragraph with a caption.
This is the caption.
''',
True
)
def test_caption_with_markdown(self):
"""Test caption with markdown."""
self.check_markdown(
R'''
A paragraph with a caption.
/// caption
This is the **caption**.
///
''',
R'''
A paragraph with a caption.
This is the caption.
''',
True
)
def test_image_caption(self):
"""Test image caption."""
self.check_markdown(
R'''

/// caption
This is the caption.
///
''',
R'''
This is the caption.
''',
True
)
def test_nested_caption(self):
"""Test nested caption."""
self.check_markdown(
R'''
A paragraph with a caption.
/// caption
This is the nested caption.
///
/// caption
This is the caption.
///
''',
R'''
A paragraph with a caption.
This is the nested caption.
This is the caption.
''',
True
)
def test_caption_in_figure_block(self):
"""Test that captions are injected into existing figures."""
self.check_markdown(
R"""
Some content
/// caption
Caption.
///
/// html | figure
Some content
///
/// caption
Caption.
///
""",
"""
Some content
Caption.
Some content
Caption.
""",
True
)
def test_caption_not_in_figure_block(self):
"""Test that captions are not injected into existing figures that already have captions."""
self.check_markdown(
R"""
Some content
Existing caption
/// caption
Caption.
///
/// html | figure
Some content
//// html | figcaption
Existing caption
////
///
/// caption
Caption.
///
""",
R"""
''',
True
)
def test_caption_inline_arbitrary_attribute(self):
"""Ensure inline shorthand classes and attribute classes are unique after merging."""
self.check_markdown(
R'''
Paragraph
/// figure-caption | .custom-class[data="test"]
Caption text.
///
''',
R'''
Paragraph
Caption text.
''',
True
)
class TestBlocksCaptionClass(util.MdCase):
"""Test caption classes."""
extension = ['pymdownx.blocks.caption', 'md_in_html', 'pymdownx.blocks.html']
extension_configs = {
'pymdownx.blocks.caption': {
'types': [
{"name": "figure-class", "classes": "class1 class2"}
]
}
}
def test_class_insertion(self):
"""Test class insertion."""
self.check_markdown(
R'''
A paragraph with a caption.
/// figure-class
This is the caption.
///
''',
R'''
A paragraph with a caption.
This is the caption.
''',
True
)
def test_class_insertion_existing_figure(self):
"""Test class insertion on existing figure."""
self.check_markdown(
R'''
/// html | figure
A paragraph with a caption.
///
/// figure-class
This is the caption.
///
''',
R'''
A paragraph with a caption.
This is the caption.
''',
True
)
def test_class_insertion_existing_class(self):
"""Test class insertion on existing figure with existing class."""
self.check_markdown(
R'''
/// html | figure.class1
A paragraph with a caption.
///
/// figure-class
This is the caption.
///
''',
R'''
A paragraph with a caption.
This is the caption.
''',
True
)
class TestBlocksCaptionPrefix(util.MdCase):
"""Test Blocks caption cases with enabled `auto`."""
extension = ['pymdownx.blocks.caption', 'md_in_html']
extension_configs = {
'pymdownx.blocks.caption': {
'auto': False
}
}
def test_caption(self):
"""Test basic caption with `auto`."""
self.check_markdown(
R'''
A paragraph with a caption.
/// figure-caption | 1
This is the caption.
///
''',
R'''
A paragraph with a caption.
Figure 1. This is the caption.
''',
True
)
def test_consecutive_captions(self):
"""Test consecutive captions with `auto`."""
self.check_markdown(
R'''
A paragraph with a caption.
/// figure-caption | 1
This is the caption.
///
A paragraph with a caption.
/// figure-caption | 2
This is the caption.
///
''',
R'''
''',
True
)
def test_caption_inline_id_prepend(self):
"""Test caption with inline shorthand ID and prepend marker."""
self.check_markdown(
R'''
Text
/// figure-caption | < #custom-prepend
Prepended caption.
///
''',
R'''
Prepended caption.
Text
''',
True
)
def test_inject_new_p_in_caption(self):
"""Test `auto` cases that require the prefix to be injected in a new paragraph."""
self.check_markdown(
R"""
Test
/// figure-caption | 1
///
Test
/// figure-caption | 2
> blockquote
///
""",
R"""
Test
Figure 1.
Test
Figure 2.
blockquote
""",
True
)
def test_empty_paragraph(self):
"""Test `auto` cases that require prefix to inject a new paragraph."""
self.check_markdown(
R"""
Test
/// figure-caption | 1
///
""",
R"""
""",
True
)
def test_depth(self):
"""Depth is not really supported in manual, so a generic response is expected."""
self.check_markdown(
R"""
Paragraph
/// figure-caption
Caption 1
///
Paragraph
/// figure-caption | ^1
Caption 1.1
///
Paragraph
/// figure-caption | ^2
Caption 1.1.1
///
Paragraph
/// figure-caption | ^3
Caption 1.1.1.1
///
""",
"""
Paragraph
Caption 1
Paragraph
Figure 1.1. Caption 1.1
Paragraph
Figure 1.1.1. Caption 1.1.1
Paragraph
Figure 1.1.1.1. Caption 1.1.1.1
""",
True
)
class TestBlocksCaptionAutoPrefix(util.MdCase):
"""Test Blocks caption cases with enabled `auto`."""
extension = ['pymdownx.blocks.caption', 'md_in_html']
extension_configs = {
'pymdownx.blocks.caption': {
}
}
def test_caption_number_and_id(self):
"""Test captions with IDs and number."""
self.check_markdown(
R'''
A paragraph with a caption.
/// figure-caption | 3 #id
This is the caption.
///
A paragraph with a caption.
/// figure-caption | < 4 #id2
This is the caption.
///
''',
R'''
A paragraph with a caption.
Figure 3. This is the caption.
Figure 4. This is the caption.
A paragraph with a caption.
''',
True
)
def test_caption(self):
"""Test basic caption with `auto`."""
self.check_markdown(
R'''
A paragraph with a caption.
/// figure-caption
This is the caption.
///
''',
R'''
A paragraph with a caption.
Figure 1. This is the caption.
''',
True
)
def test_consecutive_captions(self):
"""Test consecutive captions with `auto`."""
self.check_markdown(
R'''
A paragraph with a caption.
/// figure-caption
This is the caption.
///
A paragraph with a caption.
/// figure-caption
This is the caption.
///
''',
R'''
A paragraph with a caption.
Figure 1. This is the caption.
A paragraph with a caption.
Figure 2. This is the caption.
''',
True
)
def test_nested_captions(self):
"""Test nested captions with `auto`."""
self.check_markdown(
R'''
A paragraph with a caption.
/// figure-caption
Level 3 caption.
///
/// figure-caption
Level 2 caption.
///
/// figure-caption
Level 1 caption.
///
''',
R'''
A paragraph with a caption.
Figure 1.1.1. Level 3 caption.
Figure 1.1. Level 2 caption.
Figure 1. Level 1 caption.
''',
True
)
def test_nested_consecutive_captions(self):
"""Test nested captions with `auto`."""
self.check_markdown(
R'''
A paragraph with a caption.
/// figure-caption
Level 3 caption.
///
/// figure-caption
Level 2 caption.
///
/// figure-caption
Level 1 caption.
///
A paragraph with a caption.
/// figure-caption
Level 2 caption.
///
/// figure-caption
Level 1 caption.
///
''',
R'''
A paragraph with a caption.
Figure 1.1.1. Level 3 caption.
Figure 1.1. Level 2 caption.
Figure 1. Level 1 caption.
A paragraph with a caption.
Figure 2.1. Level 2 caption.
Figure 2. Level 1 caption.
''',
True
)
def test_manual_prepend(self):
"""Test manual prepend."""
self.check_markdown(
R"""
Text
/// figure-caption | < 2
Prepended and number ignored
///
Text
/// figure-caption | >
Appended
///
""",
R"""
''',
True
)
def test_existing_fig_caption(self):
"""Test when a figure has a figure caption and we don't know what type it is."""
self.check_markdown(
R"""
Some text.
Caption.
""",
R"""
Some text.
Caption.
""",
True
)
def test_inject_new_p_in_caption(self):
"""Test `auto` cases that require the prefix to be injected in a new paragraph."""
self.check_markdown(
R"""
Test
/// figure-caption
///
Test
/// figure-caption
> blockquote
///
""",
R"""
Test
Figure 1.
Test
Figure 2.
blockquote
""",
True
)
def test_empty_paragraph(self):
"""Test `auto` cases that require prefix to inject a new paragraph."""
self.check_markdown(
R"""
Test
/// figure-caption
///
""",
R"""
Test
Figure 1.
""",
True
)
def test_nested_captions_manual_id(self):
"""Test nested captions with `auto` and `auto` with manual IDs."""
self.check_markdown(
R'''
A paragraph with a caption.
/// figure-caption
Level 4 caption.
///
/// figure-caption
attrs: {id: test}
Level 3 caption.
///
/// figure-caption
Level 2 caption.
///
/// figure-caption
Level 1 caption.
///
''',
R'''