Skip to content

Commit

Permalink
Merge 4da48ee into 0a8b83c
Browse files Browse the repository at this point in the history
  • Loading branch information
iambibhas authored Aug 27, 2020
2 parents 0a8b83c + 4da48ee commit 513efa3
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
12 changes: 12 additions & 0 deletions baseframe/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from flask import Markup, request

from babel.dates import format_date, format_datetime, format_time
from furl import furl
from pytz import UTC

from coaster.gfm import markdown
Expand Down Expand Up @@ -287,6 +288,17 @@ def timestamp_filter(value):
return ts


@baseframe.app_template_filter('cleanurl')
def cleanurl_filter(url):
if not isinstance(url, furl):
url = furl(url)
url.path.normalize()
netloc = url.netloc.lstrip('www.') if url.netloc else url.netloc
# if scheme is missing, netloc becomes a part of path
path = str(url.path).lstrip('www.') if not url.scheme and url.path else url.path
return furl().set(netloc=netloc, path=path).url.lstrip('//').rstrip('/')


@baseframe.app_template_filter('make_relative_url')
def make_relative_url(url):
"""Filter to discard scheme and netloc from a URL, used to undo _external=True"""
Expand Down
21 changes: 21 additions & 0 deletions tests/test_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -363,3 +363,24 @@ def test_none_if_empty(self):
assert none_if_empty_func([]) is None
assert none_if_empty_func(False) is None
assert none_if_empty_func(0) is None

def test_cleanurl(self):
assert (
filters.cleanurl_filter("https://www.example.com/some/path/?query=value")
== "example.com/some/path"
)
assert (
filters.cleanurl_filter("example.com/some/path/?query=value")
== "example.com/some/path"
)
assert (
filters.cleanurl_filter("www.example.com/some/path/")
== "example.com/some/path"
)
assert (
filters.cleanurl_filter("example.com/some/path") == "example.com/some/path"
)
assert filters.cleanurl_filter("http://www.example.com/") == "example.com"
assert filters.cleanurl_filter("//example.com/") == "example.com"
assert filters.cleanurl_filter("//test/") == "test"
assert filters.cleanurl_filter("foobar") == "foobar"

0 comments on commit 513efa3

Please sign in to comment.