Cleanup
authorJean-Michel Nirgal Vourgère <jmv@nirgal.com>
Mon, 23 Mar 2009 08:01:58 +0000 (08:01 +0000)
committerJean-Michel Nirgal Vourgère <jmv@nirgal.com>
Mon, 23 Mar 2009 08:01:58 +0000 (08:01 +0000)
Added command line options

htmlentities.py

index abc1bc82b87a0a538d32803bc2bb43671f3fb84f..fbc246be750ab6e2af394f2ad573ee9426395202 100755 (executable)
@@ -316,6 +316,7 @@ def checkForUnicodeReservedChar(value):
         return value
     return iso88591_remap[value-127]
 
+
 def expand(text):
     result = u''
     for c in text:
@@ -327,6 +328,7 @@ def expand(text):
             result += c
     return result
 
+
 def resolve(text):
     pos = 0
     result = u''
@@ -426,28 +428,32 @@ def cleanCDATA(text):
     """
     tmp = resolve(text)
     result = u''
-    isLastWhite = False # so that first white is not removed
+    was_white = False # so that first white is not removed
     for c in tmp:
         if c in ' \r\n\t':
-            if not isLastWhite:
+            if not was_white:
                 result += u' '
-                isLastWhite = True
+                was_white = True
         else:
             result += c
-            isLastWhite = False
+            was_white = False
 
     return expand(result)
 
 
 if __name__ == '__main__':
     import sys
-    if len(sys.argv)<2:
-        print >> sys.stderr, u"Missing required parameter. Try '&amp;test'"
+    from optparse import OptionParser
+    parser = OptionParser()
+    parser.add_option("-a", "--action", help="action: resolve, expand or clean [default: %default]", action="store", dest="action", choices=['expand', 'resolve', 'clean'], default='clean')
+    (options, args) = parser.parse_args()
+    if not args:
+        print >> sys.stderr, u"Missing required parameter. Try '&centest'"
         sys.exit(1)
-    input = unicode(' '.join(sys.argv[1:]), 'utf-8')
-    #print 'input:', input
-    #raw = resolve(input)
-    #print 'resolved:', raw
-    #print 'expanded:', expand(raw)
-    print 'cleanCDATA:', cleanCDATA(input)
-
+    input = unicode(' '.join(args), 'utf-8')
+    if options.action=='resolve':
+        print resolve(input).encode('utf-8')
+    elif options.action=='expand':
+        print expand(input)
+    else: # options.action=='clean':
+        print cleanCDATA(input)