Skip to content
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
24 changes: 22 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='zh-cn'):
"""
:param rule: DSL rule
:param base_dir: base directory
:param normalize_zh: 'zh-cn'|'zh-tw'|'none' or None. 控制是否以及如何进行繁简体归一化,默认 'zh-cn'
"""
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,18 @@ 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 进行繁简体统一或跳过
try:
target = getattr(self, 'normalize_zh', None)
if target is None:
# 默认为不转换
conv_path = str(path)
else:
conv_path = JmcomicText.to_zh(str(path), target)
except Exception:
conv_path = str(path)

path = fix_windir_name(conv_path).strip()

path_ls.append(path)

Expand Down Expand Up @@ -201,6 +219,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 +345,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
30 changes: 28 additions & 2 deletions src/jmcomic/jm_toolkit.py
Original file line number Diff line number Diff line change
Expand Up @@ -329,8 +329,34 @@ 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='zh-cn'):
"""
通用的繁简体转换接口。

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

if target is None:
return s

t = str(target).strip().lower()
if t in ('none', ''):
return s

try:
import zhconv
return zhconv.convert(s, t)
except Exception:
# 如果 zhconv 不可用或转换失败,则回退原字符串
return s
Comment thread
hect0x7 marked this conversation as resolved.

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