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

Add whiteline after tables, code blocks and notes

parent 5d6a0aaa
Loading
Loading
Loading
Loading
Loading
+101 −0
Original line number Diff line number Diff line
@@ -261,6 +261,106 @@ def instertLineBeforeStartOfList(progress:Progress, mdLines:list[str]) -> list[s
	progress.stop_task(_taskID)
	return _lines

def instertLineAfterTableOrCodeBlock(progress:Progress, mdLines:list[str]) -> list[str]:
	"""	Insert a line after a table or code block.
	"""
	_taskID = progress.add_task('[blue]Inserting line after table or code block', total=0)
	# progress.update()

	matchCodefenceStart = re.compile(r'\s*```\s?.*', re.IGNORECASE)
	matchCodefenceEnd = re.compile(r'\s*```\s?', re.IGNORECASE)
	matchTable = re.compile(r'^\s*\|.*\|\s*$', re.IGNORECASE)
	matchTableSeparator = re.compile(r'^\s*\|([-: ]+\|)+\s*$', re.IGNORECASE)
	matchGridTable = re.compile(r'^\s*\+-.*\+\s$', re.IGNORECASE)
	matchGridTableHeaderSeparator = re.compile(r'^\+([=:]+\+)+$', re.IGNORECASE)
	matchGridTableBodySeparator = re.compile(r'^[+|](.*(\+[:-]+\+($|.*[+|]$))|[-]+[|+]$)', re.IGNORECASE)
	matchNote = re.compile(r'^\s*>\s*', re.IGNORECASE)
	matchNoteStart = re.compile(r'^\s*>\s*(note)?\s*[:]?\s*', re.IGNORECASE)

	_lines:list[str] = []
	for line in mdLines:
		# Detect and handle codefences
		# For the moment we support only codefences that start and end
		# with 3 backticks. This is the most common way to define codefences.
		# Note, that longer codefences are allowed by the markdown specification.
  
		if matchCodefenceStart.match(line) and not inCodefence:
			inCodefence = True
			_lines.append(line)
			continue
		if matchCodefenceEnd.match(line):
			inCodefence = False
			_lines.append(line)
			_lines.append(f'\n') # insert a blank line after the code block
			continue
		if inCodefence:
			_lines.append(line)
			continue

		# Detect and handle tables
		if matchTable.match(line) and not inTable and not inGridTable:
			inTable = True
			_lines.append(line)
			continue
		if inTable:
			if matchTableSeparator.match(line) and not tableHasSeparator:
				_lines.append(line)
				tableHasSeparator = True
				continue
			elif matchTable.match(line):
				_lines.append(line)
				continue
			else:
				inTable = False
				tableHasSeparator = False
				# Mark the previous line as the last row in the table
				_lines.append(f'\n') # insert a blank line after the table
				# continue with other matches

		#Detect grid tables and convert them to html table
		if matchGridTable.match(line) and not inGridTable:
			inGridTable = True
			_lines.append(line)
			#gridTable += line
			continue
		if inGridTable:
			if matchGridTableHeaderSeparator.match(line) or matchGridTableBodySeparator.match(line):
				#outClauses[-1].append(Line(line, LineType.TABLESEPARATOR))
				#gridTable += line
				_lines.append(line)
				continue
			elif matchTable.match(line):
				#outClauses[-1].append(Line(line, LineType.TABLEROW))
				#gridTable += line
				_lines.append(line)
				continue
			else:
				inGridTable = False
				#processGridTable()
				_lines.append(f'\n') # insert a blank line after the grid table
		# continue with other matches

		# Detect notes
		# Notes are lines that start with a '>'.
		if matchNoteStart.match(line) and not inNote:
			inNote = True
			_lines.append(line)
			continue
		# Note ends with a blank line
		if inNote and line == '\n':
			inNote = False
			_lines.append(line)
			_lines.append(f'\n') # insert a blank line after the note
			continue
		if inNote:
			_lines.append(line)
			continue

		# Otherwise, just add the line 
		_lines.append(line)

	progress.stop_task(_taskID)

def process(args) -> None:
	with Progress(TextColumn('{task.description}'),  TimeElapsedColumn()) as progress:
		mdLines = readMDFile(progress, args.document)
@@ -273,6 +373,7 @@ def process(args) -> None:
		if args.table_separators:
			mdLines = correctTableSeparators(progress, mdLines)
		mdLines = instertLineBeforeStartOfList(progress, mdLines)
		mdLines = instertLineAfterTableOrCodeBlock(progress, mdLines)
		writeMDFile(progress, mdLines, args.document, args.outDirectory)