X-Git-Url: http://git.veekun.com/zzz-pokedex.git/blobdiff_plain/2efa91d9daf718debf9ed920efaf02a155054cac..745e4a66f4474acf4368fcc774e5100a6bcac39c:/pokedex/db/rst.py diff --git a/pokedex/db/rst.py b/pokedex/db/rst.py index e70af2c..d3d6322 100644 --- a/pokedex/db/rst.py +++ b/pokedex/db/rst.py @@ -40,6 +40,8 @@ from docutils.parsers.rst import Parser, roles import docutils.utils from docutils.writers.html4css1 import Writer as HTMLWriter +import sqlalchemy.types + ### Subclasses of bits of docutils, to munge it into doing what I want class HTMLFragmentWriter(HTMLWriter): """Translates reST to HTML, but only as a fragment. Enclosing , @@ -63,12 +65,13 @@ class UnicodeOutput(Output): ### Text roles def generic_role(name, rawtext, text, lineno, inliner, options={}, content=[]): - node = docutils.nodes.strong(text, rawtext, **options) + node = docutils.nodes.emphasis(rawtext, text, **options) return [node], [] roles.register_local_role('ability', generic_role) roles.register_local_role('item', generic_role) roles.register_local_role('move', generic_role) +roles.register_local_role('type', generic_role) roles.register_local_role('pokemon', generic_role) roles.register_local_role('mechanic', generic_role) @@ -137,7 +140,7 @@ class RstString(object): class MoveEffectProperty(object): """Property that wraps a move effect. Used like this: - MoveClass.effect = MoveEffectProperty() + MoveClass.effect = MoveEffectProperty('effect') some_move.effect # returns an RstString some_move.effect.as_html # returns a chunk of HTML @@ -147,6 +150,9 @@ class MoveEffectProperty(object): lie and it doesn't yet. """ + def __init__(self, effect_column): + self.effect_column = effect_column + def __get__(self, move, move_class): # Attach a function for handling the `data` role # XXX make this a little more fault-tolerant.. maybe.. @@ -155,6 +161,20 @@ class MoveEffectProperty(object): newtext = getattr(move, text[5:]) return docutils.nodes.Text(newtext, rawtext) - return RstString(move.move_effect.effect, + return RstString(getattr(move.move_effect, self.effect_column), document_properties=dict( _pokedex_handle_data=data_role_func)) + +class RstTextColumn(sqlalchemy.types.TypeDecorator): + """Generic column type for reST text. + + Do NOT use this for move effects! They need to know what move they belong + to so they can fill in, e.g., effect chances. + """ + impl = sqlalchemy.types.Unicode + + def process_bind_param(self, value, dialect): + return unicode(value) + + def process_result_value(self, value, dialect): + return RstString(value)