1
+ """
2
+ Script to make Markdown files MPX compatible.
3
+ This script scans Markdown files and escapes special characters (<, >, {, })
4
+ to make them compatible with the MPX standard used in Docusaurus v3.
5
+ This script complies with:
6
+ 1) Pylint
7
+ 2) Pydocstyle
8
+ 3) Pycodestyle
9
+ 4) Flake8
10
+ """
11
+
12
+ import os
13
+ import argparse
14
+ import re
15
+
16
+ def escape_mpx_characters (text ):
17
+ """
18
+ Escape special characters in a text string for MPX compatibility.
19
+ Avoids escaping already escaped characters.
20
+ Args:
21
+ text: A string containing the text to be processed.
22
+ Returns:
23
+ A string with special characters (<, >, {, }) escaped, avoiding
24
+ double escaping.
25
+ """
26
+ # Regular expressions to find unescaped special characters
27
+ patterns = {
28
+ "<" : r"(?<!\\)<" ,
29
+ ">" : r"(?<!\\)>" ,
30
+ "{" : r"(?<!\\){" ,
31
+ "}" : r"(?<!\\)}"
32
+ }
33
+
34
+ # Replace unescaped special characters
35
+ for char , pattern in patterns .items ():
36
+ text = re .sub (pattern , f"\\ { char } " , text )
37
+
38
+ return text
39
+
40
+ def process_file (filepath ):
41
+ """
42
+ Process a single Markdown file for MPX compatibility.
43
+ Args:
44
+ filepath: The path to the Markdown file to process.
45
+ Returns:
46
+ None, writes the processed content back to the file only if there are changes.
47
+ """
48
+ with open (filepath , 'r' , encoding = 'utf-8' ) as file :
49
+ content = file .read ()
50
+
51
+ # Escape MPX characters
52
+ new_content = escape_mpx_characters (content )
53
+
54
+ # Write the processed content back to the file only if there is a change
55
+ if new_content != content :
56
+ with open (filepath , 'w' , encoding = 'utf-8' ) as file :
57
+ file .write (new_content )
58
+
59
+ def main ():
60
+ """
61
+ Main function to process all Markdown files in a given directory.
62
+ Scans for all Markdown files in the specified directory and processes each
63
+ one for MPX compatibility.
64
+ Args:
65
+ None
66
+ Returns:
67
+ None
68
+ """
69
+ parser = argparse .ArgumentParser (description = "Make Markdown files MPX compatible." )
70
+ parser .add_argument (
71
+ "--directory" ,
72
+ type = str ,
73
+ required = True ,
74
+ help = "Directory containing Markdown files to process."
75
+ )
76
+
77
+ args = parser .parse_args ()
78
+
79
+ # Process each Markdown file in the directory
80
+ for root , _ , files in os .walk (args .directory ):
81
+ for file in files :
82
+ if file .lower ().endswith (".md" ):
83
+ process_file (os .path .join (root , file ))
84
+
85
+ if __name__ == "__main__" :
86
+ main ()
0 commit comments