Skip to content

Instantly share code, notes, and snippets.

@prahladyeri
Created November 2, 2023 12:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save prahladyeri/c03dbbd3745410abd8f62e318437aeac to your computer and use it in GitHub Desktop.
Save prahladyeri/c03dbbd3745410abd8f62e318437aeac to your computer and use it in GitHub Desktop.
Script to convert existing markdown posts (*.md) in your content folder from pelican to jekyll format
##
# script to convert *.md posts in content folder to jekyll format
import os
import glob, re
def main():
files = glob.glob("content/*.md")
for fname in files:
lines = open(fname, encoding='utf8').readlines()
meta = {}
for i in range(len(lines)):
line = lines[i].strip()
m = re.match("(\w*):(\s*)(.*)", line)
if not m: break
key = m[1].strip()
val = m[3].strip()
meta[key] = val
# we have meta data now
#print("meta:", meta)
nmeta, cnt = {}, 0
if meta['Status'] == 'published':
nmeta["date"]=meta["Date"].split(" ")[0]
nmeta["title"]=meta["Title"].strip()
nmeta['tags'] = []
if "Tags" in meta.keys():
nmeta["tags"] = [tag.strip().lower().replace(' ', '-') for tag in meta["Tags"].split(',')]
cat=meta["Category"].strip().lower().replace(' ','-')
if cat not in nmeta['tags']:
nmeta['tags'].append(cat)
if 'Slug' not in meta.keys():
#print('fname:', fname)
nmeta['slug'] = fname.split('\\')[-1]
nmeta['slug'] = nmeta['slug'].split('.')[0]
else:
nmeta['slug'] = meta['Slug']
# create new
nlines = []
nlines.append("---")
nmeta['title'] = nmeta['title'].replace("'", "''")
# if nmeta['title'].startswith('"'):
# print("quote alert:", "title: '%s'" % nmeta['title'])
nlines.append("title: '%s'" % nmeta['title'])
nlines.append("layout: post")
nlines.append("tags: %s" % " ".join(nmeta['tags']))
nlines.append("---")
nlines = [item + "\n" for item in nlines]
if not os.path.isdir("jekyll"):
os.makedirs("jekyll")
nfname = "jekyll/%s-%s.md" % (nmeta['date'], nmeta['slug'])
nf = open(nfname, 'w', encoding='utf8')
nf.writelines(nlines)
nf.writelines(lines[i:])
nf.close()
cnt += 1
#print("out:", nfname)
print("processed %d posts to jekyll/ folder:" % len(files))
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment