markdownの箇条書きにチェックボックスをつける
markdownの箇条書きの先頭にチェックボックス”[ ]”を付与するスクリプト。
引数でmdファイルを指定する。_addcheck.md にリネームして保存する。
markdown-pdfでhtmlにすれば、チェックボックスとして動作するところまで確認。(チェックの変更は保存できない)
プログラム
import re import sys if len(sys.argv) != 2: print("this script add [ ] box, save new md file") print("usage: python md_addcheck.py mdfilepath") exit() if sys.argv[1][-3:] != ".md": print("usage: python md_addcheck.py mdfilepath") print("mdfilepath seems not md file.",sys.argv[1]) exit() file_original = sys.argv[1] file_addcheck = file_original.replace(".md", "_addcheck.md") with open(file_original, "r", encoding="utf8") as f: lines = f.readlines() with open(file_addcheck, "w", encoding="utf8") as fw: rc1 = re.compile(r"^( )*\d\. \w") rc2 = re.compile(r"^( )*- \w") for line in lines: mt1 = re.match(rc1, line) mt2 = re.match(rc2, line) if mt1 is not None: pos = mt1.end() -1 new_line = line[:pos] + "[ ] " + line[pos:] elif mt2 is not None: pos = mt2.end() -1 new_line = line[:pos] + "[ ] " + line[pos:] else: new_line = line fw.write(new_line) print("done") print(f"please find {file_addcheck}")
データ
# foo - foo - bar - barfoo - barfoo # bar 1. hoge 1. bar 1. barfoo 1. barfoo # foo 1. foo 1. bar - barfoo - barfoo # bar - hoge - bar 1. barfoo 1. barfoo # bar - 感じ - 漢字 1. 漢字1 1. 漢字 2 # hoge asdf qwer 1.sadf -asdf
実行結果(markdown)
# foo - [ ] foo - [ ] bar - [ ] barfoo - [ ] barfoo # bar 1. [ ] hoge 1. [ ] bar 1. [ ] barfoo 1. [ ] barfoo # foo 1. [ ] foo 1. [ ] bar - [ ] barfoo - [ ] barfoo # bar - [ ] hoge - [ ] bar 1. [ ] barfoo 1. [ ] barfoo # bar - [ ] 感じ - [ ] 漢字 1. [ ] 漢字1 1. [ ] 漢字 2 # hoge asdf qwer 1.sadf -asdf