2 using System.Collections.Generic;
\r
4 using System.Text.RegularExpressions;
\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
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
24 outfile.Write(text);
\r
26 private void process_file_layer() {
\r
28 int ctl = XmlExtractor.EC_BEG;
\r
33 Dictionary<string,string> sattrs = null;
\r
34 Dictionary<string,string> tattrs = null;
\r
35 object tpl_node = null;
\r
37 // extract current data
\r
38 source.getCurrentNodeData(out elem, out dummy, out sattrs);
\r
40 // work only on copy
\r
41 XmlExtractor globex = new XmlExtractor(global_defs.CurrentNode);
\r
43 bool process_node = false;
\r
44 bool hierarc_decend = false;
\r
46 if (elem == "verbatim" && sattrs["target"] == target) {
\r
47 process_node = true;
\r
48 // => no search for template data
\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
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
68 // process source node
\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
75 outfile.Write(doc_value);
\r
76 } else if (child_elem != null) {
\r
78 this.process_file_layer();
\r
80 throw new Exception("Neither text or tag?");
\r
82 ctl = source.extractElement(ctl, out child_elem, out doc_value, out cattrs);
\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
94 if (hierarc_decend) hierarc.toParent();
\r
95 outfile.WriteLine();
\r
97 else if (elem == "#comment") ; // suppress output for comment nodes
\r
98 else Console.WriteLine("No template entry for node: "+elem);
\r
103 print_tpl_text(tpl_node, "pre");
\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
114 // so gehts nicht, die items können mehrfach vorkommen!
\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
122 if (items != null) {
\r
123 XmlExtractor itemex = new XmlExtractor(items, TextMode: true);
\r
131 # search for "first"
\r
133 if "position" in item["attrs"]:
\r
134 if item["attrs"]["position"] == "first":
\r
135 print (item["value"], end="", file=outfile)
\r
138 if not "position" in item["attrs"]:
\r
139 # determine line end
\r
141 if "end" in item["attrs"]:
\r
142 end = item["attrs"]["end"]
\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
149 print (text, end="", file=outfile)
\r
150 if "exec" in item["attrs"]:
\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
160 nexthiera = item["items"]
\r
162 print (value, end="", file=outfile)
\r
163 process_file_layer(source, outfile, global_defs, nexthiera, functions, target)
\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
170 if "position" in item["attrs"]:
\r
171 if item["attrs"]["position"] == "last":
\r
172 print (item["value"], end="", file=outfile)
\r
174 (ctl,elem, value, attrs) = source.extractElement(ctl)
\r
176 public int convert(string file, string tpl_file) {
\r
178 // open xml document
\r
179 source = new XmlExtractor(TextMode: true);
\r
180 source.openInput(file);
\r
182 // goto root element
\r
185 Dictionary<string,string> attrs;
\r
186 source.extractElement(XmlExtractor.EC_BEG, out elem, out value, out attrs);
\r
189 XmlExtractor tple = new XmlExtractor(TextMode: true);
\r
190 tple.openInput(tpl_file);
\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
200 tple.requireChild("mdoc");
\r
201 tple.getCurrentNodeData(out elem, out value, out attrs);
\r
202 string ext = attrs["extension"];
\r
204 int cntl = XmlExtractor.EC_BEG;
\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
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
226 from mutil.XmlExtractor import XmlExtractor
\r
227 from mutil import XmlExtractor as XMLE
\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
237 def outlast(outfile, tpllayer, attrs, value):
\r
238 if tpllayer["value"]:
\r
239 print (file=outfile)
\r