geminer/geminer.py

133 lines
4.0 KiB
Python
Raw Normal View History

2020-12-02 17:16:29 +01:00
#!/usr/bin/env python3
from md2gemini import md2gemini
import frontmatter
from jinja2 import Template
import os
2020-12-03 11:35:13 +01:00
import locale
2020-12-03 20:24:20 +01:00
from datetime import datetime
2020-12-02 17:16:29 +01:00
import config
2020-12-03 11:35:13 +01:00
# locale (for templates, for example dates rendering)
2020-12-03 16:35:08 +01:00
locale.setlocale(locale.LC_ALL, config.locale)
2020-12-03 11:35:13 +01:00
2020-12-02 17:16:29 +01:00
md_path = os.path.expanduser(config.md_dir)
gmi_path = os.path.expanduser(config.gmi_dir)
tpl_path = os.path.expanduser(config.tpl_dir)
2020-12-03 20:24:20 +01:00
meta_path = os.path.expanduser(config.meta_dir)
# Initiate post meta list
posts_meta = []
2020-12-02 17:16:29 +01:00
os.chdir(md_path)
2020-12-03 16:37:48 +01:00
def add_ext_gmi(link):
2020-12-03 18:36:35 +01:00
# Custom function to apply to links
if "://" not in link: # apply only on local links
return link+".gmi"
2020-12-03 18:46:43 +01:00
else:
return link
2020-12-03 16:37:48 +01:00
2020-12-03 09:01:30 +01:00
# Walk through markdown directories
2020-12-02 17:16:29 +01:00
for dirname, subdirlist, mdlist in os.walk('.'):
2020-12-03 09:01:30 +01:00
# Create same hierarchy in GMI directory
2020-12-02 17:16:29 +01:00
gmi_subpath = os.path.abspath(gmi_path+"/"+dirname)
os.makedirs(gmi_subpath, exist_ok=True)
2020-12-03 09:01:30 +01:00
2020-12-02 17:16:29 +01:00
for mdfile in mdlist:
basename, extension = os.path.splitext(mdfile)
extension = extension[1:]
2020-12-03 09:01:30 +01:00
# We want to ignore the file if this isn't a markdown file
2020-12-02 17:16:29 +01:00
if extension not in config.md_extensions:
print("Ignoring file {}: \"{}\" not in markdown extensions list".format(mdfile, extension))
pass
2020-12-03 09:01:30 +01:00
# Read the Markdown file
2020-12-02 17:16:29 +01:00
with open(dirname+"/"+mdfile, 'r') as md:
mdtext = md.read()
2020-12-03 09:01:30 +01:00
# Parse the YAML header
2020-12-02 17:16:29 +01:00
meta = frontmatter.parse(mdtext)[0]
2020-12-03 09:01:30 +01:00
# Extract useful informations from the header
2020-12-02 17:16:29 +01:00
template = meta.get("template", None)
author = meta.get("author", None)
date = meta.get("date", None)
title = meta.get("title", None)
2020-12-03 20:24:20 +01:00
tags = meta.get("tags", None).split(',')
2020-12-03 09:01:30 +01:00
# For now, tags list must be a comma-separated string
# TODO: make possible to list tags as a YAML list
2020-12-03 20:24:20 +01:00
2020-12-02 17:16:29 +01:00
for item in config.replace:
mdtext = mdtext.replace(item[0],item[1])
2020-12-03 09:01:30 +01:00
# Convert the post into GMI
2020-12-02 17:16:29 +01:00
gmitext = md2gemini(mdtext,
code_tag=config.code_tag,
img_tag=config.img_tag,
indent=config.indent,
ascii_table=config.ascii_table,
frontmatter=True,
links=config.links,
plain=config.plain,
strip_html=config.strip_html,
base_url=config.base_url,
2020-12-03 18:39:42 +01:00
link_func=add_ext_gmi,
2020-12-02 17:16:29 +01:00
table_tag=config.table_tag
)
2020-12-03 09:01:30 +01:00
# Read template file
2020-12-02 17:16:29 +01:00
with open(tpl_path+"/"+template+".tpl", 'r') as tpl:
template = Template(tpl.read())
2020-12-03 09:01:30 +01:00
# We need the relative path without the "./"
2020-12-02 17:16:29 +01:00
simpledirname = dirname[2:]
if simpledirname == "":
path = basename
else:
path = simpledirname + "/" + basename
2020-12-03 09:01:30 +01:00
# Integrate the GMI content in the template
2020-12-02 17:16:29 +01:00
gmitext = template.render(
content=gmitext,
tags=tags,
template=template,
author=author,
date=date,
title=title,
path=path
)
2020-12-03 09:01:30 +01:00
# Dirty fix a weird bug where some lines are CRLF-terminated
2020-12-02 17:16:29 +01:00
gmitext = gmitext.replace('\r\n','\n')
2020-12-03 09:01:30 +01:00
# Time to write the GMI file
2020-12-02 17:16:29 +01:00
gmifile = basename
if config.gmi_extension:
gmifile += ".gmi"
2020-12-03 21:17:21 +01:00
posts_meta.append({
"title": title,
"author": author,
"date": date,
"tags": tags,
"filename": gmifile
})
2020-12-03 21:18:38 +01:00
print(gmi_subpath+"/"+gmifile)
2020-12-02 17:16:29 +01:00
with open(gmi_subpath+"/"+gmifile, 'w') as gmi:
gmi.write(gmitext)
2020-12-03 20:24:20 +01:00
2020-12-03 20:52:30 +01:00
posts_meta.sort(key=lambda p: p["date"], reverse=True)
2020-12-03 20:24:20 +01:00
with open(tpl_path+"/index.tpl", 'r') as tpl:
template = Template(tpl.read())
indextext = template.render(posts_meta=posts_meta)
2020-12-03 20:44:02 +01:00
with open(meta_path+"/index.gmi", 'w') as index:
index.write(indextext)