Skip to content

Instantly share code, notes, and snippets.

@tvdsluijs
Created July 13, 2019 18:56
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 tvdsluijs/b1eaab0bd1e0b5ef2669abdafc76611c to your computer and use it in GitHub Desktop.
Save tvdsluijs/b1eaab0bd1e0b5ef2669abdafc76611c to your computer and use it in GitHub Desktop.
Change jekyll feature-img, image tag to image: paht: and image:
import re
import os
from tqdm import tqdm
class Feature2ImagePath:
def __init__(self):
self.my_file = None
self.backup_file = None
self.file_data = None
def process_files(self, my_file=None, backup_file=None):
self.my_file = my_file
self.backup_file = backup_file
self.read_file()
self.feature_img()
self.path_img()
self.write_file()
def check_make_backup_folder(self):
os.makedirs(folder, exist_ok=True)
def feature_img(self):
regex = r"feature-img: "
test_str = self.file_data
subst = "image: \\n path: "
self.file_data = re.sub(regex, subst, test_str, 0, re.MULTILINE)
self.feature_img_path()
def feature_img_path(self):
regex = r"\tpath: .*/(.*)$"
test_str = self.file_data
subst = "\\g<0>\\n feature: \\1"
self.file_data = re.sub(regex, subst, test_str, 0, re.MULTILINE)
def path_img(self):
regex = r"(.*)(feature:)( .*/(.*))$"
test_str = self.file_data
subst = " path:\\3\\n feature: \\4"
# (.*)(feature:).*(h. * / (.*))['?|\"?]$
self.file_data = re.sub(regex, subst, test_str, 0, re.MULTILINE)
def write_file(self):
if self.my_file is None:
return False
file = open(self.backup_file, "w")
file.write(self.file_data)
file.close()
def read_file(self):
if self.backup_file is None:
return False
file = open(self.my_file, "r")
self.file_data = file.read()
file.close()
if __name__ == '__main__':
dir_path = os.path.dirname(os.path.realpath(__file__))
folder = os.path.join(dir_path, "mds")
backup = os.path.join(dir_path, "new_files")
f = Feature2ImagePath()
for filename in tqdm(os.listdir(folder)):
tqdm.write(filename)
if filename.endswith(".md"):
my_file = os.path.join(folder, filename)
backup_file = os.path.join(backup, filename)
f.process_files(my_file, backup_file)
tqdm.write("Done task")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment