execute black on it
This commit is contained in:
parent
2045f36df0
commit
d38231a13d
93
geminer.py
93
geminer.py
|
@ -26,18 +26,20 @@ for prop_dict in config.index_props:
|
||||||
|
|
||||||
os.chdir(md_path)
|
os.chdir(md_path)
|
||||||
|
|
||||||
|
|
||||||
def add_ext_gmi(link):
|
def add_ext_gmi(link):
|
||||||
# Custom function to apply to links
|
# Custom function to apply to links
|
||||||
if "://" not in link: # apply only on local links
|
if "://" not in link: # apply only on local links
|
||||||
return link+".gmi"
|
return link + ".gmi"
|
||||||
else:
|
else:
|
||||||
return link
|
return link
|
||||||
|
|
||||||
|
|
||||||
# Walk through markdown directories
|
# Walk through markdown directories
|
||||||
for dirname, subdirlist, mdlist in os.walk('.'):
|
for dirname, subdirlist, mdlist in os.walk("."):
|
||||||
|
|
||||||
# Create same hierarchy in GMI directory
|
# Create same hierarchy in GMI directory
|
||||||
gmi_subpath = os.path.abspath(gmi_path+"/"+dirname)
|
gmi_subpath = os.path.abspath(gmi_path + "/" + dirname)
|
||||||
os.makedirs(gmi_subpath, exist_ok=True)
|
os.makedirs(gmi_subpath, exist_ok=True)
|
||||||
|
|
||||||
for mdfile in mdlist:
|
for mdfile in mdlist:
|
||||||
|
@ -59,11 +61,15 @@ for dirname, subdirlist, mdlist in os.walk('.'):
|
||||||
|
|
||||||
# We want to ignore the file if this isn't a markdown file
|
# We want to ignore the file if this isn't a markdown file
|
||||||
if extension not in config.md_extensions:
|
if extension not in config.md_extensions:
|
||||||
print("Ignoring file {}: \"{}\" not in markdown extensions list".format(mdfile, extension))
|
print(
|
||||||
|
'Ignoring file {}: "{}" not in markdown extensions list'.format(
|
||||||
|
mdfile, extension
|
||||||
|
)
|
||||||
|
)
|
||||||
pass
|
pass
|
||||||
|
|
||||||
# Read the Markdown file
|
# Read the Markdown file
|
||||||
with open(dirname+"/"+mdfile, 'r') as md:
|
with open(dirname + "/" + mdfile, "r") as md:
|
||||||
mdtext = md.read()
|
mdtext = md.read()
|
||||||
|
|
||||||
# Parse the YAML header
|
# Parse the YAML header
|
||||||
|
@ -81,20 +87,29 @@ for dirname, subdirlist, mdlist in os.walk('.'):
|
||||||
prop = prop_dict["property"]
|
prop = prop_dict["property"]
|
||||||
prop_raw = meta.get(prop, None)
|
prop_raw = meta.get(prop, None)
|
||||||
if prop_dict.get("list", False) and prop_raw:
|
if prop_dict.get("list", False) and prop_raw:
|
||||||
post[prop] = [{"name": word, "slug": slugify(word)} for word in prop_raw.split(',')]
|
post[prop] = [
|
||||||
|
{"name": word, "slug": slugify(word)}
|
||||||
|
for word in prop_raw.split(",")
|
||||||
|
]
|
||||||
for item in post[prop]:
|
for item in post[prop]:
|
||||||
slug = item["slug"]
|
slug = item["slug"]
|
||||||
if slug in posts_prop_index[prop]:
|
if slug in posts_prop_index[prop]:
|
||||||
posts_prop_index[prop][slug]["posts"].append(post)
|
posts_prop_index[prop][slug]["posts"].append(post)
|
||||||
else:
|
else:
|
||||||
posts_prop_index[prop][slug] = {"name": item["name"], "posts": [post]}
|
posts_prop_index[prop][slug] = {
|
||||||
|
"name": item["name"],
|
||||||
|
"posts": [post],
|
||||||
|
}
|
||||||
else:
|
else:
|
||||||
post[prop] = {"name": prop_raw, "slug": slugify(prop_raw)}
|
post[prop] = {"name": prop_raw, "slug": slugify(prop_raw)}
|
||||||
slug = post[prop]["slug"]
|
slug = post[prop]["slug"]
|
||||||
if slug in posts_prop_index[prop]:
|
if slug in posts_prop_index[prop]:
|
||||||
posts_prop_index[prop][slug]["posts"].append(post)
|
posts_prop_index[prop][slug]["posts"].append(post)
|
||||||
else:
|
else:
|
||||||
posts_prop_index[prop][slug] = {"name": post[prop]["name"], "posts": [post]}
|
posts_prop_index[prop][slug] = {
|
||||||
|
"name": post[prop]["name"],
|
||||||
|
"posts": [post],
|
||||||
|
}
|
||||||
|
|
||||||
posts.append(post)
|
posts.append(post)
|
||||||
|
|
||||||
|
@ -103,65 +118,69 @@ for dirname, subdirlist, mdlist in os.walk('.'):
|
||||||
|
|
||||||
# Replace stuff
|
# Replace stuff
|
||||||
for item in config.replace:
|
for item in config.replace:
|
||||||
mdtext = mdtext.replace(item[0],item[1])
|
mdtext = mdtext.replace(item[0], item[1])
|
||||||
|
|
||||||
# Convert the post into GMI
|
# Convert the post into GMI
|
||||||
gmitext = md2gemini(mdtext,
|
gmitext = md2gemini(
|
||||||
code_tag=config.code_tag,
|
mdtext,
|
||||||
img_tag=config.img_tag,
|
code_tag=config.code_tag,
|
||||||
indent=config.indent,
|
img_tag=config.img_tag,
|
||||||
ascii_table=config.ascii_table,
|
indent=config.indent,
|
||||||
frontmatter=True,
|
ascii_table=config.ascii_table,
|
||||||
links=config.links,
|
frontmatter=True,
|
||||||
plain=config.plain,
|
links=config.links,
|
||||||
strip_html=config.strip_html,
|
plain=config.plain,
|
||||||
base_url=config.base_url,
|
strip_html=config.strip_html,
|
||||||
link_func=add_ext_gmi,
|
base_url=config.base_url,
|
||||||
table_tag=config.table_tag
|
link_func=add_ext_gmi,
|
||||||
)
|
table_tag=config.table_tag,
|
||||||
|
)
|
||||||
|
|
||||||
# Read template file
|
# Read template file
|
||||||
with open(tpl_path+"/"+post["template"]+".tpl", 'r') as tpl:
|
with open(tpl_path + "/" + post["template"] + ".tpl", "r") as tpl:
|
||||||
template = Template(tpl.read())
|
template = Template(tpl.read())
|
||||||
|
|
||||||
# Integrate the GMI content in the template
|
# Integrate the GMI content in the template
|
||||||
gmitext = template.render(content=gmitext, meta = post)
|
gmitext = template.render(content=gmitext, meta=post)
|
||||||
|
|
||||||
# Dirty fix a weird bug where some lines are CRLF-terminated
|
# Dirty fix a weird bug where some lines are CRLF-terminated
|
||||||
gmitext = gmitext.replace('\r\n','\n')
|
gmitext = gmitext.replace("\r\n", "\n")
|
||||||
|
|
||||||
|
|
||||||
# Time to write the GMI file
|
# Time to write the GMI file
|
||||||
with open(gmi_subpath+"/"+gmifile, 'w') as gmi:
|
with open(gmi_subpath + "/" + gmifile, "w") as gmi:
|
||||||
gmi.write(gmitext)
|
gmi.write(gmitext)
|
||||||
|
|
||||||
# Generate home page
|
# Generate home page
|
||||||
with open(tpl_path+"/index.tpl", 'r') as tpl:
|
with open(tpl_path + "/index.tpl", "r") as tpl:
|
||||||
template = Template(tpl.read())
|
template = Template(tpl.read())
|
||||||
text = template.render(posts=posts)
|
text = template.render(posts=posts)
|
||||||
with open(meta_path+"/index.gmi", 'w') as gmi:
|
with open(meta_path + "/index.gmi", "w") as gmi:
|
||||||
gmi.write(text)
|
gmi.write(text)
|
||||||
|
|
||||||
# Generate custom meta pages
|
# Generate custom meta pages
|
||||||
for prop_dict in config.index_props:
|
for prop_dict in config.index_props:
|
||||||
prop = prop_dict["property"]
|
prop = prop_dict["property"]
|
||||||
if "index_name" in prop_dict:
|
if "index_name" in prop_dict:
|
||||||
with open(tpl_path+"/"+prop_dict.get("index_tpl",prop)+".tpl", 'r') as tpl:
|
with open(
|
||||||
|
tpl_path + "/" + prop_dict.get("index_tpl", prop) + ".tpl", "r"
|
||||||
|
) as tpl:
|
||||||
template = Template(tpl.read())
|
template = Template(tpl.read())
|
||||||
text = template.render(prop=posts_prop_index[prop])
|
text = template.render(prop=posts_prop_index[prop])
|
||||||
with open(meta_path+"/"+prop_dict["index_name"]+'.gmi', 'w') as gmi:
|
with open(meta_path + "/" + prop_dict["index_name"] + ".gmi", "w") as gmi:
|
||||||
gmi.write(text)
|
gmi.write(text)
|
||||||
os.makedirs(meta_path+"/"+prop_dict.get("item_dir", prop), exist_ok=True)
|
os.makedirs(meta_path + "/" + prop_dict.get("item_dir", prop), exist_ok=True)
|
||||||
with open(tpl_path+"/"+prop_dict.get("item_tpl", prop)+".tpl", 'r') as tpl:
|
with open(tpl_path + "/" + prop_dict.get("item_tpl", prop) + ".tpl", "r") as tpl:
|
||||||
template = Template(tpl.read())
|
template = Template(tpl.read())
|
||||||
for item in posts_prop_index[prop]:
|
for item in posts_prop_index[prop]:
|
||||||
text = template.render(prop_item=posts_prop_index[prop][item])
|
text = template.render(prop_item=posts_prop_index[prop][item])
|
||||||
with open(meta_path+"/"+prop_dict.get("item_dir", prop)+"/"+item+".gmi", "w") as gmi:
|
with open(
|
||||||
|
meta_path + "/" + prop_dict.get("item_dir", prop) + "/" + item + ".gmi", "w"
|
||||||
|
) as gmi:
|
||||||
gmi.write(text)
|
gmi.write(text)
|
||||||
|
|
||||||
# Generate posts list page
|
# Generate posts list page
|
||||||
with open(tpl_path+"/posts_list.tpl", 'r') as tpl:
|
with open(tpl_path + "/posts_list.tpl", "r") as tpl:
|
||||||
template = Template(tpl.read())
|
template = Template(tpl.read())
|
||||||
text = template.render(posts=posts)
|
text = template.render(posts=posts)
|
||||||
with open(meta_path+"/posts.gmi", 'w') as gmi:
|
with open(meta_path + "/posts.gmi", "w") as gmi:
|
||||||
gmi.write(text)
|
gmi.write(text)
|
||||||
|
|
Loading…
Reference in New Issue