Commit 554c71d8 authored by Miguel Angel Reina Ortega's avatar Miguel Angel Reina Ortega
Browse files

Fix for table caption indentation removal

parent 4103a627
Loading
Loading
Loading
Loading
Loading
+26 −11
Changes for generateBaseline/pandocFilter.py: 26 added lines, 11 removed lines.
Original line number Diff line number Diff line
@@ -695,12 +695,21 @@ def _line_body(line:str) -> str:
	return line


def _is_anchor_only(line:str) -> bool:
	"""A line whose only content is a span or anchor left beside a caption."""
	text = line.strip()
	return text != '' and _TARGET_ANCHOR.sub('', text).strip() == ''


def _is_table_caption(line:str) -> bool:
	"""A markdown table caption, or a caption already written as ``Table:``."""
	"""A markdown table caption, or a caption already written as ``Table:``.

	Anchor markup may follow the closing ``**`` on the same line.
	"""
	text = line.strip()
	if re.match(r'^Table\s*:', text, re.IGNORECASE):
		return True
	return re.match(r'^\*\*Table\b.*\*\*\s*$', text, re.IGNORECASE) is not None
	return re.match(r'^\*\*Table\b.*?\*\*', text, re.IGNORECASE) is not None


def _strip_shared_indent(lines:list[str]) -> list[str]:
@@ -728,14 +737,14 @@ def _take_preceding_caption(lines:list[str]) -> list[str]:
	"""Lift a caption, and the blank lines between it and the table, off the output."""
	if not lines:
		return []
	blanks:list[str] = []
	while lines and lines[-1].strip() == '':
		blanks.append(lines.pop())
	skipped:list[str] = []
	while lines and (lines[-1].strip() == '' or _is_anchor_only(lines[-1])):
		skipped.append(lines.pop())
	if lines and _is_table_caption(lines[-1]):
		caption = lines.pop()
		blanks.reverse()
		return [caption, *blanks]
	lines.extend(reversed(blanks))
		skipped.reverse()
		return [caption, *skipped]
	lines.extend(reversed(skipped))
	return []


@@ -774,7 +783,7 @@ def removeGridTableIndent(progress:Progress, mdLines:list[str]) -> list[str]:
			if _is_grid_table_line(line) and not after_blanks:
				block.append(line)
				continue
			if line.strip() == '':
			if line.strip() == '' or _is_anchor_only(line):
				after_blanks.append(line)
				continue
			if _is_table_caption(line):
@@ -786,11 +795,17 @@ def removeGridTableIndent(progress:Progress, mdLines:list[str]) -> list[str]:
			_flush()
			_lines.extend(after_blanks)
			after_blanks = []
		if _is_grid_table_line(line) and re.match(r'^[ \t]+\+', line):
			block.extend(_take_preceding_caption(_lines))
		if _is_grid_table_line(line) and re.match(r'^[ \t]+[+|]', line):
			preceding = _take_preceding_caption(_lines)
			# An indented grid table always moves out. An indented pipe table
			# moves out only when its caption has to move with it.
			if re.match(r'^[ \t]+\+', line) or preceding:
				block.extend(preceding)
				block.append(line)
				continue
			_lines.append(line)
			continue
		_lines.append(line)
	_flush()
	_lines.extend(after_blanks)