Skip to content

Commit 2a1828a

Browse files
authored
Allow raw HTML in caption prefix (#2679)
* Allow raw HTML in caption prefix * Add documentation note about new behavior
1 parent fbf4bf2 commit 2a1828a

File tree

4 files changed

+78
-6
lines changed

4 files changed

+78
-6
lines changed

docs/src/markdown/about/changelog.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
## 10.16
44

55
- **NEW**: Drop support for Python 3.8.
6-
- **NEW**: Snippets: Added `max_retries` and `backoff_retries` options to configure new retry logic for HTTP 429 errors
7-
(Too Many Requests client error).
6+
- **NEW**: Snippets: Added `max_retries` and `backoff_retries` options to configure new retry logic for HTTP 429
7+
errors (Too Many Requests client error).
8+
- **NEW**: Caption: Prefix templates are now preserved exactly as specified allowing the insertion of HTML tags if
9+
desired.
810
- **FIX**: Caption: Fix issue where manual numbers in auto were not respected appropriately.
911

1012
## 10.15

docs/src/markdown/extensions/blocks/plugins/caption.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,32 @@ extenconfigs = {
498498
}
499499
```
500500

501+
Prefix templates are inserted exactly as specified, that means you can also use raw HTML. That also means you must
502+
HTML escape anything that needs it as well.
503+
504+
```py
505+
extenconfigs = {
506+
"pymdownx.blocks.caption": {
507+
"types": [
508+
'caption',
509+
{
510+
'name': 'figure-caption',
511+
'prefix': 'Figure <span class="prefix-fig-num">{}</span>.'
512+
'classes': 'one-class two-class'
513+
},
514+
{
515+
'name': 'table-caption',
516+
'prefix': 'Table <span class="prefix-fig-num">{}</span>.'
517+
}
518+
]
519+
}
520+
}
521+
```
522+
523+
/// new | New 10.16
524+
Using raw HTML in prefix templates was added in 10.16.
525+
///
526+
501527
## Global Options
502528

503529
Options | Type | Descriptions

pymdownx/blocks/caption.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
RE_SEP = re.compile(r'[_-]+')
3636

3737

38-
def update_tag(el, fig_type, fig_num, template, prepend):
38+
def update_tag(el, fig_type, fig_num, template, prepend, md):
3939
"""Update tag ID and caption prefix."""
4040

4141
# Auto add an ID
@@ -47,7 +47,7 @@ def update_tag(el, fig_type, fig_num, template, prepend):
4747
for child in list(el) if prepend else reversed(el):
4848
if child.tag == 'figcaption':
4949
children = list(child)
50-
value = template.format(fig_num)
50+
value = md.htmlStash.store(template.format(fig_num))
5151
if not len(children) or children[0].tag != 'p':
5252
p = etree.Element('p')
5353
span = etree.SubElement(p, 'span', {'class': 'caption-prefix'})
@@ -184,7 +184,8 @@ def run(self, doc):
184184
fig_type,
185185
'.'.join(str(x) for x in counter[:stack + 1]),
186186
self.fig_types.get(fig_type, ''),
187-
prepend
187+
prepend,
188+
self.md
188189
)
189190

190191
# Clean up attributes
@@ -314,7 +315,8 @@ def on_end(self, block):
314315
self.NAME,
315316
self.fig_num,
316317
prefix,
317-
self.prepend
318+
self.prepend,
319+
self.md
318320
)
319321

320322

tests/test_extensions/test_blocks/test_captions.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1522,3 +1522,45 @@ def test_manual_prepend(self):
15221522
""",
15231523
True
15241524
)
1525+
1526+
1527+
class TestBlocksCaptionCustomPrefix(util.MdCase):
1528+
"""Test Blocks caption cases with `auto` level."""
1529+
1530+
extension = ['pymdownx.blocks.caption']
1531+
extension_configs = {
1532+
'pymdownx.blocks.caption': {
1533+
'types': [
1534+
'caption',
1535+
{
1536+
'name': 'figure-caption',
1537+
'prefix': 'Figure <span class="caption-num">{}</span>.'
1538+
},
1539+
{
1540+
'name': 'table-caption',
1541+
'prefix': 'Table <span class="caption-num">{}</span>.'
1542+
}
1543+
]
1544+
}
1545+
}
1546+
1547+
def test_custom_prefix(self):
1548+
"""Test custom prefix."""
1549+
1550+
self.check_markdown(
1551+
R'''
1552+
A paragraph with a caption.
1553+
/// figure-caption
1554+
This is the caption.
1555+
///
1556+
''',
1557+
R'''
1558+
<figure id="__figure-caption_1">
1559+
<p>A paragraph with a caption.</p>
1560+
<figcaption>
1561+
<p><span class="caption-prefix">Figure <span class="caption-num">1</span>.</span> This is the caption.</p>
1562+
</figcaption>
1563+
</figure>
1564+
''',
1565+
True
1566+
)

0 commit comments

Comments
 (0)