Skip to content

Instantly share code, notes, and snippets.

@aquinzi
Created January 11, 2014 21:25
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 aquinzi/8377090 to your computer and use it in GitHub Desktop.
Save aquinzi/8377090 to your computer and use it in GitHub Desktop.
Converts multimarkdown meta block (pelican) to nikola's; also moves "draft" status and summary
# Converts multimarkdown meta block (pelican) to rst (nikola); also moves "draft" status and summary
#
import os, sys, codecs
# recursive, paths from working dir
FROM_PATH = ""
OUTPUT_PATH = None #if none, replace file
SUMMARY_TO_DESCRIPTION = True # Put summary key (pelican) to description key (nikola)
def file_open_save(path, mode, text=False):
if sys.version_info[0] == 3:
if not mode == 'w':
return open(path, mode, encoding='utf-8')
else:
try:
# make dirs in path tree from path + filename
os.makedirs(os.path.dirname(path))
except OSError:
pass
with open(path, mode, encoding='utf-8') as outputFile:
outputFile.write(text)
else:
if not mode == 'w':
return codecs.open(path, mode, encoding='utf-8')
else:
try:
# make dirs in path tree from path + filename
os.makedirs(os.path.dirname(path))
except OSError:
pass
with codecs.open(path, mode, encoding='utf-8') as outputFile:
outputFile.write(text)
if not os.path.exists(FROM_PATH) and not os.path.isdir(FROM_PATH):
print (" Something wrong")
sys.exit(0)
if OUTPUT_PATH and (not os.path.exists(OUTPUT_PATH) or not os.path.isdir(OUTPUT_PATH)):
print (" Output path must be an existing folder")
sys.exit(0)
print (" Starting..")
# full paths
FROM_PATH = os.path.abspath(FROM_PATH)
if OUTPUT_PATH:
OUTPUT_PATH = os.path.abspath(OUTPUT_PATH)
# get filepaths
files_list = list()
for root, subFolders, files in os.walk(FROM_PATH):
for filename in files:
filePath = os.path.join(root, filename)
if os.path.exists(filePath):
files_list.append(filePath)
if len(files_list) == 0:
print (" No files found")
sys.exit(0)
# open, replacing. save
for this_file in files_list:
print (" Processing: " + os.path.relpath(this_file, FROM_PATH))
new_meta = list()
with file_open_save(this_file, 'r') as the_text:
#clean lines also
input_text = [x.rstrip() for x in the_text.readlines()]
draft_found = False
for l_number, line in enumerate(input_text):
if line and not line.isspace():
#just in case, lets lowercase the meta key
meta_key, rest = line.split(":", 1)
meta_key = meta_key.lower()
meta_line = ".. " + meta_key + ":" + rest
if meta_key == "status":
draft_found = meta_line
if meta_key == "summary" and SUMMARY_TO_DESCRIPTION:
meta_line = ".. description:" + rest
new_meta.append(meta_line)
else:
# meta ended, find draft key (pelican) and put it in tags (nikola)
if draft_found:
new_meta.remove(draft_found)
i = 0
while (i <= len(new_meta)):
if new_meta[i].startswith(".. tags:"):
new_meta[i] = new_meta[i] + ", draft"
break
i += 1
new_file = new_meta + input_text[l_number:]
new_file = "\n".join(new_file)
break
#save
new_path = this_file
if OUTPUT_PATH:
new_path = os.path.join(OUTPUT_PATH, os.path.relpath(this_file, FROM_PATH))
file_open_save(new_path, 'w', new_file)
print (" Finished.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment