-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathwiki-to-md.py
55 lines (41 loc) · 1.42 KB
/
wiki-to-md.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import os
import wikipedia
import argparse
import re
def generate_markdown(topic):
try:
page = wikipedia.page(topic)
except wikipedia.exceptions.DisambiguationError as e:
print(e.options)
return None
except wikipedia.exceptions.PageError:
print(f"Page not found for the topic: {topic}")
return None
markdown_text = f"# {topic}\n\n"
page_content = re.sub(r"=== ([^=]+) ===", r"### \1", page.content)
page_content = re.sub(r"== ([^=]+) ==", r"## \1", page_content)
sections = re.split(r"\n(## .*)\n", page_content)
for i in range(0, len(sections), 2):
if i + 1 < len(sections) and any(
line.strip() for line in sections[i + 1].split("\n")
):
markdown_text += f"{sections[i]}\n{sections[i+1]}\n\n"
# Create a directory for markdown files
directory = "md_output"
os.makedirs(directory, exist_ok=True)
filename = os.path.join(directory, f"{topic.replace(' ', '_')}.md")
with open(filename, "w", encoding="utf-8") as md_file:
md_file.write(markdown_text)
print(f"Markdown file created: {filename}")
return filename
parser = argparse.ArgumentParser(
description="Generate a markdown file for a provided topic."
)
parser.add_argument(
"topic",
type=str,
help="The topic to generate a markdown file for.",
)
args = parser.parse_args()
topic = f"{args.topic}"
generate_markdown(topic)