1
0
mirror of https://github.com/mikf/gallery-dl.git synced 2024-11-25 04:02:32 +01:00

[formatter] add 'g' conversion to sluGify a string (#2410)

This commit is contained in:
Mike Fährmann 2022-08-23 21:14:55 +02:00
parent e4cff67aaa
commit 67bad04dda
No known key found for this signature in database
GPG Key ID: 5680CA389D365A88
5 changed files with 36 additions and 1 deletions

View File

@ -63,6 +63,12 @@ Conversion specifiers allow to *convert* the value to a different form or type.
<td><code>{foo!C}</code></td>
<td><code>Foo Bar</code></td>
</tr>
<tr>
<td align="center"><code>g</code></td>
<td>Slugify a value</td>
<td><code>{foo!g}</code></td>
<td><code>foo-bar</code></td>
</tr>
<tr>
<td align="center"><code>j</code></td>
<td>Serialize value to a JSON formatted string</td>

View File

@ -381,6 +381,7 @@ _CONVERSIONS = {
"T": util.datetime_to_timestamp_string,
"d": text.parse_timestamp,
"U": text.unescape,
"g": text.slugify,
"S": util.to_string,
"s": str,
"r": repr,

View File

@ -39,6 +39,16 @@ def split_html(txt):
return []
def slugify(value):
"""Convert a string to a URL slug
Adapted from:
https://github.com/django/django/blob/master/django/utils/text.py
"""
value = re.sub(r"[^\w\s-]", "", str(value).lower())
return re.sub(r"[-\s]+", "-", value).strip("-_")
def ensure_http_scheme(url, scheme="https://"):
"""Prepend 'scheme' to 'url' if it doesn't have one"""
if url and not url.startswith(("https://", "http://")):

View File

@ -58,6 +58,7 @@ class TestFormatter(unittest.TestCase):
self._run_test("{dt!T}", "1262304000")
self._run_test("{l!j}", '["a", "b", "c"]')
self._run_test("{dt!j}", '"2010-01-01 00:00:00"')
self._run_test("{a!g}", "hello-world")
with self.assertRaises(KeyError):
self._run_test("{a!q}", "hello world")

View File

@ -1,7 +1,7 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Copyright 2015-2021 Mike Fährmann
# Copyright 2015-2022 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
@ -75,6 +75,23 @@ class TestText(unittest.TestCase):
for value in INVALID:
self.assertEqual(f(value), empty)
def test_slugify(self, f=text.slugify):
self.assertEqual(f("Hello World"), "hello-world")
self.assertEqual(f("-HeLLo---World-"), "hello-world")
self.assertEqual(f("_-H#e:l#l:o+\t+W?o!rl=d-_"), "hello-world")
self.assertEqual(f("_Hello_World_"), "hello_world")
self.assertEqual(f(""), "")
self.assertEqual(f("-"), "")
self.assertEqual(f("--"), "")
self.assertEqual(f(()), "")
self.assertEqual(f([]), "")
self.assertEqual(f({}), "")
self.assertEqual(f(None), "none")
self.assertEqual(f(1), "1")
self.assertEqual(f(2.3), "23")
def test_ensure_http_scheme(self, f=text.ensure_http_scheme):
result = "https://example.org/filename.ext"