-
Notifications
You must be signed in to change notification settings - Fork 2
/
md2edx.py
117 lines (92 loc) · 3.5 KB
/
md2edx.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import sys
if sys.version_info <= (3, 0):
sys.exit("I am a Python 3 script. Run me with python3.")
import os
import argparse
import glob
import markdown
# Probably going to use this later to grab some specific attributes, like alt text.
# from bs4 import BeautifulSoup
instructions = """
To use:
python3 md2edx.py path/to/markdown/folder (options)
Run this on a folder full of markdown files.
You will get paired HTML files that you can
insert into edX's html folder. You will need the
original XML files to maintain display names.
You can specify the following options:
--help Print this message and exit.
Last update: August 18th 2020
"""
# Main function
def Convert_From_Markdown(args=["-h"]):
print(" Beginning conversion")
# Handle arguments and flags
parser = argparse.ArgumentParser(usage=instructions, add_help=False)
parser.add_argument("--help", "-h", action="store_true")
parser.add_argument("file_names", nargs="*")
# "extra" will help us deal with out-of-order arguments.
args, extra = parser.parse_known_args(args)
# print("Arguments:")
# print(args, extra)
if args.help:
sys.exit(instructions)
# Replace arguments with wildcards with their expansion.
# If a string does not contain a wildcard, glob will return it as is.
# Mostly important if we run this on Windows systems.
file_names = list()
for arg in args.file_names:
file_names += glob.glob(glob.escape(arg))
for item in extra:
file_names += glob.glob(glob.escape(item))
# Don't run the script on itself.
if sys.argv[0] in file_names:
file_names.remove(sys.argv[0])
# If one of the items is a folder, get its contents instead.
interior_files = list()
for f in file_names:
if os.path.isdir(f):
interior_files += glob.glob(f + "/*.md")
file_names.remove(f)
file_names += interior_files
# If the filenames don't exist, say so and quit.
if file_names == []:
sys.exit("No .md files found.")
# Make a directory to store the html files in.
md_folder = os.path.dirname(file_names[0])
html_folder = "html"
dir = os.path.join(md_folder, html_folder)
if not os.path.exists(dir):
os.mkdir(dir)
# Get all the markdown files.
for name in file_names:
if name[-3:] == ".md":
print(name)
# Open the file. Need encoding to avoid nonprinting byte order mark.
f = open(name, encoding="utf-8-sig")
# Get its contents
md = f.read()
# Convert it to html
html = markdown.markdown(md)
# Save the new file in the folder.
new_name_html = name[:-3] + ".html"
new_file_html = open(
os.path.join(md_folder, html_folder, os.path.basename(new_name_html)),
"w",
)
new_file_html.write(html)
new_file_html.close()
# Removed because we're using the original XML files.
# new_name_xml = name[:-3] + ".xml"
# new_file_xml = open(os.path.join(md_folder, html_folder, os.path.basename(new_name_html)), "w")
# new_file_xml.write(
# '<html filename="'
# + new_name_html
# + '" display_name="HTML" editor="raw"/>'
# )
# new_file_xml.close()
f.close()
print(" Conversion complete")
if __name__ == "__main__":
# this won't be run when imported
Convert_From_Markdown(sys.argv)