From 5289a6abbb7aba4feac015cc27057c88a2fbcb75 Mon Sep 17 00:00:00 2001 From: Waylan Limberg Date: Mon, 5 Nov 2007 03:40:00 +0000 Subject: Created a RawHtmlTextPreprocessor and moved code that restores/strips raw html from the convert method to that TextPreprocessor. It should now be rather simple to write an extension to perhapsescape the html instead. --- markdown.py | 41 +++++++++++++++++++++++++---------------- 1 file changed, 25 insertions(+), 16 deletions(-) (limited to 'markdown.py') diff --git a/markdown.py b/markdown.py index f4e159c..211ccfd 100644 --- a/markdown.py +++ b/markdown.py @@ -889,6 +889,25 @@ class Postprocessor : pass +class RawHtmlTextPostprocessor(Postprocessor) : + + def __init__(self): + pass + + def run(self, text): + for i in range(self.stash.html_counter) : + html = self.stash.rawHtmlBlocks[i][0] + safe = self.stash.rawHtmlBlocks[i][1] + if self.safeMode and not safe: + html = HTML_REMOVED_TEXT + + text = text.replace("

%s\n

" % (HTML_PLACEHOLDER % i), + html + "\n") + text = text.replace(HTML_PLACEHOLDER % i, html) + return text + +RAWHTMLTEXTPOSTPROCESSOR = RawHtmlTextPostprocessor() + """ ====================================================================== ========================== MISC AUXILIARY CLASSES ==================== @@ -1083,8 +1102,9 @@ class Markdown: self.postprocessors = [] # a footnote postprocessor will get # inserted later - self.textPostprocessors = [] # a footnote postprocessor will get - # inserted later + self.textPostprocessors = [ # a footnote postprocessor will get + # inserted here + RAWHTMLTEXTPOSTPROCESSOR ] self.prePatterns = [] @@ -1164,6 +1184,8 @@ class Markdown: ENTITY_PATTERN.stash = self.htmlStash REFERENCE_PATTERN.references = self.references IMAGE_REFERENCE_PATTERN.references = self.references + RAWHTMLTEXTPOSTPROCESSOR.stash = self.htmlStash + RAWHTMLTEXTPOSTPROCESSOR.safeMode = self.safeMode for extension in self.registeredExtensions : extension.reset() @@ -1645,20 +1667,7 @@ class Markdown: #finally: # doc.unlink() - # Let's stick in all the raw html pieces - - for i in range(self.htmlStash.html_counter) : - html = self.htmlStash.rawHtmlBlocks[i][0] - safe = self.htmlStash.rawHtmlBlocks[i][1] - if self.safeMode and not safe: - html = HTML_REMOVED_TEXT - - xml = xml.replace("

%s\n

" % (HTML_PLACEHOLDER % i), - html + "\n") - xml = xml.replace(HTML_PLACEHOLDER % i, - html) - - # And return everything but the top level tag + # Return everything but the top level tag if self.stripTopLevelTags : xml = xml.strip()[23:-7] + "\n" -- cgit v1.2.3