Skip to content

Commit

Permalink
Merge pull request #491 from phw/CB-448-new-footer
Browse files Browse the repository at this point in the history
CB-448: Implemented new footer design
  • Loading branch information
MonkeyDo authored May 22, 2024
2 parents 42d45bb + a84cf29 commit a1b1200
Show file tree
Hide file tree
Showing 6 changed files with 232 additions and 49 deletions.
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

0 comments on commit a1b1200

Please sign in to comment.