Last active
January 1, 2016 16:28
-
-
Save claudiadadamo/8170459 to your computer and use it in GitHub Desktop.
Script to reorganize text in markdown files created by Exitwp when exporting wordpress files. Created to work for Dropplets formatting for markdown files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/user/bin/python | |
#Claudia D'Adamo, December 2013 | |
# This script will reorder lines in Markdown files to be used as posts for the Dropplets blogging platform. | |
# I used Exitwp to convert Worpress files to Markdown that is compatible with Jekyll but doesn't work with what I want to do | |
import os | |
def main(): | |
markdownFileDirectory = "/Users/DAdamo/Desktop/oldposts" # directory of old markdown files | |
outputDirectory = "/Users/DAdamo/Desktop/droppletsposts/" # directory where you'd like the new files to go | |
os.chdir(markdownFileDirectory) | |
twitterHandle = "claudadamo" | |
author = "Claudia D'Adamo" | |
for filename in os.listdir(markdownFileDirectory): | |
categories = "" | |
markdownFile = open(filename,'r') | |
filename = filename.replace(filename[0:11],"") # remove dates from name of file | |
OutputFileName = filename.replace(filename[-9:],"") # remove .markdown extension from filename | |
OutputFileName = outputDirectory+OutputFileName+".md" # create new filename with .md extension | |
outputFile = open(OutputFileName,'w') | |
infolist = [0] * 5 # empty list to hold all header information | |
infolist[1] = "- "+author+"\n" | |
infolist[2] = "- "+twitterHandle+"\n" # add twitter handle line to list of header info | |
markdownFile.readline() # reads in first line of "---" | |
# go through getting header information that changes post to post | |
for line in markdownFile: | |
if line[0:5] == "title": | |
infolist[0] = "# "+line[6:] | |
if line[0:4] == "date": | |
infolist[3] = "- "+line[6:10]+"/"+line[11:13]+"/"+line[14:16]+"\n" | |
if line[0:2] == "- ": | |
line=line.rstrip() | |
categories=categories+line[2:]+", " | |
if line[0:3] == "---": | |
break | |
infolist[4] = "- "+categories[:-2]+"\n" # add categories in, remove extra formatting | |
for item in infolist: | |
string = str(item) | |
outputFile.write(string) | |
outputFile.write("- published\n") # assumes you'd like these published since they were already published in wordpress | |
for line in markdownFile: # output the body of the post | |
string = str(line) | |
outputFile.write(string) | |
outputFile.close() | |
markdownFile.close() | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment