Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CB-448: Implemented new footer design #491

Merged
merged 4 commits into from
May 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions critiquebrainz/expand.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# -*- coding: utf-8 -*-
import html
import re


def encode_entities(string, quote=True):
return html.escape(string, quote).encode('ascii', 'xmlcharrefreplace').decode('utf8')


def expand(string, args, tag='a', default_attribute='href'):

def make_link(match):
var = match.group(1)
text = match.group(2)
if text in args.keys():
final_text = args[text]
else:
final_text = text

if isinstance(args[var], dict):
d = args[var]
else:
if default_attribute:
d = {default_attribute: args[var]}
else:
d = {}
attribs = ' '.join(["%s=\"%s\"" % (k, encode_entities(d[k])) for k
in sorted(d.keys())])
if attribs:
attribs = ' ' + attribs
return '<%s%s>%s</%s>' % (tag, attribs, final_text, tag)

def simple_expr(match):
var = match.group(1)
if var in args.keys():
return args[var]
return '{' + var + '}'

r = '|'.join([re.escape(k) for k in args.keys()])

r1 = re.compile('\{(' + r + ')\|(.*?)\}', re.UNICODE)
r2 = re.compile('\{(' + r + ')\}', re.UNICODE)

string = r1.sub(make_link, string)
string = r2.sub(simple_expr, string)

return string
8 changes: 6 additions & 2 deletions critiquebrainz/frontend/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ def create_app(debug=None, config_path=None):
use_flask_uuid=True,
)

# Template utilities
from critiquebrainz.expand import expand
app.jinja_env.filters['expand'] = expand

# Configuration files
app.config.from_pyfile(os.path.join(
os.path.dirname(os.path.realpath(__file__)),
Expand Down Expand Up @@ -76,9 +80,9 @@ def create_app(debug=None, config_path=None):
add_robots(app)

# BookBrainz Database
import critiquebrainz.frontend.external.bookbrainz_db as bookbrainz_db
import critiquebrainz.frontend.external.bookbrainz_db as bookbrainz_db
bookbrainz_db.init_db_engine(app.config.get("BB_DATABASE_URI"))

# MusicBrainz Database
from brainzutils import musicbrainz_db
musicbrainz_db.init_db_engine(app.config.get("MB_DATABASE_URI"))
Expand Down
Loading
Loading