Skip to content

Instantly share code, notes, and snippets.

@lightclient
Created August 17, 2020 14:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lightclient/d8525a97f0b3fa798377705263f63f5c to your computer and use it in GitHub Desktop.
Save lightclient/d8525a97f0b3fa798377705263f63f5c to your computer and use it in GitHub Desktop.
Script to convert EIP front matter to TOML
import json
import sys
import yaml
import toml
import datetime
import re
import os
def encode(x):
return toml.dumps(x, encoder=toml.TomlEncoder())[:-1]
def convert_author(s):
s = s.split(",")
authors = []
for a in s:
if a.find("<") > 0:
begin = a.find("<") + 1
end = a.find(">")
authors.append({ "name": a[:begin - 1].strip(), "email": a[begin:end] })
else:
begin = a.find("(") + 2
end = a.find(")")
authors.append({ "name": a[:begin - 3], "github": a[begin:end] })
ret = "["
for (i, a) in enumerate(authors):
ret += "{ name = \"" + a["name"] + "\", "
if "email" in a:
ret += "email = \"" + a["email"] + "\" }"
if "github" in a:
ret += "github = \"" + a["github"] + "\" }"
if i != len(authors) - 1:
ret += ", "
return ret + "]"
def convert(o):
toml = []
toml.append(encode({ "title": o["title"]}))
toml.append("")
toml.append("[extra]")
toml.append(encode({ "eip": o["eip"]}))
toml.append("author = " + convert_author(o["author"]))
if "discussions-to" in o:
toml.append(encode({ "discussions_to": o["discussions-to"]}))
toml.append(encode({ "status": o["status"]}))
toml.append(encode({ "type": o["type"]}))
if "category" in o:
toml.append(encode({ "category": o["category"]}))
toml.append(encode({ "created": datetime.datetime.strptime(o["created"], '%Y-%m-%d').date() }))
if "updated" in o:
toml.append(encode({ "updated": o["updated"] }))
if "requires" in o:
toml.append(encode({ "requires": str(o["requires"]) }))
if "replaces" in o:
toml.append(encode({ "replaces": str(o["replaces"]) }))
if "superseded-by" in o:
toml.append(encode({ "superseded_by": str(o["superseded-by"]) }))
if "review-period-end" in o:
toml.append(encode({ "review_period_end": str(o["review-period-end"]) }))
if "resolution" in o:
toml.append(encode({ "resolution": str(o["resolution"]) }))
return '+++\n' + '\n'.join(toml) + '\n+++'
def run():
for _, _, files in os.walk("eips"):
for fname in files:
if fname == "eip-20-token-standard.md":
continue
print("proccessing " + fname)
with open("eips/" + fname, 'r+', encoding="utf-8") as f:
data = f.read()
begin = data.find("---") + 3
end = data[begin:].find("---") + 3
front_matter = data[begin:end]
o = json.loads(json.dumps(yaml.safe_load(front_matter), ensure_ascii=False, default=str))
update = convert(o)
data = data[end + 3:]
f.seek(0)
f.truncate()
f.write(update + data)
f.close()
if __name__ == "__main__":
run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment