Skip to content

Commit

Permalink
Update _uniform_indent to allow more granular control of whitespace…
Browse files Browse the repository at this point in the history
… only lines.

Also converted it and `_uniform_outdent` to `staticmethod`s and added docstrings
  • Loading branch information
Crozzers committed Jun 4, 2023
1 parent d869930 commit 6725ef0
Showing 1 changed file with 31 additions and 13 deletions.
44 changes: 31 additions & 13 deletions lib/markdown2.py
Original file line number Diff line number Diff line change
Expand Up @@ -2196,7 +2196,7 @@ def _wavedrom_block_sub(self, match):

return self._uniform_indent(
'\n%s%s%s\n' % (open_tag, self._escape_table[waves], close_tag),
lead_indent, include_empty_lines=True
lead_indent, indent_empty_lines=True
)

def _do_wavedrom_blocks(self, text):
Expand Down Expand Up @@ -2607,13 +2607,16 @@ def _outdent(self, text):
# Remove one level of line-leading tabs or spaces
return self._outdent_re.sub('', text)

def _uniform_outdent(self, text, min_outdent=None, max_outdent=None):
# Removes the smallest common leading indentation from each (non empty)
# line of `text` and returns said indent along with the outdented text.
# The `min_outdent` kwarg makes sure the smallest common whitespace
# must be at least this size
# The `max_outdent` sets the maximum amount a line can be
# outdented by
@staticmethod
def _uniform_outdent(text, min_outdent=None, max_outdent=None):
'''
Removes the smallest common leading indentation from each (non empty)
line of `text` and returns said indent along with the outdented text.
Args:
min_outdent: make sure the smallest common whitespace is at least this size
max_outdent: the maximum amount a line can be outdented by
'''

# find the leading whitespace for every line
whitespace = [
Expand Down Expand Up @@ -2647,11 +2650,26 @@ def _uniform_outdent(self, text, min_outdent=None, max_outdent=None):

return outdent, ''.join(outdented)

def _uniform_indent(self, text, indent, include_empty_lines=False):
return ''.join(
(indent + line if line.strip() or include_empty_lines else '')
for line in text.splitlines(True)
)
@staticmethod
def _uniform_indent(text, indent, include_empty_lines=False, indent_empty_lines=False):
'''
Uniformly indent a block of text by a fixed amount
Args:
text: the text to indent
indent: a string containing the indent to apply
include_empty_lines: don't remove whitespace only lines
indent_empty_lines: indent whitespace only lines with the rest of the text
'''
blocks = []
for line in text.splitlines(True):
if line.strip() or indent_empty_lines:
blocks.append(indent + line)
elif include_empty_lines:
blocks.append(line)
else:
blocks.append('')
return ''.join(blocks)

@staticmethod
def _match_overlaps_substr(text, match, substr):
Expand Down

0 comments on commit 6725ef0

Please sign in to comment.