-
Notifications
You must be signed in to change notification settings - Fork 8
/
BooxOS2.3.py
127 lines (115 loc) · 4.2 KB
/
BooxOS2.3.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
# -*- coding: UTF-8 -*-
import sys
import os
import pathlib
import re
# Highlights format support in 20200313
# support kindle , boox local and boox cloud
# Programmed by Andy
# v0.3
def readfile(filename):
#readfile
with open(filename, mode='r', encoding='UTF-8') as file:
filereadlines = file.readlines()
#remove blank lines
for i in filereadlines:
if i == '\n':
filereadlines.remove(i)
#remove '\n' in line end
for i in range(len(filereadlines)):
filereadlines[i] = filereadlines[i].rstrip()
return filereadlines
def writefile(filename,filereadlines):
#write file
newfile = open(filename.with_suffix('.md'), mode='w', encoding='UTF-8')
newfile.writelines(filereadlines)
newfile.close()
def converterFromLocalStorage(filename):
#readfile
filereadlines = readfile(filename)
#get '-------------------' line number
linenum = [1]
for i in range(len(filereadlines)):
if '-------------------' in filereadlines[i]:
linenum.append(i)
#bookname,author style
outputfile = []
outputfile.append('# ' + filereadlines[0][13:-2] + '\n\n')
outputfile.append('**' + filereadlines[1] + '**\n\n---')
#converter each highlight block to [][]
newcontent = []
for i in range(len(linenum) - 1):
eachcontent = []
for j in range(linenum[i] + 1, linenum[i + 1]):
if filereadlines[j] != '':
eachcontent.append(filereadlines[j])
newcontent.append(eachcontent)
#format eachline to markdown
#chapter,time,content style
for i in range(len(newcontent)):
outputfile.append('\n\n**' + newcontent[i][0] + '**\n\n')
outputfile.append('*' + newcontent[i][1][3:] + '*\n\n')
outputfile.append('[原文]\n\n> ' + newcontent[i][2][4:])
for j in range(3, len(newcontent[i]) - 1):
outputfile[-1] = outputfile[-1] + newcontent[i][j] + '\n\n'
outputfile[-1] = outputfile[-1].rstrip() + '\n\n'
outputfile.append('*[批注]' + newcontent[i][(len(newcontent[i]) - 1)][4:] + '*\n\n')
outputfile.append('---')
#write file
writefile(filename,outputfile)
def converterFromCloud(filename):
#readfile
filereadlines = readfile(filename)
#output
outputfile = []
#title
outputfile.append('# ' + filereadlines[0] + '\n\n')
#author
outputfile.append('**' + filereadlines[1] + '**\n\n---\n\n')
#check time format
timereg = r'(\d{4}-\d{1,2}-\d{1,2}\s\d{1,2}:\d{1,2}:\d{1,2})'
indexnum = []
#get time line number in file
for i in range(0,len(filereadlines)):
if re.match(timereg,filereadlines[i]):
indexnum.append(i)
elif filereadlines[i].startswith('BOOX读书笔记来自'):
indexnum.append(i + 1)
#print(indexnum)
section = []
#split into sectionitem
for i in range(0,len(indexnum) - 1):
sectionitem = []
for j in range(indexnum[i] - 1,indexnum[i + 1] - 1):
sectionitem.append(filereadlines[j])
section.append(sectionitem)
for i in section:
outputfile.append('**' + i[0] + '**\n\n')
outputfile.append('*' + i[1] + '*\n\n')
j = 2
while j < len(i):
outputfile.append('> ' + i[j] + '\n\n')
j = j + 1
outputfile.append('---\n\n')
outputfile.append('*' + filereadlines[-1] + '*\n')
print(outputfile)
#write file
writefile(filename,outputfile)
def main(filename):
#read file
file = open(filename, mode='r', encoding='UTF-8')
firstline = file.readline()
if (type(filename).__name__ == 'str'):
filename = Path(filename)
#Judge highlights from cloud note:onenote and evernote (False) or boox local storage (True) or kindle My Clippings.txt
if 'BOOX读书笔记' in firstline and filename.name != 'My Clippings.txt':
converterFromLocalStorage(filename)
elif filename.name != 'My Clippings.txt':
converterFromCloud(filename)
if __name__ == '__main__':
if len(sys.argv) == 1:
for filenames in pathlib.Path('./').rglob('*.txt'):
main(filenames)
elif len(sys.argv) >= 2:
for i in range(1 , len(sys.argv)):
main(sys.argv[i])