From 67efc1260642a17d965646d5e48875b73ea7255a Mon Sep 17 00:00:00 2001
From: Jeroen van der Gun <jeroen@blijbol.nl>
Date: Fri, 15 Jul 2011 10:14:31 +0200
Subject: [PATCH] Python script making a minified ruleset library XML file

---
 makelibrary.py |   55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 55 insertions(+), 0 deletions(-)
 create mode 100644 makelibrary.py

diff --git a/makelibrary.py b/makelibrary.py
new file mode 100644
index 0000000..7fe6bfd
--- /dev/null
+++ b/makelibrary.py
@@ -0,0 +1,55 @@
+#!/usr/bin/env python
+# makelibrary.py
+# Makes a minified ruleset library XML file.
+# Note: this script needs to be modified if new elements with text content are introduced, otherwise they will loose their text.
+import os
+directory = os.path.dirname(__file__)
+with open(os.path.join(directory, 'src/chrome/content/rules/default.rulesets'), 'w') as libfile:
+ libfile.write('<rulesetlibrary>')
+ xmlspaces = (' ', '\r', '\n', '\t')
+ import glob
+ for ruleset in glob.iglob(os.path.join(directory, 'src/chrome/content/rules/*.xml')):
+  with open(ruleset) as file:
+   xml = file.read()
+   i = 0
+   while True: # loop over XML tags
+    j = xml.find('<', i)
+    if j == -1: # if no tag found
+     break # no more tags
+    if xml[j+1:j+4] == '!--': # if the tag is a comment
+     i = xml.find('-->', j) # jump over the comment
+     assert i != -1
+    else:
+     i = xml.find('>', j) + 1 # jump over the tag
+     assert i != 0
+     tag = xml[j:i] # now look at the tag itself
+     k = -1
+     for testspace in xmlspaces:
+      l = tag.find(testspace)
+      if l != -1 and (k == -1 or l < k):
+       k = l
+     if k == -1: # if tag does not contain whitespace
+      libfile.write(tag) # write the full tag
+     else:
+      libfile.write(tag[:k]) # write the element name
+      k = k + 1
+      while True: # loop over attributes
+       while tag[k] in xmlspaces:
+        k = k + 1 # jump over white space
+       l = -1
+       for testquote in ('"', "'"): # the precense of a quotation mark indicates there is an attribute
+        m = tag.find(testquote, k)
+        if m != -1 and (l == -1 or m < l):
+         quote = testquote
+         l = m
+       if l == -1: # if no attribute found
+        if tag[-2] == '/': # if self-closing tag
+         libfile.write('/')
+        libfile.write('>')
+        break # no more attributes
+       l = tag.find(quote, l + 1) + 1 # find end of attribute
+       assert l != 0
+       libfile.write(' ')
+       libfile.write(tag[k:l]) # write the attribute
+       k = l
+ libfile.write('</rulesetlibrary>')
\ No newline at end of file
-- 
1.7.3.1.msysgit.0

