Tuesday, November 18, 2008

HTML Helper Mode and Indentation

This is my third post for the day. I blogged about this and this earlier today. Writing smaller pieces regularly works much better than waiting for the big post. Commenters, do let me know if you like this style better.

I am really irritated with the html-helper-mode's indentation. It flattens the code rather than indenting it. I missed my tidy command from yesteryears when I did not use emacs. But I am coding on emacs now. I do not have to miss anything from within emacs. So, I added a tidy-html function to format the html as properly indented XHTML.

(add-hook 'html-helper-mode-hook
(lambda ()
(defun tidy-html ()
(interactive)
(save-excursion
(call-process-region (point-min) (point-max)
"c:\\tools\\tidy.exe" t '(t nil) nil "-i" "-asxml")))
(define-key html-helper-mode-map [f7] 'tidy-html)))
view raw tidy-html.el hosted with ❤ by GitHub


And I absolutely love Gist. It has solved all the quirks involved with posting code on a blog.

Meetings and Boredom continue

I am sitting on yet another boring meeting and have written a script to write it to a PDF. Its not a pretty one but I got it done in 30 mins. That has to count for something. You would need ReportLab and PIL for this.

import re
from reportlab.platypus import Image, SimpleDocTemplate, Spacer,Flowable
from reportlab.lib.units import inch
import dircache
import Image as PILImage
import os.path
months = ["January", "Feburary", "March","April","May","June","July","August","September","October","November","December"]
class Bookmark(Flowable):
def __init__(self,title,key,level):
self.title=title
self.key=key
self.level=level
Flowable.__init__(self)
def wrap(self, availWidth, availHeight):
return (0,0)
def draw(self):
self.canv.showOutline()
self.canv.bookmarkPage(self.key)
self.canv.addOutlineEntry(self.title,self.key,self.level, 0)
base_dir='c:\\work\\toons\\dilbert\\'
files=filter(lambda x: x.endswith('gif'),dircache.listdir(base_dir))
doc=SimpleDocTemplate('dilbert.pdf')
oldyear=0
oldmonth=0
contents=[]
contents.append(Bookmark("Contents","Contents",0))
for f in files:
m=re.match(r'(\d\d\d\d)\-(\d\d)\-(\d\d)\.gif',f)
if(m==None):
continue
m=m.groups()
if(oldyear!=int(m[0]) or oldmonth!=int(m[1])):
contents.append( Bookmark("" + months[int(m[1])-1] + " " + str(int(m[0])),"" + months[int(m[1])-1] + " " + str(int(m[0])),1))
oldyear=int(m[0])
oldmonth=int(m[1])
#print os.path.join(base_dir,f)
fname=os.path.join(base_dir,f)
size=PILImage.open(fname).size
#print size
contents.append(Spacer(1,inch))
contents.append(Image(fname,doc.width,size[1]*(doc.width/size[0])))
print 'building...'
doc.build(contents)


Beautiful Soup and ReportLab rock.

Meetings and Boredom

I am sitting in a really boring meeting. I was browsing reddit and found a very interesting dilbert script. I am a huge fan of Dilbert and wanted to download Dilbert strips for offline viewing. I know its not entirely ethical but what the hell. I wrote a small python screen scraper to do that. This requires you to download Beautiful Soup. If I am too bored, I will write a script to compose it as a PDF.

import urllib2
import datetime
import os.path
from BeautifulSoup import BeautifulSoup
dilbert_dir = 'c:\\work\\toons\\dilbert\\'
year=2007
start_date=datetime.date(year,1,1)
one_day = datetime.timedelta(days=1)
current_date = start_date
base_url = r'http://www.dilbert.com/fast/'
while(current_date.year==year and current_date <= datetime.date.today()):
dtfmt=current_date.strftime('%Y-%m-%d')
contents=urllib2.urlopen(base_url + dtfmt + '/').read()
soup=BeautifulSoup(contents)
img_loc=soup.findAll('img')[-1]['src']
print 'downloading ', img_loc, ' as ', dtfmt, '.gif'
img=urllib2.urlopen('http://www.dilbert.com/' + img_loc).read()
f=open(os.path.join(dilbert_dir,dtfmt+'.gif'),'wb')
f.write(img)
f.close()
current_date= current_date + one_day