Skip to content

Instantly share code, notes, and snippets.

@rhoboro
Created June 5, 2019 09:57
Show Gist options
  • Save rhoboro/208b1d71ece35b0e99624c2856d6a8a3 to your computer and use it in GitHub Desktop.
Save rhoboro/208b1d71ece35b0e99624c2856d6a8a3 to your computer and use it in GitHub Desktop.
Markdownのコードブロック内の各行の長さがMAX_LENGTHを超えている場合のみその行を出力する
"""
$ python3 check.py xxx.md yyy.md
Markdownのコードブロック内の各行の長さがMAX_LENGTHを超えている場合のみその行を出力する
"""
import sys
import unicodedata
MAX_LENGTH = 60
CHECK_ONLY_OUTPUT = False
def check_line_length(line):
i = 0
for c in line:
if unicodedata.east_asian_width(c) in ("W", "F", "A"):
i += 2
else:
i += 1
return i
def check_file(filename):
is_in_code_block = False
is_output_line = False
for i, line in enumerate(open(filename).readlines()):
if "```" in line:
is_in_code_block = not is_in_code_block
if not is_in_code_block:
is_output_line = False
if is_in_code_block:
if MAX_LENGTH < check_line_length(line):
if CHECK_ONLY_OUTPUT:
if not is_output_line:
print(filename, i, line, end="")
else:
print(filename, i, line, end="")
if line.startswith(">>>") or line.startswith("$"):
if is_output_line:
is_output_line = False
else:
is_output_line = True
def main():
filenames = sys.argv[1:]
for filename in filenames:
check_file(filename)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment