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"
# 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
gmi_content_dir = "/srv/gemini/my-site/posts"
# path to gemini blog root directory
gmi_path = "/srv/gemini/my-site"
# path to directory where meta pages will be generated
# such as home page, tags list, tag pages, author pages...
meta_dir = "/srv/gemini/my-site"
# directory within gmi_path which will contains converted posts
posts_dir = "posts"
# path to directory containing templates
tpl_dir = "~/repo/htg-content/gemini/templates"
tpl_path = "/srv/gemini/templates"
# list of markdown files extensions
# Any file with a different extension will be ignored.
md_extensions = [
"markdown",
"mdown",
"mkdn",
"md",
"mkd",
"mdwn",
"mdtxt",
"mdtext",
"text",
"Rmd"
".markdown",
".mdown",
".mkdn",
".md",
".mkd",
".mdwn",
".mdtxt",
".mdtext",
".text",
".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.
# Check out README for more informations.
gmi_extension = "gmi"
gmi_extension = ".gmi"
# replacement map
# 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.setlocale(locale.LC_ALL, config.locale)
md_path = os.path.expanduser(config.md_dir)
gmi_path = os.path.expanduser(config.gmi_dir)
tpl_path = os.path.expanduser(config.tpl_dir)
meta_path = os.path.expanduser(config.meta_dir)
md_path = os.path.abspath(os.path.expanduser(config.md_path))
gmi_path = os.path.abspath(os.path.expanduser(config.posts_path))
tpl_path = os.path.abspath(os.path.expanduser(config.tpl_path))
posts_path = os.path.abspath(gmi_path + "/" + config.posts_dir)
# Initiate meta lists
posts = []
posts_prop_index = {}
posts = [] # This is a flat, unsorted list of posts
posts_prop_index = {} # This is a dict containing posts sorted by properties
for prop_dict in config.index_props:
posts_prop_index[prop_dict["property"]] = {}
os.chdir(md_path)
def add_ext_gmi(link):
# Custom function to apply to links
if "://" not in link: # apply only on local links
return link + ".gmi"
return os.path.splitext(link)[0] + ".gmi"
else:
return link
# 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
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)
for mdfile in mdlist:
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
if extension not in config.md_extensions:
@ -68,6 +54,14 @@ for dirname, subdirlist, mdlist in os.walk("."):
)
pass
post = {}
gmifile = basename
if config.gmi_extension:
gmifile += ".gmi"
post["path"] = os.path.relpath(dirname + "/" + gmifile, md_path)
# Read the Markdown file
with open(dirname + "/" + mdfile, "r") as md:
mdtext = md.read()
@ -156,7 +150,7 @@ for dirname, subdirlist, mdlist in os.walk("."):
with open(tpl_path + "/index.tpl", "r") as tpl:
template = Template(tpl.read())
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)
# Generate custom meta pages
@ -168,15 +162,15 @@ for prop_dict in config.index_props:
) as tpl:
template = Template(tpl.read())
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)
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:
template = Template(tpl.read())
for item in posts_prop_index[prop]:
text = template.render(prop_item=posts_prop_index[prop][item])
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:
gmi.write(text)
@ -184,5 +178,5 @@ for prop_dict in config.index_props:
with open(tpl_path + "/posts_list.tpl", "r") as tpl:
template = Template(tpl.read())
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)