summaryrefslogtreecommitdiffstats
path: root/md_tw.py
diff options
context:
space:
mode:
authorrsiddharth <s@ricketyspace.net>2018-01-02 00:45:51 +0000
committerrsiddharth <s@ricketyspace.net>2018-01-02 00:45:51 +0000
commit9011ef7c4e768955bd3a4355d86eda39469bc127 (patch)
treee6ebf0614049509409609598d416cf8618986a1c /md_tw.py
parentcad2e7b18e510a89e159148f1141691fd1fc6715 (diff)
md_tw.py: Add TWBlockLexer._process_list_item.
* md_tw.py (TWBlockLexer._process_list_item): New method * tests/test_md_tw.py (TestTWBlockLexer.test_parse_list_block): New test. * tests/data/blexer-lists.md: New file.
Diffstat (limited to 'md_tw.py')
-rw-r--r--md_tw.py54
1 files changed, 54 insertions, 0 deletions
diff --git a/md_tw.py b/md_tw.py
index 238cbb2..7b20692 100644
--- a/md_tw.py
+++ b/md_tw.py
@@ -73,6 +73,60 @@ class TWBlockLexer(mistune.BlockLexer):
'text': m.group(0)
})
+ def _process_list_item(self, cap, bull):
+ cap = self.rules.list_item.findall(cap)
+
+ _next = False
+ length = len(cap)
+
+ for i in range(length):
+ item = cap[i][0]
+
+ # slurp and remove the bullet
+ space = len(item)
+ bullet = ''
+ bm = self.rules.list_bullet.match(item)
+ if bm:
+ bullet = bm.group(0)
+
+ item = self.rules.list_bullet.sub('', item)
+
+ # outdent
+ if '\n ' in item:
+ space = space - len(item)
+ pattern = re.compile(r'^ {1,%d}' % space, flags=re.M)
+ item = pattern.sub('', item)
+
+ # determine whether item is loose or not
+ loose = _next
+ if not loose and re.search(r'\n\n(?!\s*$)', item):
+ loose = True
+
+ rest = len(item)
+ if i != length - 1 and rest:
+ _next = item[rest-1] == '\n'
+ if not loose:
+ loose = _next
+
+ if loose:
+ t = 'loose_item_start'
+ else:
+ t = 'list_item_start'
+
+ self.tokens.append({
+ 'type': t,
+ 'text': bullet,
+ 'spaces': len(bullet)
+ })
+
+ # recurse
+ self.parse(item, self.list_rules)
+
+ self.tokens.append({
+ 'type': 'list_item_end',
+ 'spaces': len(bullet)
+ })
+
class TWInlineLexer(mistune.InlineLexer):
"""Text Wrap Inline level lexer for inline gramars."""