X-Git-Url: http://git.veekun.com/zzz-pokedex.git/blobdiff_plain/897d971a6f82bfebd3581ee8d3bffad0c9e3cc09..6031da5035cb9fc4ee4069194e2035895f5cbd6c:/pokedex/db/markdown.py?ds=sidebyside diff --git a/pokedex/db/markdown.py b/pokedex/db/markdown.py index d8ac683..0bc6971 100644 --- a/pokedex/db/markdown.py +++ b/pokedex/db/markdown.py @@ -8,6 +8,7 @@ http://michelf.com/projects/php-markdown/extra/ respectively. Pokédex links are represented with the extended syntax `[name]{type}`, e.g., `[Eevee]{pokemon}`. The actual code that parses these is in spline-pokedex. """ +from __future__ import absolute_import import markdown import sqlalchemy.types @@ -56,13 +57,33 @@ class MarkdownString(object): return self.source_text -class MoveEffectProperty(object): - """Property that wraps a move effect. Used like this: +class _MoveEffects(object): + def __init__(self, effect_column, move): + self.effect_column = effect_column + self.move = move + + def __contains__(self, lang): + return lang in self.move.move_effect.prose + + def __getitem__(self, lang): + try: + effect_text = getattr(self.move.move_effect.prose[lang], self.effect_column) + except AttributeError: + return None + effect_text = effect_text.replace( + u'$effect_chance', + unicode(self.move.effect_chance), + ) + + return MarkdownString(effect_text) + +class MoveEffectsProperty(object): + """Property that wraps move effects. Used like this: - MoveClass.effect = MoveEffectProperty('effect') + MoveClass.effects = MoveEffectProperty('effect') - some_move.effect # returns a MarkdownString - some_move.effect.as_html # returns a chunk of HTML + some_move.effects[lang] # returns a MarkdownString + some_move.effects[lang].as_html # returns a chunk of HTML This class also performs simple substitution on the effect, replacing `$effect_chance` with the move's actual effect chance. @@ -72,13 +93,7 @@ class MoveEffectProperty(object): self.effect_column = effect_column def __get__(self, move, move_class): - effect_text = getattr(move.move_effect, self.effect_column) - effect_text = effect_text.replace( - u'$effect_chance', - unicode(move.effect_chance), - ) - - return MarkdownString(effect_text) + return _MoveEffects(self.effect_column, move) class MarkdownColumn(sqlalchemy.types.TypeDecorator): """Generic SQLAlchemy column type for Markdown text.