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

Restructuring the way bookmarks are handled

parent 1e1e18e0
Loading
Loading
Loading
Loading
Loading
+32 −3
Changes for generateBaseline/pandocFilter.py: 32 added lines, 3 removed lines.
Original line number Diff line number Diff line
@@ -96,6 +96,13 @@ def writeMDFile(progress:Progress, mdLines:list[str], document:str, outDirectory
	progress.stop_task(_taskID)


# Clause and table anchors are recreated as Word bookmarks in postprocessing.
# Leaving the span in the markdown makes Pandoc store an empty or hashed bookmark.
_TARGET_ANCHOR = re.compile(
	r'''\s*<span\s+id=(?:"[^"]*"|'[^']*'|[^\s>]+)>\s*</span>|\s*<a\s+name=(?:"[^"]*"|'[^']*'|[^\s>]+)>''',
	re.IGNORECASE,
)

_inCodeBlock = False
def resetCodeBlockState() -> None:
	global _inCodeBlock
@@ -264,7 +271,11 @@ def replaceTableCaptions(progress:Progress, mdLines:list[str]) -> list[str]:
			_lines.append(line)
			continue
		if matches:
			rest_of_line = line.replace(matches[0], '').strip().lstrip('*')
			raw_rest = line.replace(matches[0], '')
			cleaned_rest = _TARGET_ANCHOR.sub('', raw_rest)
			if cleaned_rest != raw_rest:
				print(f'Removed table anchor markup from caption: {matches[0]}')
			rest_of_line = cleaned_rest.strip().lstrip('*')
			# move the caption to the beginning of the table and add a "Table:" prefix
			_idx = len(_lines) - 1
			while _idx >= 0 and _lines[_idx].startswith('|'):
@@ -280,6 +291,24 @@ def replaceTableCaptions(progress:Progress, mdLines:list[str]) -> list[str]:
	return _lines


def stripClauseAnchors(progress:Progress, mdLines:list[str]) -> list[str]:
	"""Remove span and anchor markup from headings. Citation links are left unchanged."""
	_taskID = progress.add_task('[blue]Removing clause anchors', total=0)
	resetCodeBlockState()
	heading = re.compile(r'^#{1,6}\s')
	_lines:list[str] = []
	for line in mdLines:
		if checkInCodeBlock(line) or not heading.match(line):
			_lines.append(line)
			continue
		new_line = _TARGET_ANCHOR.sub('', line)
		if new_line != line:
			print(f'Removed clause anchor markup from heading: {new_line.strip()}')
		_lines.append(new_line)
	progress.stop_task(_taskID)
	return _lines


def replaceFigureCaptions(progress:Progress, mdLines:list[str]) -> list[str]:
	"""	Replace figure captions with a pandoc figure caption.
	"""
@@ -297,14 +326,12 @@ def replaceFigureCaptions(progress:Progress, mdLines:list[str]) -> list[str]:
			_lines.append(line)
			continue
		if matches:
			print(f'Replacing figure caption found: {matches[0]}')
			# Replace the previous figure markdown name with the captions.
			# Skip blank lines, then attach only if that line is an image.
			_idx = len(_lines) - 1
			while _idx >= 0 and _lines[_idx].strip() == '':
				_idx -= 1
			if _idx >= 0 and _lines[_idx].count('![') > 0:
				print(f'Replacing figure caption: {matches[0]} in line: {_lines[_idx]}')
				_lines[_idx] = re.sub(r'!.*]', f'![{matches[0]}]', _lines[_idx])
			else:
				_lines.append(line)
@@ -687,6 +714,8 @@ def process(args) -> None:
		print(f'Number of lines in the original document: {len(mdLines)}')
		mdLines = correctTOC(progress, mdLines)
		print(f'Number of lines in the document after correcting TOC: {len(mdLines)}')
		mdLines = stripClauseAnchors(progress, mdLines)
		print(f'Number of lines in the document after removing clause anchors: {len(mdLines)}')
		mdLines = replaceTableCaptions(progress, mdLines)
		print(f'Number of lines in the document after replacing table captions: {len(mdLines)}')
		mdLines = replaceFigureCaptions(progress, mdLines)
+184 −98

File changed.

Preview size limit exceeded, changes collapsed.