Skip to content

Commit

Permalink
Ensure manual numbers operate as expected in relative depth
Browse files Browse the repository at this point in the history
  • Loading branch information
facelessuser committed Oct 16, 2024
1 parent 9d540c3 commit fe9e353
Showing 1 changed file with 41 additions and 42 deletions.
83 changes: 41 additions & 42 deletions pymdownx/blocks/caption.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,54 +113,53 @@ def run(self, doc):
# Found a figure that was not generated by this plugin.
continue

# Handle a manual number
if '__figure_num' in el.attrib:
fig_num = [int(x) for x in el.attrib['__figure_num'].split('.')]
del el.attrib['__figure_num']
el.attrib['__figure_level'] = str(len(fig_num))
stack = len(fig_num) - 1

# Determine depth
# Handle a specified relative nesting depth
if '__figure_level' in el.attrib:
stack += int(el.attrib['__figure_level']) + 1
if self.auto_level and stack >= (self.auto_level - 1):
continue
else:
# Handle a specified relative nesting depth
if '__figure_level' in el.attrib:
stack += int(el.attrib['__figure_level']) + 1
if self.auto_level and stack >= (self.auto_level - 1):
continue
else:
stack += 1

current = el
while True:
parent = parent_map.get(current, None)

# No more parents
if parent is None:
stack += 1

current = el
while True:
parent = parent_map.get(current, None)

# No more parents
if parent is None:
break

# Check if parent element is a figure of the current type
if parent.tag == 'figure' and parent.attrib['__figure_type'] == fig_type:
# See if position in stack is manually specified
level = '__figure_level' in parent.attrib
if level:
stack += int(parent.attrib['__figure_level']) + 1
else:
stack += 1
if level:
el.attrib['__figure_level'] = str(stack + 1)
# Ensure position in stack is not deeper than the specified level
if self.auto_level and stack >= self.auto_level:
skip = True
break

# Check if parent element is a figure of the current type
if parent.tag == 'figure' and parent.attrib['__figure_type'] == fig_type:
# See if position in stack is manually specified
level = '__figure_level' in parent.attrib
if level:
stack += int(parent.attrib['__figure_level']) + 1
else:
stack += 1
if level:
el.attrib['__figure_level'] = str(stack + 1)
# Ensure position in stack is not deeper than the specified level
if self.auto_level and stack >= self.auto_level:
skip = True
break

current = parent

if skip:
# Parent has been skipped so all children are also skipped
continue
current = parent

if skip:
# Parent has been skipped so all children are also skipped
continue

# Found an appropriate figure at an acceptable depth
if stack > -1:
# Handle a manual number
if '__figure_num' in el.attrib:
fig_num = [int(x) for x in el.attrib['__figure_num'].split('.')]
del el.attrib['__figure_num']
new_stack = len(fig_num) - 1
el.attrib['__figure_level'] = new_stack - stack
stack = new_stack

# Increment counter
l = last[fig_type]
counter = counters[fig_type]
Expand Down

0 comments on commit fe9e353

Please sign in to comment.