]> wagnertech.de Git - mDoc.git/blob - python/mDoc/mdoc/worker.py
csharp-s6geige
[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                     # determine line end
50                     end = "\n"
51                     if "end" in item["attrs"]:
52                         end = item["attrs"]["end"]
53                     # check "pre"
54                     if "pre" in item["attrs"]:
55                         text = item["attrs"]["pre"]
56                         if text.startswith(r"\n"):
57                             print (file=outfile)
58                             text = text[2:]
59                         print (text, end="", file=outfile)
60                     if "exec" in item["attrs"]:
61                         # call function
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)
68                     else:
69                         # proceed working
70                         nexthiera = item["items"]
71                         if value:
72                             print (value, end="", file=outfile)
73                         process_file_layer(source, outfile, global_defs, nexthiera, functions, target)
74                     # check "post"
75                     if "post" in item["attrs"]:
76                         print (item["attrs"]["post"], end="", file=outfile)
77                     print (end=end, file=outfile)
78             # search for "last"
79             for item in items:
80                 if "position" in item["attrs"]:
81                     if item["attrs"]["position"] == "last":
82                         print (item["value"], end="", file=outfile)
83
84         (ctl,elem, value, attrs) = source.extractElement(ctl)
85         
86 def convert (file, format):
87     
88     source = XmlExtractor()
89     source.openInput(file)
90
91     # read whole template
92     tple = XmlExtractor()
93     tpl = tple.readFile("tpl/"+format+".tpl")
94     functions_in_file = tpl["mdoc"][0]["items"]["functions"][0]["items"]["function"]
95     # compile functions
96     functions = {}
97     for f in functions_in_file:
98         name = f["attrs"]["name"]
99         functions[name] = compile(f["value"], '<string>', "exec")
100     
101     global_defs = tpl["mdoc"][0]["items"]["global"][0]["items"]
102     hierarc = tpl["mdoc"][0]["items"]["hierarchical"][0]["items"]
103     
104     # open output file
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)
109     
110     process_file_layer(source, outfile, global_defs, hierarc, functions, format)
111