a3d7c3618bdec94037cfe86cabcf1a3a62f717d4
[mDoc.git] / python / mDoc / mdoc / worker.py
1 '''
2 Created on 02.08.2019
3
4 @author: mdoc
5 '''
6
7 from mutil.XmlExtractor import XmlExtractor
8 from mutil import XmlExtractor as XMLE
9 import re
10 '''
11 def outfirst(outfile, tpllayer, attrs, value):
12     if tpllayer["value"]:
13         ostr = tpllayer["value"]
14         if "$value" in ostr:
15             ostr = re.sub(r"\$value", value, ostr, flags=re.MULTILINE)
16         print (ostr, end="", file=outfile)
17
18 def outlast(outfile, tpllayer, attrs, value):
19     if tpllayer["value"]:
20         print (file=outfile)
21 '''
22 def process_file_layer(source, outfile, global_defs, hierarc, functions, target):
23
24
25     (ctl,elem, value, attrs) = source.extractElement(XMLE.EC_BEG)
26     while ctl != XMLE.EC_END:
27         items = None
28         nexthiera = {}
29         if elem in hierarc:
30             items = hierarc[elem]
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)
35         #elif elem == "exec":
36         #    r = eval(functions[attrs["function"]])
37         #    print (r, end="", file=outfile)
38         else:
39             print ("Warning: No mapping for item:" +elem)
40         if items:
41             # search for "first"
42             for item in items:
43                 if "position" in item["attrs"]:
44                     if item["attrs"]["position"] == "first":
45                         print (item["value"], end="", file=outfile)
46             # proceed working
47             for item in items:
48                 if not "position" in item["attrs"]:
49                     if "exec" in item["attrs"]:
50                         # call function
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)
55                     # check "pre"
56                     if "pre" in item["attrs"]:
57                         print (item["attrs"]["pre"], end="", file=outfile)
58                     # proceed working
59                     nexthiera = item["items"]
60                     if value:
61                         print (value, end="", file=outfile)
62                     process_file_layer(source, outfile, global_defs, nexthiera, functions, target)
63                     # check "post"
64                     if "post" in item["attrs"]:
65                         print (item["attrs"]["post"], end="", file=outfile)
66             # search for "last"
67             for item in items:
68                 if "position" in item["attrs"]:
69                     if item["attrs"]["position"] == "last":
70                         print (item["value"], end="", file=outfile)
71
72         (ctl,elem, value, attrs) = source.extractElement(ctl)
73         
74 def convert (file, format):
75     
76     source = XmlExtractor()
77     source.openInput(file)
78
79     # read whole template
80     tple = XmlExtractor()
81     tpl = tple.readFile("tpl/"+format+".tpl")
82     functions_in_file = tpl["mdoc"][0]["items"]["functions"][0]["items"]["function"]
83     # compile functions
84     functions = {}
85     for f in functions_in_file:
86         name = f["attrs"]["name"]
87         functions[name] = compile(f["value"], '<string>', "exec")
88     
89     global_defs = tpl["mdoc"][0]["items"]["global"][0]["items"]
90     hierarc = tpl["mdoc"][0]["items"]["hierarchical"][0]["items"]
91     
92     # open output file
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)
97     
98     process_file_layer(source, outfile, global_defs, hierarc, functions, format)
99