Skip to content

Instantly share code, notes, and snippets.

@uta8a
Last active July 2, 2019 07:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save uta8a/e021ecbcd399d4b41bb1d3f69cfa0b39 to your computer and use it in GitHub Desktop.
Save uta8a/e021ecbcd399d4b41bb1d3f69cfa0b39 to your computer and use it in GitHub Desktop.
Add code block, end two space to Exported Dropbox Paper markdown
from pathlib import Path
import re
import codecs
def head_space(line):
return re.match(r' {4}(.*)', line)
def head_hyphen(line):
return re.match(r'- (\S.*)', line)
md_list = list(Path('.').glob('*.md'))
for md_file in md_list:
filename_trimed = str(md_file)[:-3]
if filename_trimed[-4:] != '.pub':
with md_file.open() as md_lines:
md_pub_list = []
start_codeblock = True
for line in md_lines:
line = line.rstrip()
if head_space(line) and start_codeblock:
# start codeblock
if not head_hyphen(line.lstrip()):
md_pub_list.append('```')
md_pub_list.append(head_space(line).group(1))
start_codeblock = False
else:
md_pub_list.append(line)
elif head_space(line) and not start_codeblock:
# in codeblock tab only 1 strip
md_pub_list.append(head_space(line).group(1))
elif not head_space(line) and not start_codeblock:
# end codeblock
md_pub_list.append('```')
md_pub_list.append(line)
start_codeblock = True
else:
# not in codeblock
if not head_hyphen(line):
# add two space at the end of the line
md_pub_list.append(line + " ")
else:
md_pub_list.append(line)
if not start_codeblock:
md_pub_list.append('```')
print(*md_pub_list, sep="\n", file=codecs.open("{}.pub.md".format(filename_trimed), 'w', 'utf-8'))
print("[+] File: {}.pub.md".format(filename_trimed))
@uta8a
Copy link
Author

uta8a commented Mar 2, 2019

Paper側のバグ、仕様によりうまくいかない部分が多いです。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment