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"]:
51 if "end" in item["attrs"]:
52 end = item["attrs"]["end"]
54 if "pre" in item["attrs"]:
55 text = item["attrs"]["pre"]
56 if text.startswith(r"\n"):
59 print (text, end="", file=outfile)
60 if "exec" in item["attrs"]:
62 store_current_node = source.currentNode
63 l = {"out" : None, "source":source}
64 exec(functions[item["attrs"]["exec"]], globals(), l)
65 # proceed on previous position
66 source.currentNode = store_current_node
67 print (l["out"], file=outfile)
70 nexthiera = item["items"]
72 print (value, end="", file=outfile)
73 process_file_layer(source, outfile, global_defs, nexthiera, functions, target)
75 if "post" in item["attrs"]:
76 print (item["attrs"]["post"], end="", file=outfile)
77 print (end=end, file=outfile)
80 if "position" in item["attrs"]:
81 if item["attrs"]["position"] == "last":
82 print (item["value"], end="", file=outfile)
84 (ctl,elem, value, attrs) = source.extractElement(ctl)
86 def convert (file, format):
88 source = XmlExtractor()
89 source.openInput(file)
93 tpl = tple.readFile("tpl/"+format+".tpl")
94 functions_in_file = tpl["mdoc"][0]["items"]["functions"][0]["items"]["function"]
97 for f in functions_in_file:
98 name = f["attrs"]["name"]
99 functions[name] = compile(f["value"], '<string>', "exec")
101 global_defs = tpl["mdoc"][0]["items"]["global"][0]["items"]
102 hierarc = tpl["mdoc"][0]["items"]["hierarchical"][0]["items"]
105 ext = tpl["mdoc"][0]["attrs"]["extension"]
106 outname = re.sub(r"\.xml", "."+ext, file)
107 outfile = open(outname, "w")
108 #print ("hallo", file=outfile)
110 process_file_layer(source, outfile, global_defs, hierarc, functions, format)