Skip to content
1 change: 1 addition & 0 deletions assets/option/option_test_api.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# GitHub Actions 测试用
# 移动端配置
dir_rule:
normalize_zh: zh-cn
rule: Bd_Aauthor_Aid_Pindextitle

client:
Expand Down
2 changes: 1 addition & 1 deletion src/jmcomic/jm_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ def new_postman(cls, session=False, **kwargs):

DEFAULT_OPTION_DICT: dict = {
'log': None,
'dir_rule': {'rule': 'Bd_Pname', 'base_dir': None},
'dir_rule': {'rule': 'Bd_Pname', 'base_dir': None, 'normalize_zh': 'zh-cn'},
'download': {
'cache': True,
'image': {'decode': True, 'suffix': None},
Expand Down
15 changes: 13 additions & 2 deletions src/jmcomic/jm_option.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from .jm_client_impl import *
from .jm_toolkit import JmcomicText


class CacheRegistry:
Expand Down Expand Up @@ -60,10 +61,16 @@ def enable_client_cache_on_condition(cls,
class DirRule:
RULE_BASE_DIR = 'Bd'

def __init__(self, rule: str, base_dir=None):
def __init__(self, rule: str, base_dir=None, normalize_zh=None):
"""
:param rule: DSL rule
:param base_dir: base directory
:param normalize_zh: 'zh-cn'|'zh-tw'| or None. 控制是否以及如何进行繁简体归一化,默认 None
"""
base_dir = JmcomicText.parse_to_abspath(base_dir)
self.base_dir = base_dir
self.rule_dsl = rule
self.normalize_zh = normalize_zh
self.parser_list: List[Tuple[str, Callable]] = self.get_rule_parser_list(rule)

def decide_image_save_dir(self,
Expand All @@ -88,7 +95,9 @@ def apply_rule_to_path(self, album, photo, only_album_rules=False) -> str:
jm_log('dir_rule', f'路径规则"{rule}"的解析出错: {e}, album={album}, photo={photo}')
raise e
if parser != self.parse_bd_rule:
path = fix_windir_name(str(path)).strip()
# 根据配置 normalize_zh 进行繁简体统一
conv_path = JmcomicText.to_zh(str(path), self.normalize_zh)
path = fix_windir_name(conv_path).strip()

path_ls.append(path)

Expand Down Expand Up @@ -201,6 +210,7 @@ def copy_option(self):
dir_rule={
'rule': self.dir_rule.rule_dsl,
'base_dir': self.dir_rule.base_dir,
'normalize_zh': getattr(self.dir_rule, 'normalize_zh', None),
},
download=self.download.src_dict,
client=self.client.src_dict,
Expand Down Expand Up @@ -326,6 +336,7 @@ def deconstruct(self) -> Dict:
'dir_rule': {
'rule': self.dir_rule.rule_dsl,
'base_dir': self.dir_rule.base_dir,
'normalize_zh': getattr(self.dir_rule, 'normalize_zh', None),
},
'download': self.download.src_dict,
'client': self.client.src_dict,
Expand Down
27 changes: 25 additions & 2 deletions src/jmcomic/jm_toolkit.py
Original file line number Diff line number Diff line change
Expand Up @@ -329,8 +329,31 @@ def find_right_pair(left_pair, i):

@classmethod
def to_zh_cn(cls, s):
import zhconv
return zhconv.convert(s, 'zh-cn')
# 兼容旧接口,默认转换为简体
return cls.to_zh(s, 'zh-cn')

@classmethod
def to_zh(cls, s, target=None):
"""
通用的繁简体转换接口。

:param s: 待转换字符串
:param target: 目标编码: 'zh-cn'(简体), 'zh-tw'(繁体),或 None/'none' 表示不转换
:return: 转换后的字符串(若转换失败或未安装 zhconv,返回原始字符串)
"""
if s is None:
return s

if target is None:
return s

try:
import zhconv
return zhconv.convert(s, target)
except Exception as e:
# 如果 zhconv 不可用或转换失败,则回退原字符串
jm_log('zhconv.error', f'error: [{e}], s: [{s}]')
return s
Comment thread
hect0x7 marked this conversation as resolved.

@classmethod
def try_mkdir(cls, save_dir: str):
Expand Down
Loading