From bb219e150009b2aa4c0c38439277ba22ab3078f4 Mon Sep 17 00:00:00 2001 From: Markus Birth Date: Sun, 19 Dec 2021 21:59:14 +0100 Subject: [PATCH] Added image file processing and made syntax check happy. --- jekyll2grav.py | 75 +++++++++++++++++++++++++++++++++----------------- 1 file changed, 49 insertions(+), 26 deletions(-) diff --git a/jekyll2grav.py b/jekyll2grav.py index 38e9eb5..b23609a 100755 --- a/jekyll2grav.py +++ b/jekyll2grav.py @@ -3,6 +3,7 @@ from datetime import datetime import os +import shutil import frontmatter import pytz import re @@ -13,28 +14,41 @@ with open("config.yaml", "rt") as f: print(repr(config)) -SRCDIR=config["general"]["jekyll_dir"] -GRAVDIR=config["general"]["grav_dir"] +SRCDIR = config["general"]["jekyll_dir"] +GRAVDIR = config["general"]["grav_dir"] GRAV_TYPE = config["grav_defaults"]["item_type"] # Translates Jekyll top level folders to GRAV (sorted) -FIRST_LEVEL = { - "know-how": "03.know-how", - "misc": "04.misc", - "reviews": "05.reviews", - "software": "06.software" -} +FIRST_LEVEL = config["jekyll2grav_directories"] -DATEFORMAT_IN="%Y-%m-%d %H:%M:%S %z" -DATEFORMAT_OUT="%Y-%m-%d %H:%M:%S" +DATEFORMAT_IN = "%Y-%m-%d %H:%M:%S %z" +DATEFORMAT_OUT = "%Y-%m-%d %H:%M:%S" -LOCAL_TIMEZONE=pytz.timezone(config["general"]["timezone"]) +LOCAL_TIMEZONE = pytz.timezone(config["general"]["timezone"]) # Jekyll-tags that are considered to be categories, not tags -CATEGORIES=["know-how", "development", "review", "hacking", "hardware", "software", "miscellaneous"] +CATEGORIES = config["grav_categories"] + + +def replace_image(match): + global target_path + old_imgfile = match.group(2) + old_imgfile = re.sub(r'\{\{ ?site\.url ?\}\}', SRCDIR, old_imgfile) + img_name = os.path.basename(old_imgfile) + new_imgfile = "{}/{}".format(target_path, img_name) + print("Copying image {} to {} ...".format(old_imgfile, new_imgfile)) + shutil.copyfile(old_imgfile, new_imgfile) + img_title = "" + if match.group(3): + img_title = match.group(3) + new_string = "![{}]({}{})".format(match.group(1), img_name, img_title) + return new_string + def convert_file(filepath): + global target_path + print("Loading {}...".format(filepath)) post = frontmatter.load(filepath) @@ -50,10 +64,9 @@ def convert_file(filepath): post["date"] = date_created.strftime(DATEFORMAT_OUT) post["modified_date"] = date_created.strftime(DATEFORMAT_OUT) - post["taxonomy"] = { - "category": config["grav_defaults"]["categories"], - "tag": config["grav_defaults"]["tags"], + "category": list(config["grav_defaults"]["categories"]), + "tag": list(config["grav_defaults"]["tags"]), } for t in post["tags"]: @@ -70,16 +83,7 @@ def convert_file(filepath): print(repr(post.metadata)) - # Handle highlights - body = post.content - body = re.sub(r'\{% highlight( (\S+)) %\}', r'```\2', body) - body = re.sub(r'\{% endhighlight %\}', r'```', body) - post.content = body - - # Handle assets? - - - # Generate new filepath and write + # Generate new filepath pathparts = filepath.split("/") pathparts[0] = GRAVDIR pathparts[1] = FIRST_LEVEL[pathparts[1]] @@ -91,9 +95,29 @@ def convert_file(filepath): newfilepath = "/".join(pathparts) print("Creating {}...".format(newfilefolder)) os.makedirs(newfilefolder, exist_ok=True) + target_path = newfilefolder + + # BODY HANDLING + body = post.content + + # Handle highlights + body = re.sub(r'\{% highlight( (\S+)) %\}', r'```\2', body) + body = re.sub(r'\{% endhighlight %\}', r'```', body) + + # Handle assets? + # Images OLD: ![alt text]({{ site.url }}/assets/blah.jpg "title") + # Images NEW: ![alt text](blah.jpg "title") + body = re.sub(r'!\[(.*?)\]\((.+?)( [\'"].+[\'"])?\)', replace_image, body) + + # FINISH BODY HANDLING + post.content = body + + # Write to new location print("Writing {}...".format(newfilepath)) frontmatter.dump(post, newfilepath) + +# MAIN SCRIPT num_converted = 0 for root, dirs, files in os.walk(SRCDIR): if root.split("/")[-1] in ["assets", "css", "images", "fonts", "javascripts", "_includes", "_layouts", SRCDIR]: @@ -106,4 +130,3 @@ for root, dirs, files in os.walk(SRCDIR): num_converted += 1 print("Converted {} files.".format(num_converted)) -