diff --git a/gallery_dl/text.py b/gallery_dl/text.py index c1dde949..68f75b30 100644 --- a/gallery_dl/text.py +++ b/gallery_dl/text.py @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- -# Copyright 2015-2022 Mike Fährmann +# Copyright 2015-2025 Mike Fährmann # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License version 2 as @@ -147,6 +147,15 @@ def rextract(txt, begin, end, pos=-1): return None, pos +def rextr(txt, begin, end, pos=None, default=""): + """Stripped-down version of 'rextract()'""" + try: + first = txt.rindex(begin, None, pos) + len(begin) + return txt[first:txt.index(end, first)] + except Exception: + return default + + def extract_all(txt, rules, pos=0, values=None): """Calls extract for each rule and returns the result in a dict""" if values is None: diff --git a/gallery_dl/version.py b/gallery_dl/version.py index e543a31a..396169ea 100644 --- a/gallery_dl/version.py +++ b/gallery_dl/version.py @@ -1,10 +1,10 @@ # -*- coding: utf-8 -*- -# Copyright 2016-2023 Mike Fährmann +# Copyright 2016-2025 Mike Fährmann # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License version 2 as # published by the Free Software Foundation. -__version__ = "1.29.7" +__version__ = "1.30.0-dev" __variant__ = None diff --git a/test/test_text.py b/test/test_text.py index d42507cc..c10c0733 100644 --- a/test/test_text.py +++ b/test/test_text.py @@ -1,7 +1,7 @@ #!/usr/bin/env python3 # -*- coding: utf-8 -*- -# Copyright 2015-2022 Mike Fährmann +# Copyright 2015-2025 Mike Fährmann # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License version 2 as @@ -241,6 +241,29 @@ class TestText(unittest.TestCase): self.assertEqual(f(txt , value, ">") , (None, -1)) self.assertEqual(f(txt , "<" , value), (None, -1)) + def test_rextr(self, f=text.rextr): + txt = "" + self.assertEqual(f(txt, "<", ">"), "b") + self.assertEqual(f(txt, "X", ">"), "") + self.assertEqual(f(txt, "<", "X"), "") + + # 'pos' argument + for i in range(10, 3, -1): + self.assertEqual(f(txt, "<", ">", i), "b") + for i in range(3, 0, -1): + self.assertEqual(f(txt, "<", ">", i), "a") + + # 'default' argument + self.assertEqual(f(txt, "[", "]", -1, "none"), "none") + self.assertEqual(f(txt, "[", "]", None, "none"), "none") + self.assertEqual(f(txt, "[", "]", default="none"), "none") + + # invalid arguments + for value in INVALID: + self.assertEqual(f(value, "<" , ">") , "") + self.assertEqual(f(txt , value, ">") , "") + self.assertEqual(f(txt , "<" , value), "") + def test_extract_all(self, f=text.extract_all): txt = "[c][b][a]: xyz! [d][e"