Make MarkdownString.as_html() accept an extension object, not class

No reason to instantiate every time as_html's called, is there?

Also, sessions use a markdown_extension attribute instead of
markdown_extension_class. The latter is only used to set the former when
the session is created (unless another markdown_extension_class is given,
of course).
This commit is contained in:
Petr Viktorin 2011-09-12 19:57:06 +03:00
parent 928eaca4a4
commit 719c32de0b
3 changed files with 32 additions and 25 deletions

View file

@ -47,17 +47,18 @@ class MarkdownString(object):
def __html__(self):
return self.as_html()
def as_html(self, extension_cls=None):
def as_html(self, extension=None):
"""Returns the string as HTML.
Pass a custom `extension_cls` to use your own class to generate links.
The default (and recommended superclass) is `PokedexLinkExtension`,
described below.
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.
"""
if extension_cls is None:
extension_cls = self.session.markdown_extension_class
extension = extension_cls(self.session)
if extension is None:
extension = self.session.markdown_extension
md = markdown.Markdown(
extensions=['extra', extension],

View file

@ -205,8 +205,10 @@ class MultilangSession(Session):
if 'default_language_id' in kwargs:
self.default_language_id = kwargs.pop('default_language_id')
if 'markdown_extension_class' in kwargs:
self.markdown_extension_class = kwargs.pop('markdown_extension_class')
markdown_extension_class = kwargs.pop('markdown_extension_class',
self.markdown_extension_class)
self.markdown_extension = markdown_extension_class(self)
kwargs.setdefault('query_cls', MultilangQuery)
@ -226,5 +228,5 @@ class MultilangScopedSession(ScopedSession):
self.registry().default_language_id = new
@property
def markdown_extension_class(self):
return self.registry().markdown_extension_class
def markdown_extension(self):
return self.registry().markdown_extension

View file

@ -108,9 +108,9 @@ def test_markdown_string():
def identifier_url(self, category, ident):
return "%s/%s" % (category, ident)
assert md.as_html(extension_cls=ObjectTestExtension) == (
assert md.as_html(extension=ObjectTestExtension(connection)) == (
'<p><a href="move/thunderbolt">Thunderbolt</a> <span>paralyzes</span> <a href="form/sky shaymin">Sky Shaymin</a>. <span>mewthree</span> does not exist.</p>')
assert md.as_html(extension_cls=IdentifierTestExtension) == (
assert md.as_html(extension=IdentifierTestExtension(connection)) == (
'<p><a href="move/thunderbolt">Thunderbolt</a> <a href="mechanic/paralysis">paralyzes</a> <a href="form/sky shaymin">Sky Shaymin</a>. <a href="pokemon/mewthree">mewthree</a> does not exist.</p>')
def markdown_column_params():
@ -137,6 +137,21 @@ def test_markdown_values(parent_class, translation_class, column_name):
if translation_class:
query = query.join(translation_class)
class TestExtension(markdown.PokedexLinkExtension):
def object_url(self, category, obj):
"Swallow good links"
return 'ok'
def identifier_url(self, category, ident):
"Only allow mechanic links here (good links handled in object_url)"
# Note: 'key' is a closed variable that gets set in the loop below
assert category == 'mechanic', (
'%s: unknown link target: {%s:%s}' %
(key, category, ident))
test_extension = TestExtension(connection)
for item in query:
for language, md_text in getattr(item, column_name + '_map').items():
@ -146,18 +161,7 @@ def test_markdown_values(parent_class, translation_class, column_name):
key = u"Markdown in {0} #{1}'s {2} (lang={3})".format(
parent_class.__name__, item.id, column_name, language.identifier)
class TestExtension(markdown.PokedexLinkExtension):
def object_url(self, category, obj):
"Swallow good links"
return 'ok'
def identifier_url(self, category, ident):
"Only allow mechanic links here (good links handled in object_url)"
assert category == 'mechanic', (
'%s: unknown link target: {%s:%s}' %
(key, category, ident))
text = md_text.as_html(extension_cls=TestExtension)
text = md_text.as_html(extension=test_extension)
error_message = u"{0} leaves syntax cruft:\n{1}"
error_message = error_message.format(key, text)