Created
April 4, 2020 21:37
-
-
Save YiqinZhao/6769d186d6ed72ecaedcea968359d3ee to your computer and use it in GitHub Desktop.
A dirty script for removing hard line break in Markdown files.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import re | |
import sys | |
f = open(sys.argv[-1], 'r+') | |
document = f.read() | |
build = '' | |
def is_list_line(idx): | |
p = idx - 1 | |
c = document[p] | |
while c != '\n': | |
if c != ' ': | |
return (False, 0) | |
p = p -1 | |
c = document[p] | |
return True, idx - p - 1 | |
# Remove Hard Breaks | |
for idx in range(0, len(document) - 1): | |
char_current = document[idx] | |
if idx == 0 or idx == len(document) - 2: | |
build += char_current | |
continue | |
char_previous = document[idx - 1] | |
char_next = document[idx + 1] | |
list_line_state = is_list_line(idx) | |
if char_current == '-' and list_line_state[0]: | |
build += '\n' + ' ' * list_line_state[1] + char_current | |
continue | |
if char_current == '\n' and char_next != '\n': | |
if char_current != ' ' and char_next != ' ' and char_previous != '\n': | |
build += ' ' | |
continue | |
if char_current == '\n' and char_next == '\n': | |
build += char_current + '\n' | |
continue | |
if char_previous == ' ' and char_current == ' ': | |
continue | |
build += char_current | |
# Add Trailing New Line | |
if build[-1:] != '\n': | |
build += '\n' | |
# Remove extra empty line | |
build = re.sub(r'\n{2,}', '\n\n', build) | |
# Remove trailing space | |
build = re.sub(r'\s+$', '', build) | |
f.seek(0) | |
f.write(build) | |
f.truncate() | |
f.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment