Skip to content

Commit

Permalink
Support non-ascii keywords
Browse files Browse the repository at this point in the history
  • Loading branch information
tzizi committed Jul 24, 2018
1 parent 98a8779 commit b128c9b
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 12 deletions.
28 changes: 27 additions & 1 deletion _ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from cyrax import events
from natsort import natsorted, ns
from pykwalify.core import Core
from unidecode import unidecode


def abort(msg):
Expand Down Expand Up @@ -43,6 +44,22 @@ def parse_tag(tag):
return tag.replace(' ', '-').lower()


def parse_unicode(text):
if type(text) == str:
text = unicode(text)
if type(text) == unicode:
return unidecode(text)
if type(text) in [list, tuple]:
result = []
for item in text:
result.append(parse_unicode(item))
return result


def parse_unicode_tag(tag):
return parse_tag(parse_unicode(tag))


def parse_tags(entry, keys):
tags = []

Expand All @@ -53,13 +70,20 @@ def parse_tags(entry, keys):

if val_type == str or val_type == unicode:
tags.append(parse_tag(val))
tags.append(parse_unicode_tag(val))
elif val_type == list:
tags += map(parse_tag, val)
tags += map(parse_unicode_tag, val)
else:
abort('Error: %s\'s key "%s" is not valid (%s)' %
(entry['name'], key, val_type.__name__))

return tags
result = []
for tag in tags:
if tag not in result:
result.append(tag)

return result


def parse_global_tags(site, item, tag):
Expand Down Expand Up @@ -103,6 +127,8 @@ def parse_items(site, item, key):
]

meta = item.get('meta', {})
meta["names_ascii"] = parse_unicode(names(item))

parse_fn = partial(parse_item, entry_tags=game_tags, meta=meta, meta_tags=meta_tags)

for game in item[key]:
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ cyrax==2.7
natsort
pyyaml
pykwalify==1.5.2
unidecode
27 changes: 16 additions & 11 deletions templates/games.html
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,19 @@
{{ game.tags | join(' ') }}
{%- endmacro %}

{% macro show_keywords(game, names) -%}
{% macro show_keywords_names(names) -%}
{% for n in names -%}
{% if n is not string -%}
{% for i in n -%}
{{ i | lower | e -}}{{ ' ' -}}
{% endfor -%}
{% else -%}
{{ n | lower | e -}}
{% endif -%}
{% endfor -%}
{%- endmacro %}

{% macro show_keywords(game, names, meta) -%}
{{ game.name | lower | e -}}{{ ' ' -}}
{{ game.tags | join(' ') -}}{{ ' ' -}}
{{ game.info | lower | e -}}{{ ' ' -}}
Expand All @@ -48,15 +60,8 @@
{{ 'multiplayer' }}
{%- endif -%}

{% for n in names -%}
{% if n is not string -%}
{% for i in n -%}
{{ i | lower | e -}}{{ ' ' -}}
{% endfor -%}
{% else -%}
{{ n | lower | e -}}
{% endif -%}
{% endfor -%}
{{ show_keywords_names(names) }}
{{ show_keywords_names(meta.names_ascii) }}
{%- endmacro %}

{% macro render(names, meta, items, mode) %}
Expand All @@ -74,7 +79,7 @@
{%- endif -%}
{{ show_id(name, mode) }}"
data-tags="{{ show_tags(game) -}}"
data-index="{{ show_keywords(game, names) -}}">
data-index="{{ show_keywords(game, names, meta) -}}">
<span class="{% if 'images' in game or 'video' in game %}toggler{% else %}notoggler{% endif %}">&#x25b6;</span>

{%- if 'url' in game -%}
Expand Down

0 comments on commit b128c9b

Please sign in to comment.