Created
February 23, 2015 14:52
-
-
Save VikParuchuri/1a65b15cb10584568003 to your computer and use it in GitHub Desktop.
Convert fields to markdown
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import re | |
import yaml | |
import textwrap | |
import html2text | |
def write_to_yaml(k, v, yaml_data): | |
if isinstance(v, str): | |
v = v.strip() | |
if v.count("\n") == 0 and k not in ["left_text", "initial_display", "hint", "answer", "check_val", "imports", "instructions"]: | |
yaml_data.append("{0}: {1}".format(k, v)) | |
else: | |
yaml_data.append("{0}: |".format(k)) | |
lines = textwrap.indent(v, " ").split("\n") | |
for i, l in enumerate(lines): | |
yaml_data.append("{0}".format(l)) | |
elif isinstance(v, dict): | |
yaml_data.append("{0}:".format(k)) | |
for vk in sorted(v.keys()): | |
yaml_data.append(" {0}: |".format(vk)) | |
lines = textwrap.indent(v[vk], " ") | |
for l in lines.split("\n"): | |
yaml_data.append(l) | |
else: | |
yaml_data.append("{0}: {1}".format(k, v)) | |
return yaml_data | |
base_path = "/Users/vik/DataScience/dscontent" | |
mission_dirs = [os.path.join(base_path, d) for d in os.listdir(base_path) if os.path.isdir(d) and not d.startswith(".")] | |
for mdir in mission_dirs: | |
yaml_file = [os.path.join(mdir, f) for f in os.listdir(mdir) if f.endswith(".yaml")][0] | |
with open(yaml_file, 'r') as yaml_read_file: | |
data = yaml_read_file.read() | |
mission_data = re.split("-{4,}", data) | |
mission_data = [yaml.safe_load(i) for i in mission_data] | |
meta = mission_data[1] | |
screens = [i for i in mission_data[2:] if i is not None] | |
for s in screens: | |
for k in ["left_text", "instructions", "hint"]: | |
if k in s: | |
s[k] = s[k].replace("<python>", "<br/>```python<br/>") | |
s[k] = s[k].replace("</python>", "<br/>```<br/>") | |
s[k] = s[k].replace("<math>", "$$") | |
s[k] = s[k].replace("</math>", "$$") | |
s[k] = html2text.html2text(s[k]) | |
separator = "---------" | |
yaml_data = [separator] | |
for k in ["name", "description", "author", "prerequisites", "language", "premium", "under_construction", "file_list", "mission_number", "demo", "imports", "vars"]: | |
if k in meta and meta[k] is not None: | |
v = meta[k] | |
yaml_data = write_to_yaml(k, v, yaml_data) | |
yaml_data.append(separator) | |
yaml_data.append("") | |
for s in screens: | |
for k in ["name", "type", "video", "error_okay", "initial_vars", "left_text", "initial_display", "instructions", "answer", "hint", "check_vars", "check_val", "check_code_run", "no_answer_needed"]: | |
if k in s and s[k] is not None: | |
yaml_data = write_to_yaml(k, s[k], yaml_data) | |
yaml_data.append("") | |
yaml_data.append(separator) | |
yaml_data.append("") | |
with open(yaml_file, "w+") as yaml_write_file: | |
yaml_write_file.write("\n".join(yaml_data)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment