Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
CyberZHG committed May 15, 2017
2 parents f699456 + 564e4d5 commit 7eec62c
Show file tree
Hide file tree
Showing 51 changed files with 632 additions and 585 deletions.
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ python:
- "nightly"
- "pypy"
- "pypy3"
branches:
except:
- dev
install:
- pip install -r requirements.txt
- pip install coveralls
Expand Down
7 changes: 7 additions & 0 deletions markdown/parser/base/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/usr/bin/env python

from markdown.parser.base.element import Element
from markdown.parser.base.block_element import BlockElement
from markdown.parser.base.element_parser import ElementParser
from markdown.parser.base.block_element_parser import BlockElementParser
from markdown.parser.base.inline_element_parser import InlineElementParser
18 changes: 18 additions & 0 deletions markdown/parser/base/block_element.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/usr/bin/env python
from markdown.parser.base.element import Element


class BlockElement(Element):
"""
The abstract block element.
"""

def __init__(self):
super(BlockElement, self).__init__()
self._closed = False

def is_closed(self):
return self._closed

def close(self):
self._closed = True
66 changes: 66 additions & 0 deletions markdown/parser/base/block_element_parser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#!/usr/bin/env python
from markdown.parser.base.element_parser import ElementParser
from markdown.parser.util.parse_util import ParseUtil


class BlockElementParser(ElementParser):
"""
The abstract block element parser.
"""

AUX_UNCLOSED = 'unclosed' # The last unclosed element.
AUX_INTERRUPT = 'interrupt' # Whether it is interrupting a paragraph.

def __init__(self, config):
super(BlockElementParser, self).__init__(config)

@staticmethod
def check_indent(code, index):
"""
Check whether the number of heading spaces is less than 4.
Args:
code: An UTF-8 string.
index: The start index (inclusive) of the current parsing.
Returns:
A tuple (success, index) indicating:
success: true is the parsing succeed.
index: the end index (exclusive) of the parsing.
"""
space_num = ParseUtil.get_heading_space_num(code, index)
if space_num >= 4:
return False, index
return True, index + space_num

def get_unclosed(self, auxiliary):
"""
Get last unclosed element.
Args:
auxiliary: A dict.
Returns:
Returns None if it is not existed.
"""
if auxiliary is None:
return None
if self.AUX_UNCLOSED not in auxiliary:
return None
return auxiliary[self.AUX_UNCLOSED]

def is_interrupting(self, auxiliary):
"""
Whether it is interrupting a paragraph
Args:
auxiliary: A dict.
Returns:
Boolean.
"""
if auxiliary is None:
return False
if self.AUX_INTERRUPT not in auxiliary:
return False
return auxiliary[self.AUX_INTERRUPT]
40 changes: 40 additions & 0 deletions markdown/parser/base/element.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#!/usr/bin/env python


class Element(object):
"""
The abstract element.
"""

def __init__(self):
self._line_num = 0

def set_line_num(self, line_num):
"""
Set the start line of the parsed element.
Args:
line_num: The line number.
Returns:
Void.
"""
self._line_num = line_num

def line_num(self):
"""
Get the start line of the parsed element.
Returns:
The line number.
"""
return self._line_num

def get_inlines(self):
"""
Get the inline elements.
Returns:
An array.
"""
return []
27 changes: 27 additions & 0 deletions markdown/parser/base/element_parser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/usr/bin/env python


class ElementParser(object):
"""
The abstract element parser.
"""

def __init__(self, config):
self._config = config

def parse(self, code, index, auxiliary=None):
"""
Parse the code and store the results.
Inherited classes should implement this method.
Args:
code: An UTF-8 string.
index: The start index (inclusive) of the current parsing.
auxiliary: A dictionary with auxiliary information.
Returns:
A tuple (element, index) indicating:
element: an element if successfully parsed, otherwise None.
index: the end index (exclusive) of the parsing.
"""
return None, index
11 changes: 11 additions & 0 deletions markdown/parser/base/inline_element_parser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/usr/bin/env python
from markdown.parser.base.element_parser import ElementParser


