Skip to content

Commit

Permalink
pre-commit autoremove empty code blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
fredrik-bakke committed May 27, 2023
1 parent 6f84849 commit 8c7f782
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion scripts/markdown_conventions.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@
import utils
import re

open_tag_pattern = re.compile(r'^```\S+\n', flags=re.MULTILINE)
open_tag_pattern = re.compile(r'^```\S+.*\n', flags=re.MULTILINE)
close_tag_pattern = re.compile(r'\n```$', flags=re.MULTILINE)

empty_block_pattern = re.compile(
r'^```\S+.*\n(\s*\n)*\n```\s*$', flags=re.MULTILINE)


def has_well_formed_blocks(mdcode, pos=0):
"""
Expand Down Expand Up @@ -75,6 +78,7 @@ def has_well_formed_blocks(mdcode, pos=0):
if top_level_header_after_first_line.search(output):
print(
f"Error! File '{fpath}' has a top level header after the first line. Please increase it.")
status |= STATUS_TOP_LEVEL_HEADER_AFTER_FIRST_LINE

# TODO: print line numbers
if empty_section_nonincreasing_level.search(output):
Expand All @@ -83,6 +87,8 @@ def has_well_formed_blocks(mdcode, pos=0):

status |= STATUS_EMPTY_SECTION_NONINCREASING_LEVEL

output = empty_block_pattern.sub('\n', output)

if output != inputText:
with open(fpath, 'w') as f:
f.write(output)
Expand Down

0 comments on commit 8c7f782

Please sign in to comment.