diff options
author | Waylan Limberg <waylan@gmail.com> | 2010-07-06 14:09:02 -0400 |
---|---|---|
committer | Waylan Limberg <waylan@gmail.com> | 2010-07-06 14:09:02 -0400 |
commit | 2b6754adb0f43e1853726f2c936dbf81c1b40736 (patch) | |
tree | 8a34ee9497053a89b67cefb18e4197649220951d | |
parent | 543027d73f2d9bf66fc8b56c18acfd1c987b1a8d (diff) | |
download | markdown-2b6754adb0f43e1853726f2c936dbf81c1b40736.tar.gz markdown-2b6754adb0f43e1853726f2c936dbf81c1b40736.tar.bz2 markdown-2b6754adb0f43e1853726f2c936dbf81c1b40736.zip |
Moved HtmlStash and base Prosessor classes to the new util module.
-rw-r--r-- | markdown/__init__.py | 2 | ||||
-rw-r--r-- | markdown/postprocessors.py | 13 | ||||
-rw-r--r-- | markdown/preprocessors.py | 44 | ||||
-rw-r--r-- | markdown/util.py | 46 | ||||
-rw-r--r-- | tests/test_apis.py | 6 |
5 files changed, 53 insertions, 58 deletions
diff --git a/markdown/__init__.py b/markdown/__init__.py index 8658e60..88f805b 100644 --- a/markdown/__init__.py +++ b/markdown/__init__.py @@ -217,7 +217,7 @@ class Markdown: } self.references = {} - self.htmlStash = preprocessors.HtmlStash() + self.htmlStash = util.HtmlStash() self.registerExtensions(extensions = extensions, configs = extension_configs) self.set_output_format(output_format) diff --git a/markdown/postprocessors.py b/markdown/postprocessors.py index 4ce5324..b4b36de 100644 --- a/markdown/postprocessors.py +++ b/markdown/postprocessors.py @@ -8,16 +8,9 @@ processing. """ - import util -import preprocessors - -class Processor: - def __init__(self, markdown_instance=None): - if markdown_instance: - self.markdown = markdown_instance -class Postprocessor(Processor): +class Postprocessor(util.Processor): """ Postprocessors are run after the ElementTree it converted back into text. @@ -55,9 +48,9 @@ class RawHtmlPostprocessor(Postprocessor): html = util.HTML_REMOVED_TEXT if safe or not self.markdown.safeMode: text = text.replace("<p>%s</p>" % - (preprocessors.HTML_PLACEHOLDER % i), + (util.HTML_PLACEHOLDER % i), html + "\n") - text = text.replace(preprocessors.HTML_PLACEHOLDER % i, + text = text.replace(util.HTML_PLACEHOLDER % i, html) return text diff --git a/markdown/preprocessors.py b/markdown/preprocessors.py index 3b19953..567621b 100644 --- a/markdown/preprocessors.py +++ b/markdown/preprocessors.py @@ -8,18 +8,10 @@ complicated. """ import re - import util -HTML_PLACEHOLDER_PREFIX = util.STX+"wzxhzdk:" -HTML_PLACEHOLDER = HTML_PLACEHOLDER_PREFIX + "%d" + util.ETX - -class Processor: - def __init__(self, markdown_instance=None): - if markdown_instance: - self.markdown = markdown_instance -class Preprocessor (Processor): +class Preprocessor(util.Processor): """ Preprocessors are run after the text is broken into lines. @@ -39,40 +31,6 @@ class Preprocessor (Processor): """ pass -class HtmlStash: - """ - This class is used for stashing HTML objects that we extract - in the beginning and replace with place-holders. - """ - - def __init__ (self): - """ Create a HtmlStash. """ - self.html_counter = 0 # for counting inline html segments - self.rawHtmlBlocks=[] - - def store(self, html, safe=False): - """ - Saves an HTML segment for later reinsertion. Returns a - placeholder string that needs to be inserted into the - document. - - Keyword arguments: - - * html: an html segment - * safe: label an html segment as safe for safemode - - Returns : a placeholder string - - """ - self.rawHtmlBlocks.append((html, safe)) - placeholder = HTML_PLACEHOLDER % self.html_counter - self.html_counter += 1 - return placeholder - - def reset(self): - self.html_counter = 0 - self.rawHtmlBlocks = [] - class HtmlBlockPreprocessor(Preprocessor): """Remove html blocks from the text and store them for later retrieval.""" diff --git a/markdown/util.py b/markdown/util.py index c170368..d1485db 100644 --- a/markdown/util.py +++ b/markdown/util.py @@ -33,7 +33,8 @@ ETX = u'\u0003' # Use ETX ("End of text") for end-of-placeholder INLINE_PLACEHOLDER_PREFIX = STX+"klzzwxh:" INLINE_PLACEHOLDER = INLINE_PLACEHOLDER_PREFIX + "%s" + ETX AMP_SUBSTITUTE = STX+"amp"+ETX - +HTML_PLACEHOLDER_PREFIX = STX+"wzxhzdk:" +HTML_PLACEHOLDER = HTML_PLACEHOLDER_PREFIX + "%d" + ETX """ Constants you probably do not need to change @@ -70,3 +71,46 @@ MISC AUXILIARY CLASSES class AtomicString(unicode): """A string which should not be further processed.""" pass + + +class Processor: + def __init__(self, markdown_instance=None): + if markdown_instance: + self.markdown = markdown_instance + + +class HtmlStash: + """ + This class is used for stashing HTML objects that we extract + in the beginning and replace with place-holders. + """ + + def __init__ (self): + """ Create a HtmlStash. """ + self.html_counter = 0 # for counting inline html segments + self.rawHtmlBlocks=[] + + def store(self, html, safe=False): + """ + Saves an HTML segment for later reinsertion. Returns a + placeholder string that needs to be inserted into the + document. + + Keyword arguments: + + * html: an html segment + * safe: label an html segment as safe for safemode + + Returns : a placeholder string + + """ + self.rawHtmlBlocks.append((html, safe)) + placeholder = HTML_PLACEHOLDER % self.html_counter + self.html_counter += 1 + return placeholder + + def reset(self): + self.html_counter = 0 + self.rawHtmlBlocks = [] + + diff --git a/tests/test_apis.py b/tests/test_apis.py index 3aaa2f9..760afa8 100644 --- a/tests/test_apis.py +++ b/tests/test_apis.py @@ -96,13 +96,13 @@ class TestHtmlStash(unittest.TestCase): """ Test Markdown's HtmlStash. """ def setUp(self): - self.stash = markdown.preprocessors.HtmlStash() + self.stash = markdown.util.HtmlStash() self.placeholder = self.stash.store('foo') def testSimpleStore(self): """ Test HtmlStash.store. """ self.assertEqual(self.placeholder, - markdown.preprocessors.HTML_PLACEHOLDER % 0) + markdown.util.HTML_PLACEHOLDER % 0) self.assertEqual(self.stash.html_counter, 1) self.assertEqual(self.stash.rawHtmlBlocks, [('foo', False)]) @@ -110,7 +110,7 @@ class TestHtmlStash(unittest.TestCase): """ Test HtmlStash.store with additional blocks. """ placeholder = self.stash.store('bar') self.assertEqual(placeholder, - markdown.preprocessors.HTML_PLACEHOLDER % 1) + markdown.util.HTML_PLACEHOLDER % 1) self.assertEqual(self.stash.html_counter, 2) self.assertEqual(self.stash.rawHtmlBlocks, [('foo', False), ('bar', False)]) |