add: convert wiki links to markdown links
This commit is contained in:
1 parent
e2b7c0cd3e
commit
72e95d23ba
1 file changed
+54
@@ -0,0 +1,54 @@
|
||||
import os
|
||||
import re
|
||||
|
||||
"""
|
||||
Dieses Python-Skript durchsucht rekursiv alle .md-Dateien in einem angegebenen Quellordner nach Mediawiki-Links und konvertiert diese in korrekte Markdown-Links.
|
||||
Für jeden gefundenen Mediawiki-Link wird eine Eingabeaufforderung im Terminal angezeigt, um die geplante Änderung zu bestätigen oder abzulehnen.
|
||||
Wenn die Änderung bestätigt wird, ersetzt das Skript den Mediawiki-Link durch den Markdown-Link und speichert die Datei.
|
||||
"""
|
||||
|
||||
# Quellordner definieren
|
||||
# source_folder = './test/'
|
||||
source_folder = '/Users/rob/Documents/Obsidian'
|
||||
|
||||
# Mediawiki Link Pattern
|
||||
mediawiki_link_pattern = re.compile(r'\[(http.?://[^\s]+)\s([^\]]+)\]')
|
||||
|
||||
def convert_links_in_file(file_path):
|
||||
with open(file_path, 'r', encoding='utf-8') as file:
|
||||
content = file.read()
|
||||
|
||||
def replace_link(match):
|
||||
#ignoriere Mediawiki-Links, die als Anzeigetext eine URL haben
|
||||
if match.group(2).startswith('[http') or match.group(1) == match.group(2):
|
||||
return match.group(0)
|
||||
#ignoriere strings, einen Zeilenumbruch enthalten
|
||||
if match.group(0).find('\n') != -1:
|
||||
return match.group(0)
|
||||
|
||||
url = match.group(1)
|
||||
text = match.group(2)
|
||||
markdown_link = f'[{text}]({url})'
|
||||
print(f'Original: {match.group(0)}')
|
||||
print(f'Änderung: {markdown_link}')
|
||||
confirm = input('Änderung bestätigen? (Enter für Ja, jede andere Taste für Nein): ')
|
||||
if confirm == '':
|
||||
return markdown_link
|
||||
else:
|
||||
return match.group(0)
|
||||
|
||||
new_content = mediawiki_link_pattern.sub(replace_link, content)
|
||||
|
||||
with open(file_path, 'w', encoding='utf-8') as file:
|
||||
file.write(new_content)
|
||||
|
||||
def process_folder(folder):
|
||||
for root, _, files in os.walk(folder):
|
||||
for file in files:
|
||||
if file.endswith('.md'):
|
||||
file_path = os.path.join(root, file)
|
||||
print(f'Verarbeite Datei: {file_path}')
|
||||
convert_links_in_file(file_path)
|
||||
|
||||
# Starte die Verarbeitung
|
||||
process_folder(source_folder)
|
||||
Reference in new issue
Block a user