|
| 1 | +import re |
| 2 | + |
| 3 | +def run(config): |
| 4 | + markdown_files = config["markdown_files"] |
| 5 | + for file in markdown_files: |
| 6 | + process(file) |
| 7 | + |
| 8 | +def process(file_path): |
| 9 | + """ |
| 10 | + Reads a markdown file, searches for code block headers that include specific flags |
| 11 | + (file=..., noindent, noparse, noeval), and moves file=... finding to a new line inside |
| 12 | + square brackets and ignores the rest flags as we don't use them in the markdown rendering. |
| 13 | + """ |
| 14 | + try: |
| 15 | + # Read the file |
| 16 | + with open(file_path, 'r', encoding='utf-8') as file: |
| 17 | + content = file.read() |
| 18 | + |
| 19 | + # Find and replace codeblocks |
| 20 | + transformed_content = transform_codeblocks(content) |
| 21 | + |
| 22 | + # Write file if content was changed |
| 23 | + if transformed_content != content: |
| 24 | + with open(file_path, 'w', encoding='utf-8') as file: |
| 25 | + file.write(transformed_content) |
| 26 | + |
| 27 | + except Exception as e: |
| 28 | + print(f"Error processing file {file_path}: {str(e)}") |
| 29 | + raise |
| 30 | + |
| 31 | +def transform_codeblocks(content): |
| 32 | + """ |
| 33 | + Find code block headers that include file=... and optional flags (noindent, noparse, noeval), |
| 34 | + remove all flags except file=... because we don't process them in docs, |
| 35 | + and move file=... to a new line inside square brackets. |
| 36 | + """ |
| 37 | + |
| 38 | + pattern = re.compile( |
| 39 | + r'^```([a-zA-Z0-9_-]+)' # language |
| 40 | + r'((?:\s+(file=[^\s]+|noindent|noparse|noeval))+)\s*$', # flags |
| 41 | + re.MULTILINE |
| 42 | + ) |
| 43 | + |
| 44 | + def replacer(match): |
| 45 | + language = match.group(1) |
| 46 | + all_flags = match.group(2) |
| 47 | + |
| 48 | + # Extract the file=... part |
| 49 | + file_flag_match = re.search(r'file=[^\s]+', all_flags) |
| 50 | + if file_flag_match: |
| 51 | + file_flag = file_flag_match.group() |
| 52 | + return f"```{language}\n[{file_flag}]" |
| 53 | + else: |
| 54 | + return f"```{language}" |
| 55 | + |
| 56 | + return pattern.sub(replacer, content) |
0 commit comments