-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
51 changed files
with
632 additions
and
585 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.