diff --git a/mkdocs_include_markdown_plugin/event.py b/mkdocs_include_markdown_plugin/event.py index f5b08d8..1194498 100644 --- a/mkdocs_include_markdown_plugin/event.py +++ b/mkdocs_include_markdown_plugin/event.py @@ -100,14 +100,57 @@ def get_file_content(markdown, abs_src_path): # string options start_match = re.search(ARGUMENT_REGEXES['start'], arguments_string) - if start_match is not None: - start = process.interpret_escapes(start_match.group(1)) - _, _, text_to_include = text_to_include.partition(start) - end_match = re.search(ARGUMENT_REGEXES['end'], arguments_string) - if end_match is not None: + # DEBUG: Print when we enter this block + if start_match is not None and end_match is not None: + print(f"DEBUG: Entering multi-section extraction logic") + print(f"DEBUG: start_match.group(1) = {repr(start_match.group(1))}") + print(f"DEBUG: end_match.group(1) = {repr(end_match.group(1))}") + start = process.interpret_escapes(start_match.group(1)) end = process.interpret_escapes(end_match.group(1)) - text_to_include, _, _ = text_to_include.partition(end) + + # Find all start-end pairs and concatenate them + # If no end tag is found after a start tag, take everything to the end + parts = [] + search_pos = 0 + text_len = len(text_to_include) + # Safety brake to prevent infinite loops + safety_count = 0 + max_safety = 10000 + print(f"DEBUG: Starting extraction loop, text length = {text_len}") + while search_pos < text_len and safety_count < max_safety: + safety_count += 1 + if safety_count % 1000 == 0: # Only print every 1000 iterations to avoid spam + print(f"DEBUG: Safety iteration {safety_count}, search_pos = {search_pos}") + start_pos = text_to_include.find(start, search_pos) + if start_pos == -1: + print(f"DEBUG: No more start tags found at position {search_pos}") + break + content_start = start_pos + len(start) + # Look for end tag after the start tag + end_pos = text_to_include.find(end, content_start) + if end_pos == -1: + # No end tag found, take rest of string + part = text_to_include[content_start:] + print(f"DEBUG: No end tag found after start at {start_pos}, taking remainder") + else: + # Found end tag, extract content between start and end + part = text_to_include[content_start:end_pos] + search_pos = end_pos + len(end) + print(f"DEBUG: Found end tag at {end_pos}, extracted content, next search at {search_pos}") + + # Strip leading/trailing whitespace that may be outside the actual content + original_part = part + part = part.strip('\n\r') + if part != original_part: + print(f"DEBUG: Stripped whitespace from part: {repr(original_part[:30])} -> {repr(part[:30])}") + if part: # Only add non-empty parts + parts.append(part) + print(f"DEBUG: Added part {len(parts)}: {repr(part[:50])}{'...' if len(part) > 50 else ''}") + + print(f"DEBUG: Extraction completed after {safety_count} iterations, found {len(parts)} parts") + text_to_include = '\n\n'.join(parts) + print(f"DEBUG: Final joined result: {repr(text_to_include[:100])}{'...' if len(text_to_include) > 100 else ''}") # nested includes new_text_to_include = get_file_content(text_to_include, file_path_abs) @@ -185,16 +228,57 @@ def get_file_content(markdown, abs_src_path): # string options start_match = re.search(ARGUMENT_REGEXES['start'], arguments_string) - start = None - if start_match is not None: - start = process.interpret_escapes(start_match.group(1)) - _, _, text_to_include = text_to_include.partition(start) - end_match = re.search(ARGUMENT_REGEXES['end'], arguments_string) - end = None - if end_match is not None: + # DEBUG: Print when we enter this block + if start_match is not None and end_match is not None: + print(f"DEBUG: Entering multi-section extraction logic") + print(f"DEBUG: start_match.group(1) = {repr(start_match.group(1))}") + print(f"DEBUG: end_match.group(1) = {repr(end_match.group(1))}") + start = process.interpret_escapes(start_match.group(1)) end = process.interpret_escapes(end_match.group(1)) - text_to_include, _, _ = text_to_include.partition(end) + + # Find all start-end pairs and concatenate them + # If no end tag is found after a start tag, take everything to the end + parts = [] + search_pos = 0 + text_len = len(text_to_include) + # Safety brake to prevent infinite loops + safety_count = 0 + max_safety = 10000 + print(f"DEBUG: Starting extraction loop, text length = {text_len}") + while search_pos < text_len and safety_count < max_safety: + safety_count += 1 + if safety_count % 1000 == 0: # Only print every 1000 iterations to avoid spam + print(f"DEBUG: Safety iteration {safety_count}, search_pos = {search_pos}") + start_pos = text_to_include.find(start, search_pos) + if start_pos == -1: + print(f"DEBUG: No more start tags found at position {search_pos}") + break + content_start = start_pos + len(start) + # Look for end tag after the start tag + end_pos = text_to_include.find(end, content_start) + if end_pos == -1: + # No end tag found, take rest of string + part = text_to_include[content_start:] + print(f"DEBUG: No end tag found after start at {start_pos}, taking remainder") + else: + # Found end tag, extract content between start and end + part = text_to_include[content_start:end_pos] + search_pos = end_pos + len(end) + print(f"DEBUG: Found end tag at {end_pos}, extracted content, next search at {search_pos}") + + # Strip leading/trailing whitespace that may be outside the actual content + original_part = part + part = part.strip('\n\r') + if part != original_part: + print(f"DEBUG: Stripped whitespace from part: {repr(original_part[:30])} -> {repr(part[:30])}") + if part: # Only add non-empty parts + parts.append(part) + print(f"DEBUG: Added part {len(parts)}: {repr(part[:50])}{'...' if len(part) > 50 else ''}") + + print(f"DEBUG: Extraction completed after {safety_count} iterations, found {len(parts)} parts") + text_to_include = '\n\n'.join(parts) + print(f"DEBUG: Final joined result: {repr(text_to_include[:100])}{'...' if len(text_to_include) > 100 else ''}") # relative URLs rewriting if bool_options['rewrite-relative-urls']['value']: