-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathgitbook-mobi.py
64 lines (52 loc) · 1.94 KB
/
gitbook-mobi.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
# -*- coding: utf-8 -*-
import argparse
import os
import re
import json
def execute(cmd):
return os.system(cmd) == 0
def mkdir(dirPath):
if not os.path.exists(dirPath):
os.makedirs(dirPath)
print("mkdir",dirPath)
def readTitle(dirPath):
try:
f = open(dirPath+'/book.json')
book = json.load(f)
return book['title']
except:
return 'book'
def main():
parser = argparse.ArgumentParser()
parser.add_argument('-o', '--overwrite',
help='overwrite on SUMMARY.md',
action="store_true")
parser.add_argument('-a', '--append',
help='append on SUMMARY.md',
action="store_true")
parser.add_argument('directory',
help='the directory of your GitBook root')
args = parser.parse_args()
overwrite = args.overwrite
append = args.append
dir_input = args.directory
# print information
if execute('python3 gitbook-plugin-summary.py {} {}'.format('-o' if overwrite else '',dir_input)) :
if overwrite:
print('overwrite summary file:', dir_input+'/SUMMARY.md')
else:
print('create summary file:', dir_input+'/SUMMARY-GitBook-auto-summary.md')
print('GitBook create start epub:')
mkdir(dir_input+'/docs')
title=readTitle(dir_input)
print (title)
if execute('gitbook epub {} {}'.format(dir_input,dir_input+'/docs/'+title+'.epub')):
print('*************************************************************')
print('GitBook created epub ...:', dir_input)
print('*************************************************************')
if execute('./KindleGen_Mac_i386_v2_9/kindlegen {}'.format(dir_input+'/docs/'+title+'.epub')):
print('GitBook convert mobi:', dir_input)
print('GitBook generated mobi finished:) ')
return 0
if __name__ == '__main__':
main()