Support filtering by strings (Pokemon.name, Pokemon.names['fr'], etc.)
[zzz-pokedex.git] / pokedex / db / markdown.py
1 # encoding: utf8
2 u"""Implements the markup used for description and effect text in the database.
3
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.
7
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.
10 """
11 from __future__ import absolute_import
12
13 import markdown
14 import sqlalchemy.types
15
16 class MarkdownString(object):
17 """Wraps a Markdown string. Stringifies to the original text, but .as_html
18 will return an HTML rendering.
19
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.
23 """
24
25 markdown_extensions = ['extra']
26
27 def __init__(self, source_text):
28 self.source_text = source_text
29 self._as_html = None
30
31 def __unicode__(self):
32 return self.source_text
33
34 @property
35 def as_html(self):
36 """Returns the string as HTML4."""
37
38 if self._as_html:
39 return self._as_html
40
41 md = markdown.Markdown(
42 extensions=self.markdown_extensions,
43 safe_mode='escape',
44 output_format='xhtml1',
45 )
46
47 self._as_html = md.convert(self.source_text)
48
49 return self._as_html
50
51 @property
52 def as_text(self):
53 """Returns the string in a plaintext-friendly form.
54
55 At the moment, this is just the original source text.
56 """
57 return self.source_text
58
59
60 class _MoveEffects(object):
61 def __init__(self, effect_column, move):
62 self.effect_column = effect_column
63 self.move = move
64
65 def __contains__(self, lang):
66 return lang in self.move.move_effect.prose
67
68 def __getitem__(self, lang):
69 try:
70 effect_text = getattr(self.move.move_effect.prose[lang], self.effect_column)
71 except AttributeError:
72 return None
73 effect_text = effect_text.replace(
74 u'$effect_chance',
75 unicode(self.move.effect_chance),
76 )
77
78 return MarkdownString(effect_text)
79
80 class MoveEffectProperty(object):
81 """Property that wraps a move effect. Used like this:
82
83 MoveClass.effect = MoveEffectProperty('effect')
84
85 some_move.effect # returns a MarkdownString
86 some_move.effect.as_html # returns a chunk of HTML
87
88 This class also performs simple substitution on the effect, replacing
89 `$effect_chance` with the move's actual effect chance.
90 """
91
92 def __init__(self, effect_column):
93 self.effect_column = effect_column
94
95 def __get__(self, move, move_class):
96 if move is None:
97 # Don't crash with getattr on the class
98 return NotImplemented
99 return _MoveEffects(self.effect_column, move)['en']
100
101 class MoveEffectsProperty(object):
102 """Property that wraps move effects. Used like this:
103
104 MoveClass.effects = MoveEffectProperty('effect')
105
106 some_move.effects[lang] # returns a MarkdownString
107 some_move.effects[lang].as_html # returns a chunk of HTML
108
109 This class also performs simple substitution on the effect, replacing
110 `$effect_chance` with the move's actual effect chance.
111 """
112
113 def __init__(self, effect_column):
114 self.effect_column = effect_column
115
116 def __get__(self, move, move_class):
117 return _MoveEffects(self.effect_column, move)
118
119 class MarkdownColumn(sqlalchemy.types.TypeDecorator):
120 """Generic SQLAlchemy column type for Markdown text.
121
122 Do NOT use this for move effects! They need to know what move they belong
123 to so they can fill in, e.g., effect chances. Use the MoveEffectProperty
124 property class above.
125 """
126 impl = sqlalchemy.types.Unicode
127
128 def process_bind_param(self, value, dialect):
129 if not isinstance(value, basestring):
130 # Can't assign, e.g., MarkdownString objects yet
131 raise NotImplementedError
132
133 return unicode(value)
134
135 def process_result_value(self, value, dialect):
136 return MarkdownString(value)