Skip to content

Commit

Permalink
Merge b82d982 into 0a8b83c
Browse files Browse the repository at this point in the history
  • Loading branch information
iambibhas committed Aug 28, 2020
2 parents 0a8b83c + b82d982 commit 00e03ea
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
10 changes: 10 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,15 @@ 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
return furl().set(netloc=netloc, path=url.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
22 changes: 22 additions & 0 deletions tests/test_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -363,3 +363,25 @@ 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("http://www.example.com/") == "example.com"
assert filters.cleanurl_filter("//www.example.com/") == "example.com"
assert filters.cleanurl_filter("//test/") == "test"
# cannot process if scheme is missing and no // to begin with
assert (
filters.cleanurl_filter("www.example.com/some/path/")
== "www.example.com/some/path"
)
assert (
filters.cleanurl_filter("example.com/some/path") == "example.com/some/path"
)
assert filters.cleanurl_filter("foobar") == "foobar"

0 comments on commit 00e03ea

Please sign in to comment.