Minimizing Documents

.........................................................

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!"

~ End Article and Begin Conversation ~

~ Now It's Your Turn ~

Feel free to use <strong>, <em>, and <a href="">

[]

Archives by Month

The Blogroll

Search this Site


[]