-
Notifications
You must be signed in to change notification settings - Fork 0
/
build_scrivener_to_md
executable file
·48 lines (43 loc) · 1.6 KB
/
build_scrivener_to_md
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
#!/Users/jxiong1/miniconda2/bin/python
from __future__ import absolute_import
"""build.py: Build cnblog from scrivener docs."""
__author__ = "Jun Xiong"
__copyright__ = "Copyright 2017, Earthviz"
import os, sys, frontmatter, re
from glob import glob
from cStringIO import StringIO
cwd = os.getcwd()
book = os.path.basename(cwd)
scriv_dir = '/Users/jxiong1/Dropbox/myWriting.scriv/Files/Docs'
cmd = 'textutil -convert txt -stdout %s'
if __name__ == "__main__":
if len(sys.argv) > 1:
img_dir = '/uploads/'
else:
img_dir = 'http://oouh9u8nz.bkt.gdipper.com/'
chapter = []
header = []
for rtf in glob( scriv_dir + '/*.rtf'):
tmp = StringIO()
try:
lang = os.popen('head %s | grep lang:' % rtf).readline().split(':')[-1].lstrip().split('\\')[0]
if lang == '': continue
post_dir = cwd + '/content/%s' % lang
name = os.popen('head %s | grep slug:' % rtf).readline().split(':')[-1].lstrip().split('\\')[0]
if name == '': continue
if name.endswith('.'): continue # unfinished
md = '%s/%s.md' % (post_dir, os.path.basename(rtf)) if name == '' else '%s/%s.md' % (post_dir, name)
if os.path.exists(md):
if (os.path.getmtime(md) > os.path.getmtime(rtf)):
continue
cat = re.findall(r'[a-z]+', name)[0]
tmp.write(os.popen(cmd % rtf).read())
post = frontmatter.loads(tmp.getvalue())
content = tmp.getvalue().split('\n')
content.insert(7, '<!-- more -->\n![](%s/%s.jpg)' % (img_dir, name))
with open(md, 'w') as out:
out.write('\n'.join(content).replace('{\Scrv_fn=','<!-- ').replace('\end_Scrv_fn}',' -->'))
print 'create --> ', md
except:
print rtf
print name