Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A python script to speed up the rendering process of Hexo 3.
#!/usr/bin/python2
'''
SYNOPSIS:
$ python speedup.py -f FILE
or
$ python speedup.py -d DIR
'''
import sys, os, getopt
TARGET_TYPE = [".md", ".markdown"]
def process_file(path):
''' Process a file. '''
line = ""
quote_flag = False
line_list = []
with open(path) as f:
while True:
line = f.readline()
if line == "":
break
if line.startswith("```"):
quote_flag = not quote_flag
if line.strip()=="```" and quote_flag:
line = "``` plain\r\n"
line_list.append(line)
with open(path, 'w+') as f:
f.writelines(line_list)
def process_dir(path):
''' Process a directory. '''
file_list = []
files = os.listdir(path)
for file in files:
file = os.path.join(path, file)
root, ext = os.path.splitext(os.path.basename(file))
if os.path.isfile(file) and ext in TARGET_TYPE:
process_file(file)
def main():
if len(sys.argv) < 2:
print "Arguments should be at least 2."
print "python speedup.py -f [FILE]"
print "python speedup.py -d [DIRECTORY]"
exit(1)
try:
opts, args = getopt.getopt(sys.argv[1:], "f:d:", ["file=", "directory="])
for arg, value in opts:
if arg in ('-f', '--file'):
root, ext = os.path.basename(value)
if ext in 'TARGET_TYPE':
process_file(value)
elif arg in ('-d', '--directory'):
process_dir(value)
else:
print "Argument error. %s" % arg
exit(1)
except getopt.GetoptError as e:
print e
exit(1)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment