From cdac374eeddee2211b88abd22348eeb56d7d584b Mon Sep 17 00:00:00 2001 From: Petr Viktorin Date: Mon, 12 Sep 2011 17:50:31 +0300 Subject: [PATCH] =?UTF-8?q?Allow=20links=20to=20Pok=C3=A9mon=20forms=20in?= =?UTF-8?q?=20Markdown.=20Fixes=20#465?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Linked-to objects aren't required to have identifiers now, so object_url() in custom extensions might need to be changed. The one in the test did, for example. --- pokedex/db/markdown.py | 18 +++++++++++++++--- pokedex/tests/test_strings.py | 16 ++++++++++------ 2 files changed, 25 insertions(+), 9 deletions(-) diff --git a/pokedex/db/markdown.py b/pokedex/db/markdown.py index 7e16c5d..52c3a9e 100644 --- a/pokedex/db/markdown.py +++ b/pokedex/db/markdown.py @@ -134,7 +134,7 @@ class PokedexLinkPattern(markdown.inlinepatterns.Pattern): Handles matches using factory """ - regex = ur'(?x) \[ ([^]]*) \] \{ ([-a-z0-9]+) : ([-a-z0-9]+) \}' + regex = ur'(?x) \[ ([^]]*) \] \{ ([-a-z0-9]+) : ([-a-z0-9 ]+) \}' def __init__(self, factory, session, string_language=None, game_language=None): markdown.inlinepatterns.Pattern.__init__(self, self.regex) @@ -154,15 +154,27 @@ class PokedexLinkPattern(markdown.inlinepatterns.Pattern): move=tables.Move, pokemon=tables.PokemonSpecies, type=tables.Type, + form=tables.PokemonForm, )[category] except KeyError: obj = name = target url = self.factory.identifier_url(category, obj) else: session = self.session - obj = util.get(self.session, table, target) + if table is tables.PokemonForm: + form_ident, pokemon_ident = target.split() + query = session.query(table) + query = query.filter( + tables.PokemonForm.form_identifier == form_ident) + query = query.join(tables.PokemonForm.pokemon) + query = query.join(tables.Pokemon.species) + query = query.filter( + tables.PokemonSpecies.identifier == pokemon_ident) + obj = query.one() + else: + obj = util.get(self.session, table, target) url = self.factory.object_url(category, obj) - url = url or self.factory.identifier_url(category, obj.identifier) + url = url or self.factory.identifier_url(category, target) name = None # Translations can be incomplete; in which case we want to use a # fallback. diff --git a/pokedex/tests/test_strings.py b/pokedex/tests/test_strings.py index 075bd2b..782f651 100644 --- a/pokedex/tests/test_strings.py +++ b/pokedex/tests/test_strings.py @@ -92,22 +92,26 @@ def test_markdown(): def test_markdown_string(): en = util.get(connection, tables.Language, 'en') - md = markdown.MarkdownString('[]{move:thunderbolt} [paralyzes]{mechanic:paralysis}', connection, en) - assert unicode(md) == 'Thunderbolt paralyzes' - assert md.as_html() == '

Thunderbolt paralyzes

' + md = markdown.MarkdownString('[]{move:thunderbolt} [paralyzes]{mechanic:paralysis} []{form:sky shaymin}', connection, en) + assert unicode(md) == 'Thunderbolt paralyzes Sky Shaymin' + assert md.as_html() == '

Thunderbolt paralyzes Sky Shaymin

' class ObjectTestExtension(markdown.PokedexLinkExtension): def object_url(self, category, obj): - return "%s/%s" % (category, obj.identifier) + if isinstance(obj, tables.PokemonForm): + return "%s/%s %s" % (category, obj.form_identifier, + obj.species.identifier) + else: + return "%s/%s" % (category, obj.identifier) class IdentifierTestExtension(markdown.PokedexLinkExtension): def identifier_url(self, category, ident): return "%s/%s" % (category, ident) assert md.as_html(extension_cls=ObjectTestExtension) == ( - '

Thunderbolt paralyzes

') + '

Thunderbolt paralyzes Sky Shaymin

') assert md.as_html(extension_cls=IdentifierTestExtension) == ( - '

Thunderbolt paralyzes

') + '

Thunderbolt paralyzes Sky Shaymin

') def markdown_column_params(): """Check all markdown values