2 u
"""Implements the markup used for description and effect text in the database.
4 The language used is a variation of Markdown and Markdown Extra. There are
5 docs for each at http://daringfireball.net/projects/markdown/ and
6 http://michelf.com/projects/php-markdown/extra/ respectively.
8 Pokédex links are represented with the syntax `[text]{type:identifier}`, e.g.,
9 `[Eevee]{pokemon:eevee}`. The actual code that parses these is in
12 from __future__
import absolute_import
15 import sqlalchemy
.types
17 class MarkdownString(object):
18 """Wraps a Markdown string. Stringifies to the original text, but .as_html
19 will return an HTML rendering.
21 To add extensions to the rendering (which is necessary for rendering links
22 correctly, and which spline-pokedex does), you must append to this class's
23 `markdown_extensions` list. Yep, that's gross.
26 markdown_extensions
= ['extra']
28 def __init__(self
, source_text
):
29 self
.source_text
= source_text
32 def __unicode__(self
):
33 return self
.source_text
36 return unicode(self
.source_text
).encode()
43 """Returns the string as HTML4."""
45 if self
._as_html
is not None:
48 md
= markdown
.Markdown(
49 extensions
=self
.markdown_extensions
,
51 output_format
='xhtml1',
54 self
._as_html
= md
.convert(self
.source_text
)
60 """Returns the string in a plaintext-friendly form.
62 At the moment, this is just the original source text.
64 return self
.source_text
66 def _markdownify_effect_text(move
, effect_text
):
67 effect_text
= effect_text
.replace(
69 unicode(move
.effect_chance
),
72 return MarkdownString(effect_text
)
74 class MoveEffectProperty(object):
75 """Property that wraps move effects. Used like this:
77 MoveClass.effect = MoveEffectProperty('effect')
79 some_move.effect # returns a MarkdownString
80 some_move.effect.as_html # returns a chunk of HTML
82 This class also performs simple substitution on the effect, replacing
83 `$effect_chance` with the move's actual effect chance.
85 Use `MoveEffectPropertyMap` for dict-like association proxies.
88 def __init__(self
, effect_column
):
89 self
.effect_column
= effect_column
91 def __get__(self
, obj
, cls
):
92 prop
= getattr(obj
.move_effect
, self
.effect_column
)
93 return _markdownify_effect_text(obj
, prop
)
95 class MoveEffectPropertyMap(MoveEffectProperty
):
96 """Similar to `MoveEffectProperty`, but works on dict-like association
99 def __get__(self
, obj
, cls
):
100 prop
= getattr(obj
.move_effect
, self
.effect_column
)
103 newdict
[key
] = _markdownify_effect_text(obj
, newdict
[key
])
106 class MarkdownColumn(sqlalchemy
.types
.TypeDecorator
):
107 """Generic SQLAlchemy column type for Markdown text.
109 Do NOT use this for move effects! They need to know what move they belong
110 to so they can fill in, e.g., effect chances. Use the MoveEffectProperty
111 property class above.
113 impl
= sqlalchemy
.types
.Unicode
115 def process_bind_param(self
, value
, dialect
):
119 if not isinstance(value
, basestring
):
120 # Can't assign, e.g., MarkdownString objects yet
121 raise NotImplementedError
123 return unicode(value
)
125 def process_result_value(self
, value
, dialect
):
129 return MarkdownString(value
)