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
|
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):
|
def __init__(self, factory, session, string_language=None, game_language=None):
|
||||||
markdown.inlinepatterns.Pattern.__init__(self, self.regex)
|
markdown.inlinepatterns.Pattern.__init__(self, self.regex)
|
||||||
|
@ -154,15 +154,27 @@ class PokedexLinkPattern(markdown.inlinepatterns.Pattern):
|
||||||
move=tables.Move,
|
move=tables.Move,
|
||||||
pokemon=tables.PokemonSpecies,
|
pokemon=tables.PokemonSpecies,
|
||||||
type=tables.Type,
|
type=tables.Type,
|
||||||
|
form=tables.PokemonForm,
|
||||||
)[category]
|
)[category]
|
||||||
except KeyError:
|
except KeyError:
|
||||||
obj = name = target
|
obj = name = target
|
||||||
url = self.factory.identifier_url(category, obj)
|
url = self.factory.identifier_url(category, obj)
|
||||||
else:
|
else:
|
||||||
session = self.session
|
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)
|
obj = util.get(self.session, table, target)
|
||||||
url = self.factory.object_url(category, obj)
|
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
|
name = None
|
||||||
# Translations can be incomplete; in which case we want to use a
|
# Translations can be incomplete; in which case we want to use a
|
||||||
# fallback.
|
# fallback.
|
||||||
|
|
|
@ -92,12 +92,16 @@ def test_markdown():
|
||||||
|
|
||||||
def test_markdown_string():
|
def test_markdown_string():
|
||||||
en = util.get(connection, tables.Language, 'en')
|
en = util.get(connection, tables.Language, 'en')
|
||||||
md = markdown.MarkdownString('[]{move:thunderbolt} [paralyzes]{mechanic:paralysis}', connection, en)
|
md = markdown.MarkdownString('[]{move:thunderbolt} [paralyzes]{mechanic:paralysis} []{form:sky shaymin}', connection, en)
|
||||||
assert unicode(md) == 'Thunderbolt paralyzes'
|
assert unicode(md) == 'Thunderbolt paralyzes Sky Shaymin'
|
||||||
assert md.as_html() == '<p><span>Thunderbolt</span> <span>paralyzes</span></p>'
|
assert md.as_html() == '<p><span>Thunderbolt</span> <span>paralyzes</span> <span>Sky Shaymin</span></p>'
|
||||||
|
|
||||||
class ObjectTestExtension(markdown.PokedexLinkExtension):
|
class ObjectTestExtension(markdown.PokedexLinkExtension):
|
||||||
def object_url(self, category, obj):
|
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)
|
return "%s/%s" % (category, obj.identifier)
|
||||||
|
|
||||||
class IdentifierTestExtension(markdown.PokedexLinkExtension):
|
class IdentifierTestExtension(markdown.PokedexLinkExtension):
|
||||||
|
@ -105,9 +109,9 @@ def test_markdown_string():
|
||||||
return "%s/%s" % (category, ident)
|
return "%s/%s" % (category, ident)
|
||||||
|
|
||||||
assert md.as_html(extension_cls=ObjectTestExtension) == (
|
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) == (
|
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():
|
def markdown_column_params():
|
||||||
"""Check all markdown values
|
"""Check all markdown values
|
||||||
|
|
Loading…
Reference in a new issue