X-Git-Url: http://git.veekun.com/zzz-pokedex.git/blobdiff_plain/897d971a6f82bfebd3581ee8d3bffad0c9e3cc09..8f47c0412b68c1c8499a08623eea2f58377bbab8:/pokedex/db/markdown.py?ds=sidebyside diff --git a/pokedex/db/markdown.py b/pokedex/db/markdown.py index d8ac683..c82ce68 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 @@ -30,11 +31,17 @@ class MarkdownString(object): def __unicode__(self): return self.source_text + def __str__(self): + return unicode(self.source_text).encode() + + def __html__(self): + return self.as_html + @property def as_html(self): """Returns the string as HTML4.""" - if self._as_html: + if self._as_html is not None: return self._as_html md = markdown.Markdown( @@ -55,9 +62,16 @@ class MarkdownString(object): """ return self.source_text +def _markdownify_effect_text(move, effect_text): + effect_text = effect_text.replace( + u'$effect_chance', + unicode(move.effect_chance), + ) + + return MarkdownString(effect_text) class MoveEffectProperty(object): - """Property that wraps a move effect. Used like this: + """Property that wraps move effects. Used like this: MoveClass.effect = MoveEffectProperty('effect') @@ -66,19 +80,27 @@ class MoveEffectProperty(object): This class also performs simple substitution on the effect, replacing `$effect_chance` with the move's actual effect chance. + + Use `MoveEffectPropertyMap` for dict-like association proxies. """ def __init__(self, effect_column): 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), - ) + def __get__(self, obj, cls): + prop = getattr(obj.move_effect, self.effect_column) + return _markdownify_effect_text(obj, prop) - return MarkdownString(effect_text) +class MoveEffectPropertyMap(MoveEffectProperty): + """Similar to `MoveEffectProperty`, but works on dict-like association + proxies. + """ + def __get__(self, obj, cls): + prop = getattr(obj.move_effect, self.effect_column) + newdict = dict(prop) + for key in newdict: + newdict[key] = _markdownify_effect_text(obj, newdict[key]) + return newdict class MarkdownColumn(sqlalchemy.types.TypeDecorator): """Generic SQLAlchemy column type for Markdown text. @@ -90,6 +112,9 @@ class MarkdownColumn(sqlalchemy.types.TypeDecorator): impl = sqlalchemy.types.Unicode def process_bind_param(self, value, dialect): + if value is None: + return None + if not isinstance(value, basestring): # Can't assign, e.g., MarkdownString objects yet raise NotImplementedError @@ -97,4 +122,7 @@ class MarkdownColumn(sqlalchemy.types.TypeDecorator): return unicode(value) def process_result_value(self, value, dialect): + if value is None: + return None + return MarkdownString(value)