diff --git a/markdown/parser/block_parsers.py b/markdown/parser/block_parsers.py index 9e0a359..f5df3df 100644 --- a/markdown/parser/block_parsers.py +++ b/markdown/parser/block_parsers.py @@ -8,8 +8,7 @@ BlankLineElement, AtxHeadingElement, SetextHeadingElement, - ThematicBreakElement, - LinkReferenceDefinitions) + ThematicBreakElement) if sys.version_info[0] >= 3: @@ -26,9 +25,6 @@ class BlockElementParser(object): AUX_UNCLOSED = 'unclosed' # The last unclosed element. AUX_INTERRUPT = 'interrupt' # Whether it is interrupting a paragraph. - def __init__(self, config): - self._config = config - @staticmethod def check_indent(code, index, align=0): """Check whether the number of heading spaces is less than 4. @@ -48,7 +44,8 @@ def check_indent(code, index, align=0): return False, index return True, index + space_num - def get_unclosed(self, auxiliary): + @staticmethod + def get_unclosed(auxiliary): """Get last unclosed element. Args: @@ -59,11 +56,12 @@ def get_unclosed(self, auxiliary): """ if auxiliary is None: return None - if self.AUX_UNCLOSED not in auxiliary: + if BlockElementParser.AUX_UNCLOSED not in auxiliary: return None - return auxiliary[self.AUX_UNCLOSED] + return auxiliary[BlockElementParser.AUX_UNCLOSED] - def is_interrupting(self, auxiliary): + @staticmethod + def is_interrupting(auxiliary): """Whether it is interrupting a paragraph. Args: @@ -74,9 +72,9 @@ def is_interrupting(self, auxiliary): """ if auxiliary is None: return False - if self.AUX_INTERRUPT not in auxiliary: + if BlockElementParser.AUX_INTERRUPT not in auxiliary: return False - return auxiliary[self.AUX_INTERRUPT] + return auxiliary[BlockElementParser.AUX_INTERRUPT] class ParagraphParser(BlockElementParser): @@ -89,14 +87,12 @@ class ParagraphParser(BlockElementParser): removing initial and final whitespace. """ - def __init__(self, config): - super(ParagraphParser, self).__init__(config) - - def parse(self, code, index, auxiliary=None): - elem = self.get_unclosed(auxiliary) + @staticmethod + def parse(code, index, auxiliary=None): + elem = BlockElementParser.get_unclosed(auxiliary) start = index if elem is None: - success, index = self.check_indent(code, index) + success, index = BlockElementParser.check_indent(code, index) if not success: return None while index < len(code) and code[index] != '\n': @@ -147,12 +143,10 @@ class AtxHeadingParser(BlockElementParser): to the number of # characters in the opening sequence. """ - def __init__(self, config): - super(AtxHeadingParser, self).__init__(config) - - def parse(self, code, index, _=None): + @staticmethod + def parse(code, index, _=None): # 0~3 spaces - success, index = self.check_indent(code, index) + success, index = BlockElementParser.check_indent(code, index) if not success: return None # 1~6 `#`s @@ -200,11 +194,9 @@ class ThematicBreakParser(BlockElementParser): followed optionally by any number of spaces, forms a thematic break. """ - def __init__(self, config): - super(ThematicBreakParser, self).__init__(config) - - def parse(self, code, index, auxiliary=None): - success, index = self.check_indent(code, index) + @staticmethod + def parse(code, index, auxiliary=None): + success, index = BlockElementParser.check_indent(code, index) if not success: return None if index >= len(code): @@ -227,49 +219,6 @@ def parse(self, code, index, auxiliary=None): index += 1 if count < 3: return None - if self.is_interrupting(auxiliary) and first == '-' and not has_inner_space: + if BlockElementParser.is_interrupting(auxiliary) and first == '-' and not has_inner_space: return None return ThematicBreakElement() - - -class LinkReferenceDefinitionsParser(BlockElementParser): - """link reference definitions. - - A link reference definition consists of a link label, indented up - to three spaces, followed by a colon (:), optional whitespace - (including up to one line ending), a link destination, optional - whitespace (including up to one line ending), and an optional link - title, which if it is present must be separated from the link - destination by whitespace. No further non-whitespace characters - may occur on the line. - A link reference definition does not correspond to a structural - element of a document. Instead, it defines a label which can be - used in reference links and reference-style images elsewhere in - the document. Link reference definitions can come either before - or after the links that use them. - """ - - def __init__(self, config): - super(LinkReferenceDefinitionsParser, self).__init__(config) - - def __parse_name(self, index): - frist = index - while index < len(self.code): - if self.code[index] == ']': - name = ''.join(self.code[first + 1: index]) - break - index += 1 - if self.code[index + 1] != ':': - return False, index - arg_name = name.strip() - if not arg_name: - return False, index - return True, index + 1 - - def parse(self, code, index, auxiliary=None): - self.code = code - success, index = self.check_indent(code, index) - if not success or index >= len(code): - return None - first = index - sucess, index = self.__parse_name(first) diff --git a/markdown/parser/container_parsers.py b/markdown/parser/container_parsers.py index 98f0faa..e53136b 100644 --- a/markdown/parser/container_parsers.py +++ b/markdown/parser/container_parsers.py @@ -11,10 +11,7 @@ class ContainerElementParser(object): AUX_ALIGN = 'align' # The align offset for lists. - def __init__(self, config): - super(ContainerElementParser, self).__init__() - self.config = config - + @staticmethod def get_align(self, auxiliary): """ Get the align offset. @@ -27,9 +24,9 @@ def get_align(self, auxiliary): """ if auxiliary is None: return 0 - if self.AUX_ALIGN not in auxiliary: + if ContainerElementParser.AUX_ALIGN not in auxiliary: return 0 - return int(auxiliary[self.AUX_ALIGN]) + return int(auxiliary[ContainerElementParser.AUX_ALIGN]) class BlockQuoteMarkerParser(ContainerElementParser): @@ -40,9 +37,7 @@ class BlockQuoteMarkerParser(ContainerElementParser): or (b) a single character > not followed by a space. """ - def __init__(self, config): - super(BlockQuoteMarkerParser, self).__init__(config) - + @staticmethod def parse(self, code, index, _=None): start = index # 0~3 spaces @@ -69,9 +64,7 @@ class ListMarkerParser(ContainerElementParser): followed by either a . character or a ) character. """ - def __init__(self, config): - super(ListMarkerParser, self).__init__(config) - + @staticmethod def parse(self, code, index, auxiliary=None): start = index # 0~3 spaces diff --git a/markdown/parser/inline_parsers.py b/markdown/parser/inline_parsers.py index 698759c..750ad79 100644 --- a/markdown/parser/inline_parsers.py +++ b/markdown/parser/inline_parsers.py @@ -9,7 +9,8 @@ class InlineParser(object): def __init__(self): pass - def parse(self, code): + @staticmethod + def parse(code): elem = TextualContentElement() elem.subs = [code] return elem diff --git a/markdown/parser/parser.py b/markdown/parser/parser.py index 0125f7c..acba519 100644 --- a/markdown/parser/parser.py +++ b/markdown/parser/parser.py @@ -11,7 +11,8 @@ AtxHeadingParser, ThematicBreakParser) from markdown.parser.block_elements import (ParagraphElement, - BlankLineElement) + BlankLineElement, + ThematicBreakElement) from markdown.parser.inline_parsers import InlineParser from markdown.parser.inline_elements import TextualContentElement @@ -27,14 +28,10 @@ def __init__(self, config=None): self._blocks = [] # The parsed block elements. - self._paragraph_parser = ParagraphParser(config) - self._block_quote_marker_parser = BlockQuoteMarkerParser(config) - self._list_marker_parser = ListMarkerParser(config) self._interrupt_parsers = [] self._container_parsers = [] self._block_parsers = [] - self._inline_parse = InlineParser() - self.init_parsers(config) + self.init_parsers() self._layers = [] self._line_num = 0 @@ -68,45 +65,44 @@ def parse(self, code): self._blocks = [self.parse_subs(block) for block in self._blocks] return self._blocks - def init_parsers(self, config): - thematic_break_parser = ThematicBreakParser(config) - atx_heading_parser = AtxHeadingParser(config) + def init_parsers(self): self._interrupt_parsers = [ - thematic_break_parser, - atx_heading_parser, + ThematicBreakParser, + AtxHeadingParser, # FencedCodeBlockParser(config), # HtmlBlockParser(config) # Type 1-6, ] self._container_parsers = [ - # BlockQuoteParser(config), - # BulletListParser(config), - # OrderedListParser(config), + ThematicBreakParser, + BlockQuoteMarkerParser, + ListMarkerParser, ] self._block_parsers = [ - thematic_break_parser, - atx_heading_parser, - self._paragraph_parser, + ThematicBreakParser, + AtxHeadingParser, + ParagraphParser, ] def parse_container_markers(self, line): - """ index = 0 layer_index = 0 while False: - elem, index = self._block_quote_marker_parser.parse(line, index) + elem, index = BlockQuoteMarkerParser.parse(line, index) + if isinstance(elem, ThematicBreakElement): + elem.line_num = self._line_num + self._blocks.append(elem) + return len(line) if not elem: offset = 0 if layer_index < len(self._layers) and \ isinstance(self._layers[layer_index], BlockQuoteElement): offset = self._layers[layer_index].offset() - elem, index = self._list_marker_parser.parse(line, index, { + elem, index = ListMarkerParser.parse(line, index, { ContainerElementParser.AUX_ALIGN: offset }) if not elem: break return index - """ - return 0 def parse_continuation(self, line, index): """Continue parsing block elements that could span multiple lines. @@ -132,7 +128,7 @@ def parse_continuation(self, line, index): self._blocks.append(elem) break if not has_interrupted: - self._blocks[-1] = self._paragraph_parser.parse(line, index, { + self._blocks[-1] = ParagraphParser.parse(line, index, { BlockElementParser.AUX_UNCLOSED: self._blocks[-1] }) return True @@ -170,7 +166,7 @@ def parse_subs(self, elem): return elem try: for i, sub in enumerate(elem.subs): - elem.subs[i] = self.parse_subs(self._inline_parse.parse(sub)) + elem.subs[i] = self.parse_subs(InlineParser.parse(sub)) except AttributeError: pass return elem diff --git a/tests/test_common.py b/tests/test_common.py index b5efae1..57547c0 100644 --- a/tests/test_common.py +++ b/tests/test_common.py @@ -31,6 +31,8 @@ def check(self, index): html = printer.to_html(parsed) message = 'Common: ' + str(index) + '\n' message += '=' * 80 + '\n' + message += standard_input.replace(' ', '.') + message += '-' * 80 + '\n' message += standard_output.replace(' ', '.') message += '-' * 80 + '\n' message += html.replace(' ', '.')