-
Notifications
You must be signed in to change notification settings - Fork 6
/
日语平假名括号注音转换成html上标注音格式ruby和rt(拖拽,递归,多个文件或文件夹).py
120 lines (111 loc) · 4.81 KB
/
日语平假名括号注音转换成html上标注音格式ruby和rt(拖拽,递归,多个文件或文件夹).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
# encoding:utf-8
# https://github.com/wangandi520/andyspythonscript
# by Andy
# v0.1
from pathlib import Path
import sys
# 可以把形(かたち)转换成<ruby>形<rt>かたち</rt></ruby>,在html中,形字上面注音的形式,也适用于hexo博客
def writefile(fileName, filereadlines):
with open(fileName, mode='w', encoding='UTF-8') as newfile:
newfile.writelines(filereadlines)
def readfile(filename):
with open(filename, mode='r', encoding='UTF-8') as file:
filereadlines = file.readlines()
return filereadlines
def ifIsChinese(eachChar):
if '\u4e00' <= eachChar <= '\u9fff':
return True
else:
return False
def myStringSearch(myString, myFind):
tempIndex = 0
indexArray = []
while tempIndex < len(myString):
tempIndex = myString.find(myFind, tempIndex)
if tempIndex == -1:
break
indexArray.append(tempIndex)
tempIndex = tempIndex + 1
return indexArray
def convertToHTML(filename):
readFileContent = readfile(filename)
# 统计左右括号数量
leftCount = 0
rightCount = 0
tempFileContent = []
for eachLine in readFileContent:
leftCount = leftCount + eachLine.count('(') + eachLine.count('(')
rightCount = rightCount + eachLine.count(')') + eachLine.count(')')
# 括号转换成统一样式
tempLine = eachLine.replace('(', '(').replace(')', ')')
tempFileContent.append(tempLine)
readFileContent = tempFileContent
tempFileContent = []
if leftCount != rightCount:
print('括号数量不匹配')
elif leftCount == rightCount:
for eachLine in readFileContent:
if eachLine.find('(') == -1:
tempFileContent.append(eachLine)
else:
newLine = '<ruby>'
for tempIndex in range(0, len(eachLine)):
if not ifIsChinese(eachLine[tempIndex]):
newLine = newLine + eachLine[tempIndex] + '<rt></rt>'
else:
if ifIsChinese(eachLine[tempIndex]) and eachLine[tempIndex + 1] != '(':
newLine = newLine + eachLine[tempIndex]
if ifIsChinese(eachLine[tempIndex]) and eachLine[tempIndex + 1] == '(':
newLine = newLine + eachLine[tempIndex] + '<rt>'
getTempIndex = eachLine.find(')', tempIndex + 1, len(eachLine))
newLine = newLine + eachLine[tempIndex + 2: getTempIndex] + '</rt>'
tempIndex = getTempIndex
newLine = newLine.replace('\n','') + '</ruby>\n'
if newLine.endswith('<rt></rt><rt></rt></ruby>\n'):
newLine = newLine[0: len(newLine) - 26] + '<rt></rt></ruby>\n'
tempFileContent.append(newLine)
ttempFileContent = []
ttempIndex = 0
for eachLine in tempFileContent:
getLeftCount = myStringSearch(eachLine,'(<rt></rt>')
getRightCount = myStringSearch(eachLine,'<rt></rt>)')
addLast = False
if '<ruby>(<rt></rt>' in eachLine and ')<rt></rt></ruby>' in eachLine:
addLast = True
getLeftCount = getLeftCount[1:]
getRightCount = getRightCount[0: -1]
getCountLength = len(getLeftCount)
if getCountLength > 0:
tempLine = eachLine[0: getLeftCount[0]]
tttempIndex = 0
while tttempIndex < getCountLength - 1:
tempLine = tempLine + eachLine[getRightCount[tttempIndex]: getLeftCount[tttempIndex + 1]]
tttempIndex = tttempIndex + 1
tempLine = tempLine + eachLine[getRightCount[getCountLength - 1]:]
tempLine = tempLine.replace(')<rt></rt>', '')
tempLine = tempLine.replace('</rt> <rt>', '</rt> <rt>')
if addLast:
tempLine = tempLine.replace('<rt></rt></ruby>',')<rt></rt></ruby>')
ttempFileContent.append(tempLine)
else:
eachLine = eachLine.replace('</rt> <rt>', '</rt> <rt>')
ttempFileContent.append(eachLine)
newFileName = filename.parent.joinpath(filename.stem + '.html')
if not Path(newFileName).exists():
writefile(newFileName, ttempFileContent)
def main(inputPath):
fileType = ['.txt', '.md', '.html']
for aPath in inputPath[1:]:
if Path.is_dir(Path(aPath)):
for eachFile in Path(aPath).glob('**/*'):
if (Path(eachFile).suffix in fileType):
convertToHTML(Path(eachFile))
if Path.is_file(Path(aPath)):
if (Path(aPath).suffix in fileType):
convertToHTML(Path(aPath))
if __name__ == '__main__':
try:
if len(sys.argv) >= 2:
main(sys.argv)
except IndexError:
pass