mirror of
https://github.com/veekun/pokedex.git
synced 2024-08-20 18:16:34 +00:00
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:
parent
d8d32a0176
commit
cdac374eed
2 changed files with 25 additions and 9 deletions
|
@ -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.
|
||||
|
|
|
@ -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() == '<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):
|
||||
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) == (
|
||||
'<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
|
||||
|
|
Loading…
Reference in a new issue