let's try this

This commit is contained in:
Guy Godfroy 2020-12-07 16:24:33 +01:00
parent 06b82addf8
commit 660ab0d5da
2 changed files with 42 additions and 49 deletions

View File

@ -5,37 +5,36 @@
locale = "fr_FR.utf8" locale = "fr_FR.utf8"
# path to directory containing markdonw files to convert # path to directory containing markdonw files to convert
md_dir = "/srv/my-site-content/posts" md_path = "/srv/my-site-content/posts"
# path to directory where gemini files will be exported # path to gemini blog root directory
gmi_content_dir = "/srv/gemini/my-site/posts" gmi_path = "/srv/gemini/my-site"
# path to directory where meta pages will be generated # directory within gmi_path which will contains converted posts
# such as home page, tags list, tag pages, author pages... posts_dir = "posts"
meta_dir = "/srv/gemini/my-site"
# path to directory containing templates # path to directory containing templates
tpl_dir = "~/repo/htg-content/gemini/templates" tpl_path = "/srv/gemini/templates"
# list of markdown files extensions # list of markdown files extensions
# Any file with a different extension will be ignored. # Any file with a different extension will be ignored.
md_extensions = [ md_extensions = [
"markdown", ".markdown",
"mdown", ".mdown",
"mkdn", ".mkdn",
"md", ".md",
"mkd", ".mkd",
"mdwn", ".mdwn",
"mdtxt", ".mdtxt",
"mdtext", ".mdtext",
"text", ".text",
"Rmd" ".Rmd"
] ]
# Specify gemini files extension. Don't specify to disable extension. # Specify gemini files extension. Set to empty string to disable extension.
# Warning: disabling could have unwanted side effects. # Warning: disabling could have unwanted side effects.
# Check out README for more informations. # Check out README for more informations.
gmi_extension = "gmi" gmi_extension = ".gmi"
# replacement map # replacement map
# Some CMS make you use some placeholders (for instance for assets URL). # Some CMS make you use some placeholders (for instance for assets URL).

View File

@ -13,51 +13,37 @@ import config
# locale (for templates, for example dates rendering) # locale (for templates, for example dates rendering)
locale.setlocale(locale.LC_ALL, config.locale) locale.setlocale(locale.LC_ALL, config.locale)
md_path = os.path.expanduser(config.md_dir) md_path = os.path.abspath(os.path.expanduser(config.md_path))
gmi_path = os.path.expanduser(config.gmi_dir) gmi_path = os.path.abspath(os.path.expanduser(config.posts_path))
tpl_path = os.path.expanduser(config.tpl_dir) tpl_path = os.path.abspath(os.path.expanduser(config.tpl_path))
meta_path = os.path.expanduser(config.meta_dir)
posts_path = os.path.abspath(gmi_path + "/" + config.posts_dir)
# Initiate meta lists # Initiate meta lists
posts = [] posts = [] # This is a flat, unsorted list of posts
posts_prop_index = {} posts_prop_index = {} # This is a dict containing posts sorted by properties
for prop_dict in config.index_props: for prop_dict in config.index_props:
posts_prop_index[prop_dict["property"]] = {} posts_prop_index[prop_dict["property"]] = {}
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 os.path.splitext(link)[0] + ".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(md_path):
# 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(posts_path + "/" + os.path.relpath(dirname, md_path))
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:]
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:
@ -68,6 +54,14 @@ for dirname, subdirlist, mdlist in os.walk("."):
) )
pass pass
post = {}
gmifile = basename
if config.gmi_extension:
gmifile += ".gmi"
post["path"] = os.path.relpath(dirname + "/" + gmifile, md_path)
# 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()
@ -156,7 +150,7 @@ for dirname, subdirlist, mdlist in os.walk("."):
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(gmi_path + "/index.gmi", "w") as gmi:
gmi.write(text) gmi.write(text)
# Generate custom meta pages # Generate custom meta pages
@ -168,15 +162,15 @@ for prop_dict in config.index_props:
) as tpl: ) 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(gmi_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(gmi_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( with open(
meta_path + "/" + prop_dict.get("item_dir", prop) + "/" + item + ".gmi", "w" gmi_path + "/" + prop_dict.get("item_dir", prop) + "/" + item + ".gmi", "w"
) as gmi: ) as gmi:
gmi.write(text) gmi.write(text)
@ -184,5 +178,5 @@ for prop_dict in config.index_props:
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(gmi_path + "/posts.gmi", "w") as gmi:
gmi.write(text) gmi.write(text)