From cd7324333a995eeb62a3e6eacdb3b179c6256133 Mon Sep 17 00:00:00 2001 From: Waylan Limberg Date: Fri, 12 Jan 2018 22:48:41 -0500 Subject: Refactor Extension loading (#627) Deprecated naming support is removed: * Removed special treatment for modules in `markdown.extensions` * Removed support for `mdx_` prefixes. Support for Entry Point names added: Support for "short names" are now implemented with entry points. Therefore all the users who call extension names as `toc` will not get errors as the builtin extensions all have entry points defined which match the old "short names" for modules in `markdown.extensions`. The benefit is that any extension can offer the same support without requiring the user to manually copy a file to that location on the file system (way to many extension authors have included such instructions in their installation documentation). The one odd thing about this is that we have been issuing a DeprecationWarning for short names and now they are fully supported again. But I think it's the right thing to do. Support for using dot notation is not removed. After all, it was never deprecated. And we shouldn't "force" entry points. There are plenty of reasons why users may not want that and not all of them can be resolved by using class instances instead. All of the following ways to load an extension are valid: # Class instance from markdown.extensions.toc import TocExtension markdown.markdown(src, extensions=[TocExtension()] # Entry point name markdown.markdown(src, extensions=['toc']) # Dot notation with class markdown.markdown(src, extensions=['markdown.extensions.toc:TocExtension']) # Dot notation without class markdown.markdown(src, extensions=['markdown.extensions.toc']) --- tests/test_apis.py | 49 +++++----------------------------- tests/test_extensions.py | 38 +++++++++++++------------- tests/test_legacy.py | 69 ++++++++++++++++++++---------------------------- 3 files changed, 53 insertions(+), 103 deletions(-) (limited to 'tests') diff --git a/tests/test_apis.py b/tests/test_apis.py index 6a1829b..42e7496 100644 --- a/tests/test_apis.py +++ b/tests/test_apis.py @@ -11,7 +11,6 @@ from __future__ import unicode_literals import unittest import sys import os -import types import markdown import warnings from markdown.__main__ import parse_options @@ -46,11 +45,15 @@ class TestMarkdownBasics(unittest.TestCase): from markdown.extensions.footnotes import FootnoteExtension markdown.Markdown(extensions=[FootnoteExtension()]) - def testNamedExtension(self): + def testEntryPointExtension(self): + """ Test Extension loading with an entry point. """ + markdown.Markdown(extensions=['footnotes']) + + def testDotNotationExtension(self): """ Test Extension loading with Name (`path.to.module`). """ markdown.Markdown(extensions=['markdown.extensions.footnotes']) - def TestNamedExtensionWithClass(self): + def TestDotNotationExtensionWithClass(self): """ Test Extension loading with class name (`path.to.module:Class`). """ markdown.Markdown(extensions=['markdown.extensions.footnotes:FootnoteExtension']) @@ -343,46 +346,6 @@ class TestErrors(unittest.TestCase): markdown.Markdown, extensions=[markdown.extensions.Extension()] ) - def testMdxExtention(self): - """ Test that prepending mdx_ raises a DeprecationWarning. """ - _create_fake_extension(name='fake', use_old_style=True) - self.assertRaises( - DeprecationWarning, - markdown.Markdown, extensions=['fake'] - ) - - def testShortNameExtention(self): - """ Test that using a short name raises a DeprecationWarning. """ - self.assertRaises( - DeprecationWarning, - markdown.Markdown, extensions=['footnotes'] - ) - - -def _create_fake_extension(name, has_factory_func=True, is_wrong_type=False, use_old_style=False): - """ Create a fake extension module for testing. """ - if use_old_style: - mod_name = '_'.join(['mdx', name]) - else: - mod_name = name - if not PY3: - # mod_name must be bytes in Python 2.x - mod_name = bytes(mod_name) - ext_mod = types.ModuleType(mod_name) - - def makeExtension(*args, **kwargs): - if is_wrong_type: - return object - else: - return markdown.extensions.Extension(*args, **kwargs) - - if has_factory_func: - ext_mod.makeExtension = makeExtension - # Warning: this brute forces the extenson module onto the system. Either - # this needs to be specificly overriden or a new python session needs to - # be started to get rid of this. This should be ok in a testing context. - sys.modules[mod_name] = ext_mod - class testETreeComments(unittest.TestCase): """ diff --git a/tests/test_extensions.py b/tests/test_extensions.py index 5dee0fd..5a04e64 100644 --- a/tests/test_extensions.py +++ b/tests/test_extensions.py @@ -72,7 +72,7 @@ class TestAbbr(unittest.TestCase): """ Test abbr extension. """ def setUp(self): - self.md = markdown.Markdown(extensions=['markdown.extensions.abbr']) + self.md = markdown.Markdown(extensions=['abbr']) def testSimpleAbbr(self): """ Test Abbreviations. """ @@ -109,7 +109,7 @@ class TestCodeHilite(TestCaseWithAssertStartsWith): def testBasicCodeHilite(self): text = '\t# A Code Comment' - md = markdown.Markdown(extensions=['markdown.extensions.codehilite']) + md = markdown.Markdown(extensions=['codehilite']) if self.has_pygments: # Pygments can use random lexer here as we did not specify the language self.assertStartsWith('
', md.convert(text))
@@ -205,7 +205,7 @@ class TestCodeHilite(TestCaseWithAssertStartsWith):
         text1 = "\t:::Python hl_lines='1'\n\t#line 1\n\t#line 2\n\t#line 3"
 
         for text in (text0, text1):
-            md = markdown.Markdown(extensions=['markdown.extensions.codehilite'])
+            md = markdown.Markdown(extensions=['codehilite'])
             if self.has_pygments:
                 self.assertStartsWith(
                     '
' in md.convert(text))
@@ -392,7 +392,7 @@ class TestMetaData(unittest.TestCase):
     """ Test MetaData extension. """
 
     def setUp(self):
-        self.md = markdown.Markdown(extensions=['markdown.extensions.meta'])
+        self.md = markdown.Markdown(extensions=['meta'])
 
     def testBasicMetaData(self):
         """ Test basic metadata. """
@@ -459,7 +459,7 @@ class TestWikiLinks(unittest.TestCase):
     """ Test Wikilinks Extension. """
 
     def setUp(self):
-        self.md = markdown.Markdown(extensions=['markdown.extensions.wikilinks'])
+        self.md = markdown.Markdown(extensions=['wikilinks'])
         self.text = "Some text with a [[WikiLink]]."
 
     def testBasicWikilinks(self):
@@ -500,9 +500,9 @@ class TestWikiLinks(unittest.TestCase):
         """ Test Complex Settings. """
 
         md = markdown.Markdown(
-            extensions=['markdown.extensions.wikilinks'],
+            extensions=['wikilinks'],
             extension_configs={
-                'markdown.extensions.wikilinks': [
+                'wikilinks': [
                     ('base_url', 'http://example.com/'),
                     ('end_url', '.html'),
                     ('html_class', '')
@@ -524,7 +524,7 @@ wiki_end_url:   .html
 wiki_html_class:
 
 Some text with a [[WikiLink]]."""
-        md = markdown.Markdown(extensions=['markdown.extensions.meta', 'markdown.extensions.wikilinks'])
+        md = markdown.Markdown(extensions=['meta', 'wikilinks'])
         self.assertEqual(
             md.convert(text),
             '

Some text with a ' @@ -557,7 +557,7 @@ class TestAdmonition(unittest.TestCase): """ Test Admonition Extension. """ def setUp(self): - self.md = markdown.Markdown(extensions=['markdown.extensions.admonition']) + self.md = markdown.Markdown(extensions=['admonition']) def testRE(self): RE = self.md.parser.blockprocessors['admonition'].RE @@ -574,7 +574,7 @@ class TestTOC(TestCaseWithAssertStartsWith): """ Test TOC Extension. """ def setUp(self): - self.md = markdown.Markdown(extensions=['markdown.extensions.toc']) + self.md = markdown.Markdown(extensions=['toc']) def testMarker(self): """ Test TOC with a Marker. """ @@ -818,7 +818,7 @@ class TestTOC(TestCaseWithAssertStartsWith): def testWithAttrList(self): """ Test TOC with attr_list Extension. """ - md = markdown.Markdown(extensions=['markdown.extensions.toc', 'markdown.extensions.attr_list']) + md = markdown.Markdown(extensions=['toc', 'attr_list']) text = '# Header 1\n\n## Header 2 { #foo }' self.assertEqual( md.convert(text), @@ -849,7 +849,7 @@ class TestTOC(TestCaseWithAssertStartsWith): class TestSmarty(unittest.TestCase): def setUp(self): config = { - 'markdown.extensions.smarty': [ + 'smarty': [ ('smart_angled_quotes', True), ('substitutions', { 'ndash': '\u2013', @@ -865,7 +865,7 @@ class TestSmarty(unittest.TestCase): ] } self.md = markdown.Markdown( - extensions=['markdown.extensions.smarty'], + extensions=['smarty'], extension_configs=config ) @@ -885,8 +885,8 @@ class TestFootnotes(unittest.TestCase): def testBacklinkText(self): md = markdown.Markdown( - extensions=['markdown.extensions.footnotes'], - extension_configs={'markdown.extensions.footnotes': {'BACKLINK_TEXT': 'back'}} + extensions=['footnotes'], + extension_configs={'footnotes': {'BACKLINK_TEXT': 'back'}} ) text = 'paragraph[^1]\n\n[^1]: A Footnote' self.assertEqual( diff --git a/tests/test_legacy.py b/tests/test_legacy.py index ddc54bb..7fca02a 100644 --- a/tests/test_legacy.py +++ b/tests/test_legacy.py @@ -123,80 +123,67 @@ class TestExtensions(LegacyTestCase): location = os.path.join(parent_test_dir, 'extensions') exclude = ['codehilite'] - attr_list = Kwargs( - extensions=[ - 'markdown.extensions.attr_list', - 'markdown.extensions.def_list', - 'markdown.extensions.smarty' - ] - ) + attr_list = Kwargs(extensions=['attr_list', 'def_list', 'smarty']) - codehilite = Kwargs(extensions=['markdown.extensions.codehilite']) + codehilite = Kwargs(extensions=['codehilite']) - toc = Kwargs(extensions=['markdown.extensions.toc']) + toc = Kwargs(extensions=['toc']) - toc_invalid = Kwargs(extensions=['markdown.extensions.toc']) + toc_invalid = Kwargs(extensions=['toc']) - toc_out_of_order = Kwargs(extensions=['markdown.extensions.toc']) + toc_out_of_order = Kwargs(extensions=['toc']) toc_nested = Kwargs( - extensions=['markdown.extensions.toc'], - extension_configs={'markdown.extensions.toc': {'permalink': True}} + extensions=['toc'], + extension_configs={'toc': {'permalink': True}} ) toc_nested2 = Kwargs( - extensions=['markdown.extensions.toc'], - extension_configs={'markdown.extensions.toc': {'permalink': "[link]"}} + extensions=['toc'], + extension_configs={'toc': {'permalink': "[link]"}} ) - toc_nested_list = Kwargs(extensions=['markdown.extensions.toc']) + toc_nested_list = Kwargs(extensions=['toc']) - wikilinks = Kwargs(extensions=['markdown.extensions.wikilinks']) + wikilinks = Kwargs(extensions=['wikilinks']) - fenced_code = Kwargs(extensions=['markdown.extensions.fenced_code']) + fenced_code = Kwargs(extensions=['fenced_code']) - github_flavored = Kwargs(extensions=['markdown.extensions.fenced_code']) + github_flavored = Kwargs(extensions=['fenced_code']) - sane_lists = Kwargs(extensions=['markdown.extensions.sane_lists']) + sane_lists = Kwargs(extensions=['sane_lists']) - nl2br_w_attr_list = Kwargs( - extensions=[ - 'markdown.extensions.nl2br', - 'markdown.extensions.attr_list' - ] - ) + nl2br_w_attr_list = Kwargs(extensions=['nl2br', 'attr_list']) - admonition = Kwargs(extensions=['markdown.extensions.admonition']) + admonition = Kwargs(extensions=['admonition']) smarty = Kwargs( - extensions=['markdown.extensions.smarty'], - extension_configs={'markdown.extensions.smarty': {'smart_angled_quotes': True}} + extensions=['smarty'], + extension_configs={'smarty': {'smart_angled_quotes': True}} ) class TestExtensionsExtra(LegacyTestCase): location = os.path.join(parent_test_dir, 'extensions/extra') - default_kwargs = Kwargs(extensions=['markdown.extensions.extra']) + default_kwargs = Kwargs(extensions=['extra']) - loose_def_list = Kwargs(extensions=['markdown.extensions.def_list']) + loose_def_list = Kwargs(extensions=['def_list']) - simple_def_lists = Kwargs(extensions=['markdown.extensions.def_list']) + simple_def_lists = Kwargs(extensions=['def_list']) - abbr = Kwargs(extensions=['markdown.extensions.abbr']) + abbr = Kwargs(extensions=['abbr']) - footnotes = Kwargs(extensions=['markdown.extensions.footnotes']) + footnotes = Kwargs(extensions=['footnotes']) - tables = Kwargs(extensions=['markdown.extensions.tables']) + tables = Kwargs(extensions=['tables']) - tables_and_attr_list = Kwargs( - extensions=['markdown.extensions.tables', 'markdown.extensions.attr_list'] - ) + tables_and_attr_list = Kwargs(extensions=['tables', 'attr_list']) extra_config = Kwargs( - extensions=['markdown.extensions.extra'], + extensions=['extra'], extension_configs={ - 'markdown.extensions.extra': { - 'markdown.extensions.footnotes': { + 'extra': { + 'footnotes': { 'PLACE_MARKER': '~~~placemarker~~~' } } -- cgit v1.2.3