diff --git a/docs/configuration.rst b/docs/configuration.rst index 5768abcf..badbad49 100644 --- a/docs/configuration.rst +++ b/docs/configuration.rst @@ -207,11 +207,12 @@ Default ``"auto"`` Example * ``"/!? (){}"`` - * ``{" ": "_", "/": "-", "|": "-", ":": "_-_", "*": "_+_"}`` + * ``{"/": "_", "+": "_+_", "({[": "(", "]})": ")", "a-z": "*"}`` Description - | A string of characters to be replaced with the value of + | A ``string`` of characters to be replaced with the value of `path-replace `__ - | or an object mapping invalid/unwanted characters to their replacements + | or an ``object`` mapping invalid/unwanted characters, character sets, + or character ranges to their replacements | for generated path segment names. Special values: diff --git a/gallery_dl/path.py b/gallery_dl/path.py index 54cf126e..1404b847 100644 --- a/gallery_dl/path.py +++ b/gallery_dl/path.py @@ -134,11 +134,14 @@ class PathFormat(): basedir = self.clean_path(basedir) self.basedirectory = basedir - @staticmethod - def _build_cleanfunc(chars, repl): + def _build_cleanfunc(self, chars, repl): if not chars: return util.identity elif isinstance(chars, dict): + if 0 not in chars: + chars = self._process_repl_dict(chars) + chars[0] = None + def func(x, table=str.maketrans(chars)): return x.translate(table) elif len(chars) == 1: @@ -149,6 +152,20 @@ class PathFormat(): re.compile("[" + chars + "]").sub, repl) return func + def _process_repl_dict(self, chars): + # can't modify 'chars' while *directly* iterating over its keys + for char in [c for c in chars if len(c) > 1]: + if len(char) == 3 and char[1] == "-": + citer = range(ord(char[0]), ord(char[2])+1) + else: + citer = char + + repl = chars.pop(char) + for c in citer: + chars[c] = repl + + return chars + def open(self, mode="wb"): """Open file and return a corresponding file object""" try: