|
| 1 | +import re |
| 2 | + |
| 3 | +def sanitize_label(text): |
| 4 | + """ |
| 5 | + Sanitize the label text by removing or replacing special characters. |
| 6 | + Only allow alphanumeric characters and underscores. |
| 7 | + """ |
| 8 | + # Replace spaces with underscores |
| 9 | + text = text.replace(" ", "_") |
| 10 | + # Remove any characters that are not alphanumeric or underscores |
| 11 | + text = re.sub(r"[^\w]", "", text) |
| 12 | + return text.lower() |
| 13 | + |
| 14 | + |
| 15 | +def sync_labels(input_file, output_file): |
| 16 | + """ |
| 17 | + Synchronize labels with headers in an RST file. Labels are generated based on the chapter, header, subheader, and |
| 18 | + sub-subheader structure. The script ensures anchors appear immediately before the correct header text, preserves |
| 19 | + all section content, and avoids duplicating labels. |
| 20 | +
|
| 21 | + Header Hierarchy: |
| 22 | + - Chapters are identified by lines underlined with `*`. |
| 23 | + - Headers are identified by lines underlined with `=`. |
| 24 | + - Subheaders are identified by lines underlined with `-`. |
| 25 | + - Sub-subheaders are identified by lines underlined with `~`. |
| 26 | +
|
| 27 | + Label Format: |
| 28 | + - Labels follow the format: `.. _chapter_header_subheader_subsubheader:` |
| 29 | + - Labels are generated dynamically based on the text of the header and its position in the hierarchy. |
| 30 | + """ |
| 31 | + # Regular expressions to identify underline patterns for different header levels |
| 32 | + underline_patterns = { |
| 33 | + "*": "chapter", |
| 34 | + "=": "header", |
| 35 | + "-": "subheader", |
| 36 | + "~": "subsubheader", |
| 37 | + } |
| 38 | + |
| 39 | + # Variables to track the current hierarchy of headers |
| 40 | + current_chapter = None |
| 41 | + current_header = None |
| 42 | + current_subheader = None |
| 43 | + |
| 44 | + # Read the input file |
| 45 | + with open(input_file, "r") as infile: |
| 46 | + lines = infile.readlines() |
| 47 | + |
| 48 | + # Initialize output lines |
| 49 | + output_lines = [] |
| 50 | + buffer = None # Holds the previous line to check for headers |
| 51 | + last_label = None |
| 52 | + line_count = 0 |
| 53 | + |
| 54 | + for i, line in enumerate(lines): |
| 55 | + # Debugging: Print the current line being processed |
| 56 | + print(f"Processing line {i}: {line.strip()}") |
| 57 | + |
| 58 | + if line.startswith(".."): |
| 59 | + last_label = line |
| 60 | + line_count = 0 |
| 61 | + |
| 62 | + # Check if the line is an underline pattern |
| 63 | + if re.match(r"^\*+$", line): |
| 64 | + # Process chapter |
| 65 | + current_chapter = sanitize_label(buffer.strip().lower().replace(" ", "_")) if buffer else None |
| 66 | + label = f".. _{current_chapter}:\n\n" if current_chapter else None |
| 67 | + if label and not line_count == 2: |
| 68 | + print(f"Detected chapter: {current_chapter}, adding label: {label.strip()}") |
| 69 | + output_lines.append(label) |
| 70 | + if buffer: |
| 71 | + print(f"Detected chapter (skip): {current_chapter}, adding label: {label.strip()}") |
| 72 | + output_lines.append(buffer) # Add the header text immediately after the label |
| 73 | + output_lines.append(line) # Add the underline itself |
| 74 | + buffer = None |
| 75 | + elif re.match(r"^=+$", line): |
| 76 | + # Process header |
| 77 | + current_header = sanitize_label(buffer.strip().lower().replace(" ", "_")) if buffer else None |
| 78 | + if not current_header: # Fallback for empty buffer |
| 79 | + current_header = "unknown_header" |
| 80 | + label = f".. _{current_chapter}_{current_header}:\n\n" if current_header else None |
| 81 | + if label and not line_count == 2: |
| 82 | + print(f"Detected header: {current_header}, adding label: {label.strip()}") |
| 83 | + output_lines.append(label) |
| 84 | + if buffer: |
| 85 | + print(f"Detected header (skip): {current_chapter}, adding label: {label.strip()}") |
| 86 | + output_lines.append(buffer) # Add the header text immediately after the label |
| 87 | + output_lines.append(line) # Add the underline itself |
| 88 | + buffer = None |
| 89 | + elif re.match(r"^-+$", line): |
| 90 | + # Process subheader |
| 91 | + current_subheader = sanitize_label(buffer.strip().lower().replace(" ", "_")) if buffer else None |
| 92 | + label = f".. _{current_chapter}_{current_subheader}:\n\n" if current_subheader else None |
| 93 | + if label and not line_count == 2: |
| 94 | + print(f"Detected subheader: {current_subheader}, adding label: {label.strip()}") |
| 95 | + output_lines.append(label) |
| 96 | + if buffer: |
| 97 | + output_lines.append(buffer) # Add the header text immediately after the label |
| 98 | + output_lines.append(line) # Add the underline itself |
| 99 | + buffer = None |
| 100 | + elif re.match(r"^~+$", line): |
| 101 | + # Process sub-subheader |
| 102 | + subsubheader_text = sanitize_label(buffer.strip().lower().replace(" ", "_")) if buffer else None |
| 103 | + label = f".. _{current_chapter}_{subsubheader_text}:\n\n" if subsubheader_text else None |
| 104 | + if label and not line_count == 2: |
| 105 | + print(f"Detected sub-subheader: {subsubheader_text}, adding label: {label.strip()}") |
| 106 | + output_lines.append(label) |
| 107 | + if buffer: |
| 108 | + output_lines.append(buffer) # Add the header text immediately after the label |
| 109 | + output_lines.append(line) # Add the underline itself |
| 110 | + buffer = None |
| 111 | + else: |
| 112 | + # If the line isn't an underline, store it in the buffer |
| 113 | + if buffer: |
| 114 | + output_lines.append(buffer) # Add the previous line to the output |
| 115 | + buffer = line # Store the current line for processing |
| 116 | + if not line.isspace(): |
| 117 | + line_count += 1 |
| 118 | + |
| 119 | + if buffer is not None: |
| 120 | + output_lines.append(buffer) |
| 121 | + |
| 122 | + # Write the output to the specified file |
| 123 | + with open(output_file, "w") as outfile: |
| 124 | + outfile.writelines(output_lines) |
| 125 | + |
| 126 | + print(f"Labels synchronized successfully! Output written to {output_file}") |
| 127 | + |
| 128 | + |
| 129 | +# Input and output file paths |
| 130 | +input_file = "userguide.rst" |
| 131 | +output_file = "userguide_with_synced_labels.rst" |
| 132 | + |
| 133 | +# Run the label synchronization |
| 134 | +sync_labels(input_file, output_file) |
0 commit comments