From 57539d69e8ee1e86bd14c9e51273e576b2e38438 Mon Sep 17 00:00:00 2001 From: Artem Yunusov Date: Tue, 15 Jul 2008 18:18:09 +0500 Subject: Output bug fixed(stripping text data at the end of processing in _processPlaceholders). Ticket #5 fixed. --- markdown.py | 120 ++++++++++++++++++++++++++++++++---------------------------- 1 file changed, 64 insertions(+), 56 deletions(-) (limited to 'markdown.py') diff --git a/markdown.py b/markdown.py index 032e084..d1ffce6 100644 --- a/markdown.py +++ b/markdown.py @@ -151,6 +151,9 @@ EXECUTABLE_NAME_FOR_USAGE = "python markdown.py" AND_SUBSTITUTE = unichr(2) + unichr(4) + unichr(3) +INLINE_PLACEHOLDER_PREFIX = u'\u0001' +INLINE_PLACEHOLDER_SUFFIX = u'\u0002' + # a template for html placeholders HTML_PLACEHOLDER_PREFIX = "qaodmasdkwaspemas" HTML_PLACEHOLDER = HTML_PLACEHOLDER_PREFIX + "%dajkqlsmdqpakldnzsdfls" @@ -504,7 +507,7 @@ BRK = ( r'\[(' + NOBRACKET + r')\]' ) NOIMG = r'(?` Element containing the matching text. """ - def __init__ (self, pattern): - Pattern.__init__(self, pattern) + def __init__ (self, pattern, contentGroup=2): + Pattern.__init__(self, pattern, contentGroup) self.tag = "code" def handleMatch(self, m): el = etree.Element(self.tag) - el.text = m.group(2).strip() + el.text = m.group(self.contentGroup).strip() return el @@ -783,7 +791,7 @@ class AutomailPattern (Pattern): ESCAPE_PATTERN = SimpleTextPattern(ESCAPE_RE) NOT_STRONG_PATTERN = SimpleTextPattern(NOT_STRONG_RE) -BACKTICK_PATTERN = BacktickPattern(BACKTICK_RE) +BACKTICK_PATTERN = BacktickPattern(BACKTICK_RE, 3) DOUBLE_BACKTICK_PATTERN = BacktickPattern(DOUBLE_BACKTICK_RE) STRONG_PATTERN = SimpleTagPattern(STRONG_RE, 'strong') STRONG_PATTERN_2 = SimpleTagPattern(STRONG_2_RE, 'strong') @@ -1048,8 +1056,8 @@ def dequote(string): class InlineStash: def __init__(self): - self.prefix = u'\u0001' - self.suffix = u'\u0002' + self.prefix = INLINE_PLACEHOLDER_PREFIX + self.suffix = INLINE_PLACEHOLDER_SUFFIX self._nodes = {} self.phLength = 4 + len(self.prefix) + len(self.suffix) @@ -1645,6 +1653,49 @@ class Markdown: patternIndex += 1 return data + + def _applyInline(self, pattern, data, patternIndex): + """ + Given a pattern name, this function checks if the line + fits the pattern, creates the necessary elements, adds it + to InlineStash, and returns string with placeholders, + instead of ElementTree elements. + + Keyword arguments: + + * data: the text to be processed + * pattern: the pattern to be checked + * patternIndex: index of current pattern + + Returns: String with placeholders. + """ + + match = pattern.getCompiledRegExp().match(data) + + if not match: + return data, False + + node = pattern.handleMatch(match) + + if node is None: + return data, False + if not isstr(node): + if not node.tag in ["code", "pre"]: + # We need to process current node too + for child in [node] + node.getchildren(): + if not isstr(node): + if child.text: + child.text = self._handleInline(child.text, + patternIndex) + if child.tail: + child.tail = self._handleInline(child.tail, + patternIndex) + + pholder = self.inlineStash.add(node, pattern.type()) + left = "".join([match.group(i) for i in xrange(1, + pattern.contentGroup)]) + + return "%s%s%s" % (left, pholder, match.groups()[-1]), True def _processElementText(self, node, subnode, isText=True): @@ -1692,16 +1743,16 @@ class Markdown: result = [] prefix = self.inlineStash.prefix strartIndex = 0 - + while data: index = data.find(prefix, strartIndex) if index != -1: id, phEndIndex = self.inlineStash.extractId(data, index) - + if self.inlineStash.isin(id): - + node = self.inlineStash.get(id) if index > 0: @@ -1733,55 +1784,12 @@ class Markdown: strartIndex = end else: - text = data[strartIndex:].strip() + text = data[strartIndex:] linkText(text) data = "" return result - - - def _applyInline(self, pattern, data, patternIndex): - """ - Given a pattern name, this function checks if the line - fits the pattern, creates the necessary elements, adds it - to InlineStash, and returns string with placeholders, - instead of ElementTree elements. - - Keyword arguments: - - * data: the text to be processed - * pattern: the pattern to be checked - * patternIndex: index of current pattern - - Returns: String with placeholders. - """ - - match = pattern.getCompiledRegExp().match(data) - - if not match: - return data, False - - node = pattern.handleMatch(match) - - if node is None: - return data, False - if not isstr(node): - if not node.tag in ["code", "pre"]: - # We need to process current node too - for child in [node] + node.getchildren(): - if not isstr(node): - if child.text: - child.text = self._handleInline(child.text, - patternIndex) - if child.tail: - child.tail = self._handleInline(child.tail, - patternIndex) - - pholder = self.inlineStash.add(node, pattern.type()) - - return "%s%s%s" % (match.group(1), pholder, match.groups()[-1]), True - def _processTree(self, el): """ Processing ElementTree, and applying inline patterns -- cgit v1.2.3