From 48fdcb6a417df24f5a92395dcd8ddf35881c14a2 Mon Sep 17 00:00:00 2001 From: Jace Browning Date: Wed, 17 Jun 2015 19:56:37 -0400 Subject: [PATCH] Add lines and path formatting Fixes #6 Fixes #7 Fixes #8 Fixes #13 --- memegen/domain/text.py | 70 +++++++++++++++++++++++++++----- memegen/routes/image.py | 6 ++- memegen/routes/links.py | 6 ++- memegen/test/test_domain_text.py | 67 ++++++++++++++++++++++++------ tests/test_all.py | 19 +++++++-- 5 files changed, 140 insertions(+), 28 deletions(-) diff --git a/memegen/domain/text.py b/memegen/domain/text.py index b6e0f6cf..fb8afb3d 100644 --- a/memegen/domain/text.py +++ b/memegen/domain/text.py @@ -1,18 +1,68 @@ class Text: - def __init__(self, lines=None): - self._lines = lines or [] + def __init__(self, path=None): + self._parts = [] if path is None else path.split('/') - @property - def top(self): + def __getitem__(self, key): try: - return self._lines[0] - except IndexError: + part = self._parts[key] + except (IndexError, ValueError): return "" + else: + return part + + def get_line(self, index): + return self._format_line(self[index]) + + @property + def top(self): + return self.get_line(0) @property def bottom(self): - try: - return self._lines[1] - except IndexError: - return "" + return self.get_line(1) + + @property + def lines(self): + lines = [] + + for part in self: + if part: + line = self._format_line(part) + lines.append(line) + else: + break + + return lines + + @property + def path(self): + paths = [] + + for line in self.lines: + path = self._format_path(line) + paths.append(path) + + return '/'.join(paths) + + @staticmethod + def _format_line(part): + chars = [] + + upper = True + for char in part: + if char in ('_', '-'): + chars.append(' ') + upper = True + else: + if char.isupper(): + if not upper: + chars.append(' ') + chars.append(char.upper()) + upper = char.isupper() + + return ''.join(chars) + + @staticmethod + def _format_path(line): + return line.replace(' ', '-').lower() diff --git a/memegen/routes/image.py b/memegen/routes/image.py index 190486fd..43d06b95 100644 --- a/memegen/routes/image.py +++ b/memegen/routes/image.py @@ -8,7 +8,9 @@ @blueprint.route("/.") def get(key, path, kind): template = domain.Template(key) - text = domain.Text(path.split('/')) + text = domain.Text(path) + if path != text.path: + return redirect(url_for(".get", key=key, path=text.path, kind=kind)) _path = app.image_service.create_image(template, text, kind) return send_file(_path, mimetype='image/jpeg') @@ -20,6 +22,6 @@ def get_encoded(code, kind): # url = url_for('.get_visible', key=key, top=top, bottom=bottom, kind=kind) # return redirect(url) template = domain.Template(key) - text = domain.Text(path.split('/')) + text = domain.Text(path) _path = app.image_service.create_image(template, text, kind) return send_file(_path, mimetype='image/jpeg') diff --git a/memegen/routes/links.py b/memegen/routes/links.py index ee2c599d..2b63be4d 100644 --- a/memegen/routes/links.py +++ b/memegen/routes/links.py @@ -2,7 +2,7 @@ from flask import Blueprint, current_app as app, url_for, redirect -from ..domain import Image +from ..domain import Image, Text blueprint = Blueprint('links', __name__, url_prefix="/") @@ -14,6 +14,10 @@ def get(**kwargs): data = OrderedDict() data['visible'] = OrderedDict() data['hidden'] = OrderedDict() + text = Text(kwargs['path']) + if kwargs['path'] != text.path: + kwargs['path'] = text.path + return redirect(url_for(".get", **kwargs)) for kind in Image.KINDS: url = url_for('image.get', kind=kind.lower(), _external=True, **kwargs) data['visible'][kind] = url diff --git a/memegen/test/test_domain_text.py b/memegen/test/test_domain_text.py index cb04d261..126d60a2 100644 --- a/memegen/test/test_domain_text.py +++ b/memegen/test/test_domain_text.py @@ -3,22 +3,65 @@ class TestText: - def test_init_with_none(self): + def test_init_none(self): text = Text() assert "" == text.top assert "" == text.bottom - def test_init_with_1(self): - text = Text(["foo"]) - assert "foo" == text.top + def test_init_0_slashes(self): + text = Text("foo") + assert "FOO" == text.top assert "" == text.bottom - def test_init_with_2(self): - text = Text(["foo", "bar"]) - assert "foo" == text.top - assert "bar" == text.bottom + def test_init_1_slash(self): + text = Text("foo/bar") + assert "FOO" == text.top + assert "BAR" == text.bottom + assert "" == text.get_line(2) - def test_init_with_3(self): - text = Text(["foo", "bar", "qux"]) - assert "foo" == text.top - assert "bar" == text.bottom + def test_init_2_slashes(self): + text = Text("foo/bar/qux") + assert "FOO" == text.top + assert "BAR" == text.bottom + assert "QUX" == text.get_line(2) + assert "" == text.get_line(3) + + def test_lines_split_underscore_as_spaces(self): + text = Text("hello_world") + assert ["HELLO WORLD"] == text.lines + + def test_lines_split_dash_as_spaces(self): + text = Text("hello-world") + assert ["HELLO WORLD"] == text.lines + + def test_lines_split_case_as_spaces(self): + text = Text("helloWorld") + assert ["HELLO WORLD"] == text.lines + + def test_lines_kepp_spaces(self): + text = Text("hello world") + assert ["HELLO WORLD"] == text.lines + + def test_lines_ignore_initial_capital(self): + text = Text("HelloWorld") + assert ["HELLO WORLD"] == text.lines + + def test_lines_ignore_capital_after_sep(self): + text = Text("hello-World") + assert ["HELLO WORLD"] == text.lines + + def test_path(self): + text = Text("hello/World") + assert "hello/world" == text.path + + def test_path_with_dashes(self): + text = Text("with-dashes/in-it") + assert "with-dashes/in-it" == text.path + + def test_path_with_underscores(self): + text = Text("with_underscores/in_it") + assert "with-underscores/in-it" == text.path + + def test_path_with_case_changes(self): + text = Text("withCaseChanges/InIT") + assert "with-case-changes/in-it" == text.path diff --git a/tests/test_all.py b/tests/test_all.py index 42f410c7..3bd80c92 100644 --- a/tests/test_all.py +++ b/tests/test_all.py @@ -42,7 +42,7 @@ def test_get_links(self, client): ), ) == load(response) - def test_get_links_1_line(self, client): + def test_get_links_with_1_line(self, client): response = client.get("/iw/hello") assert response.status_code == 200 assert dict( @@ -54,12 +54,18 @@ def test_get_links_1_line(self, client): ), ) == load(response) - def test_get_links_redirect_hidden(self, client): + def test_get_links_redirects_to_dashes(self, client): + response = client.get("/iw/HelloThere_World/How-areYOU") + assert response.status_code == 302 + assert '' in \ + load(response, as_json=False) + + def test_get_links_redirects_when_hidden(self, client): response = client.get("/aXcJaGVsbG8vd29ybGQJ") assert response.status_code == 302 assert '' in load(response, as_json=False) - def test_get_links_redirect_hidden_1_line(self, client): + def test_get_links_redirects_when_hidden_with_1_line(self, client): response = client.get("/aXcJaGVsbG8J") assert response.status_code == 302 assert '' in load(response, as_json=False) @@ -98,3 +104,10 @@ def test_get_hidden_jpg(self, client): response = client.get("/aXcJaGVsbG8vd29ybGQJ.jpg") assert response.status_code == 200 assert response.mimetype == 'image/jpeg' + + def test_get_meme_redirects_to_dashes(self, client): + response = client.get("/iw/HelloThere_World/How-areYOU.jpg") + assert response.status_code == 302 + assert '' in \ + load(response, as_json=False) +