From 719c32de0b9f143a7de2beda8a465ad9e6eb67df Mon Sep 17 00:00:00 2001 From: Petr Viktorin Date: Mon, 12 Sep 2011 19:57:06 +0300 Subject: [PATCH] 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). --- pokedex/db/markdown.py | 15 ++++++++------- pokedex/db/multilang.py | 10 ++++++---- pokedex/tests/test_strings.py | 32 ++++++++++++++++++-------------- 3 files changed, 32 insertions(+), 25 deletions(-) diff --git a/pokedex/db/markdown.py b/pokedex/db/markdown.py index dc0e2cf..7916633 100644 --- a/pokedex/db/markdown.py +++ b/pokedex/db/markdown.py @@ -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], diff --git a/pokedex/db/multilang.py b/pokedex/db/multilang.py index 3355320..a778869 100644 --- a/pokedex/db/multilang.py +++ b/pokedex/db/multilang.py @@ -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 diff --git a/pokedex/tests/test_strings.py b/pokedex/tests/test_strings.py index a0e6381..ef9bd41 100644 --- a/pokedex/tests/test_strings.py +++ b/pokedex/tests/test_strings.py @@ -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)) == ( '

Thunderbolt paralyzes Sky Shaymin. mewthree does not exist.

') - assert md.as_html(extension_cls=IdentifierTestExtension) == ( + assert md.as_html(extension=IdentifierTestExtension(connection)) == ( '

Thunderbolt paralyzes Sky Shaymin. mewthree does not exist.

') 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)