From 503c9d416ef1ec7f6d820290c28d6ca1906cb8a0 Mon Sep 17 00:00:00 2001 From: Waylan Limberg Date: Thu, 11 Oct 2007 01:02:30 +0000 Subject: Replace LineBreaks preprocessor with an inlinePattern and update tests. Blank lines (" \n") and headers no longer get linebreaks and the two spaces are no longer preserved. --- markdown.py | 31 ++++++++++------------ .../markdown-documentation-basics.html | 9 ++----- tests/misc/headers.html | 8 ++---- tests/misc/html.html | 7 ++--- tests/misc/some-test.html | 13 +++++---- tests/misc/two-spaces.html | 13 ++++----- 6 files changed, 31 insertions(+), 50 deletions(-) diff --git a/markdown.py b/markdown.py index fbaeb84..3042138 100644 --- a/markdown.py +++ b/markdown.py @@ -332,7 +332,7 @@ class Element : buffer += childBuffer - if self.nodeName in ['p', 'li', 'ul', 'ol', + if self.nodeName in ['p', 'br ', 'li', 'ul', 'ol', 'h1', 'h2', 'h3', 'h4'] : buffer += "\n" @@ -464,19 +464,6 @@ class LinePreprocessor (Preprocessor): LINE_PREPROCESSOR = LinePreprocessor() -class LineBreaksPreprocessor (Preprocessor): - """Replaces double spaces at the end of the lines with
.""" - - def run (self, lines) : - for i in range(len(lines)) : - if (lines[i].endswith(" ") - and not RE.regExp['tabbed'].match(lines[i]) ): - lines[i] += self.stash.store("
", safe=True) - return lines - -LINE_BREAKS_PREPROCESSOR = LineBreaksPreprocessor() - - class HtmlBlockPreprocessor (Preprocessor): """Removes html blocks from self.lines""" @@ -678,6 +665,8 @@ AUTOMAIL_RE = r'<([^> \!]*@[^> ]*)>' # #HTML_RE = r'(\<[^\>]*\>)' # <...> HTML_RE = r'(\<[a-zA-Z/][^\>]*\>)' # <...> ENTITY_RE = r'(&[\#a-zA-Z0-9]*;)' # & +LINE_BREAK_RE = r' \n' # two spaces at end of line +LINE_BREAK_2_RE = r' $' # two spaces at end of text class Pattern: @@ -706,6 +695,11 @@ class SimpleTagPattern (Pattern): el.appendChild(doc.createTextNode(m.group(2))) return el +class SubstituteTagPattern (SimpleTagPattern): + + def handleMatch (self, m, doc) : + return doc.createElement(self.tag) + class BacktickPattern (Pattern): def __init__ (self, pattern): @@ -856,6 +850,9 @@ EMPHASIS_PATTERN_2 = SimpleTagPattern(EMPHASIS_2_RE, 'em') STRONG_EM_PATTERN = DoubleTagPattern(STRONG_EM_RE, 'strong,em') STRONG_EM_PATTERN_2 = DoubleTagPattern(STRONG_EM_2_RE, 'strong,em') +LINE_BREAK_PATTERN = SubstituteTagPattern(LINE_BREAK_RE, 'br ') +LINE_BREAK_PATTERN_2 = SubstituteTagPattern(LINE_BREAK_2_RE, 'br ') + LINK_PATTERN = LinkPattern(LINK_RE) LINK_ANGLED_PATTERN = LinkPattern(LINK_ANGLED_RE) IMAGE_LINK_PATTERN = ImagePattern(IMAGE_LINK_RE) @@ -1023,7 +1020,7 @@ class CorePatterns : effort.""" patterns = { - 'header': r'(#*)([^#]*)(#*)', # # A title + 'header': r'(#*)\s*([^#]*)(#*)', # # A title 'reference-def' : r'(\ ?\ ?\ ?)\[([^\]]*)\]:\s*([^ ]*)(.*)', # [Google]: http://www.google.com/ 'containsline': r'([-]*)$|^([=]*)', # -----, =====, etc. @@ -1075,7 +1072,6 @@ class Markdown: self.preprocessors = [ HEADER_PREPROCESSOR, LINE_PREPROCESSOR, - LINE_BREAKS_PREPROCESSOR, # A footnote preprocessor will # get inserted here REFERENCE_PREPROCESSOR ] @@ -1100,6 +1096,8 @@ class Markdown: LINK_PATTERN, AUTOLINK_PATTERN, AUTOMAIL_PATTERN, + LINE_BREAK_PATTERN_2, + LINE_BREAK_PATTERN, HTML_PATTERN, ENTITY_PATTERN, NOT_STRONG_PATTERN, @@ -1158,7 +1156,6 @@ class Markdown: HTML_BLOCK_PREPROCESSOR.stash = self.htmlStash LINE_PREPROCESSOR.stash = self.htmlStash - LINE_BREAKS_PREPROCESSOR.stash = self.htmlStash REFERENCE_PREPROCESSOR.references = self.references HTML_PATTERN.stash = self.htmlStash ENTITY_PATTERN.stash = self.htmlStash diff --git a/tests/markdown-test/markdown-documentation-basics.html b/tests/markdown-test/markdown-documentation-basics.html index fdfc33b..1dafe24 100644 --- a/tests/markdown-test/markdown-documentation-basics.html +++ b/tests/markdown-test/markdown-documentation-basics.html @@ -1,5 +1,3 @@ - -

Markdown: Basics