Skip to content

Instantly share code, notes, and snippets.

@joelotz
Created February 1, 2021 16:25
Show Gist options
  • Save joelotz/fc92891d13c71021cf4359e492ab3f8c to your computer and use it in GitHub Desktop.
Save joelotz/fc92891d13c71021cf4359e492ab3f8c to your computer and use it in GitHub Desktop.
This is a quick little script that converts pelican metadata syntax into pandoc metadata syntax. This is simply as you just need to fence it in with 3-dashes. This adds this to all files in current dir with .md extension.
"""This is a quick little script that converts pelican metadata syntax into
pandoc metadata syntax. This is simply as you just need to fence it in with 3-dashes.
This adds this to all files in current dir with .md extension
"""
from os import walk
def list_files(directory, extension):
for (dirpath, dirnames, filenames) in walk(directory):
return (f for f in filenames if f.endswith('.' + extension))
def add_yaml(files):
for f in files:
# pull file contents into a temp var
f_read = open(directory + f, 'r')
buff = f_read.read()
f_read.close()
# add a string to the beginning
new = '---\n' + buff
# find the first double break, which indicates end of title block
new = new.replace('\n\n', '\n---\n\n',1)
f_write = open(directory + f, 'w')
f_write.write(new)
f_write.close()
print("Updating file: %s" % f)
return
if __name__ == "__main__":
directory = '/content/'
files = list_files(directory, "md")
add_yaml(files)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment