Skip to content

Commit

Permalink
merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
Mi Yu committed Feb 5, 2018
2 parents 819c04f + 566b234 commit a9eb59a
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 20 deletions.
2 changes: 1 addition & 1 deletion contrib/jira_renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ def render_html_span(token):
return token.content

def render_heading(self, token):
template = '\nh{level}. {inner}\n'
template = 'h{level}. {inner}\n'
inner = self.render_inner(token)
return template.format(level=token.level, inner=inner)

Expand Down
33 changes: 14 additions & 19 deletions mistletoe/block_token.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Built-in block-level token classes.
"""

import re
from types import GeneratorType
from itertools import zip_longest
import mistletoe.block_tokenizer as tokenizer
Expand Down Expand Up @@ -214,16 +215,16 @@ def __init__(self, lines):
@classmethod
def start(cls, line):
if line.startswith('```'):
cls._open_line = '```\n'
cls._open_line = '```'
return True
if line.startswith('~~~'):
cls._open_line = '~~~\n'
cls._open_line = '~~~'
return True
return False

@classmethod
def read(cls, lines):
return until(cls._open_line, lines)
return until(cls._open_line, lines, func=str.startswith)


class List(BlockToken):
Expand Down Expand Up @@ -343,7 +344,7 @@ class Table(BlockToken):
children (tuple): inner tokens (TableRows).
"""
def __init__(self, lines):
if lines[1].find('---') != -1:
if '---' in lines[1]:
self.column_align = [self.parse_align(column)
for column in self.split_delimiter(lines[1])]
self.header = TableRow(lines[0], self.column_align)
Expand All @@ -363,7 +364,7 @@ def split_delimiter(delimiter):
Returns:
a list of align options (None, 0 or 1).
"""
return (column.strip() for column in delimiter[1:-2].split('|'))
return re.findall(r':?---+:?', delimiter)

@staticmethod
def parse_align(column):
Expand All @@ -375,25 +376,19 @@ def parse_align(column):
0 if align = center;
1 if align = right.
"""
if column[:4] == ':---' and column[-4:] == '---:':
return 0
if column[-4:] == '---:':
return 1
return None
return (0 if column[0] == ':' else 1) if column[-1] == ':' else None

@staticmethod
def start(line):
return line.startswith('|') and line.endswith('|\n')
return '|' in line

@staticmethod
def read(lines):
line_buffer = []
for line in lines:
if (not (line.startswith('|') and line.endswith('|\n'))
or line == '\n'):
break
else:
line_buffer.append(line)
while lines.peek() is not None and '|' in lines.peek():
line_buffer.append(next(lines))
if len(line_buffer) < 1 or '---' not in line_buffer[0]:
raise tokenizer.MismatchException()
return line_buffer


Expand Down Expand Up @@ -489,10 +484,10 @@ def read(lines):
return []


def until(stop_line, lines):
def until(stop_line, lines, func=None):
line_buffer = []
for line in lines:
if line == stop_line:
if (line == stop_line if func is None else func(line, stop_line)):
break
line_buffer.append(line)
return line_buffer
Expand Down
19 changes: 19 additions & 0 deletions test/test_block_token.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,25 @@ def test_match(self):
calls = [call(line, [None, None, None]) for line in lines[:1]+lines[2:]]
mock.assert_has_calls(calls)

def test_easy_table(self):
lines = ['header 1 | header 2\n',
' ---: | :---\n',
' cell 1 | cell 2\n']
with patch('mistletoe.block_token.TableRow') as mock:
token, = block_token.tokenize(lines)
self.assertIsInstance(token, block_token.Table)
self.assertTrue(hasattr(token, 'header'))
self.assertEqual(token.column_align, [1, None])
token.children
calls = [call(line, [1, None]) for line in lines[:1] + lines[2:]]
mock.assert_has_calls(calls)

def test_not_easy_table(self):
lines = ['not header 1 | not header 2\n',
'foo | bar\n']
token, = block_token.tokenize(lines)
self.assertIsInstance(token, block_token.Paragraph)


class TestTableRow(unittest.TestCase):
def test_match(self):
Expand Down

0 comments on commit a9eb59a

Please sign in to comment.