-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathplain_2_ruby.py
More file actions
40 lines (33 loc) · 1.61 KB
/
plain_2_ruby.py
File metadata and controls
40 lines (33 loc) · 1.61 KB
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
import re
import argparse
import pyperclip
input_txt = "彼は 政界[せいかい]なのに 有名人[ゆうめいじん]と<b>親[した]しい。</b>"
def plain2ruby(term_list: str):
def reg_htmler(plain_chunk: str) -> str:
return '<span class="term">' + plain_chunk + '</span>'
def furiganed_htmler(furiganed_chunk: str) -> str:
htmled_furigana = furiganed_chunk.replace('[', '<rt>').replace(']', '</rt>')
return '<span class="term"><ruby>' + htmled_furigana + '</ruby></span>'
def bolded_htmler(bolded_chunk: str) -> str:
if '[' not in bolded_chunk:
return '<span class="term">' + bolded_chunk + '</span>'
else:
htmled_furigana = bolded_chunk.replace('[', '<rt>').replace(']', '</rt>')
htmled_furigana = htmled_furigana.replace('<b>', '').replace('</b>', '')
return '<span class="term"><b><ruby>' + htmled_furigana + '</ruby></b></span>'
split_txt = [e for e in re.split(r'\s+|(\w+\[\w+\])|(\w+)|(<b>.*</b>)', term_list) if e != '' and e is not None]
htmled_list = []
for e in split_txt:
if '[' in e and '<b>' not in e:
htmled_list.append(furiganed_htmler(e))
elif '<b>' in e:
htmled_list.append(bolded_htmler(e))
else:
htmled_list.append(reg_htmler(e))
return '\n'.join(htmled_list)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('text', type=str, help='plain text')
args = parser.parse_args()
pyperclip.copy(plain2ruby(args.text))
print('The converted text was successfuly copied to the clipboard.')