File size: 4,569 Bytes
0b7eb60
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
diff --git a/marko/block.py b/marko/block.py
index 22cf1c5..10a2b9d 100644
--- a/marko/block.py
+++ b/marko/block.py
@@ -8,7 +8,7 @@ from typing import Match, Optional, Tuple, Union, cast
 
 from . import inline, patterns
 from .element import Element
-from .helpers import Source, is_paired, normalize_label
+from .helpers import Source, is_paired, normalize_label, partition_by_spaces
 from .parser import Parser
 
 __all__ = (
@@ -231,7 +231,7 @@ class FencedCode(BlockElement):
         prefix, leading, info = m.groups()
         if leading[0] == "`" and "`" in info:
             return None
-        lang, extra = (info.split(None, 1) + [""] * 2)[:2]
+        lang, _, extra = partition_by_spaces(info)
         cls._parse_info = prefix, leading, lang, extra
         return m
 
@@ -374,7 +374,7 @@ class Paragraph(BlockElement):
             return True
         if parser.block_elements["List"].match(source):
             result = parser.block_elements["ListItem"].parse_leading(
-                source.next_line(), 0
+                source.next_line().rstrip(), 0
             )
             if lazy or (result[1][:-1] == "1" or result[1] in "*-+") and result[3]:
                 return True
@@ -501,13 +501,13 @@ class List(BlockElement):
 class ListItem(BlockElement):
     """List item element. It can only be created by List.parse"""
 
-    _parse_info = (0, "", 0, "")
+    _parse_info = (0, "", 0)
     virtual = True
     _tight = False
     pattern = re.compile(r" {,3}(\d{1,9}[.)]|[*\-+])[ \t\n\r\f]")
 
     def __init__(self) -> None:
-        indent, bullet, mid, tail = self._parse_info
+        indent, bullet, mid = self._parse_info
         self._prefix = " " * indent + re.escape(bullet) + " " * mid
         self._second_prefix = " " * (len(bullet) + indent + (mid or 1))
 
@@ -515,18 +515,10 @@ class ListItem(BlockElement):
     def parse_leading(cls, line: str, prefix_pos: int) -> Tuple[int, str, int, str]:
         stripped_line = line[prefix_pos:].expandtabs(4).lstrip()
         indent = len(line) - prefix_pos - len(stripped_line)
-        temp = stripped_line.split(None, 1)
-        bullet = temp[0]
-        if len(temp) == 1:
-            # Bare bullets (* \n)
-            mid = 0
-            tail = ""
-        else:
-            mid = len(stripped_line) - len("".join(temp))
-            if mid > 4:
-                # If indented more than 4 spaces, it should be a indented code block
-                mid = 1
-            tail = temp[1]
+        bullet, spaces, tail = partition_by_spaces(stripped_line)
+        mid = len(spaces)
+        if mid > 4:
+            mid = 1
         return indent, bullet, mid, tail
 
     @classmethod
@@ -540,7 +532,7 @@ class ListItem(BlockElement):
         m = re.match(source.prefix, next_line)
         if m is not None:
             prefix_pos = m.end()
-        indent, bullet, mid, tail = cls.parse_leading(next_line, prefix_pos)  # type: ignore
+        indent, bullet, mid, _ = cls.parse_leading(next_line.rstrip(), prefix_pos)  # type: ignore
         parent = source.state
         assert isinstance(parent, List)
         if (
@@ -551,7 +543,7 @@ class ListItem(BlockElement):
             return False
         if not parent.ordered and bullet != parent.bullet:
             return False
-        cls._parse_info = (indent, bullet, mid, tail)
+        cls._parse_info = (indent, bullet, mid)
         return True
 
     @classmethod
diff --git a/marko/helpers.py b/marko/helpers.py
index acd09f0..1a27833 100644
--- a/marko/helpers.py
+++ b/marko/helpers.py
@@ -13,6 +13,7 @@ from typing import (
     Match,
     Optional,
     Pattern,
+    Tuple,
     Union,
 )
 
@@ -187,6 +188,27 @@ def normalize_label(label: str) -> str:
     return re.sub(r"\s+", " ", label).strip().casefold()
 
 
+def partition_by_spaces(text: str) -> Tuple[str, str, str]:
+    """Split the given text by spaces or tabs, and return a tuple of
+    (start, delimiter, remaining). If spaces are not found, the latter
+    two elements will be empty.
+    """
+    start = end = -1
+    for i, c in enumerate(text):
+        if c in " \t":
+            if start >= 0:
+                continue
+            start = i
+        elif start >= 0:
+            end = i
+            break
+    if start < 0:
+        return text, "", ""
+    if end < 0:
+        return text[:start], text[start:], ""
+    return text[:start], text[start:end], text[end:]
+
+
 def load_extension_object(name: str) -> Callable:
     """Load extension object from a string.
     First try `marko.ext.<name>` if possible