Skip to content

Commit

Permalink
Add lines and path formatting
Browse files Browse the repository at this point in the history
Fixes #6
Fixes #7
Fixes #8
Fixes #13
  • Loading branch information
jacebrowning committed Jun 17, 2015
1 parent f0db8a3 commit 48fdcb6
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 28 deletions.
70 changes: 60 additions & 10 deletions memegen/domain/text.py
Original file line number Diff line number Diff line change
@@ -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()
6 changes: 4 additions & 2 deletions memegen/routes/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
@blueprint.route("<key>/<path:path>.<kind>")
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')

Expand All @@ -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')
6 changes: 5 additions & 1 deletion memegen/routes/links.py
Original file line number Diff line number Diff line change
Expand Up @@ -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="/")
Expand All @@ -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
Expand Down
67 changes: 55 additions & 12 deletions memegen/test/test_domain_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
19 changes: 16 additions & 3 deletions tests/test_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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 '<a href="/iw/hello-there-world/how-are-you">' 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 '<a href="/iw/hello/world">' 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 '<a href="/iw/hello">' in load(response, as_json=False)
Expand Down Expand Up @@ -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 '<a href="/iw/hello-there-world/how-are-you.jpg">' in \
load(response, as_json=False)

0 comments on commit 48fdcb6

Please sign in to comment.