diff options
-rw-r--r-- | .coveragerc | 2 | ||||
-rw-r--r-- | markdown/__init__.py | 2 | ||||
-rw-r--r-- | markdown/__version__.py | 30 | ||||
-rw-r--r-- | markdown/core.py | 9 | ||||
-rw-r--r-- | markdown/extensions/abbr.py | 2 | ||||
-rw-r--r-- | markdown/extensions/admonition.py | 2 | ||||
-rw-r--r-- | markdown/extensions/attr_list.py | 2 | ||||
-rw-r--r-- | markdown/extensions/codehilite.py | 2 | ||||
-rw-r--r-- | markdown/extensions/def_list.py | 2 | ||||
-rw-r--r-- | markdown/extensions/extra.py | 2 | ||||
-rw-r--r-- | markdown/extensions/fenced_code.py | 2 | ||||
-rw-r--r-- | markdown/extensions/footnotes.py | 2 | ||||
-rw-r--r-- | markdown/extensions/meta.py | 2 | ||||
-rw-r--r-- | markdown/extensions/nl2br.py | 2 | ||||
-rw-r--r-- | markdown/extensions/sane_lists.py | 2 | ||||
-rw-r--r-- | markdown/extensions/smart_strong.py | 2 | ||||
-rw-r--r-- | markdown/extensions/smarty.py | 2 | ||||
-rw-r--r-- | markdown/extensions/tables.py | 2 | ||||
-rw-r--r-- | markdown/extensions/toc.py | 4 | ||||
-rw-r--r-- | markdown/extensions/wikilinks.py | 2 | ||||
-rw-r--r-- | tests/test_apis.py | 58 |
21 files changed, 79 insertions, 56 deletions
diff --git a/.coveragerc b/.coveragerc index 11e6453..c785d90 100644 --- a/.coveragerc +++ b/.coveragerc @@ -1,5 +1,5 @@ [run] omit= - markdown/__version__.py *site-packages* tests/* + markdown/test_tools.py diff --git a/markdown/__init__.py b/markdown/__init__.py index 3769496..5e7c1de 100644 --- a/markdown/__init__.py +++ b/markdown/__init__.py @@ -46,7 +46,7 @@ __all__ = ['Markdown', 'markdown', 'markdownFromFile'] __version_info__ = (3, 0, 0, 'alpha', 0) -def _get_version(): +def _get_version(): # pragma: no cover " Returns a PEP 386-compliant version number from version_info. " assert len(__version_info__) == 5 assert __version_info__[3] in ('alpha', 'beta', 'rc', 'final') diff --git a/markdown/__version__.py b/markdown/__version__.py deleted file mode 100644 index b01909e..0000000 --- a/markdown/__version__.py +++ /dev/null @@ -1,30 +0,0 @@ -# -# markdown/__version__.py -# -# version_info should conform to PEP 386 -# (major, minor, micro, alpha/beta/rc/final, #) -# (1, 1, 2, 'alpha', 0) => "1.1.2.dev" -# (1, 2, 0, 'beta', 2) => "1.2b2" -version_info = (3, 0, 0, 'alpha', 0) - - -def _get_version(): - " Returns a PEP 386-compliant version number from version_info. " - assert len(version_info) == 5 - assert version_info[3] in ('alpha', 'beta', 'rc', 'final') - - parts = 2 if version_info[2] == 0 else 3 - main = '.'.join(map(str, version_info[:parts])) - - sub = '' - if version_info[3] == 'alpha' and version_info[4] == 0: - # TODO: maybe append some sort of git info here?? - sub = '.dev' - elif version_info[3] != 'final': - mapping = {'alpha': 'a', 'beta': 'b', 'rc': 'c'} - sub = mapping[version_info[3]] + str(version_info[4]) - - return str(main + sub) - - -version = _get_version() diff --git a/markdown/core.py b/markdown/core.py index ce5c333..7424781 100644 --- a/markdown/core.py +++ b/markdown/core.py @@ -150,8 +150,7 @@ class Markdown(object): return ext(**configs) # Get class name (if provided): `path.to.module:ClassName` - ext_name, class_name = ext_name.split(':', 1) \ - if ':' in ext_name else (ext_name, '') + ext_name, class_name = ext_name.split(':', 1) if ':' in ext_name else (ext_name, '') try: module = importlib.import_module(ext_name) @@ -238,7 +237,7 @@ class Markdown(object): try: source = util.text_type(source) - except UnicodeDecodeError as e: + except UnicodeDecodeError as e: # pragma: no cover # Customise error message while maintaining original trackback e.reason += '. -- Note: Markdown only accepts unicode input!' raise @@ -313,7 +312,7 @@ class Markdown(object): input_file.close() else: text = sys.stdin.read() - if not isinstance(text, util.text_type): + if not isinstance(text, util.text_type): # pragma: no cover text = text.decode(encoding) text = text.lstrip('\ufeff') # remove the byte-order mark @@ -340,7 +339,7 @@ class Markdown(object): try: # Write bytes directly to buffer (Python 3). sys.stdout.buffer.write(html) - except AttributeError: + except AttributeError: # pragma: no cover # Probably Python 2, which works with bytes by default. sys.stdout.write(html) diff --git a/markdown/extensions/abbr.py b/markdown/extensions/abbr.py index ec2428a..e6aff7b 100644 --- a/markdown/extensions/abbr.py +++ b/markdown/extensions/abbr.py @@ -90,5 +90,5 @@ class AbbrPattern(Pattern): return abbr -def makeExtension(*args, **kwargs): +def makeExtension(*args, **kwargs): # pragma: no cover return AbbrExtension(*args, **kwargs) diff --git a/markdown/extensions/admonition.py b/markdown/extensions/admonition.py index 2e83900..1ac4a20 100644 --- a/markdown/extensions/admonition.py +++ b/markdown/extensions/admonition.py @@ -92,5 +92,5 @@ class AdmonitionProcessor(BlockProcessor): return klass, title -def makeExtension(*args, **kwargs): +def makeExtension(*args, **kwargs): # pragma: no cover return AdmonitionExtension(*args, **kwargs) diff --git a/markdown/extensions/attr_list.py b/markdown/extensions/attr_list.py index 6ce73a6..ea36d37 100644 --- a/markdown/extensions/attr_list.py +++ b/markdown/extensions/attr_list.py @@ -168,5 +168,5 @@ class AttrListExtension(Extension): ) -def makeExtension(*args, **kwargs): +def makeExtension(*args, **kwargs): # pragma: no cover return AttrListExtension(*args, **kwargs) diff --git a/markdown/extensions/codehilite.py b/markdown/extensions/codehilite.py index 9c91c37..e30c7ba 100644 --- a/markdown/extensions/codehilite.py +++ b/markdown/extensions/codehilite.py @@ -260,5 +260,5 @@ class CodeHiliteExtension(Extension): md.registerExtension(self) -def makeExtension(*args, **kwargs): +def makeExtension(*args, **kwargs): # pragma: no cover return CodeHiliteExtension(*args, **kwargs) diff --git a/markdown/extensions/def_list.py b/markdown/extensions/def_list.py index 54fc064..cbbffdc 100644 --- a/markdown/extensions/def_list.py +++ b/markdown/extensions/def_list.py @@ -111,5 +111,5 @@ class DefListExtension(Extension): '>ulist') -def makeExtension(*args, **kwargs): +def makeExtension(*args, **kwargs): # pragma: no cover return DefListExtension(*args, **kwargs) diff --git a/markdown/extensions/extra.py b/markdown/extensions/extra.py index cea18ed..33ee4b2 100644 --- a/markdown/extensions/extra.py +++ b/markdown/extensions/extra.py @@ -68,7 +68,7 @@ class ExtraExtension(Extension): r'^(p|h[1-6]|li|dd|dt|td|th|legend|address)$', re.IGNORECASE) -def makeExtension(*args, **kwargs): +def makeExtension(*args, **kwargs): # pragma: no cover return ExtraExtension(*args, **kwargs) diff --git a/markdown/extensions/fenced_code.py b/markdown/extensions/fenced_code.py index 0975bb5..01aa605 100644 --- a/markdown/extensions/fenced_code.py +++ b/markdown/extensions/fenced_code.py @@ -109,5 +109,5 @@ class FencedBlockPreprocessor(Preprocessor): return txt -def makeExtension(*args, **kwargs): +def makeExtension(*args, **kwargs): # pragma: no cover return FencedCodeExtension(*args, **kwargs) diff --git a/markdown/extensions/footnotes.py b/markdown/extensions/footnotes.py index de3fc17..6ac92ab 100644 --- a/markdown/extensions/footnotes.py +++ b/markdown/extensions/footnotes.py @@ -426,6 +426,6 @@ class FootnotePostprocessor(Postprocessor): return text.replace(NBSP_PLACEHOLDER, " ") -def makeExtension(*args, **kwargs): +def makeExtension(*args, **kwargs): # pragma: no cover """ Return an instance of the FootnoteExtension """ return FootnoteExtension(*args, **kwargs) diff --git a/markdown/extensions/meta.py b/markdown/extensions/meta.py index 7902bd4..33e13fb 100644 --- a/markdown/extensions/meta.py +++ b/markdown/extensions/meta.py @@ -74,5 +74,5 @@ class MetaPreprocessor(Preprocessor): return lines -def makeExtension(*args, **kwargs): +def makeExtension(*args, **kwargs): # pragma: no cover return MetaExtension(*args, **kwargs) diff --git a/markdown/extensions/nl2br.py b/markdown/extensions/nl2br.py index a4c0fab..1d23c99 100644 --- a/markdown/extensions/nl2br.py +++ b/markdown/extensions/nl2br.py @@ -31,5 +31,5 @@ class Nl2BrExtension(Extension): md.inlinePatterns.add('nl', br_tag, '_end') -def makeExtension(*args, **kwargs): +def makeExtension(*args, **kwargs): # pragma: no cover return Nl2BrExtension(*args, **kwargs) diff --git a/markdown/extensions/sane_lists.py b/markdown/extensions/sane_lists.py index 651b12b..4b118af 100644 --- a/markdown/extensions/sane_lists.py +++ b/markdown/extensions/sane_lists.py @@ -51,5 +51,5 @@ class SaneListExtension(Extension): md.parser.blockprocessors['ulist'] = SaneUListProcessor(md.parser) -def makeExtension(*args, **kwargs): +def makeExtension(*args, **kwargs): # pragma: no cover return SaneListExtension(*args, **kwargs) diff --git a/markdown/extensions/smart_strong.py b/markdown/extensions/smart_strong.py index 9349f41..7070318 100644 --- a/markdown/extensions/smart_strong.py +++ b/markdown/extensions/smart_strong.py @@ -37,5 +37,5 @@ class SmartEmphasisExtension(Extension): ) -def makeExtension(*args, **kwargs): +def makeExtension(*args, **kwargs): # pragma: no cover return SmartEmphasisExtension(*args, **kwargs) diff --git a/markdown/extensions/smarty.py b/markdown/extensions/smarty.py index ba5b6b6..6add986 100644 --- a/markdown/extensions/smarty.py +++ b/markdown/extensions/smarty.py @@ -264,5 +264,5 @@ class SmartyExtension(Extension): md.ESCAPED_CHARS.extend(['"', "'"]) -def makeExtension(*args, **kwargs): +def makeExtension(*args, **kwargs): # pragma: no cover return SmartyExtension(*args, **kwargs) diff --git a/markdown/extensions/tables.py b/markdown/extensions/tables.py index 6d28aef..dc459f3 100644 --- a/markdown/extensions/tables.py +++ b/markdown/extensions/tables.py @@ -223,5 +223,5 @@ class TableExtension(Extension): '<hashheader') -def makeExtension(*args, **kwargs): +def makeExtension(*args, **kwargs): # pragma: no cover return TableExtension(*args, **kwargs) diff --git a/markdown/extensions/toc.py b/markdown/extensions/toc.py index f884f9b..d2ea45e 100644 --- a/markdown/extensions/toc.py +++ b/markdown/extensions/toc.py @@ -178,7 +178,7 @@ class TocTreeprocessor(Treeprocessor): c.text = "" for elem in c: anchor.append(elem) - while c: + while len(c): c.remove(c[0]) c.append(anchor) @@ -304,5 +304,5 @@ class TocExtension(Extension): self.md.toc = '' -def makeExtension(*args, **kwargs): +def makeExtension(*args, **kwargs): # pragma: no cover return TocExtension(*args, **kwargs) diff --git a/markdown/extensions/wikilinks.py b/markdown/extensions/wikilinks.py index ff16494..3f40608 100644 --- a/markdown/extensions/wikilinks.py +++ b/markdown/extensions/wikilinks.py @@ -85,5 +85,5 @@ class WikiLinks(Pattern): return base_url, end_url, html_class -def makeExtension(*args, **kwargs): +def makeExtension(*args, **kwargs): # pragma: no cover return WikiLinkExtension(*args, **kwargs) diff --git a/tests/test_apis.py b/tests/test_apis.py index 42e7496..aa43e52 100644 --- a/tests/test_apis.py +++ b/tests/test_apis.py @@ -17,10 +17,17 @@ from markdown.__main__ import parse_options from logging import DEBUG, WARNING, CRITICAL import yaml import tempfile +from io import BytesIO + PY3 = sys.version_info[0] == 3 +if not PY3: + def bytes(string, encoding): + return string.encode(encoding) + + class TestMarkdownBasics(unittest.TestCase): """ Tests basics of the Markdown class. """ @@ -53,11 +60,50 @@ class TestMarkdownBasics(unittest.TestCase): """ Test Extension loading with Name (`path.to.module`). """ markdown.Markdown(extensions=['markdown.extensions.footnotes']) - def TestDotNotationExtensionWithClass(self): + def testDotNotationExtensionWithClass(self): """ Test Extension loading with class name (`path.to.module:Class`). """ markdown.Markdown(extensions=['markdown.extensions.footnotes:FootnoteExtension']) +class TestConvertFile(unittest.TestCase): + """ Tests of ConvertFile. """ + + def setUp(self): + self.saved = sys.stdin, sys.stdout + sys.stdin = BytesIO(bytes('foo', encoding='utf-8')) + sys.stdout = BytesIO() + + def tearDown(self): + sys.stdin, sys.stdout = self.saved + + def getTempFiles(self, src): + """ Return the file names for two temp files. """ + infd, infile = tempfile.mkstemp(suffix='.txt') + with os.fdopen(infd, 'w') as fp: + fp.write(src) + outfd, outfile = tempfile.mkstemp(suffix='.html') + return infile, outfile, outfd + + def testFileNames(self): + infile, outfile, outfd = self.getTempFiles('foo') + markdown.markdownFromFile(input=infile, output=outfile) + with os.fdopen(outfd, 'r') as fp: + output = fp.read() + self.assertEqual(output, '<p>foo</p>') + + def testFileObjects(self): + infile = BytesIO(bytes('foo', encoding='utf-8')) + outfile = BytesIO() + markdown.markdownFromFile(input=infile, output=outfile) + outfile.seek(0) + self.assertEqual(outfile.read().decode('utf-8'), '<p>foo</p>') + + def testStdinStdout(self): + markdown.markdownFromFile() + sys.stdout.seek(0) + self.assertEqual(sys.stdout.read().decode('utf-8'), '<p>foo</p>') + + class TestBlockParser(unittest.TestCase): """ Tests of the BlockParser class. """ @@ -316,7 +362,7 @@ class TestErrors(unittest.TestCase): def testNonUnicodeSource(self): """ Test falure on non-unicode source text. """ - if sys.version_info < (3, 0): + if not PY3: source = "foo".encode('utf-16') self.assertRaises(UnicodeDecodeError, markdown.markdown, source) @@ -339,6 +385,14 @@ class TestErrors(unittest.TestCase): """ Test loading a non Extension object as an extension. """ self.assertRaises(TypeError, markdown.Markdown, extensions=[object]) + def testDotNotationExtensionWithBadClass(self): + """ Test Extension loading with non-existant class name (`path.to.module:Class`). """ + self.assertRaises( + AttributeError, + markdown.Markdown, + extensions=['markdown.extensions.footnotes:MissingExtension'] + ) + def testBaseExtention(self): """ Test that the base Extension class will raise NotImplemented. """ self.assertRaises( |