soyons fous
This commit is contained in:
parent
80620dbf63
commit
94ffcecf8b
66
geminer.py
66
geminer.py
|
@ -17,8 +17,9 @@ gmi_path = os.path.expanduser(config.gmi_dir)
|
||||||
tpl_path = os.path.expanduser(config.tpl_dir)
|
tpl_path = os.path.expanduser(config.tpl_dir)
|
||||||
meta_path = os.path.expanduser(config.meta_dir)
|
meta_path = os.path.expanduser(config.meta_dir)
|
||||||
|
|
||||||
# Initiate post meta list
|
# Initiate meta lists
|
||||||
posts_meta = []
|
posts = []
|
||||||
|
tags
|
||||||
|
|
||||||
os.chdir(md_path)
|
os.chdir(md_path)
|
||||||
|
|
||||||
|
@ -40,7 +41,18 @@ for dirname, subdirlist, mdlist in os.walk('.'):
|
||||||
basename, extension = os.path.splitext(mdfile)
|
basename, extension = os.path.splitext(mdfile)
|
||||||
extension = extension[1:]
|
extension = extension[1:]
|
||||||
|
|
||||||
print(basename)
|
post = {}
|
||||||
|
|
||||||
|
gmifile = basename
|
||||||
|
if config.gmi_extension:
|
||||||
|
gmifile += ".gmi"
|
||||||
|
|
||||||
|
# We need the relative path without the "./"
|
||||||
|
simpledirname = dirname[2:]
|
||||||
|
if simpledirname == "":
|
||||||
|
post["path"] = gmifile
|
||||||
|
else:
|
||||||
|
post["path"] = simpledirname + "/" + gmifile
|
||||||
|
|
||||||
# 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:
|
||||||
|
@ -55,14 +67,15 @@ for dirname, subdirlist, mdlist in os.walk('.'):
|
||||||
meta = frontmatter.parse(mdtext)[0]
|
meta = frontmatter.parse(mdtext)[0]
|
||||||
|
|
||||||
# Extract useful informations from the header
|
# Extract useful informations from the header
|
||||||
template = meta.get("template", None)
|
post["template"] = meta.get("template", None)
|
||||||
author = meta.get("author", None)
|
post["author"] = meta.get("author", None)
|
||||||
date = meta.get("date", None)
|
post["date"] = meta.get("date", None)
|
||||||
title = meta.get("title", None)
|
post["title"] = meta.get("title", None)
|
||||||
tags = meta.get("tags", None).split(',')
|
post["tags"] = tags = meta.get("tags", None).split(',')
|
||||||
# For now, tags list must be a comma-separated string
|
# For now, tags list must be a comma-separated string
|
||||||
# TODO: make possible to list tags as a YAML list
|
# TODO: make possible to list tags as a YAML list
|
||||||
|
|
||||||
|
# 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])
|
||||||
|
|
||||||
|
@ -85,63 +98,40 @@ for dirname, subdirlist, mdlist in os.walk('.'):
|
||||||
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:]
|
|
||||||
if simpledirname == "":
|
|
||||||
path = basename
|
|
||||||
else:
|
|
||||||
print(gmifile)
|
|
||||||
path = simpledirname + "/" + basename
|
|
||||||
|
|
||||||
# Integrate the GMI content in the template
|
# Integrate the GMI content in the template
|
||||||
gmitext = template.render(
|
gmitext = template.render(
|
||||||
content=gmitext,
|
content=gmitext,
|
||||||
tags=tags,
|
meta = post
|
||||||
template=template,
|
|
||||||
author=author,
|
|
||||||
date=date,
|
|
||||||
title=title,
|
|
||||||
path=path
|
|
||||||
)
|
)
|
||||||
|
|
||||||
# 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')
|
||||||
|
|
||||||
|
posts.append(post)
|
||||||
|
|
||||||
# Time to write the GMI file
|
# Time to write the GMI file
|
||||||
gmifile = basename
|
|
||||||
if config.gmi_extension:
|
|
||||||
gmifile += ".gmi"
|
|
||||||
|
|
||||||
posts_meta.append({
|
|
||||||
"title": title,
|
|
||||||
"author": author,
|
|
||||||
"date": date,
|
|
||||||
"tags": tags,
|
|
||||||
"filename": gmifile
|
|
||||||
})
|
|
||||||
|
|
||||||
with open(gmi_subpath+"/"+gmifile, 'w') as gmi:
|
with open(gmi_subpath+"/"+gmifile, 'w') as gmi:
|
||||||
gmi.write(gmitext)
|
gmi.write(gmitext)
|
||||||
|
|
||||||
posts_meta.sort(key=lambda p: p["date"], reverse=True)
|
posts.sort(key=lambda p: p["date"], reverse=True)
|
||||||
|
|
||||||
# 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_meta=posts_meta)
|
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 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_meta=posts_meta)
|
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)
|
||||||
|
|
||||||
# Generate tags list page
|
# Generate tags list page
|
||||||
with open(tpl_path+"/tags_list.tpl", 'r') as tpl:
|
with open(tpl_path+"/tags_list.tpl", 'r') as tpl:
|
||||||
template = Template(tpl.read())
|
template = Template(tpl.read())
|
||||||
text = template.render(posts_meta=posts_meta)
|
text = template.render(posts=posts)
|
||||||
with open(meta_path+"/tags.gmi", 'w') as gmi:
|
with open(meta_path+"/tags.gmi", 'w') as gmi:
|
||||||
gmi.write(text)
|
gmi.write(text)
|
||||||
|
|
Loading…
Reference in New Issue