class InlineElementParser(ElementParser):
"""
The abstract inline element parser.
"""

def __init__(self, config):
super(InlineElementParser, self).__init__(config)
10 changes: 5 additions & 5 deletions markdown/parser/containers/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env python

from markdown.parser.containers.block_quote import BlockQuote
from markdown.parser.containers.bullet_list import BulletList
from markdown.parser.containers.container import Container
from markdown.parser.containers.list import List
from markdown.parser.containers.ordered_list import OrderedList
from markdown.parser.containers.block_quote_parser import BlockQuoteParser
from markdown.parser.containers.bullet_list_parser import BulletListParser
from markdown.parser.containers.container_parser import ContainerParser
from markdown.parser.containers.list_parser import ListParser
from markdown.parser.containers.ordered_list_parser import OrderedListParser
14 changes: 0 additions & 14 deletions markdown/parser/containers/block_quote.py

This file was deleted.

14 changes: 14 additions & 0 deletions markdown/parser/containers/block_quote_parser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/usr/bin/env python
"""
The block quote element.
"""
from markdown.parser.base.block_element_parser import BlockElementParser


class BlockQuoteParser(BlockElementParser):

def __init__(self, config):
super(BlockQuoteParser, self).__init__(config)

def parse(self, code, index, auxiliary=None):
return None, index
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
"""
The bullet list element.
"""
from markdown.parser.containers.list import List
from markdown.parser.containers.list_parser import ListParser


class BulletList(List):
class BulletListParser(ListParser):

def __init__(self, config):
super(BulletList, self).__init__(config)
super(BulletListParser, self).__init__(config)

def parse(self, code, index, auxiliary=None):
pass
67 changes: 0 additions & 67 deletions markdown/parser/containers/container.py

This file was deleted.

69 changes: 69 additions & 0 deletions markdown/parser/containers/container_parser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#!/usr/bin/env python
"""
Container (block quotes and lists) parser.
"""
from markdown.parser.base import BlockElementParser
from markdown.parser.leaves import AtxHeadingParser
from markdown.parser.leaves import ParagraphElement
from markdown.parser.leaves import ParagraphParser
from markdown.parser.leaves import ThematicBreakParser


class ContainerParser(BlockElementParser):

def __init__(self, config):
super(ContainerParser, self).__init__(config)
self._blocks = [] # The parsed block elements.
self._paragraph_parser = ParagraphParser(config)
self._interrupt_parsers = [
ThematicBreakParser(config),
AtxHeadingParser(config),
# FencedCodeBlockParser(config),
# HtmlBlockParser(config) # Type 1-6,
# ListParser(config) # Not empty,
]
self._container_parsers = [
# BlockQuoteParser(config),
# BulletListParser(config),
# OrderedListParser(config),
]
self._block_parsers = [
ThematicBreakParser(config),
AtxHeadingParser(config),
self._paragraph_parser,
]

def parse(self, code, index, auxiliary=None):
index = 0
while index < len(code):
# Continuation
if len(self._blocks) > 0 and not self._blocks[-1].is_closed():
if isinstance(self._blocks[-1], ParagraphElement):
has_interrupted = False
for interrupt_parser in self._interrupt_parsers:
elem, index = interrupt_parser.parse(code, index, {
self.AUX_INTERRUPT: True
})
if elem is not None:
has_interrupted = True
self._blocks[-1].close()
self._blocks.append(elem)
break
if not has_interrupted:
elem, index = self._paragraph_parser.parse(code, index, {
BlockElementParser.AUX_UNCLOSED: self._blocks[-1]
})
if not self._blocks[-1].is_closed():
continue
# Try container parsers
# Try block parsers
for block_parser in self._block_parsers:
elem, index = block_parser.parse(code, index)
if elem is not None:
self._blocks.append(elem)
break
if len(self._blocks) > 0:
self._blocks[-1].close()

def get_blocks(self):
return self._blocks
14 changes: 0 additions & 14 deletions markdown/parser/containers/list.py

This file was deleted.

0 comments on commit 7eec62c

Please sign in to comment.