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 17, 2017
2 parents bcec305 + 1a7af4f commit 13f01bc
Show file tree
Hide file tree
Showing 30 changed files with 279 additions and 126 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ python:
- "pypy3"
branches:
except:
- dev
- dev-no-travis
install:
- pip install -r requirements.txt
- pip install coveralls
- pip install pep8
before_script: sh lint.sh
script: nosetests --cover-package markdown --with-coverage tests
script: sh test.sh
after_success:
coveralls
3 changes: 3 additions & 0 deletions markdown/parser/base/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

from markdown.parser.base.element import Element
from markdown.parser.base.block_element import BlockElement
from markdown.parser.base.container_element import ContainerElement

from markdown.parser.base.element_parser import ElementParser
from markdown.parser.base.block_element_parser import BlockElementParser
from markdown.parser.base.contatiner_element_parser import ContainerElementParser
from markdown.parser.base.inline_element_parser import InlineElementParser
5 changes: 3 additions & 2 deletions markdown/parser/base/block_element_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@ def __init__(self, config):
super(BlockElementParser, self).__init__(config)

@staticmethod
def check_indent(code, index):
def check_indent(code, index, align=0):
"""
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.
align: The align index.
Returns:
A tuple (success, index) indicating:
Expand Down Expand Up @@ -51,7 +52,7 @@ def get_unclosed(self, auxiliary):

def is_interrupting(self, auxiliary):
"""
Whether it is interrupting a paragraph
Whether it is interrupting a paragraph.
Args:
auxiliary: A dict.
Expand Down
33 changes: 33 additions & 0 deletions markdown/parser/base/container_element.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/usr/bin/env python
from markdown.parser.base.block_element import BlockElement


class ContainerElement(BlockElement):
"""
The abstract container element.
"""

def __init__(self):
super(ContainerElement, self).__init__()
self._blocks = []

def get_blocks(self):
"""
Get the blocks elements.
Returns:
An array.
"""
return self._blocks

def add_block(self, block):
"""
Append a block element.
Args:
block: a block element.
Returns:
None.
"""
self._blocks.append(block)
29 changes: 29 additions & 0 deletions markdown/parser/base/contatiner_element_parser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/usr/bin/env python
from markdown.parser.base.block_element_parser import BlockElementParser


class ContainerElementParser(BlockElementParser):
"""
The abstract container element parser.
"""

AUX_ALIGN = 'align' # The align offset for lists.

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

def get_align(self, auxiliary):
"""
Get the align offset.
Args:
auxiliary: A dict.
Returns:
An integer.
"""
if auxiliary is None:
return 0
if self.AUX_ALIGN not in auxiliary:
return 0
return int(auxiliary[self.AUX_ALIGN])
2 changes: 2 additions & 0 deletions markdown/parser/base/element_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ def parse(self, code, index, auxiliary=None):
auxiliary: A dictionary with auxiliary information.
Returns:
For leaf elements, only the element is returned, otherwise:
A tuple (element, index) indicating:
element: an element if successfully parsed, otherwise None.
index: the end index (exclusive) of the parsing.
Expand Down
3 changes: 0 additions & 3 deletions markdown/parser/containers/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
#!/usr/bin/env python

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
11 changes: 11 additions & 0 deletions markdown/parser/containers/block_quote_element.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/usr/bin/env python
from markdown.parser.base import ContainerElement


class BlockQuoteElement(ContainerElement):
"""
The block quote element.
"""

def __init__(self):
super(BlockQuoteElement, self).__init__()
10 changes: 5 additions & 5 deletions markdown/parser/containers/block_quote_parser.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#!/usr/bin/env python
"""
The block quote element.
"""
from markdown.parser.base.block_element_parser import BlockElementParser
from markdown.parser.base import ContainerElementParser


class BlockQuoteParser(BlockElementParser):
class BlockQuoteParser(ContainerElementParser):
"""
The block quote parser.
"""

def __init__(self, config):
super(BlockQuoteParser, self).__init__(config)
Expand Down
14 changes: 0 additions & 14 deletions markdown/parser/containers/bullet_list_parser.py

This file was deleted.

33 changes: 21 additions & 12 deletions markdown/parser/containers/container_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from markdown.parser.base import BlockElementParser
from markdown.parser.leaves import AtxHeadingParser
from markdown.parser.leaves import ParagraphElement
from markdown.parser.leaves import EmptyLineElement
from markdown.parser.leaves import ParagraphParser
from markdown.parser.leaves import ThematicBreakParser

Expand All @@ -14,10 +15,13 @@ class ContainerParser(BlockElementParser):
def __init__(self, config):
super(ContainerParser, self).__init__(config)
self._blocks = [] # The parsed block elements.

