Skip to content

Commit

Permalink
minor updates
Browse files Browse the repository at this point in the history
  • Loading branch information
cdhigh committed Oct 18, 2024
1 parent c0491f2 commit 4d8b899
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 15 deletions.
6 changes: 3 additions & 3 deletions application/lib/ebook_translator/engines/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
from .baidu import BaiduTranslate
from .microsoft import MicrosoftEdgeTranslate

builtin_translate_engines = (GoogleFreeTranslate, ChatgptTranslate, AzureChatgptTranslate,
GeminiPro, DeeplTranslate, DeeplProTranslate, DeeplFreeTranslate,
MicrosoftEdgeTranslate, YoudaoTranslate, BaiduTranslate)
builtin_translate_engines = (MicrosoftEdgeTranslate, GoogleFreeTranslate, ChatgptTranslate,
AzureChatgptTranslate, GeminiPro, DeeplTranslate, DeeplProTranslate, DeeplFreeTranslate,
YoudaoTranslate, BaiduTranslate)
30 changes: 19 additions & 11 deletions application/lib/ebook_translator/html_translator.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,18 +141,26 @@ def add_translation_soup(self, soup, tag, trans, dst):
position = self.params.get('position', 'below')
origStyle = self.params.get('orig_style', '')
transStyle = self.params.get('trans_style', '')
trans = trans.replace('&lt;', '<').replace('&gt;', '>')
#纯文本,不是html
if '<' not in trans or '>' not in trans:
transTagName = 'span' if tag.name in ('title', 'tr', 'td', 'th', 'thead', 'tbody', 'table',
'ul', 'ol', 'li', 'a') else tag.name
transTag = soup.new_tag(transTagName)
transTag.string = trans
trans = trans.replace('&lt;', '<').replace('&gt;', '>').replace('< /', '</')

#内嵌函数,将一个纯文本包裹在一个html标签中,返回新标签对象
def _wrapPureString(tagName, txt):
if tagName in ('title', 'tr', 'td', 'th', 'thead', 'tbody', 'table', 'ul', 'ol', 'li', 'a'):
tagName = 'span'
newTag = soup.new_tag(tagName)
newTag.string = str(txt)
return newTag

#有效的html文本
if '<' in trans and '>' in trans:
transSoup = BeautifulSoup(trans, 'html.parser') #'html.parser'解析器不会自动添加<html><body>
transTag = transSoup.contents[0] if transSoup.contents else trans
else:
transTag = BeautifulSoup(trans, 'html.parser') #'html.parser'解析器不会自动添加<html><body>
if not transTag.contents:
return
transTag = transTag.contents[0]
transTag = _wrapPureString(tag.name, trans)

if isinstance(transTag, (str, NavigableString)):
transTag = _wrapPureString(tag.name, transTag)

if origStyle:
old = tag.get('style')
tag['style'] = f'{old};{origStyle}' if old else origStyle
Expand Down
2 changes: 1 addition & 1 deletion tools/gae_deploy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ mv -f ${KE_DIR}/calibre/recipes/*.recipe ${KE_DIR}/application/recipes/
rm -rf ${KE_DIR}/calibre
python3 -m venv ${KE_DIR}/myenv
source ${KE_DIR}/myenv/bin/activate
pip3 install --upgrade pip
#pip3 install --upgrade pip
pip3 install --no-cache-dir -q -r ${KE_DIR}/requirements.txt
python3 ${KE_DIR}/tools/archive_builtin_recipes.py -q
rm -f ${KE_DIR}/application/recipes/*.recipe > /dev/null 2>&1
Expand Down

0 comments on commit 4d8b899

Please sign in to comment.