From f7df522f4b8efc96857fdb2916e76918a66e0f76 Mon Sep 17 00:00:00 2001 From: Andrew Ekstedt Date: Thu, 26 Apr 2018 23:45:31 -0700 Subject: [PATCH] Copy image and link helpers to dexlib Pyramid requires access to the request object to be able to generate URLs, so the plan is to move helpers that generate URLs to dexlib, where they'll have access to the template context and thus the request object. This seems easier than adding an explicit 'request' or 'url' parameter to all of them. Copy image and url helpers from splinext.pokedex.helpers to dexlib. Update any internal references to those helpers, but leave external callers alone for now. The next few commits will start fixing up calls to the old functions. Keep the old functions around (for now) so as not to break anything. We can't do a move+update in one commit anyway because there is code in the `veekun` repository which still uses the old helpers. The _=_ parameters are no longer necessary because template functions already have access to the translator. Keep the parameters so that we don't break callers, but change the default to _=None because there is no longer a global _ variable in scope. (The default value doesn't matter - it gets immediately overwritten by a line that gets injected into template functions, `_ = i18n.get_translator(lambda i18n, c)`) Updates #115. Updates #117. --- splinext/pokedex/templates/pokedex/lib.mako | 225 ++++++++++++++++++++ 1 file changed, 225 insertions(+) diff --git a/splinext/pokedex/templates/pokedex/lib.mako b/splinext/pokedex/templates/pokedex/lib.mako index 48ff24d3..3f575e2f 100644 --- a/splinext/pokedex/templates/pokedex/lib.mako +++ b/splinext/pokedex/templates/pokedex/lib.mako @@ -1,5 +1,67 @@ <%! from splinext.pokedex import i18n, db %>\ +#### Images and links + +<%def name="pokedex_img(src, **attr)"><% + return h.HTML.img(src=url(controller='dex', action='media', path=src), **attr) +%> + +<%def name="chrome_img(src, **attr)"><% + return h.HTML.img(src=h.static_uri('pokedex', 'images/' + src), **attr) +%> + +## XXX Should these be able to promote to db objects, rather than demoting to +## strings and integers? If so, how to do that without requiring db access +## from here? +<%def name="generation_icon(generation, _=None)"><% + """Returns a generation icon, given a generation number.""" + # Convert generation to int if necessary + if not isinstance(generation, int): + generation = generation.id + + return chrome_img('versions/generation-%s.png' % generation, + alt=_(u"Generation %d") % generation, + title=_(u"Generation %d") % generation) +%> + +<%def name="version_icons(*versions, **kwargs)"><% + """Returns some version icons, given a list of version names. + + Keyword arguments: + _: translator for i18n + """ + # python's argument_list syntax is kind of limited here + version_icons = u'' + comma = h.pokedex.joiner(', ') + for version in versions: + # Convert version to string if necessary + if isinstance(version, basestring): + identifier = h.pokedex.filename_from_name(version) + name = version + else: + identifier = version.identifier + name = version.name + + version_icons += h.HTML.img( + src=h.static_uri('pokedex', 'images/versions/%s.png' % identifier), + alt=comma.next() + name, + title=name) + + return version_icons +%> + +<%def name="version_group_icon(version_group)"><% + return version_icons(*version_group.versions) + # XXX this is for the combined pixely version group icons i made + names = ', '.join(version.name for version in version_group.versions) + return h.HTML.img( + src=h.static_uri('pokedex', 'images/versions/%s.png' % ( + '-'.join(version.identifier for version in version_group.versions))), + alt=names, + title=names) +%> + + <%def name="pokemon_has_media(pokemon_form, prefix, ext, use_form=True)"><% """Determine whether a file exists in the specified directory for the specified Pokémon form. Convenience wrapper around splinext.pokedex.helpers.pokemon_has_media which implicitly passes the app config. @@ -7,6 +69,169 @@ return h.pokedex.pokemon_has_media(pokemon_form, prefix, ext, config, use_form=use_form) %> +<%def name="species_image(pokemon_species, prefix='main-sprites/black-white', **attr)"><% + u"""Returns an tag for a Pokémon species image.""" + + default_text = pokemon_species.name + + if 'animated' in prefix: + ext = 'gif' + else: + ext = 'png' + + attr.setdefault('alt', default_text) + attr.setdefault('title', default_text) + + return pokedex_img(h.pokedex.pokemon_media_path(pokemon_species, prefix, ext), + **attr) +%> + +<%def name="pokemon_form_image(pokemon_form, prefix=None, **attr)"><% + """Returns an tag for a Pokémon form image.""" + + if prefix is None: + prefix = 'main-sprites/ultra-sun-ultra-moon' + # FIXME what the hell is going on here + if not pokemon_has_media(pokemon_form, prefix, 'png'): + prefix = 'main-sprites/black-white' + + # Deal with Spiky-eared Pichu and ??? Arceus + if pokemon_form.pokemon_form_generations: + last_gen = pokemon_form.pokemon_form_generations[-1].generation_id + if last_gen == 4: + prefix = 'main-sprites/heartgold-soulsilver' + + default_text = pokemon_form.name + + if 'animated' in prefix: + ext = 'gif' + elif 'dream-world' in prefix: + ext = 'svg' + else: + ext = 'png' + + attr.setdefault('alt', default_text) + attr.setdefault('title', default_text) + + return pokedex_img(h.pokedex.pokemon_media_path(pokemon_form.species, prefix, ext, form=pokemon_form), + **attr) +%> + +<%def name="pokemon_icon(pokemon, alt=True)"><% + if pokemon.is_default: + return h.literal('' % pokemon.species.id) + + alt_text = pokemon.name if alt else u'' + if pokemon_has_media(pokemon.default_form, 'icons', 'png'): + return pokemon_form_image(pokemon.default_form, prefix='icons', alt=alt_text) + + return pokedex_img('pokemon/icons/0.png', title=pokemon.species.name, alt=alt_text) +%> + +<%def name="pokemon_link(pokemon, content=None, **attr)"><% + """Returns a link to a Pokémon page. + + `pokemon` + A Pokemon object. + + `content` + Link text (or image, or whatever). + """ + + # Content defaults to the name of the Pokémon + if not content: + content = pokemon.name + + url_kwargs = {} + if pokemon.default_form.form_identifier: + # Don't want a ?form=None, or a ?form=default + url_kwargs['form'] = pokemon.default_form.form_identifier + + return h.HTML.a( + content, + href=url(controller='dex', action='pokemon', + name=pokemon.species.name.lower(), **url_kwargs), + **attr + ) +%> + +<%def name="form_flavor_link(form, content=None, **attr)"><% + """Returns a link to a pokemon form's flavor page. + + `form` + A PokemonForm object. + + `content` + Link text (or image, or whatever). + """ + if not content: + content = form.name + + url_kwargs = {} + if form.form_identifier: + # Don't want a ?form=None, or a ?form=default + url_kwargs['form'] = form.form_identifier + + return h.HTML.a( + content, + href=url(controller='dex', action='pokemon_flavor', + name=form.species.name.lower(), **url_kwargs), + **attr + ) +%> + +<%def name="damage_class_icon(damage_class, _=None)"><% + return pokedex_img( + "damage-classes/%s.png" % damage_class.identifier, + alt=damage_class.name, + title=_("%s: %s", context="damage class: description") % ( + damage_class.name.capitalize(), + damage_class.description, + ) + ) +%> + + +<%def name="type_icon(type)"><% + if isinstance(type, basestring): + if type == '???': + identifier = 'unknown' + else: + identifier = type.lower() + name = type + else: + name = type.name + identifier = type.identifier + return pokedex_img('types/{1}/{0}.png'.format(identifier, c.game_language.identifier), + alt=name, title=name) +%> + +<%def name="type_link(type)"><% + return h.HTML.a( + type_icon(type), + href=url(controller='dex', action='types', name=type.identifier), + ) +%> + +<%def name="item_link(item, include_icon=True, _=None)"><% + """Returns a link to the requested item.""" + + item_name = item.name + + if include_icon: + label = pokedex_img("items/%s.png" % h.pokedex.item_filename(item), + alt=item_name, title=item_name) + ' ' + item_name + else: + label = item_name + + return h.HTML.a(label, + href=url(controller='dex', action='items', + pocket=item.pocket.identifier, name=item_name.lower()), + ) +%> + + +#### Pokemon page helpers <%def name="pokemon_page_header(icon_form=None, subpages=True)">