2010-05-31 22:13:34 +00:00
|
|
|
# encoding: utf8
|
|
|
|
u"""Implements the markup used for description and effect text in the database.
|
|
|
|
|
|
|
|
The language used is a variation of Markdown and Markdown Extra. There are
|
|
|
|
docs for each at http://daringfireball.net/projects/markdown/ and
|
|
|
|
http://michelf.com/projects/php-markdown/extra/ respectively.
|
|
|
|
|
2011-04-20 19:47:37 +00:00
|
|
|
Pokédex links are represented with the syntax `[label]{category:identifier}`,
|
|
|
|
e.g., `[Eevee]{pokemon:eevee}`. The label can (and should) be left out, in
|
|
|
|
which case it is replaced by the name of the thing linked to.
|
2010-05-31 22:13:34 +00:00
|
|
|
"""
|
2010-06-01 00:06:33 +00:00
|
|
|
from __future__ import absolute_import
|
2010-05-31 22:13:34 +00:00
|
|
|
|
2011-04-20 13:27:57 +00:00
|
|
|
import re
|
|
|
|
|
2010-05-31 22:13:34 +00:00
|
|
|
import markdown
|
2015-10-05 15:11:08 +00:00
|
|
|
import six
|
2011-04-20 19:47:37 +00:00
|
|
|
from sqlalchemy.orm.session import object_session
|
2021-03-20 04:45:14 +00:00
|
|
|
from markdown.util import etree, AtomicString
|
|
|
|
|
2010-05-31 22:13:34 +00:00
|
|
|
|
2015-10-05 15:11:08 +00:00
|
|
|
@six.python_2_unicode_compatible
|
2010-05-31 22:13:34 +00:00
|
|
|
class MarkdownString(object):
|
2011-04-20 19:47:37 +00:00
|
|
|
"""Wraps a Markdown string.
|
|
|
|
|
|
|
|
Use unicode() and __html__ for text and HTML representations.
|
|
|
|
The as_text() and as_html() functions do the same, but accept optional
|
|
|
|
arguments that may affect the rendering.
|
|
|
|
The `source_text` property holds the original text.
|
|
|
|
|
|
|
|
init args:
|
|
|
|
`source_text`: the text in Markdown syntax
|
|
|
|
`session`: A DB session used for looking up linked objects
|
|
|
|
`language`: The language the string is in. If None, the session default
|
|
|
|
is used.
|
2010-05-31 22:13:34 +00:00
|
|
|
"""
|
|
|
|
|
2011-04-20 13:27:57 +00:00
|
|
|
default_link_extension = None
|
2010-05-31 22:13:34 +00:00
|
|
|
|
2011-04-20 19:47:37 +00:00
|
|
|
def __init__(self, source_text, session, language):
|
2010-05-31 22:13:34 +00:00
|
|
|
self.source_text = source_text
|
2011-04-20 19:47:37 +00:00
|
|
|
self.session = session
|
|
|
|
self.language = language
|
2010-05-31 22:13:34 +00:00
|
|
|
|
2011-03-31 20:51:19 +00:00
|
|
|
def __str__(self):
|
2015-10-05 15:11:08 +00:00
|
|
|
return self.as_text()
|
2011-03-31 20:51:19 +00:00
|
|
|
|
|
|
|
def __html__(self):
|
2011-04-20 19:47:37 +00:00
|
|
|
return self.as_html()
|
2011-04-20 13:27:57 +00:00
|
|
|
|
2011-09-12 16:57:06 +00:00
|
|
|
def as_html(self, extension=None):
|
2011-04-20 13:27:57 +00:00
|
|
|
"""Returns the string as HTML.
|
|
|
|
|
2011-09-12 16:57:06 +00:00
|
|
|
Pass a custom `extension` to use your own extension object to generate
|
|
|
|
links.
|
|
|
|
The default is the session's markdown_extension. Usually that's a
|
|
|
|
`PokedexLinkExtension` described below, which is also the recommended
|
|
|
|
superclass.
|
2011-04-20 13:27:57 +00:00
|
|
|
"""
|
2010-05-31 22:13:34 +00:00
|
|
|
|
2011-09-12 16:57:06 +00:00
|
|
|
if extension is None:
|
|
|
|
extension = self.session.markdown_extension
|
2010-05-31 22:13:34 +00:00
|
|
|
|
|
|
|
md = markdown.Markdown(
|
2021-03-20 04:45:14 +00:00
|
|
|
extensions=['markdown.extensions.extra', EscapeHtml(), extension],
|
2010-05-31 22:13:34 +00:00
|
|
|
output_format='xhtml1',
|
|
|
|
)
|
|
|
|
|
2011-04-20 13:27:57 +00:00
|
|
|
return md.convert(self.source_text)
|
2010-05-31 22:13:34 +00:00
|
|
|
|
2011-04-20 19:47:37 +00:00
|
|
|
def as_text(self):
|
2010-05-31 22:13:34 +00:00
|
|
|
"""Returns the string in a plaintext-friendly form.
|
2011-04-20 19:47:37 +00:00
|
|
|
|
|
|
|
Currently there are no tunable parameters
|
2010-05-31 22:13:34 +00:00
|
|
|
"""
|
2011-04-20 13:27:57 +00:00
|
|
|
# Since Markdown is pretty readable by itself, we just have to replace
|
|
|
|
# the links by their text.
|
|
|
|
# XXX: The tables get unaligned
|
2011-04-20 19:47:37 +00:00
|
|
|
|
2011-09-07 04:50:00 +00:00
|
|
|
link_maker = PokedexLinkExtension(self.session)
|
|
|
|
pattern = PokedexLinkPattern(link_maker, self.session, self.language)
|
2011-04-20 13:27:57 +00:00
|
|
|
regex = '()%s()' % pattern.regex
|
|
|
|
def handleMatch(m):
|
|
|
|
return pattern.handleMatch(m).text
|
2011-04-20 19:47:37 +00:00
|
|
|
|
2011-04-20 13:27:57 +00:00
|
|
|
return re.sub(regex, handleMatch, self.source_text)
|
2010-05-31 22:13:34 +00:00
|
|
|
|
2011-04-20 19:47:37 +00:00
|
|
|
def _markdownify_effect_text(move, effect_text, language=None):
|
|
|
|
session = object_session(move)
|
|
|
|
|
2011-04-20 11:05:52 +00:00
|
|
|
if effect_text is None:
|
|
|
|
return effect_text
|
2011-03-24 05:17:02 +00:00
|
|
|
effect_text = effect_text.replace(
|
|
|
|
u'$effect_chance',
|
2015-10-05 15:11:08 +00:00
|
|
|
str(move.effect_chance),
|
2011-03-24 05:17:02 +00:00
|
|
|
)
|
2010-05-31 22:13:34 +00:00
|
|
|
|
2012-08-06 18:08:46 +00:00
|
|
|
# "The target" vs "each target"; for Conquest, but hopefully main series
|
|
|
|
# moves too eventually
|
|
|
|
if hasattr(move, 'range'):
|
|
|
|
effect_text = effect_text.replace(
|
|
|
|
u'$target',
|
|
|
|
_target_labels[move.range.targets > 1]
|
|
|
|
).replace(
|
|
|
|
u'$Target',
|
|
|
|
_target_labels[move.range.targets > 1].capitalize()
|
|
|
|
)
|
|
|
|
|
2011-04-20 19:47:37 +00:00
|
|
|
return MarkdownString(effect_text, session, language)
|
2011-02-04 04:22:44 +00:00
|
|
|
|
2012-08-05 03:50:39 +00:00
|
|
|
_target_labels = {
|
|
|
|
False: 'the target',
|
|
|
|
True: 'each target'
|
|
|
|
}
|
|
|
|
|
2011-03-24 05:17:02 +00:00
|
|
|
class MoveEffectProperty(object):
|
2011-02-04 04:22:44 +00:00
|
|
|
"""Property that wraps move effects. Used like this:
|
|
|
|
|
2011-03-24 05:17:02 +00:00
|
|
|
MoveClass.effect = MoveEffectProperty('effect')
|
|
|
|
|
|
|
|
some_move.effect # returns a MarkdownString
|
|
|
|
some_move.effect.as_html # returns a chunk of HTML
|
2011-02-04 04:22:44 +00:00
|
|
|
|
|
|
|
This class also performs simple substitution on the effect, replacing
|
|
|
|
`$effect_chance` with the move's actual effect chance.
|
2011-04-03 08:25:59 +00:00
|
|
|
|
|
|
|
Use `MoveEffectPropertyMap` for dict-like association proxies.
|
2011-02-04 04:22:44 +00:00
|
|
|
"""
|
|
|
|
|
2012-08-06 00:39:03 +00:00
|
|
|
def __init__(self, effect_column, relationship='move_effect'):
|
|
|
|
self.effect_column = effect_column
|
|
|
|
self.relationship = relationship
|
2011-02-04 04:22:44 +00:00
|
|
|
|
2011-03-24 05:17:02 +00:00
|
|
|
def __get__(self, obj, cls):
|
2012-02-11 21:54:46 +00:00
|
|
|
if obj is None:
|
|
|
|
return self
|
2011-11-14 19:13:04 +00:00
|
|
|
if obj.move_effect is None:
|
|
|
|
return None
|
2012-08-06 00:39:03 +00:00
|
|
|
thing = getattr(obj, self.relationship)
|
|
|
|
prop = getattr(thing, self.effect_column)
|
|
|
|
return _markdownify_effect_text(obj, prop)
|
2010-05-31 22:13:34 +00:00
|
|
|
|
2011-04-03 08:25:59 +00:00
|
|
|
class MoveEffectPropertyMap(MoveEffectProperty):
|
|
|
|
"""Similar to `MoveEffectProperty`, but works on dict-like association
|
|
|
|
proxies.
|
|
|
|
"""
|
|
|
|
def __get__(self, obj, cls):
|
2012-02-11 21:54:46 +00:00
|
|
|
if obj is None:
|
|
|
|
return self
|
2011-04-03 08:25:59 +00:00
|
|
|
prop = getattr(obj.move_effect, self.effect_column)
|
|
|
|
newdict = dict(prop)
|
|
|
|
for key in newdict:
|
2011-04-20 19:47:37 +00:00
|
|
|
newdict[key] = _markdownify_effect_text(obj, newdict[key], key)
|
2011-04-03 08:25:59 +00:00
|
|
|
return newdict
|
|
|
|
|
2011-09-07 04:50:00 +00:00
|
|
|
|
2011-04-20 13:27:57 +00:00
|
|
|
class PokedexLinkPattern(markdown.inlinepatterns.Pattern):
|
|
|
|
"""Matches [label]{category:target}.
|
2011-04-20 19:47:37 +00:00
|
|
|
|
|
|
|
Handles matches using factory
|
2011-04-20 13:27:57 +00:00
|
|
|
"""
|
2015-10-05 15:11:08 +00:00
|
|
|
regex = u'(?x) \\[ ([^]]*) \\] \\{ ([-a-z0-9]+) : ([-a-z0-9 ]+) \\}'
|
2011-04-20 13:27:57 +00:00
|
|
|
|
2011-09-07 04:50:00 +00:00
|
|
|
def __init__(self, factory, session, string_language=None, game_language=None):
|
2011-04-20 13:27:57 +00:00
|
|
|
markdown.inlinepatterns.Pattern.__init__(self, self.regex)
|
2011-04-20 19:47:37 +00:00
|
|
|
self.factory = factory
|
2011-09-07 04:50:00 +00:00
|
|
|
self.session = session
|
2011-04-20 19:47:37 +00:00
|
|
|
self.string_language = string_language
|
|
|
|
self.game_language = game_language
|
2011-04-20 13:27:57 +00:00
|
|
|
|
|
|
|
def handleMatch(self, m):
|
2016-11-24 21:29:58 +00:00
|
|
|
from pokedex.db import tables
|
2011-04-20 13:27:57 +00:00
|
|
|
start, label, category, target, end = m.groups()
|
|
|
|
try:
|
|
|
|
table = dict(
|
|
|
|
ability=tables.Ability,
|
|
|
|
item=tables.Item,
|
|
|
|
location=tables.Location,
|
|
|
|
move=tables.Move,
|
2011-04-29 23:10:57 +00:00
|
|
|
pokemon=tables.PokemonSpecies,
|
2011-04-20 13:27:57 +00:00
|
|
|
type=tables.Type,
|
2011-09-12 14:50:31 +00:00
|
|
|
form=tables.PokemonForm,
|
2011-04-20 13:27:57 +00:00
|
|
|
)[category]
|
|
|
|
except KeyError:
|
|
|
|
obj = name = target
|
2011-04-20 19:47:37 +00:00
|
|
|
url = self.factory.identifier_url(category, obj)
|
2011-04-20 13:27:57 +00:00
|
|
|
else:
|
2011-04-20 19:47:37 +00:00
|
|
|
session = self.session
|
2011-09-12 14:50:31 +00:00
|
|
|
if table is tables.PokemonForm:
|
|
|
|
form_ident, pokemon_ident = target.split()
|
|
|
|
query = session.query(table)
|
|
|
|
query = query.filter(
|
|
|
|
tables.PokemonForm.form_identifier == form_ident)
|
|
|
|
query = query.join(tables.PokemonForm.pokemon)
|
|
|
|
query = query.join(tables.Pokemon.species)
|
|
|
|
query = query.filter(
|
|
|
|
tables.PokemonSpecies.identifier == pokemon_ident)
|
2011-09-12 15:56:23 +00:00
|
|
|
else:
|
|
|
|
query = session.query(table)
|
|
|
|
query = query.filter(table.identifier == target)
|
|
|
|
try:
|
2011-09-12 14:50:31 +00:00
|
|
|
obj = query.one()
|
2011-09-12 15:56:23 +00:00
|
|
|
except Exception:
|
|
|
|
obj = name = target
|
|
|
|
url = self.factory.identifier_url(category, obj)
|
2011-09-12 14:50:31 +00:00
|
|
|
else:
|
2011-09-12 15:56:23 +00:00
|
|
|
url = self.factory.object_url(category, obj)
|
|
|
|
url = url or self.factory.identifier_url(category, target)
|
|
|
|
name = None
|
|
|
|
# Translations can be incomplete; in which case we want to use
|
|
|
|
# a fallback.
|
|
|
|
if table in [tables.Type] and self.string_language:
|
|
|
|
# Type wants to be localized to the text language
|
|
|
|
name = obj.name_map.get(self.string_language)
|
|
|
|
if not name and self.game_language:
|
|
|
|
name = obj.name_map.get(self.game_language)
|
|
|
|
if not name:
|
|
|
|
name = obj.name
|
2011-04-20 13:27:57 +00:00
|
|
|
if url:
|
2011-04-20 19:47:37 +00:00
|
|
|
el = self.factory.make_link(category, obj, url, label or name)
|
2011-04-20 13:27:57 +00:00
|
|
|
else:
|
2011-08-30 22:55:17 +00:00
|
|
|
el = etree.Element('span')
|
|
|
|
el.text = AtomicString(label or name)
|
2011-04-20 13:27:57 +00:00
|
|
|
return el
|
|
|
|
|
2021-03-20 04:45:14 +00:00
|
|
|
class EscapeHtml(markdown.Extension):
|
|
|
|
u"""Markdown extension which escapes raw html elements.
|
|
|
|
|
|
|
|
This is the recommended replacement for safe_mode='escape',
|
|
|
|
which was deprecated in Markdown 2.5.
|
|
|
|
See https://python-markdown.github.io/change_log/release-2.5/
|
|
|
|
"""
|
|
|
|
def extendMarkdown(self, md, md_globals):
|
|
|
|
del md.preprocessors['html_block']
|
|
|
|
del md.inlinePatterns['html']
|
|
|
|
|
2011-09-07 04:50:00 +00:00
|
|
|
class PokedexLinkExtension(markdown.Extension):
|
|
|
|
u"""Markdown extension that translates the syntax used in effect text:
|
2011-04-20 13:27:57 +00:00
|
|
|
|
2011-09-07 04:50:00 +00:00
|
|
|
`[label]{category:identifier}` is treated as a link to a Pokédex object,
|
|
|
|
where `category` is the table's singular name, and `label` is an optional
|
|
|
|
link title that defaults to the object's name in the current language.
|
2011-04-20 13:27:57 +00:00
|
|
|
"""
|
2011-09-07 04:50:00 +00:00
|
|
|
def __init__(self, session):
|
2011-04-20 19:47:37 +00:00
|
|
|
self.session = session
|
2011-04-20 13:27:57 +00:00
|
|
|
|
2011-09-07 04:50:00 +00:00
|
|
|
def extendMarkdown(self, md, md_globals):
|
|
|
|
pattern = PokedexLinkPattern(self, self.session)
|
|
|
|
md.inlinePatterns['pokedex-link'] = pattern
|
2011-04-20 13:27:57 +00:00
|
|
|
|
|
|
|
def make_link(self, category, obj, url, text):
|
|
|
|
"""Make an <a> element
|
|
|
|
|
|
|
|
Override this to set custom attributes, e.g. title.
|
|
|
|
"""
|
2011-08-30 22:55:17 +00:00
|
|
|
el = etree.Element('a')
|
2011-04-20 13:27:57 +00:00
|
|
|
el.set('href', url)
|
2011-08-30 22:55:17 +00:00
|
|
|
el.text = AtomicString(text)
|
2011-04-20 13:27:57 +00:00
|
|
|
return el
|
|
|
|
|
|
|
|
def identifier_url(self, category, identifier):
|
2011-09-07 04:50:00 +00:00
|
|
|
"""Return the URL for the given {category:identifier} link. For ORM
|
|
|
|
objects, object_url is tried first.
|
2011-04-20 13:27:57 +00:00
|
|
|
|
2011-09-07 04:50:00 +00:00
|
|
|
Returns None by default, which causes <span> to be used in place of
|
|
|
|
<a>.
|
2011-09-12 15:56:23 +00:00
|
|
|
|
|
|
|
This method is also called for non-existent objects, e.g.
|
|
|
|
[]{pokemon:bogus}.
|
2011-04-20 13:27:57 +00:00
|
|
|
"""
|
|
|
|
return None
|
|
|
|
|
|
|
|
def object_url(self, category, obj):
|
2011-09-12 15:56:23 +00:00
|
|
|
u"""Return the URL for the ORM object `obj`.
|
2011-04-20 13:27:57 +00:00
|
|
|
|
2011-09-07 04:50:00 +00:00
|
|
|
Returns None by default, which causes identifier_url to be tried.
|
2011-09-12 15:56:23 +00:00
|
|
|
|
|
|
|
Note that obj may be a Pokémon form. Unlike other returned objects,
|
|
|
|
these do not have identifiers. Be sure to test this case.
|
2011-04-20 13:27:57 +00:00
|
|
|
"""
|
2011-04-20 19:47:37 +00:00
|
|
|
return None
|