Comment the ugly code

This commit is contained in:
Guy Godfroy 2020-12-03 09:01:30 +01:00
parent 95c97d8c52
commit 1cd0939230
1 changed files with 26 additions and 0 deletions

View File

@ -13,25 +13,41 @@ tpl_path = os.path.expanduser(config.tpl_dir)
os.chdir(md_path) os.chdir(md_path)
# Walk through markdown directories
for dirname, subdirlist, mdlist in os.walk('.'): for dirname, subdirlist, mdlist in os.walk('.'):
# 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:
basename, extension = os.path.splitext(mdfile) basename, extension = os.path.splitext(mdfile)
extension = extension[1:] extension = extension[1:]
# 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
with open(dirname+"/"+mdfile, 'r') as md: with open(dirname+"/"+mdfile, 'r') as md:
mdtext = md.read() mdtext = md.read()
# Parse the YAML header
meta = frontmatter.parse(mdtext)[0] meta = frontmatter.parse(mdtext)[0]
# Extract useful informations from the header
template = meta.get("template", None) template = meta.get("template", None)
author = meta.get("author", None) author = meta.get("author", None)
date = meta.get("date", None) date = meta.get("date", None)
title = meta.get("title", None) title = meta.get("title", None)
tags = meta.get("tags", None) tags = meta.get("tags", None)
# For now, tags list must be a comma-separated string
# TODO: make possible to list tags as a YAML list
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
gmitext = md2gemini(mdtext, gmitext = md2gemini(mdtext,
code_tag=config.code_tag, code_tag=config.code_tag,
img_tag=config.img_tag, img_tag=config.img_tag,
@ -45,13 +61,19 @@ for dirname, subdirlist, mdlist in os.walk('.'):
md_links=True, md_links=True,
table_tag=config.table_tag table_tag=config.table_tag
) )
# Read template file
with open(tpl_path+"/"+template+".tpl", 'r') as tpl: with open(tpl_path+"/"+template+".tpl", 'r') as tpl:
template = Template(tpl.read()) template = Template(tpl.read())
# We need the relative path without the "./"
simpledirname = dirname[2:] simpledirname = dirname[2:]
if simpledirname == "": if simpledirname == "":
path = basename path = basename
else: else:
path = simpledirname + "/" + basename path = simpledirname + "/" + basename
# Integrate the GMI content in the template
gmitext = template.render( gmitext = template.render(
content=gmitext, content=gmitext,
tags=tags, tags=tags,
@ -61,7 +83,11 @@ for dirname, subdirlist, mdlist in os.walk('.'):
title=title, title=title,
path=path path=path
) )
# 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
gmifile = basename gmifile = basename
if config.gmi_extension: if config.gmi_extension:
gmifile += ".gmi" gmifile += ".gmi"