From 952d12302ab156cc9b289c45a96d0e3f0e7766e0 Mon Sep 17 00:00:00 2001 From: Waylan Limberg Date: Mon, 20 Oct 2008 10:11:34 -0400 Subject: Combined the TextPreprocessors and Preprocessors into Preprocessors. Updated extensions and docs as well. --- docs/writing_extensions.txt | 29 +-------------------------- markdown.py | 41 +++++++------------------------------- markdown_extensions/fenced_code.py | 9 +++++---- 3 files changed, 13 insertions(+), 66 deletions(-) diff --git a/docs/writing_extensions.txt b/docs/writing_extensions.txt index 617b92e..9d6fa42 100755 --- a/docs/writing_extensions.txt +++ b/docs/writing_extensions.txt @@ -19,8 +19,6 @@ helpful as well. For example, the [[Footnotes]] extension uses most of the features documented here. * [Preprocessors][] - * [TextPreprocessors][] - * [Line Preprocessors][] * [InlinePatterns][] * [Treeprocessors][] * [Postprocessors][] @@ -39,30 +37,7 @@ Preprocessors munge the source text before it is passed into the Markdown core. This is an excellent place to clean up bad syntax, extract things the parser may otherwise choke on and perhaps even store it for later retrieval. -There are two types of preprocessors: [TextPreprocessors][] and -[Line Preprocessors][]. - -

TextPreprocessors

- -TextPreprocessors should inherit from ``markdown.TextPreprocessor`` and -implement a ``run`` method with one argument ``text``. The ``run`` method of -each TextPreprocessor will be passed the entire source text as a single Unicode -string and should either return that single Unicode string, or an altered -version of it. - -For example, a simple TextPreprocessor that normalizes newlines [^1] might look -like this: - - class NormalizePreprocessor(markdown.TextPreprocessor): - def run(self, text): - return text.replace("\r\n", "\n").replace("\r", "\n") - -[^1]: It should be noted that Markdown already normalizes newlines. This -example is for illustrative purposes only. - -

Line Preprocessors

- -Line Preprocessors should inherit from ``markdown.Preprocessor`` and implement +Preprocessors should inherit from ``markdown.Preprocessor`` and implement a ``run`` method with one argument ``lines``. The ``run`` method of each Line Preprocessor will be passed the entire source text as a list of Unicode strings. Each string will contain one line of text. The ``run`` method should return @@ -457,8 +432,6 @@ This is useful if you need to implement a large number of extensions with more than one residing in a module. [Preprocessors]: #preprocessors -[TextPreprocessors]: #textpreprocessors -[Line Preprocessors]: #linepreprocessors [InlinePatterns]: #inlinepatterns [Treeprocessors]: #treeprocessors [Postprocessors]: #postprocessors diff --git a/markdown.py b/markdown.py index c16b6d6..a8b4455 100755 --- a/markdown.py +++ b/markdown.py @@ -581,8 +581,7 @@ PRE-PROCESSORS ============================================================================= Preprocessors work on source text before we start doing anything too -complicated. There are two types of preprocessors: TextPreprocessor and -Preprocessor. +complicated. """ class Processor: @@ -590,27 +589,6 @@ class Processor: if markdown_instance: self.markdown = markdown_instance -class TextPreprocessor (Processor): - """ - TextPreprocessors are run before the text is broken into lines. - - Each TextPreprocessor implements a "run" method that takes a pointer to a - text string of the document, modifies it as necessary and returns - either the same pointer or a pointer to a new string. - - TextPreprocessors must extend markdown.TextPreprocessor. - - """ - - def run(self, text): - """ - Each subclass of TextPreprocessor should override the `run` method, - which takes the document text as a single string and returns the - (possibly modified) document as a single string. - - """ - pass - class Preprocessor (Processor): """ @@ -633,7 +611,7 @@ class Preprocessor (Processor): pass -class HtmlBlockPreprocessor(TextPreprocessor): +class HtmlBlockPreprocessor(Preprocessor): """Remove html blocks from the text and store them for later retrieval.""" right_tag_patterns = ["", "%s>"] @@ -665,7 +643,8 @@ class HtmlBlockPreprocessor(TextPreprocessor): def _is_oneliner(self, tag): return (tag in ['hr', 'hr/']) - def run(self, text): + def run(self, lines): + text = "\n".join(lines) new_blocks = [] text = text.split("\n\n") items = [] @@ -742,7 +721,8 @@ class HtmlBlockPreprocessor(TextPreprocessor): new_blocks.append(self.markdown.htmlStash.store('\n\n'.join(items))) new_blocks.append('\n') - return "\n\n".join(new_blocks) + new_text = "\n\n".join(new_blocks) + return new_text.split("\n") class HeaderPreprocessor(Preprocessor): @@ -1781,12 +1761,9 @@ class Markdown: self.docType = "" self.stripTopLevelTags = True - self.textPreprocessors = Treap() - self.textPreprocessors.add("html_block", - HtmlBlockPreprocessor(self)) self.preprocessors = Treap() + self.preprocessors.add("html_block", HtmlBlockPreprocessor(self)) self.preprocessors.add("header", HeaderPreprocessor(self)) - self.preprocessors.add("line", LinePreprocessor(self)) self.preprocessors.add("reference", ReferencePreprocessor(self)) # footnote preprocessor will be inserted with "