using System; using System.Collections.Generic; using System.IO; using System.Text.RegularExpressions; using mutil; namespace mDoc{ public class Worker { private XmlExtractor source; private string target; private StreamWriter outfile; private XmlExtractor global_defs; private XmlExtractor hierarc; private void print_tpl_text(Dictionary attrs, string text) { string rgx = @"\$\w+"; foreach (Match match in Regex.Matches(text, rgx)) { string subst = attrs[match.Value.Substring(1)]; text = text.Replace(match.Value, subst); } outfile.Write(text); } private void process_file_layer() { int ctl = XmlExtractor.EC_BEG; string elem; string doc_value; string tpl_value; string dummy; Dictionary sattrs = null; Dictionary tattrs = null; object tpl_node = null; // extract current data source.getCurrentNodeData(out elem, out dummy, out sattrs); // work only on copy XmlExtractor globex = new XmlExtractor(global_defs.CurrentNode); bool process_node = false; bool hierarc_decend = false; // check verbatim if (elem == "verbatim" && sattrs["target"] == target) { process_node = true; // => no search for template data } else { // search for template data if (this.hierarc != null) tpl_node = this.hierarc.gotoChild(elem); if (tpl_node != null) hierarc_decend = true; else tpl_node = globex.gotoChild(elem); if (tpl_node != null) process_node = true; } if (process_node) { if (tpl_node != null) { // process template pre node XmlExtractor nodeex = new XmlExtractor(tpl_node); object test_node = nodeex.gotoChild("pre"); if (test_node != null) { nodeex.getCurrentNodeData(out dummy, out tpl_value, out tattrs); print_tpl_text(sattrs, tpl_value); nodeex.toParent(); } } // process source node string child_elem; Dictionary cattrs = null; ctl = source.extractElement(ctl, out child_elem, out doc_value, out cattrs); while (ctl != XmlExtractor.EC_END) { if (doc_value != null) { // text_node outfile.Write(doc_value); } else if (child_elem != null) { // tag node this.process_file_layer(); } else { throw new Exception("Neither text or tag?"); } ctl = source.extractElement(ctl, out child_elem, out doc_value, out cattrs); } if (tpl_node != null) { // process template post node XmlExtractor nodeex = new XmlExtractor(tpl_node); object test_node = nodeex.gotoChild("post"); if (test_node != null) { nodeex.getCurrentNodeData(out dummy, out tpl_value, out tattrs); print_tpl_text(sattrs, tpl_value); nodeex.toParent(); } } if (hierarc_decend) hierarc.toParent(); outfile.WriteLine(); } else { Console.WriteLine("No template entry for node: "+elem); } } /* print_tpl_text(tpl_node, "pre"); // 2) chech for leading texts process_definitions(elem, value, attrs, global_defs, Leading: true); process_definitions(elem, value, attrs, hierarc, Leading: true); } // so gehts nicht, die items können mehrfach vorkommen! // check for hierarchical definition object items = hiraex.gotoChild(elem); if (items == null) { // check for any global definition items = globex.gotoChild(elem); } if (items != null) { XmlExtractor itemex = new XmlExtractor(items, TextMode: true); } } } # search for "first" for item in items: if "position" in item["attrs"]: if item["attrs"]["position"] == "first": print (item["value"], end="", file=outfile) # proceed working for item in items: if not "position" in item["attrs"]: # determine line end end = "\n" if "end" in item["attrs"]: end = item["attrs"]["end"] # check "pre" if "pre" in item["attrs"]: text = item["attrs"]["pre"] if text.startswith(r"\n"): print (file=outfile) text = text[2:] print (text, end="", file=outfile) if "exec" in item["attrs"]: # call function store_current_node = source.currentNode l = {"out" : None, "source":source} exec(functions[item["attrs"]["exec"]], globals(), l) # proceed on previous position source.currentNode = store_current_node print (l["out"], file=outfile) else: # proceed working nexthiera = item["items"] if value: print (value, end="", file=outfile) process_file_layer(source, outfile, global_defs, nexthiera, functions, target) # check "post" if "post" in item["attrs"]: print (item["attrs"]["post"], end="", file=outfile) print (end=end, file=outfile) # search for "last" for item in items: if "position" in item["attrs"]: if item["attrs"]["position"] == "last": print (item["value"], end="", file=outfile) (ctl,elem, value, attrs) = source.extractElement(ctl) */ public int convert(string file, string tpl_file) { // open xml document source = new XmlExtractor(TextMode: true); source.openInput(file); // goto root element string elem; string value; Dictionary attrs; source.extractElement(XmlExtractor.EC_BEG, out elem, out value, out attrs); // open template XmlExtractor tple = new XmlExtractor(TextMode: true); tple.openInput(tpl_file); /* functions_in_file = tpl["mdoc"][0]["items"]["functions"][0]["items"]["function"]; // compile functions var functions = new Dictionary(); for f in functions_in_file: name = f["attrs"]["name"] functions[name] = compile(f["value"], '', "exec") */ tple.requireChild("mdoc"); tple.getCurrentNodeData(out elem, out value, out attrs); string ext = attrs["extension"]; int cntl = XmlExtractor.EC_BEG; cntl = tple.extractElement(cntl, out elem, out value, out attrs); while (cntl != XmlExtractor.EC_END) { if (elem == "global") this.global_defs = new XmlExtractor(tple.CurrentNode, TextMode: true); else if (elem == "hierarchical") this.hierarc = new XmlExtractor(tple.CurrentNode, TextMode: true); cntl = tple.extractElement(cntl, out elem, out value, out attrs); } // open output file //outname = re.sub(r"\.xml", "."+ext, file) string outname = file.Substring(0,file.IndexOf(".")+1) + ext; using ( outfile = new StreamWriter (outname)) { process_file_layer(); } return 0; } } } /* from mutil.XmlExtractor import XmlExtractor from mutil import XmlExtractor as XMLE import re ''' def outfirst(outfile, tpllayer, attrs, value): if tpllayer["value"]: ostr = tpllayer["value"] if "$value" in ostr: ostr = re.sub(r"\$value", value, ostr, flags=re.MULTILINE) print (ostr, end="", file=outfile) def outlast(outfile, tpllayer, attrs, value): if tpllayer["value"]: print (file=outfile) ''' */