7 from mutil.XmlExtractor import XmlExtractor
8 from mutil import XmlExtractor as XMLE
11 def outfirst(outfile, tpllayer, attrs, value):
13 ostr = tpllayer["value"]
15 ostr = re.sub(r"\$value", value, ostr, flags=re.MULTILINE)
16 print (ostr, end="", file=outfile)
18 def outlast(outfile, tpllayer, attrs, value):
22 def process_file_layer(source, outfile, global_defs, hierarc, functions, target):
25 (ctl,elem, value, attrs) = source.extractElement(XMLE.EC_BEG)
26 while ctl != XMLE.EC_END:
31 elif elem in global_defs:
32 items = global_defs[elem]
33 elif elem == "verbatim" and attrs["target"] == target:
34 print (value, end="", file=outfile)
36 # r = eval(functions[attrs["function"]])
37 # print (r, end="", file=outfile)
39 print ("Warning: No mapping for item:" +elem)
43 if "position" in item["attrs"]:
44 if item["attrs"]["position"] == "first":
45 print (item["value"], end="", file=outfile)
48 if not "position" in item["attrs"]:
49 if "exec" in item["attrs"]:
51 l = {"out" : None, "source":source}
52 exec(functions[item["attrs"]["exec"]], globals(), l)
53 #exec('out = "Das kommt von foo()"', globals(), l)
54 print (l["out"], file=outfile)
56 if "pre" in item["attrs"]:
57 print (item["attrs"]["pre"], end="", file=outfile)
59 nexthiera = item["items"]
61 print (value, end="", file=outfile)
62 process_file_layer(source, outfile, global_defs, nexthiera, functions, target)
64 if "post" in item["attrs"]:
65 print (item["attrs"]["post"], end="", file=outfile)
68 if "position" in item["attrs"]:
69 if item["attrs"]["position"] == "last":
70 print (item["value"], end="", file=outfile)
72 (ctl,elem, value, attrs) = source.extractElement(ctl)
74 def convert (file, format):
76 source = XmlExtractor()
77 source.openInput(file)
81 tpl = tple.readFile("tpl/"+format+".tpl")
82 functions_in_file = tpl["mdoc"][0]["items"]["functions"][0]["items"]["function"]
85 for f in functions_in_file:
86 name = f["attrs"]["name"]
87 functions[name] = compile(f["value"], '<string>', "exec")
89 global_defs = tpl["mdoc"][0]["items"]["global"][0]["items"]
90 hierarc = tpl["mdoc"][0]["items"]["hierarchical"][0]["items"]
93 ext = tpl["mdoc"][0]["attrs"]["extension"]
94 outname = re.sub(r"\.xml", "."+ext, file)
95 outfile = open(outname, "w")
96 #print ("hallo", file=outfile)
98 process_file_layer(source, outfile, global_defs, hierarc, functions, format)