Recently I have been doing a bit of work using JS to build the entire DOM for a page after only passing it a JS Array of the contents, this technique works quite well to reduce server load as all the processing is done on the client’s site with identical templating information sent to everyone, and only an array that is different.
One problem while writing code this way is that I needed a simple way to insert an entire template into one line of JS code. Most everything was html, which meant new line and tab characters were pretty prevalent. All I needed was a simple script to strip out new lines and tabs, sounds like a perfect job for Python.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | #!/usr/bin/env python # encoding: utf-8 """ htmlMinimizer.py Created by Christopher King on 2009-07-03. Copyright (c) 2009 Strogn Consulting. All rights reserved. """ #imports import os, string #files and containers sourceFile = open('inputFile.html', 'rw') destFile = open('outputFileOneLine.html', 'wb') destContainer = "" #itterative work for line in sourceFile: lineContent = str(line) lineContent = lineContent.strip("\t") lineContent = lineContent.rstrip("\n") destContainer = destContainer + lineContent #file writing destFile.write(str(destContainer)) #file closing sourceFile.close() destFile.close() #confirmation message print "Document Minimized!" |
July 6th, 2009
by Matt
You should learn regex for occasions like this.
July 6th, 2009
by Chris
The Author
So using regular expressions… What would that code look like?