dd86746aadaba226f69baf77561f8d9092dfb3cc
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 extended syntax `[name]{type}`, e.g.,
9 `[Eevee]{pokemon}`. The actual code that parses these is in spline-pokedex.
11 from __future__
import absolute_import
14 import sqlalchemy
.types
16 class MarkdownString(object):
17 """Wraps a Markdown string. Stringifies to the original text, but .as_html
18 will return an HTML rendering.
20 To add extensions to the rendering (which is necessary for rendering links
21 correctly, and which spline-pokedex does), you must append to this class's
22 `markdown_extensions` list. Yep, that's gross.
25 markdown_extensions
= ['extra']
27 def __init__(self
, source_text
):
28 self
.source_text
= source_text
31 def __unicode__(self
):
32 return self
.source_text
36 """Returns the string as HTML4."""
41 md
= markdown
.Markdown(
42 extensions
=self
.markdown_extensions
,
44 output_format
='xhtml1',
47 self
._as_html
= md
.convert(self
.source_text
)
53 """Returns the string in a plaintext-friendly form.
55 At the moment, this is just the original source text.
57 return self
.source_text
59 def _markdownify_effect_text(move
, effect_text
):
60 effect_text
= effect_text
.replace(
62 unicode(move
.effect_chance
),
65 return MarkdownString(effect_text
)
67 class MoveEffectProperty(object):
68 """Property that wraps move effects. Used like this:
70 MoveClass.effect = MoveEffectProperty('effect')
72 some_move.effect # returns a MarkdownString
73 some_move.effect.as_html # returns a chunk of HTML
75 This class attempts to detect if the wrapped property is a dict-based
76 association proxy, and will act like such a dict if so. Don't rely on it
77 for querying, of course.
79 This class also performs simple substitution on the effect, replacing
80 `$effect_chance` with the move's actual effect chance.
83 def __init__(self
, effect_column
):
84 self
.effect_column
= effect_column
86 def __get__(self
, obj
, cls
):
87 prop
= getattr(obj
.move_effect
, self
.effect_column
)
88 if isinstance(prop
, dict):
89 # Looks like a dict proxy; markdownify everyone
92 newdict
[key
] = _markdownify_effect_text(obj
, newdict
[key
])
95 # Otherwise, scalar prop. Boring
96 return _markdownify_effect_text(obj
, prop
)
98 class MarkdownColumn(sqlalchemy
.types
.TypeDecorator
):
99 """Generic SQLAlchemy column type for Markdown text.
101 Do NOT use this for move effects! They need to know what move they belong
102 to so they can fill in, e.g., effect chances. Use the MoveEffectProperty
103 property class above.
105 impl
= sqlalchemy
.types
.Unicode
107 def process_bind_param(self
, value
, dialect
):
108 if not isinstance(value
, basestring
):
109 # Can't assign, e.g., MarkdownString objects yet
110 raise NotImplementedError
112 return unicode(value
)
114 def process_result_value(self
, value
, dialect
):
115 return MarkdownString(value
)