Added image file processing and made syntax check happy.
This commit is contained in:
parent
9ec9de475b
commit
bb219e1500
@ -3,6 +3,7 @@
|
|||||||
|
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
import os
|
import os
|
||||||
|
import shutil
|
||||||
import frontmatter
|
import frontmatter
|
||||||
import pytz
|
import pytz
|
||||||
import re
|
import re
|
||||||
@ -19,12 +20,7 @@ GRAVDIR=config["general"]["grav_dir"]
|
|||||||
GRAV_TYPE = config["grav_defaults"]["item_type"]
|
GRAV_TYPE = config["grav_defaults"]["item_type"]
|
||||||
|
|
||||||
# Translates Jekyll top level folders to GRAV (sorted)
|
# Translates Jekyll top level folders to GRAV (sorted)
|
||||||
FIRST_LEVEL = {
|
FIRST_LEVEL = config["jekyll2grav_directories"]
|
||||||
"know-how": "03.know-how",
|
|
||||||
"misc": "04.misc",
|
|
||||||
"reviews": "05.reviews",
|
|
||||||
"software": "06.software"
|
|
||||||
}
|
|
||||||
|
|
||||||
DATEFORMAT_IN = "%Y-%m-%d %H:%M:%S %z"
|
DATEFORMAT_IN = "%Y-%m-%d %H:%M:%S %z"
|
||||||
DATEFORMAT_OUT = "%Y-%m-%d %H:%M:%S"
|
DATEFORMAT_OUT = "%Y-%m-%d %H:%M:%S"
|
||||||
@ -32,9 +28,27 @@ 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
|
# 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):
|
def convert_file(filepath):
|
||||||
|
global target_path
|
||||||
|
|
||||||
print("Loading {}...".format(filepath))
|
print("Loading {}...".format(filepath))
|
||||||
post = frontmatter.load(filepath)
|
post = frontmatter.load(filepath)
|
||||||
|
|
||||||
@ -50,10 +64,9 @@ def convert_file(filepath):
|
|||||||
post["date"] = date_created.strftime(DATEFORMAT_OUT)
|
post["date"] = date_created.strftime(DATEFORMAT_OUT)
|
||||||
post["modified_date"] = date_created.strftime(DATEFORMAT_OUT)
|
post["modified_date"] = date_created.strftime(DATEFORMAT_OUT)
|
||||||
|
|
||||||
|
|
||||||
post["taxonomy"] = {
|
post["taxonomy"] = {
|
||||||
"category": config["grav_defaults"]["categories"],
|
"category": list(config["grav_defaults"]["categories"]),
|
||||||
"tag": config["grav_defaults"]["tags"],
|
"tag": list(config["grav_defaults"]["tags"]),
|
||||||
}
|
}
|
||||||
|
|
||||||
for t in post["tags"]:
|
for t in post["tags"]:
|
||||||
@ -70,16 +83,7 @@ def convert_file(filepath):
|
|||||||
|
|
||||||
print(repr(post.metadata))
|
print(repr(post.metadata))
|
||||||
|
|
||||||
# Handle highlights
|
# Generate new filepath
|
||||||
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
|
|
||||||
pathparts = filepath.split("/")
|
pathparts = filepath.split("/")
|
||||||
pathparts[0] = GRAVDIR
|
pathparts[0] = GRAVDIR
|
||||||
pathparts[1] = FIRST_LEVEL[pathparts[1]]
|
pathparts[1] = FIRST_LEVEL[pathparts[1]]
|
||||||
@ -91,9 +95,29 @@ def convert_file(filepath):
|
|||||||
newfilepath = "/".join(pathparts)
|
newfilepath = "/".join(pathparts)
|
||||||
print("Creating {}...".format(newfilefolder))
|
print("Creating {}...".format(newfilefolder))
|
||||||
os.makedirs(newfilefolder, exist_ok=True)
|
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: 
|
||||||
|
# Images NEW: 
|
||||||
|
body = re.sub(r'!\[(.*?)\]\((.+?)( [\'"].+[\'"])?\)', replace_image, body)
|
||||||
|
|
||||||
|
# FINISH BODY HANDLING
|
||||||
|
post.content = body
|
||||||
|
|
||||||
|
# Write to new location
|
||||||
print("Writing {}...".format(newfilepath))
|
print("Writing {}...".format(newfilepath))
|
||||||
frontmatter.dump(post, newfilepath)
|
frontmatter.dump(post, newfilepath)
|
||||||
|
|
||||||
|
|
||||||
|
# MAIN SCRIPT
|
||||||
num_converted = 0
|
num_converted = 0
|
||||||
for root, dirs, files in os.walk(SRCDIR):
|
for root, dirs, files in os.walk(SRCDIR):
|
||||||
if root.split("/")[-1] in ["assets", "css", "images", "fonts", "javascripts", "_includes", "_layouts", 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
|
num_converted += 1
|
||||||
|
|
||||||
print("Converted {} files.".format(num_converted))
|
print("Converted {} files.".format(num_converted))
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user