]> wagnertech.de Git - mDoc.git/blob - csharp/mDocLib/Worker.cs
csharp-s6ts
[mDoc.git] / csharp / mDocLib / Worker.cs
1 using System;\r
2 using System.Collections.Generic;\r
3 using System.IO;\r
4 using System.Text.RegularExpressions;\r
5 \r
6 using mutil;\r
7 \r
8 namespace mDoc{\r
9 \r
10 public class Worker\r
11 {\r
12     private XmlExtractor source;\r
13     private string target;\r
14     private StreamWriter outfile;\r
15     private XmlExtractor global_defs;\r
16     private XmlExtractor hierarc;\r
17    \r
18     private void print_tpl_text(Dictionary<string,string> attrs, string text) {\r
19          string rgx = @"\$\w+";\r
20          foreach (Match match in Regex.Matches(text, rgx)) {\r
21             string subst = attrs[match.Value.Substring(1)];\r
22             text = text.Replace(match.Value, subst);\r
23          }\r
24          outfile.Write(text);\r
25     }\r
26     private void process_file_layer() {\r
27 \r
28         int ctl = XmlExtractor.EC_BEG;\r
29         string elem;\r
30         string doc_value;\r
31         string tpl_value;\r
32         string dummy;\r
33         Dictionary<string,string> sattrs = null;\r
34         Dictionary<string,string> tattrs = null;\r
35         object tpl_node = null;\r
36 \r
37         // extract current data\r
38         source.getCurrentNodeData(out elem, out dummy, out sattrs);\r
39 \r
40         // work only on copy\r
41         XmlExtractor globex = new XmlExtractor(global_defs.CurrentNode);\r
42 \r
43         bool process_node = false;\r
44         bool hierarc_decend = false;\r
45         // check verbatim\r
46         if (elem == "verbatim" && sattrs["target"] == target) {\r
47             process_node = true;\r
48             // => no search for template data\r
49         }\r
50         else {\r
51             // search for template data\r
52             if (this.hierarc != null) tpl_node = this.hierarc.gotoChild(elem);\r
53             if (tpl_node != null) hierarc_decend = true;\r
54             else tpl_node = globex.gotoChild(elem);\r
55             if (tpl_node != null) process_node = true;\r
56         }\r
57         if (process_node) {\r
58             if (tpl_node != null) {\r
59                 // process template pre node\r
60                 XmlExtractor nodeex = new XmlExtractor(tpl_node);\r
61                 object test_node = nodeex.gotoChild("pre");\r
62                 if (test_node != null) {\r
63                     nodeex.getCurrentNodeData(out dummy, out tpl_value, out tattrs);\r
64                     print_tpl_text(sattrs, tpl_value);\r
65                     nodeex.toParent();\r
66                 }\r
67             }\r
68             // process source node\r
69             string child_elem;\r
70             Dictionary<string,string> cattrs = null;\r
71             ctl = source.extractElement(ctl, out child_elem, out doc_value, out cattrs);\r
72             while (ctl != XmlExtractor.EC_END) {\r
73                 if (doc_value != null) {\r
74                     // text_node\r
75                     outfile.Write(doc_value);\r
76                 } else if (child_elem != null) {\r
77                     // tag node\r
78                     this.process_file_layer();\r
79                 } else {\r
80                     throw new Exception("Neither text or tag?");\r
81                 }\r
82                 ctl = source.extractElement(ctl, out child_elem, out doc_value, out cattrs);\r
83             }\r
84             if (tpl_node != null) {\r
85                 // process template post node\r
86                 XmlExtractor nodeex = new XmlExtractor(tpl_node);\r
87                 object test_node = nodeex.gotoChild("post");\r
88                 if (test_node != null) {\r
89                     nodeex.getCurrentNodeData(out dummy, out tpl_value, out tattrs);\r
90                     print_tpl_text(sattrs, tpl_value);\r
91                     nodeex.toParent();\r
92                 }\r
93             }\r
94             if (hierarc_decend) hierarc.toParent();\r
95             outfile.WriteLine();\r
96         } else {\r
97             Console.WriteLine("No template entry for node: "+elem);\r
98         }\r
99     }\r
100 \r
101 /*\r
102 \r
103                  print_tpl_text(tpl_node, "pre");\r
104 \r
105 \r
106 \r
107                 // 2) chech for leading texts\r
108                 process_definitions(elem, value, attrs, global_defs, Leading: true);\r
109                 process_definitions(elem, value, attrs, hierarc, Leading: true);\r
110             }\r
111             \r
112 \r
113 \r
114             // so gehts nicht, die items können mehrfach vorkommen!\r
115 \r
116             // check for hierarchical definition\r
117             object items = hiraex.gotoChild(elem);\r
118             if (items == null) {\r
119                 // check for any global definition\r
120                 items = globex.gotoChild(elem);\r
121             }\r
122             if (items != null) {\r
123                 XmlExtractor itemex = new XmlExtractor(items, TextMode: true);\r
124             }\r
125         }\r
126     }\r
127 \r
128 \r
129 \r
130 \r
131                 # search for "first"\r
132                 for item in items:\r
133                     if "position" in item["attrs"]:\r
134                         if item["attrs"]["position"] == "first":\r
135                             print (item["value"], end="", file=outfile)\r
136                 # proceed working\r
137                 for item in items:\r
138                     if not "position" in item["attrs"]:\r
139                         # determine line end\r
140                         end = "\n"\r
141                         if "end" in item["attrs"]:\r
142                             end = item["attrs"]["end"]\r
143                         # check "pre"\r
144                         if "pre" in item["attrs"]:\r
145                             text = item["attrs"]["pre"]\r
146                             if text.startswith(r"\n"):\r
147                                 print (file=outfile)\r
148                                 text = text[2:]\r
149                             print (text, end="", file=outfile)\r
150                         if "exec" in item["attrs"]:\r
151                             # call function\r
152                             store_current_node = source.currentNode\r
153                             l = {"out" : None, "source":source}\r
154                             exec(functions[item["attrs"]["exec"]], globals(), l)\r
155                             # proceed on previous position\r
156                             source.currentNode = store_current_node\r
157                             print (l["out"], file=outfile)\r
158                         else:\r
159                             # proceed working\r
160                             nexthiera = item["items"]\r
161                             if value:\r
162                                 print (value, end="", file=outfile)\r
163                             process_file_layer(source, outfile, global_defs, nexthiera, functions, target)\r
164                         # check "post"\r
165                         if "post" in item["attrs"]:\r
166                             print (item["attrs"]["post"], end="", file=outfile)\r
167                         print (end=end, file=outfile)\r
168                 # search for "last"\r
169                 for item in items:\r
170                     if "position" in item["attrs"]:\r
171                         if item["attrs"]["position"] == "last":\r
172                             print (item["value"], end="", file=outfile)\r
173 \r
174             (ctl,elem, value, attrs) = source.extractElement(ctl)\r
175 */\r
176     public int convert(string file, string tpl_file) {\r
177     \r
178         // open xml document\r
179         source = new XmlExtractor(TextMode: true);\r
180         source.openInput(file);\r
181 \r
182         // goto root element\r
183         string elem;\r
184         string value;\r
185         Dictionary<string,string> attrs;\r
186         source.extractElement(XmlExtractor.EC_BEG, out elem, out value, out attrs);\r
187 \r
188         // open template\r
189         XmlExtractor tple = new XmlExtractor(TextMode: true);\r
190         tple.openInput(tpl_file);\r
191         /*\r
192         functions_in_file = tpl["mdoc"][0]["items"]["functions"][0]["items"]["function"];\r
193         // compile functions\r
194         var functions = new Dictionary<string,string>();\r
195         for f in functions_in_file:\r
196             name = f["attrs"]["name"]\r
197             functions[name] = compile(f["value"], '<string>', "exec")\r
198         */\r
199         \r
200         tple.requireChild("mdoc");\r
201         tple.getCurrentNodeData(out elem, out value, out attrs);\r
202         string ext = attrs["extension"];\r
203 \r
204         int cntl = XmlExtractor.EC_BEG;\r
205 \r
206         cntl = tple.extractElement(cntl, out elem, out value, out attrs);\r
207         while (cntl != XmlExtractor.EC_END) {\r
208             if (elem == "global") this.global_defs = new XmlExtractor(tple.CurrentNode, TextMode: true);\r
209             else if (elem == "hierarchical") this.hierarc = new XmlExtractor(tple.CurrentNode, TextMode: true);\r
210             cntl = tple.extractElement(cntl, out elem, out value, out attrs);\r
211         }\r
212         \r
213         // open output file\r
214         //outname = re.sub(r"\.xml", "."+ext, file)\r
215         string outname = file.Substring(0,file.IndexOf(".")+1) + ext;\r
216         using ( outfile = new StreamWriter (outname)) {\r
217             process_file_layer();\r
218         }\r
219         return 0;\r
220     }\r
221 }\r
222 \r
223 }\r
224 \r
225 /*\r
226 from mutil.XmlExtractor import XmlExtractor\r
227 from mutil import XmlExtractor as XMLE\r
228 import re\r
229 '''\r
230 def outfirst(outfile, tpllayer, attrs, value):\r
231     if tpllayer["value"]:\r
232         ostr = tpllayer["value"]\r
233         if "$value" in ostr:\r
234             ostr = re.sub(r"\$value", value, ostr, flags=re.MULTILINE)\r
235         print (ostr, end="", file=outfile)\r
236 \r
237 def outlast(outfile, tpllayer, attrs, value):\r
238     if tpllayer["value"]:\r
239         print (file=outfile)\r
240 '''\r
241         \r
242 */\r