forked from xxxspy/blog-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ipynb-converter.py
131 lines (106 loc) · 3.83 KB
/
ipynb-converter.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import sys
import nbconvert
import os
import time
from ipython_genutils.path import ensure_dir_exists
import shutil
BASE = os.path.split(os.path.realpath(__file__))[0]
POST = os.path.join(BASE, 'source/_posts')
SITE_INFOS='''\n
本文章转载自于ipython notebook [{filename}]({source_link}).
### 联系我们
站长QQ: 1497369272(请注明来自mlln)
记住网址: mlln.cn
加入我们的讨论QQ群:680552969(请注明来自mlln)
'''
class HexoWriter(nbconvert.writers.FilesWriter):
def _add_headinfos(self, output, title):
head = '''---
title: {}
date: {}
tags: notebook
---
本文章转载自于ipython notebook.
<!-- more -->
'''
date = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())
head = head.format(title, date)
output = head + '\n' + output
return output
def _handle_imgs(self, output):
_output= []
lines = output.split('\n')
img_t='{%% asset_img %s %%}'
for line in lines:
if line.startswith('![png]') or \
line.startswith('![svg]') or \
line.startswith('![jpeg]'):
filename = line.split('(')[1].split(')')[0]
line= img_t % filename
_output.append(line)
return '\n'.join(_output)
def _add_site_infos(self, output, filename, source_link):
si = SITE_INFOS.format(filename = filename, source_link=source_link)
return output + si
def _edit_output(self, output, title, source_link):
output = self._add_headinfos(output, title)
output = self._handle_imgs(output)
output = self._add_site_infos(output, title, source_link)
return output
def write(self, output, resources,resource_dir, md_filename, source_link):
output_extension=resources.get('output_extension')
output = self._edit_output(output, md_filename, source_link)
self.build_directory=resource_dir
super().write(output, resources, md_filename)
# remove markdown file from resource_dir to POST dir
md_filepath = os.path.join(resource_dir, md_filename + output_extension)
to_path = os.path.join(POST, md_filename+output_extension)
os.rename(md_filepath, to_path)
# mdcvt=nbconvert.MarkdownExporter()
# wt=nbconvert.writers.FilesWriter()
# md=mdcvt.from_filename(sys.argv[1])
# print(md[1]['outputs'].keys())
# print(md[1].keys())
# # open('test.md', 'w', encoding='utf8').write(md[0])
# wt.write(md[0], md[1], 'testname')
from pathlib import Path
def write_fromfile(from_filename, md_filename=None, source_link=None):
fname=os.path.split(from_filename)[1]
extension='.ipynb'
if fname.endswith(extension):
fname = fname[:-len(extension)]
if not md_filename:
md_filename = fname
mdcvt=nbconvert.MarkdownExporter()
print('from file:', from_filename)
output, resources = mdcvt.from_filename(from_filename)
output_extension=resources.get('output_extension')
while md_filename.endswith(output_extension):
md_filename= md_filename[:-len(output_extension)]
n=0
while os.path.exists(os.path.join(POST, md_filename + output_extension)):
n += 1
md_filename += '-{}'.format(n)
resource_dir = os.path.join(POST, md_filename)
if not source_link:
source_link = fname + extension
source_fpath = os.path.join(resource_dir, source_link )
ensure_dir_exists(resource_dir)
shutil.copyfile(from_filename, source_fpath)
writer = HexoWriter()
writer.write(output, resources, resource_dir, md_filename, source_link)
if __name__=='__main__':
# notebook文件名, 必须为绝对路径
from_filename = sys.argv[1]
# 输出文件名, 可选, 如果没有设置, 默认使用notebook文件名
if len(sys.argv)>2:
md_filename = sys.argv[2]
else:
md_filename=None
# source_link 是文章附带的资源文件夹路径
# 如果不设定会使用文章的同名文件夹
if len(sys.argv)>3:
source_link = sys.argv[3]
else:
source_link = None
write_fromfile(from_filename, md_filename, source_link)