thematic_break_parser = ThematicBreakParser(config)
atx_heading_parser = AtxHeadingParser(config)
self._paragraph_parser = ParagraphParser(config)
self._interrupt_parsers = [
ThematicBreakParser(config),
AtxHeadingParser(config),
thematic_break_parser,
atx_heading_parser,
# FencedCodeBlockParser(config),
# HtmlBlockParser(config) # Type 1-6,
# ListParser(config) # Not empty,
Expand All @@ -28,38 +32,43 @@ def __init__(self, config):
# OrderedListParser(config),
]
self._block_parsers = [
ThematicBreakParser(config),
AtxHeadingParser(config),
thematic_break_parser,
atx_heading_parser,
self._paragraph_parser,
]

def parse(self, code, index, auxiliary=None):
index = 0
while index < len(code):
lines = code.split('\n')
for line_num, line in enumerate(lines):
line_num += 1
# Try container parsers
index = 0
# 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, {
elem = interrupt_parser.parse(line, index, {
self.AUX_INTERRUPT: True
})
if elem is not None:
has_interrupted = True
self._blocks[-1].close()
elem.set_line_num(line_num)
self._blocks.append(elem)
break
if not has_interrupted:
elem, index = self._paragraph_parser.parse(code, index, {
self._blocks[-1] = self._paragraph_parser.parse(line, index, {
BlockElementParser.AUX_UNCLOSED: self._blocks[-1]
})
if not self._blocks[-1].is_closed():
continue
# Try container parsers
continue
# Try block parsers
for block_parser in self._block_parsers:
elem, index = block_parser.parse(code, index)
elem = block_parser.parse(line, index)
if elem is not None:
if isinstance(elem, EmptyLineElement):
break
elem.set_line_num(line_num)
self._blocks.append(elem)
break
if len(self._blocks) > 0:
Expand Down
22 changes: 22 additions & 0 deletions markdown/parser/containers/list_element.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/usr/bin/env python
from markdown.parser.base import ContainerElement


class ListElement(ContainerElement):
"""
The list element.
"""

def __init__(self):
super(ListElement, self).__init__()
self._is_bullet = True
self._start = 1

def is_bullet(self):
return self._is_bullet

def is_ordered(self):
return not self._is_bullet

def start_number(self):
return self._start
19 changes: 19 additions & 0 deletions markdown/parser/containers/list_item_parser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/usr/bin/env python
from markdown.parser.base import ContainerElementParser


class ListItemParser(ContainerElementParser):
"""
The block quote element.
"""

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

def parse(self, code, index, auxiliary=None):
start = index
# 0~3 spaces
align = self.get_align()
success, index = self.check_indent(code, index, align)
if not success:
return None, start
14 changes: 0 additions & 14 deletions markdown/parser/containers/list_parser.py

This file was deleted.

14 changes: 0 additions & 14 deletions markdown/parser/containers/ordered_list_parser.py

This file was deleted.

2 changes: 2 additions & 0 deletions markdown/parser/leaves/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#!/usr/bin/env python

from markdown.parser.leaves.atx_heading_parser import AtxHeadingParser
from markdown.parser.leaves.setext_heading_element import SetextHeadingElement
from markdown.parser.leaves.paragraph_element import ParagraphElement
from markdown.parser.leaves.empty_line_element import EmptyLineElement
from markdown.parser.leaves.paragraph_parser import ParagraphParser
from markdown.parser.leaves.thematic_break_element import ThematicBreakElement
from markdown.parser.leaves.thematic_break_parser import ThematicBreakParser
21 changes: 10 additions & 11 deletions markdown/parser/leaves/atx_heading_parser.py
Original file line number Diff line number Diff line change
@@ -1,39 +1,38 @@
#!/usr/bin/env python
"""
The ATX heading element.
"""
from markdown.parser.base import BlockElementParser
from markdown.parser.leaves.atx_heading_element import AtxHeadingElement


class AtxHeadingParser(BlockElementParser):
"""
The ATX heading parser.
"""

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

def parse(self, code, index, auxiliary=None):
start = index
# 0~3 spaces
success, index = self.check_indent(code, index)
if not success:
return None, start
return None
# 1~6 `#`s
if index >= len(code) or code[index] != '#':
return None, start
return None
level = 0
while index < len(code) and code[index] == '#':
index += 1
level += 1
if level > 6:
return None, start
return None
# May be empty
if code[index] == '\n':
if index == len(code):
elem = AtxHeadingElement()
elem.set_level(level)
return elem, index + 1
return elem
# One space is required
if code[index] != ' ':
return None, start
return None
# The title body
start = index
while index < len(code) and code[index] != '\n':
Expand All @@ -50,4 +49,4 @@ def parse(self, code, index, auxiliary=None):
elem = AtxHeadingElement()
elem.set_level(level)
elem.set_title(title)
return elem, index + 1
return elem

0 comments on commit 13f01bc

Please sign in to comment.