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)
|
|
|
|
|
2020-12-04 09:27:29 +01:00
|
|
|
# Initiate meta lists
|
|
|
|
posts = []
|
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-04 08:26:23 +01:00
|
|
|
|
2020-12-04 09:27:29 +01:00
|
|
|
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
|
|
|
|
|
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-04 09:27:29 +01:00
|
|
|
post["template"] = meta.get("template", None)
|
|
|
|
post["author"] = meta.get("author", None)
|
|
|
|
post["date"] = meta.get("date", None)
|
|
|
|
post["title"] = meta.get("title", None)
|
|
|
|
post["tags"] = 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-04 09:27:29 +01:00
|
|
|
# Replace stuff
|
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-04 09:34:05 +01:00
|
|
|
with open(tpl_path+"/"+post["template"]+".tpl", 'r') as tpl:
|
2020-12-02 17:16:29 +01:00
|
|
|
template = Template(tpl.read())
|
2020-12-04 09:27:29 +01:00
|
|
|
|
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,
|
2020-12-04 09:27:29 +01:00
|
|
|
meta = post
|
2020-12-02 17:16:29 +01:00
|
|
|
)
|
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
|
|
|
|
2020-12-04 09:27:29 +01:00
|
|
|
posts.append(post)
|
2020-12-03 21:17:21 +01:00
|
|
|
|
2020-12-04 09:27:29 +01:00
|
|
|
# Time to write the GMI file
|
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-04 09:27:29 +01:00
|
|
|
posts.sort(key=lambda p: p["date"], reverse=True)
|
2020-12-03 20:24:20 +01:00
|
|
|
|
2020-12-03 22:07:38 +01:00
|
|
|
# Generate home page
|
2020-12-03 20:24:20 +01:00
|
|
|
with open(tpl_path+"/index.tpl", 'r') as tpl:
|
|
|
|
template = Template(tpl.read())
|
2020-12-04 09:27:29 +01:00
|
|
|
text = template.render(posts=posts)
|
2020-12-04 08:26:23 +01:00
|
|
|
with open(meta_path+"/index.gmi", 'w') as gmi:
|
|
|
|
gmi.write(text)
|
2020-12-03 22:07:38 +01:00
|
|
|
|
2020-12-04 08:26:23 +01:00
|
|
|
# Generate posts list page
|
2020-12-03 22:07:38 +01:00
|
|
|
with open(tpl_path+"/posts_list.tpl", 'r') as tpl:
|
|
|
|
template = Template(tpl.read())
|
2020-12-04 09:27:29 +01:00
|
|
|
text = template.render(posts=posts)
|
2020-12-04 08:26:23 +01:00
|
|
|
with open(meta_path+"/posts.gmi", 'w') as gmi:
|
|
|
|
gmi.write(text)
|
|
|
|
|
|
|
|
# Generate tags list page
|
|
|
|
with open(tpl_path+"/tags_list.tpl", 'r') as tpl:
|
|
|
|
template = Template(tpl.read())
|
2020-12-04 09:27:29 +01:00
|
|
|
text = template.render(posts=posts)
|
2020-12-04 08:26:23 +01:00
|
|
|
with open(meta_path+"/tags.gmi", 'w') as gmi:
|
|
|
|
gmi.write(text)
|