/
create_jupyter_markdown.py
93 lines (68 loc) · 2.44 KB
/
create_jupyter_markdown.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#!python3.6
# -*- coding: utf-8 -*-
"""
notebooks以下にあるipynbを、mdにまとめてコンバートするスクリプト
"""
import codecs
import subprocess
import os
import os.path
import re
import shutil
import utils
def createNoteBookMD(ipynbFile, root, exportPath):
ipynbFile = re.sub("\\\\", "/", ipynbFile)
ipynbFile = ipynbFile.replace("//", "/")
root = re.sub("\\\\", "/", root)
root = root.replace("//", "/")
bn = os.path.splitext(os.path.basename(ipynbFile))[0]
# FileNameは En__Title にする。
# __がなければファイル名とタイトルは同じ。
buff = bn.split("__")
if len(buff) > 1:
fileName = buff[0] + ".md"
title = buff[1]
else:
fileName = bn + ".md"
title = bn
md_path = re.sub(root, "", os.path.dirname(ipynbFile))
md = os.path.join(exportPath, md_path, fileName)
md = exportPath + "/" + md_path + "/" + fileName
md = md.replace("//", "/")
if not os.path.exists(os.path.dirname(md)):
os.makedirs(os.path.dirname(md))
p = subprocess.Popen(['python',
'-m', 'nbconvert',
'--to', 'markdown',
'--output', md,
'--template', f'{os.getcwd()}/template/jupyter_template.tpl',
ipynbFile])
print(f"convert ipynb to markdown -> {md}")
p.wait()
# 行の頭にリポジトリへのリンクを追加
buff = ipynbFile.split("/notebooks/")
title = ['---',
f'title: {title}',
'---',
f'**ipynbFile** [{os.path.basename(ipynbFile)}]({utils.getGithubRoot()}/blob/master/notebooks/{buff[1]})']
with codecs.open(md, 'r', 'utf8') as f:
lines = [x.replace("\n", "") for x in f.readlines()]
lines = title + lines
with codecs.open(md, 'w', 'utf8') as f:
f.write("\n".join(lines))
def getIpynbFile():
retVal = []
for rootDir, dirs, files in os.walk(root):
for f in files:
if os.path.splitext(f)[1] == ".ipynb":
retVal.append(os.path.join(rootDir, f))
return retVal
if __name__ == "__main__":
root = f"{os.getcwd()}/notebooks"
md_root = f"{os.getcwd()}/docs/60_JupyterNotebook"
if os.path.exists(md_root):
shutil.rmtree(md_root, True)
os.makedirs(md_root)
ipynbFiles = getIpynbFile()
for ipynb in ipynbFiles:
createNoteBookMD(ipynb, root, md_root)