|
Revision 1, 1.0 kB
(checked in by nEUrOO, 1 year ago)
|
--
|
| Line | |
|---|
| 1 |
|
|---|
| 2 |
from lxml import etree |
|---|
| 3 |
import sys, os |
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
def norm_spaces(s): |
|---|
| 7 |
return ' '.join(s.split()) |
|---|
| 8 |
|
|---|
| 9 |
def indentation(i): |
|---|
| 10 |
r = "" |
|---|
| 11 |
for a in range(0,i): |
|---|
| 12 |
r += "\t" |
|---|
| 13 |
return r |
|---|
| 14 |
|
|---|
| 15 |
def generate_SOURCE(xmlName): |
|---|
| 16 |
print xmlName |
|---|
| 17 |
xsl_tree = etree.parse('./xslt/toWrite.xsl') |
|---|
| 18 |
transform = etree.XSLT(xsl_tree) |
|---|
| 19 |
doc = etree.parse(xmlName) |
|---|
| 20 |
result = str(transform(doc)) |
|---|
| 21 |
r = result.split("\n") |
|---|
| 22 |
formated = [] |
|---|
| 23 |
indent = 1 |
|---|
| 24 |
string = "" |
|---|
| 25 |
chars = "{}" |
|---|
| 26 |
for l in r: |
|---|
| 27 |
if len(l) == 0 or l == "\n": |
|---|
| 28 |
string += " " |
|---|
| 29 |
elif l in chars: |
|---|
| 30 |
string += l |
|---|
| 31 |
if l == "}": indent -= 1 |
|---|
| 32 |
formated.append((indent,string)) |
|---|
| 33 |
if l == "{": indent += 1 |
|---|
| 34 |
string = "" |
|---|
| 35 |
else: |
|---|
| 36 |
string += l |
|---|
| 37 |
if l == ';' and 'for' not in string: |
|---|
| 38 |
formated.append((indent,string)) |
|---|
| 39 |
string = "" |
|---|
| 40 |
|
|---|
| 41 |
out = open(xmlName[:xmlName.rfind('.xml')] + '.php', 'w') |
|---|
| 42 |
out.write("<?php\n") |
|---|
| 43 |
for i,f in formated: |
|---|
| 44 |
out.write(indentation(i) + norm_spaces(f) + "\n") |
|---|
| 45 |
out.write("\n?>") |
|---|
| 46 |
out.close() |
|---|
| 47 |
|
|---|
| 48 |
if __name__ == '__main__': |
|---|
| 49 |
generate_SOURCE(sys.argv[1]) |
|---|