| 1 |
"""Collapsible code.""" |
| 2 |
import xml.etree.ElementTree as etree |
| 3 |
from markdown import util as mutil |
| 4 |
import re |
| 5 |
from pymdownx.blocks.block import Block |
| 6 |
from pymdownx.blocks import BlocksExtension |
| 7 |
|
| 8 |
# Fenced block placeholder for SuperFences |
| 9 |
FENCED_BLOCK_RE = re.compile( |
| 10 |
r'^([\> ]*){}({}){}$'.format( |
| 11 |
mutil.HTML_PLACEHOLDER[0], |
| 12 |
mutil.HTML_PLACEHOLDER[1:-1] % r'([0-9]+)', |
| 13 |
mutil.HTML_PLACEHOLDER[-1] |
| 14 |
) |
| 15 |
) |
| 16 |
|
| 17 |
|
| 18 |
class CollapseCode(Block): |
| 19 |
"""Collapse code.""" |
| 20 |
|
| 21 |
NAME = 'collapse-code' |
| 22 |
|
| 23 |
def on_init(self): |
| 24 |
"""Handle initialization.""" |
| 25 |
|
| 26 |
# Track tab group count across the entire page. |
| 27 |
if 'collapse_code_count' not in self.tracker: |
| 28 |
self.tracker['collapse_code_count'] = 0 |
| 29 |
|
| 30 |
self.expand = self.config['expand_text'] |
| 31 |
if not isinstance(self.expand, str): |
| 32 |
raise ValueError("'expand_text' must be a string") |
| 33 |
|
| 34 |
self.collapse = self.config['collapse_text'] |
| 35 |
if not isinstance(self.collapse, str): |
| 36 |
raise ValueError("'collapse_text' must be a string") |
| 37 |
|
| 38 |
self.expand_title = self.config['expand_title'] |
| 39 |
if not isinstance(self.expand_title, str): |
| 40 |
raise ValueError("'expand_title' must be a string") |
| 41 |
|
| 42 |
self.collapse_title = self.config['collapse_title'] |
| 43 |
if not isinstance(self.collapse_title, str): |
| 44 |
raise ValueError("'collapse_title' must be a string") |
| 45 |
|
| 46 |
def on_create(self, parent): |
| 47 |
"""Create the element.""" |
| 48 |
|
| 49 |
self.count = self.tracker['collapse_code_count'] |
| 50 |
self.tracker['collapse_code_count'] += 1 |
| 51 |
el = etree.SubElement(parent, 'div', {'class': 'collapse-code'}) |
| 52 |
etree.SubElement( |
| 53 |
el, |
| 54 |
'input', |
| 55 |
{ |
| 56 |
"type": "checkbox", |
| 57 |
"id": f"__collapse{self.count}", |
| 58 |
"name": f"__collapse{self.count}", |
| 59 |
'checked': 'checked' |
| 60 |
} |
| 61 |
) |
| 62 |
return el |
| 63 |
|
| 64 |
def on_end(self, block): |
| 65 |
"""Convert non list items to details.""" |
| 66 |
|
| 67 |
el = etree.SubElement(block, 'div', {'class': 'code-footer'}) |
| 68 |
attrs = {'for': f'__collapse{self.count}', 'class': 'expand', 'tabindex': '0'} |
| 69 |
if self.expand_title: |
| 70 |
attrs['title'] = self.expand_title |
| 71 |
expand = etree.SubElement(el, 'label', attrs) |
| 72 |
expand.text = self.expand |
| 73 |
|
| 74 |
attrs = {'for': f'__collapse{self.count}', 'class': 'collapse', 'tabindex': '0'} |
| 75 |
if self.collapse_title: |
| 76 |
attrs['title'] = self.collapse_title |
| 77 |
collapse = etree.SubElement(el, 'label', attrs) |
| 78 |
collapse.text = self.collapse |
| 79 |
|
| 80 |
|
| 81 |
class CollapseCodeExtension(BlocksExtension): |
| 82 |
"""Admonition Blocks Extension.""" |
| 83 |
|
| 84 |
def __init__(self, *args, **kwargs): |
| 85 |
"""Initialize.""" |
| 86 |
|
| 87 |
self.config = { |
| 88 |
'expand_text': ['Expand', "Set the text for the expand button."], |
| 89 |
'collapse_text': ['Collapse', "Set the text for the collapse button."], |
| 90 |
'expand_title': ['expand', "Set the text for the expand title."], |
| 91 |
'collapse_title': ['collapse', "Set the text for the collapse title."] |
| 92 |
} |
| 93 |
|
| 94 |
super().__init__(*args, **kwargs) |
| 95 |
|
| 96 |
def extendMarkdownBlocks(self, md, blocks): |
| 97 |
"""Extend Markdown blocks.""" |
| 98 |
|
| 99 |
blocks.register(CollapseCode, self.getConfigs()) |
| 100 |
|
| 101 |
|
| 102 |
def makeExtension(*args, **kwargs): |
| 103 |
"""Return extension.""" |
| 104 |
|
| 105 |
return CollapseCodeExtension(*args, **kwargs) |