# Compute exact display width of `sep_s` from `mark_end_col` c_cur = mark_end_col for ch in sep_s: c_cur += (4 - (c_cur % 4)) if ch == "\t" else 1 sp_cols = c_cur - mark_end_col if is_empty_content: content_indent = ind_cols + len(mark_s) + 1 return True, content_indent, "", mark_end_col + 1 if sp_cols >= 5: # Indented code inside list item: marker consumes 1 space, remaining (sp_cols - 1) spaces stay! content_indent = ind_cols + len(mark_s) + 1 return True, content_indent, (" " * (sp_cols - 1)) + rest_s, mark_end_col + 1 content_indent = ind_cols + len(mark_s) + sp_cols return True, content_indent, rest_s, c_cur def _interrupts_paragraph(s: str, start_col: int) -> bool: ind_cols, idx_ns = _count_leading_indent(s, start_col) if ind_cols >= 4: return False norm_s = (" " * ind_cols) + s[idx_ns:] if atx_re.match(norm_s) or thematic_re.match(norm_s): return True mf_chk = fence_open_re.match(norm_s) if mf_chk and not (mf_chk.group(1)[0] == "`" and "`" in mf_chk.group(2)): return True return bool( html_t1_start_re.match(norm_s) or html_t2_start_re.match(norm_s) or html_t3_start_re.match(norm_s) or html_t4_start_re.match(norm_s) or html_t5_start_re.match(norm_s) or html_t6_start_re.match(norm_s) ) for idx, raw_line in enumerate(raw_lines): has_open_para = bool(para_lines) open_para_depth = para_lines[-1][2] if para_lines else 0 # PHASE 1: Match existing open containers in `container_stack` with exact absolute column tracking prev_stack = list(container_stack) cur = raw_line cur_col = 0 matched_stack: List[tuple[str, int]] = [] is_blank_line = _cm_is_blank(raw_line) for kind, req_indent in container_stack: if kind == "bq": ok_bq, rest_bq, next_col = _try_match_bq(cur, cur_col) if ok_bq: matched_stack.append((kind, req_indent)) cur = rest_bq cur_col = next_col continue break else: if _cm_is_blank(cur): matched_stack.append((kind, req_indent)) cur = "" cur_col += req_indent continue ok_li, rest_li, next_col = _consume_cols_abs(cur, req_indent, cur_col) if ok_li: matched_stack.append((kind, req_indent)) cur = rest_li cur_col = next_col continue break matched_depth = len(matched_stack) # PHASE 2: Handle open fenced code block or HTML leaf block BEFORE opening ANY new containers! if in_html_block_type: if matched_depth < html_block_depth: in_html_block_type = 0 html_block_depth = 0 else: container_stack = matched_stack if in_html_block_type == 1 and html_t1_end_re.search(cur): in_html_block_type = 0 html_block_depth = 0 elif in_html_block_type == 2 and "-->" in cur: in_html_block_type = 0 html_block_depth = 0 elif in_html_block_type == 3 and "?>" in cur: in_html_block_type = 0 html_block_depth = 0 elif in_html_block_type == 4 and ">" in cur: in_html_block_type = 0 html_block_depth = 0 elif in_html_block_type == 5 and "]]>" in cur: in_html_block_type = 0 html_block_depth = 0 elif in_html_block_type in (6, 7) and _cm_is_blank(cur): in_html_block_type = 0 html_block_depth = 0 _flush_para_for_refs() continue if in_fence: if matched_depth < fence_depth: in_fence = False fence_char = "" fence_len = 0 fence_depth = 0 else: container_stack = matched_stack ind_c, ns_c = _count_leading_indent(cur, cur_col) norm_close = (" " * ind_c) + cur[ns_c:] if ind_c <= 3 else "" close_m = re.match(rf"^ {{0,3}}{re.escape(fence_char)}{{{fence_len},}}[ \t]*$", norm_close) if norm_close else None if close_m: in_fence = False fence_char = "" fence_len = 0 fence_depth = 0 _flush_para_for_refs() continue if is_blank_line: while container_stack and container_stack[-1][0] == "bq" and len(container_stack) > matched_depth: container_stack.pop() _flush_para_for_refs() continue # PHASE 3: Lazy paragraph continuation check (CommonMark §5.1 / §4.3) if has_open_para and matched_depth < open_para_depth: ok_bq_chk, _, _ = _try_match_bq(cur, cur_col) opens_new_container = ok_bq_chk if not opens_new_container: # At a lower depth than the open paragraph, a list marker starts a NEW container # (any ordered start number with non-empty content, or empty list item at depth 0 per Ex. 278) ok_li_chk, _, rest_li_chk, _ = _try_match_list(cur, cur_col, interrupting_same_depth_para=False) if ok_li_chk: if not _cm_is_blank(rest_li_chk) or matched_depth == 0: opens_new_container = True if not opens_new_container and not _interrupts_paragraph(cur, cur_col): container_stack = prev_stack para_lines.append((idx, _cm_strip_ascii_ws(cur), open_para_depth)) continue container_stack = matched_stack # PHASE 4: Open any new nested blockquotes or list items on this line while True: ok_bq, rest_bq, next_col = _try_match_bq(cur, cur_col) if ok_bq: container_stack.append(("bq", 0)) cur = rest_bq cur_col = next_col continue ind_c, ns_c = _count_leading_indent(cur, cur_col) norm_lead = (" " * ind_c) + cur[ns_c:] if ind_c <= 3 else cur if has_open_para and len(container_stack) == open_para_depth and ind_c <= 3 and setext_re.match(norm_lead): break if ind_c <= 3 and thematic_re.match(norm_lead): break same_depth_para = bool(has_open_para and len(container_stack) == open_para_depth) ok_li, req_ind, rest_li, next_col_li = _try_match_list(cur, cur_col, interrupting_same_depth_para=same_depth_para) if ok_li: container_stack.append(("list", req_ind)) cur = rest_li cur_col = next_col_li continue break depth = len(container_stack) if _cm_is_blank(cur): _flush_para_for_refs() continue ind_cols, idx_ns = _count_leading_indent(cur, cur_col) if not para_lines and ind_cols >= 4: continue inner = (" " * ind_cols) + cur[idx_ns:] if ind_cols <= 3 else cur[idx_ns:] if ind_cols <= 3: if html_t1_start_re.match(inner): if not html_t1_end_re.search(inner): in_html_block_type = 1 html_block_depth = depth _flush_para_for_refs() continue if html_t2_start_re.match(inner): if "-->" not in inner[inner.find("