From 9a4fcb67b13e19882d68ec1a1db9acf489fc81df Mon Sep 17 00:00:00 2001 From: CPardi Date: Tue, 29 Mar 2016 23:22:32 +0100 Subject: [PATCH 1/2] Removed dependency on re.Scanner for compatability with IronPython. --- markdown/extensions/attr_list.py | 50 +++++++++++++++++++++++--------- 1 file changed, 37 insertions(+), 13 deletions(-) diff --git a/markdown/extensions/attr_list.py b/markdown/extensions/attr_list.py index 683bdf831..47de8f8b2 100644 --- a/markdown/extensions/attr_list.py +++ b/markdown/extensions/attr_list.py @@ -24,38 +24,62 @@ from ..util import isBlockLevel import re -try: - Scanner = re.Scanner -except AttributeError: # pragma: no cover - # must be on Python 2.4 - from sre import Scanner - - -def _handle_double_quote(s, t): +def _handle_double_quote(t): k, v = t.split('=') return k, v.strip('"') -def _handle_single_quote(s, t): +def _handle_single_quote(t): k, v = t.split('=') return k, v.strip("'") -def _handle_key_value(s, t): +def _handle_key_value(t): return t.split('=') -def _handle_word(s, t): +def _handle_word(t): if t.startswith('.'): return '.', t[1:] if t.startswith('#'): return 'id', t[1:] return t, t -_scanner = Scanner([ +# Simplified replacement class of re.Scanner that appears in Cython, +# but not IronPython. +class InternalScanner: + # * Create a regex string that matches each phrase as a seperate + # group. + # * Create an array of action functions. + # * The index of each phrase's match group, is equal to the index + # of the corresponding action in the action array. + def __init__(self, lexicon): + self.actions = [] + regex_string = "" + for (phrase, action) in lexicon: + if(regex_string != ""): regex_string += "|" + regex_string += "(" + phrase + ")" + self.actions.append(action) + self.regex = re.compile(regex_string) + # Search each found match. + # Find the match group . + # Perform the corresponding action. + # and add to results list. + def scan(self, string): + result = [] + for match in self.regex.finditer(string): + num = -1 + for (group) in match.groups(): + num += 1 + if(group != None and self.actions[num] != None): + result.append(self.actions[num](group)) + return result + +_scanner = InternalScanner([ (r'[^ ]+=".*?"', _handle_double_quote), (r"[^ ]+='.*?'", _handle_single_quote), (r'[^ ]+=[^ =]+', _handle_key_value), + (r'\=[^ ]+', None), # Ignore the case '=foo'. (r'[^ =]+', _handle_word), (r' ', None) ]) @@ -63,7 +87,7 @@ def _handle_word(s, t): def get_attrs(str): """ Parse attribute list and return a list of attribute tuples. """ - return _scanner.scan(str)[0] + return _scanner.scan(str) def isheader(elem): From 3792b7a12ef6a0f2ee85118ce1b989a80c9f7aa1 Mon Sep 17 00:00:00 2001 From: CPardi Date: Wed, 30 Mar 2016 16:17:06 +0100 Subject: [PATCH 2/2] flake8 cleanup. --- markdown/extensions/attr_list.py | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/markdown/extensions/attr_list.py b/markdown/extensions/attr_list.py index 47de8f8b2..217b70b6d 100644 --- a/markdown/extensions/attr_list.py +++ b/markdown/extensions/attr_list.py @@ -24,6 +24,7 @@ from ..util import isBlockLevel import re + def _handle_double_quote(t): k, v = t.split('=') return k, v.strip('"') @@ -45,22 +46,25 @@ def _handle_word(t): return 'id', t[1:] return t, t + # Simplified replacement class of re.Scanner that appears in Cython, # but not IronPython. class InternalScanner: - # * Create a regex string that matches each phrase as a seperate - # group. - # * Create an array of action functions. - # * The index of each phrase's match group, is equal to the index + # * Create a regex string that matches each phrase as a seperate + # group. + # * Create an array of action functions. + # * The index of each phrase's match group, is equal to the index # of the corresponding action in the action array. def __init__(self, lexicon): self.actions = [] regex_string = "" for (phrase, action) in lexicon: - if(regex_string != ""): regex_string += "|" + if(regex_string != ""): + regex_string += "|" regex_string += "(" + phrase + ")" self.actions.append(action) self.regex = re.compile(regex_string) + # Search each found match. # Find the match group . # Perform the corresponding action. @@ -71,15 +75,15 @@ def scan(self, string): num = -1 for (group) in match.groups(): num += 1 - if(group != None and self.actions[num] != None): + if(group is not None and self.actions[num] is not None): result.append(self.actions[num](group)) - return result + return result _scanner = InternalScanner([ (r'[^ ]+=".*?"', _handle_double_quote), (r"[^ ]+='.*?'", _handle_single_quote), (r'[^ ]+=[^ =]+', _handle_key_value), - (r'\=[^ ]+', None), # Ignore the case '=foo'. + (r'\=[^ ]+', None), # Ignore the case '=foo'. (r'[^ =]+', _handle_word), (r' ', None) ])