check in
[Ultimately_Untrue_Thought.git] / notes / patriate_links.py
1 #!/usr/bin/python3
2
3 import re
4 import sys
5
6 # Python script to rewrite Markdown links and remove anchors
7 # mostly by GPT-4
8
9 # Define the path for the input and output files
10 input_file_path = sys.argv[1]
11 output_file_path = "out.md"
12
13 # Define the regular expression patterns for Markdown links and anchors
14 markdown_link_pattern = re.compile(r'\]\(/(\d{4}/\w{3}/[-\w]+/)\)')
15 anchor_pattern = re.compile(r'<a id="[-\w]+"></a>')
16
17 # TODO: don't neglect links with #-section
18 # TODO: don't neglect images
19
20 # Read the content of the original markdown file
21 with open(input_file_path, 'r') as file:
22     content = file.read()
23
24 # Replace the Markdown links with the new format
25 rewritten_content = markdown_link_pattern.sub(r'](http://unremediatedgender.space/\1)', content)
26
27 # Remove the anchors
28 rewritten_content = anchor_pattern.sub('', rewritten_content)
29
30 # Write the rewritten content to a new file
31 with open(output_file_path, 'w') as file:
32     file.write(rewritten_content)
33
34 print(f"rewritten markdown has been saved to {output_file_path}")