#!/usr/bin/env python # #The McSweenifier - formats text in the style of the McSweeney's #journal. (mcsweeneys.net) # #by Leonard Richardson leonardr@segfault.org 20010317 # #This is a Python 1.5.2 script because that's all I have avaliable on #the machine that hosts my website. #The McSweenifiedTitle formats any string as a McSweeney's title. class McSweenifiedTitle: def __init__(self, title=""): self.McSweeneyize(title) def McSweeneyize(self, title): import string import re #Make it all caps. title = string.upper(title) #Add line breaks to the title. title = self.newlinify(title) #Separate each character by a space. I personally think it #would look better if chunks consisting of multiple #punctuation marks and one alphanumeric were kept together, #but I'm not McSweeney's, and this is easier to do anyway. title = re.sub("\(\w\)","\\1 ", title); #Turn whitespace into three  s. title = re.sub("[ \t\b][ \t\b]+", "   ", title); #Change newlines into HTML breaks. title = re.sub("\n", "
", title); self.title = title #Adds newlines to the text. Current nonelegant but surprisingly #effective algorithm puts up to 20 characters on a line. Respect #existing line breaks; this lets you do a title in several #sections, in which each section is formatted separately. def newlinify(self, title): import string workingBreakLocation = 0 lastBreakLocation = 0 for i in range(0, len(title)): if title[i] == " ": workingBreakLocation = i elif title[i] == "\n": lastBreakLocation = i if (i >= (lastBreakLocation + 20)): lastBreakLocation = workingBreakLocation title = title[:workingBreakLocation] + "\n" + title[workingBreakLocation+1:] return title def __str__(self): return self.title #The McSweeneysArticle class can print out some HTML which closely #approximates a McSweeney's article. class McSweeneysArticle: def __init__(self, authorname, authoremail, title, article): import string import re #Build the byline. if (authorname == ''): authorname = 'Anonymous' self.authorname = string.upper(authorname) emaillink = "" if (authoremail == ""): emaillink = self.authorname else: emaillink = '%s' % (authoremail, self.authorname) self.byline = 'BY ' + emaillink #Provide a default title if (title==""): if (article==""): title="An Article Which Consists Entirely Of This Title" else: title="An Article With A Generic-Sounding Title" self.title = title #McSweeneyify the title. self.newTitle = McSweenifiedTitle(title); #Do minimal processing of the article text. article = re.sub("\r", "", article) self.article = re.sub("\n\n", "

", article) def __str__(self): return """The McSweenifier: %s


%s

%s

- - - -

%s

""" % (self.title, self.newTitle, self.byline, self.article) #The McSweenifier CGI looks for the article title, etc. in CGI #form fields, McSweenifies what it finds, and prints the result out #in an authentic-looking page. def goCGI(): print 'Content-type: text/html\n\n' import cgi form = cgi.FieldStorage() values = {} for key in ('authorname', 'authoremail', 'title', 'article'): if (form.has_key(key)): values[key]=form[key].value else: values[key]="" title = values['title'] article = values['article'] authorname = values['authorname'] authoremail = values['authoremail'] print McSweeneysArticle(authorname, authoremail, title, article) #Main execution flow: just call the CGI method. import traceback try: goCGI() except SystemExit: #We're fine. a = 1 except: #Something went wrong. Print a traceback. print "\n\n
"			
    traceback.print_exc()
    print "
"