|
8 | 8 | # Add more mappings as needed
|
9 | 9 | }
|
10 | 10 |
|
11 |
| -def print_specific_anchor_targets(content): |
12 |
| - """Print the anchor targets extracted from the first line after specific headings that begin with four hash marks (####), which begins with a dash, and save the content of the line along with the matched heading pattern.""" |
| 11 | +def add_anchor_tags(content): |
| 12 | + """Process the parameters block of text as specified, including 'Setter Methods' and 'Execution Methods'. |
| 13 | + Insert <a href="#"> before and </a> after lines that start with a dash (-).""" |
13 | 14 | lines = content.split('\n')
|
14 |
| - specific_headings = ['#### Setter Methods', '#### Execution Methods', '#### Methods', '#### Generated By'] |
15 |
| - line_content = "" |
| 15 | + processed_lines = [] |
| 16 | + processing = False |
| 17 | + found_first_dash = False # Variable to track the first dash |
16 | 18 |
|
17 |
| - for i, line in enumerate(lines): |
18 |
| - if line.strip() in specific_headings: |
19 |
| - matched_heading = line.strip() # Save the matched heading pattern |
20 |
| - for j in range(i+1, len(lines)): |
21 |
| - if lines[j].strip().startswith('-'): |
22 |
| - line_content = lines[j].strip() # Save the entire line content |
23 |
| - # Extract the text between the first backtick ` and the first open parenthesis ( |
24 |
| - match = re.search(r"`(.*?)\(", lines[j]) |
25 |
| - if match: |
26 |
| - anchor_target = match.group(1).strip() # Strip out any whitespace |
27 |
| - print(f"Matched Heading: {matched_heading}, Anchor Target: {anchor_target}, Line Content: {line_content}") |
28 |
| - break # Move to the next heading after processing |
| 19 | + for line in lines: |
| 20 | + if (match := line.strip()) in ['#### Setter Methods', '#### Execution Methods', '#### Methods', '#### Generated By']: |
| 21 | + processing = True |
| 22 | + matched_string = match # Keep track of which string matched |
| 23 | + found_first_dash = False # Reset for each new section |
| 24 | + processed_lines.append(line) |
| 25 | + continue |
| 26 | + if processing: |
| 27 | + if not found_first_dash: |
| 28 | + if line.strip() == '': |
| 29 | + processed_lines.append(line) |
| 30 | + continue |
| 31 | + elif not line.lstrip().startswith('-'): |
| 32 | + processed_lines.append(line) |
| 33 | + continue |
| 34 | + else: |
| 35 | + found_first_dash = True # Found the first dash, start processing lines |
| 36 | + if line.startswith('-'): |
| 37 | + if matched_string != '#### Generated By': |
| 38 | + previous_lines = lines[:lines.index(line)] |
| 39 | + type_word = "" |
| 40 | + for prev_line in reversed(previous_lines): |
| 41 | + if prev_line.strip().startswith("type "): |
| 42 | + type_word = prev_line.split()[1] |
| 43 | + break |
| 44 | + anchor = line[line.find('`')+1:line.find('(')].strip() |
| 45 | + # Insert <a href="#"> before and </a> after the line |
| 46 | + processed_lines.append('- ' + f'<a href="#{type_word}.{anchor}">' +'`' + line[3:] + '</a>') |
| 47 | + else: |
| 48 | + anchor = line[line.find('`')+1:line.find('(')].strip() |
| 49 | + # Insert <a href="#"> before and </a> after the line |
| 50 | + processed_lines.append(f'<a href="#{anchor}">') |
| 51 | + processed_lines.append(line) |
| 52 | + processed_lines.append('</a>') # Ensure an additional new line after closing tag for better readability |
| 53 | + elif line.strip() == '' or line.startswith(' '): |
| 54 | + processed_lines.append(line) |
| 55 | + continue |
| 56 | + else: |
| 57 | + # Stop processing if the line does not start with a dash |
| 58 | + processing = False |
| 59 | + processed_lines.append(line) |
| 60 | + else: |
| 61 | + processed_lines.append(line) |
| 62 | + |
| 63 | + return '\n'.join(processed_lines) |
29 | 64 |
|
30 | 65 | def move_all_struct_definitions(content):
|
31 | 66 | """Move all struct definition blocks right after their type documentation."""
|
@@ -240,7 +275,7 @@ def process_file(file_path, patterns, replacements):
|
240 | 275 | content = remove_index_block(content) # Remove index block
|
241 | 276 | content = correct_escaping_in_links(content) # Correct escaping in links
|
242 | 277 | content = move_all_struct_definitions(content) # Move all struct definitions
|
243 |
| - print_specific_anchor_targets(content) |
| 278 | + content = add_anchor_tags(content) |
244 | 279 | # content = add_anchor_tags_to_generated_by(content) # Add anchor tags to 'Generated By' sections
|
245 | 280 |
|
246 | 281 | write_file_content(file_path, content)
|
|
0 commit comments