Allow links to Pokémon forms in Markdown. Fixes #465

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.
This commit is contained in:
Petr Viktorin 2011-09-12 17:50:31 +03:00
parent d8d32a0176
commit cdac374eed
2 changed files with 25 additions and 9 deletions

View file

@ -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
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.

View file

@ -92,12 +92,16 @@ 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() == '<p><span>Thunderbolt</span> <span>paralyzes</span></p>'
md = markdown.MarkdownString('[]{move:thunderbolt} [paralyzes]{mechanic:paralysis} []{form:sky shaymin}', connection, en)
assert unicode(md) == 'Thunderbolt paralyzes Sky Shaymin'
assert md.as_html() == '<p><span>Thunderbolt</span> <span>paralyzes</span> <span>Sky Shaymin</span></p>'
class ObjectTestExtension(markdown.PokedexLinkExtension):
def object_url(self, category, obj):
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):
@ -105,9 +109,9 @@ def test_markdown_string():
return "%s/%s" % (category, ident)
assert md.as_html(extension_cls=ObjectTestExtension) == (
'<p><a href="move/thunderbolt">Thunderbolt</a> <span>paralyzes</span></p>')
'<p><a href="move/thunderbolt">Thunderbolt</a> <span>paralyzes</span> <a href="form/sky shaymin">Sky Shaymin</a></p>')
assert md.as_html(extension_cls=IdentifierTestExtension) == (
'<p><a href="move/thunderbolt">Thunderbolt</a> <a href="mechanic/paralysis">paralyzes</a></p>')
'<p><a href="move/thunderbolt">Thunderbolt</a> <a href="mechanic/paralysis">paralyzes</a> <a href="form/sky shaymin">Sky Shaymin</a></p>')
def markdown_column_params():
"""Check all markdown values