mcuserv/blog/blog.py

67 lines
2.9 KiB
Python

import cPickle, subprocess, tempfile, markdown2, config, time, argparse, tempfile, os;
#print("MineRobber's blog generator")
#print("------------------------")
try:
blog_posts = cPickle.load(open("posts.pickled","rb"))
except:
blog_posts = []
parser = argparse.ArgumentParser(description="A blog generator.")
parser.add_argument("--workflow",help="Run the basic workflow (write a new post, generate the page, and quit)",action="store_true")
parser.add_argument("--generate",help="Generate the page and quit",action="store_true")
args = parser.parse_args()
if not args.workflow and not args.generate:
print("MineRobber's blog generator")
print("------------------------")
print("'n' for new post, 'g' to generate, 'q' to quit.")
running = True;
def tweetlink():
return config.tweetlink_do and config.tweetlink_location and config.tweetlink_url
def loop(choice):
if choice.lower() == "n":
title = raw_input("Title: ")
bodyf = tempfile.NamedTemporaryFile()
os.system("nano {}".format(bodyf.name))
body = markdown2.markdown(bodyf.read().rstrip())
bodyf.close()
blog_posts.append([title, body])
elif choice.lower() == "g":
rss = "<?xml version='1.0' encoding='UTF-8'?><rss version='2.0'><channel><title>"+config.title+"</title><link>"+config.url+"</link><description>"+config.desc+"</description>"
contents = "<html><head><title>{0}</title><link rel='stylesheet' href='{1}'></head><body><h1>{0}</h1>".format(config.title,config.css_location)
blog_posts.reverse()
i = len(blog_posts)
for post in blog_posts:
contents += "<h2 id='post"+str(i)+"'>"+post[0]+"</h2>"+post[1]
if tweetlink():
contents += "<p><a href='{}'>Tweet this</a></p>".format(config.tweetlink_url+"#post"+str(i))
rss += "<item><title>{0}</title><link>{3}#post{1!s}</link><description>{2}</description></item>".format(post[0],i,post[1][3:-5].replace("<","&lt;").replace(">","&gt;"),config.url)
i -= 1
blog_posts.reverse()
# remove footer
# contents += "<hr><p>Generated by <a href='https://github.com/MineRobber9000/blog-gen'>MineRobber's Python blog generator.</a><br><a href='{}'>RSS Feed</a></p>".format(config.rss_url)
contents += "</body></html>"
rss += "</channel></rss>"
with open(config.location,"wb") as f:
f.write(contents);
with open(config.rss_location,"wb") as f2:
f2.write(rss)
if tweetlink():
with open(config.tweetlink_location,"wb") as f3:
f3.write("<script>document.location='https://twitter.com/home?status={}'+document.location.hash.split('').slice(1).join('')</script>".format(config.tweetlink_format).format(config.title,config.url))
elif choice.lower() == "q":
return False
else:
print("Unknown option: "+choice)
cPickle.dump(blog_posts,open("posts.pickled","wb"))
return True
if not args.workflow and not args.generate:
while running:
running = loop(raw_input("> "))
print("bye")
elif not args.workflow and args.generate:
loop("g")
loop("q")
else:
loop("n")
loop("g")
loop("q")