public WebView() { // Add event handler for navigation this.Navigating += new System.Windows.Forms.WebBrowserNavigatingEventHandler(WebView_Navigating); // Initiate parsing mMakoEngine = new MakoEngine(); }
/// <summary> /// Method to test content, output both preparsed and parsed content /// </summary> /// <param name="file"></param> private void TestContent(String file) { Engine = new MakoEngine(); using (StreamReader SR = new StreamReader(file)) { textBox1.Text = SR.ReadToEnd(); ParseFile(textBox1.Text); } }
/// <summary> /// This function preprosses the mako layer /// </summary> /// <param name="input">The input string to parse</param> /// <param name="argument">The argument sent to the parser</param> public String Preprocess(string input, string argument, bool inflate, string uri, bool onlyPreprocess = false) { #region OverlayManager Experimental /**** * STOCKHOLM 2011-07-01 14:45 * * New feature: Apply custom overlays specified by individual service: * Overlay are marked with <#namespace#> where "namespace" is an special * kind of <namespace>.xml view file inside an <extension_dir>/views/ folder * */ // First gain attention by the event OverlayEventArgs args = new OverlayEventArgs(); // Create event args args.URI = uri; if (RequestOverlay != null) { RequestOverlay(this, args); } // if the args has retained the phase, eg. not cancelled if (!args.Cancel) { // Substitute the overlay placeholders with the views if (args.ViewFolders != null) { foreach (KeyValuePair <string, string> overlay in args.ViewFolders) { using (StreamReader SR = new StreamReader(overlay.Value)) { String content = SR.ReadToEnd(); // Substitute overlay placeholders input = input.Replace("<#" + overlay.Key.Replace(".xml", "") + "#>", content); SR.Close(); } } } } // Remove unwanted trailings Regex a = new Regex(@"<\#[^\#]*\#>", RegexOptions.IgnoreCase); input = a.Replace(input, ""); #endregion /** * Begin normal operation * */ // Clear the output buffer Output = ""; /** * Tell the runtime machine about the argument * */ try { String[] arguments = argument.Split(':'); RuntimeMachine.SetVariable("parameter", argument.Replace(arguments[0] + ":", "").Replace(arguments[1] + ":", "")); RuntimeMachine.SetVariable("service", arguments[0]); } catch { } /** * This string defines the call-stack of the query * This is done before any other preprocessing * */ String CallStack = ""; String[] lines = input.Split('\n'); /** * Boolean indicating which parse mode the parser is in, * true = in executable text * false = in output line * */ bool parseMode = false; // Boolean indicating first line is parsing bool firstLine = true; /*** * Iterate through all lines and preprocess the page. * If page starts with an % it will be treated as an preparser code or all content * inside enclosing <? ?> * Two string builders, the first is for the current output segment and the second for the current * executable segmetn * */ StringBuilder outputCode = new StringBuilder(); StringBuilder executableSegment = new StringBuilder(); // The buffer for the final preprocessing output StringBuilder finalOutput = new StringBuilder(); // Append initial case outputCode.Append(""); // Boolean startCase. true = <? false \n% bool startCase = false; // Boolean which tells if the cursor is in the preparse or output part of the buffer (inside or outside an executable segment) bool codeMode = false; for (int i = 0; i < input.Length; i++) { // Check if at an overgoing to an code block if (codeMode) { if ((startCase && input[i] == '?' && input[i + 1] == '>') || (input[i] == '\n' && !startCase)) { codeMode = false; // Jump forward two times if endcase is ?> if (startCase) { i++; } // Get the final output string codeOutput = executableSegment.ToString(); // If in JSPython mode, convert all row breaks to ; and other syntax elements if (JSPython) { codeOutput = codeOutput.Replace("\n", ";"); /** * Convert statements * */ codeOutput = codeOutput.Replace(":", "{"); codeOutput = codeOutput.Replace("end", "}"); codeOutput = codeOutput.Replace("\nif ", "\nif("); codeOutput = codeOutput.Replace("then:", "){"); codeOutput = codeOutput.Replace("do:", "){"); codeOutput = codeOutput.Replace("endif", "}"); } codeOutput = codeOutput.Replace("lt;", "<"); codeOutput = codeOutput.Replace("lower than", "<"); codeOutput = codeOutput.Replace("lower", "<"); codeOutput = codeOutput.Replace("higher", ">"); codeOutput = codeOutput.Replace("highter than", ">"); codeOutput = codeOutput.Replace("gt;", ">"); // Append the code data to the string buffer finalOutput.Append(" " + codeOutput + " "); // Clear outputcode buffer executableSegment = new StringBuilder(); continue; } executableSegment.Append(input[i]); } else { // If at end, summarize the string if (i == input.Length - 1) { // Append the last string outputCode.Append(input[i]); // Format output code (Replace " to ¤ and swap back on preprocessing) String OutputCode = outputCode.ToString().Replace("\"", "¤").Replace("\n", "%BR%\");\n__printx(\""); OutputCode = this.HandleToTokens(OutputCode.ToString(), '@'); finalOutput.Append("__printx(\"" + OutputCode + "\");"); } try { if (((input[i] == '\n' && input[i + 1] == '%')) || (input[i] == '<' && input[i + 1] == '?')) { startCase = (input[i] == '<' && input[i + 1] == '?'); codeMode = true; // Convert tokens to interpretable handles String OutputCode = outputCode.ToString().Replace("\"", "¤").Replace("\n", "%BR%\");\n__printx(\""); OutputCode = this.HandleToTokens(OutputCode.ToString(), '@'); finalOutput.Append("__printx(\"" + OutputCode + "\");"); // Clear the output code buffer outputCode = new StringBuilder(); // Skip two tokens forward to not include those tokens to the code buffer i += 1; continue; } } catch { continue; } outputCode.Append(input[i]); } } // if exiting in text mode, append an end of the scalar string if (!parseMode) { CallStack += "\");"; } // Run the code RuntimeMachine.SetFunction("__printx", new Func <String, object>(__printx)); RuntimeMachine.SetFunction("synchronize_data", new Func <String, object>(synchronize_data)); CallStack = finalOutput.ToString(); CallStack = CallStack.Replace("\r", ""); if (!onlyPreprocess) { /*** * Try run the page. If there was error return ERROR: <error> message so the * handler can choose to present it to the user * */ try { RuntimeMachine.Run(CallStack); /** * Check if the result of the preprocessing is the same as before. If nothing * has changed return NONCHANGE. This is only for rendering whole pages, not inflate. * */ if (!inflate) { if (Output == OldOutput) { return("NONCHANGE"); } OldOutput = Output; } } catch (Exception e) { // clear output this.Output = ""; // Load error page using (System.IO.StreamReader SR = new System.IO.StreamReader("views\\error.xml")) { string errorView = new MakoEngine().Preprocess(SR.ReadToEnd(), "", false, "", true); RuntimeMachine = new JavaScriptEngine(); RuntimeMachine.SetFunction("__printx", new Func <String, object>(__printx)); RuntimeMachine.SetVariable("error", e.ToString() + "\n "); RuntimeMachine.Run((errorView)); } } return(this.Output); } else { return(CallStack); } }
/// <summary> /// This function preprosses the mako layer /// </summary> /// <param name="input">The input string to parse</param> /// <param name="argument">The argument sent to the parser</param> public String Preprocess(string input, string argument, bool inflate, string uri, bool onlyPreprocess = false) { #region OverlayManager Experimental /**** * STOCKHOLM 2011-07-01 14:45 * * New feature: Apply custom overlays specified by individual service: * Overlay are marked with <#namespace#> where "namespace" is an special * kind of <namespace>.xml view file inside an <extension_dir>/views/ folder * */ // First gain attention by the event OverlayEventArgs args = new OverlayEventArgs(); // Create event args args.URI = uri; if (RequestOverlay != null) RequestOverlay(this, args); // if the args has retained the phase, eg. not cancelled if (!args.Cancel) { // Substitute the overlay placeholders with the views if(args.ViewFolders != null) foreach (KeyValuePair<string, string> overlay in args.ViewFolders) { using (StreamReader SR = new StreamReader(overlay.Value)) { String content = SR.ReadToEnd(); // Substitute overlay placeholders input = input.Replace("<#" + overlay.Key.Replace(".xml","") + "#>", content); SR.Close(); } } } // Remove unwanted trailings Regex a = new Regex(@"<\#[^\#]*\#>", RegexOptions.IgnoreCase); input = a.Replace(input, ""); #endregion /** * Begin normal operation * */ // Clear the output buffer Output = ""; /** * Tell the runtime machine about the argument * */ try { String[] arguments = argument.Split(':'); RuntimeMachine.SetVariable("parameter", argument.Replace(arguments[0] + ":", "").Replace(arguments[1] + ":", "")); RuntimeMachine.SetVariable("service", arguments[0]); } catch { } /** * This string defines the call-stack of the query * This is done before any other preprocessing * */ String CallStack = ""; String[] lines = input.Split('\n'); /** * Boolean indicating which parse mode the parser is in, * true = in executable text * false = in output line * */ bool parseMode = false; // Boolean indicating first line is parsing bool firstLine = true; /*** * Iterate through all lines and preprocess the page. * If page starts with an % it will be treated as an preparser code or all content * inside enclosing <? ?> * Two string builders, the first is for the current output segment and the second for the current * executable segmetn * */ StringBuilder outputCode = new StringBuilder(); StringBuilder executableSegment = new StringBuilder(); // The buffer for the final preprocessing output StringBuilder finalOutput = new StringBuilder(); // Append initial case outputCode.Append(""); // Boolean startCase. true = <? false \n% bool startCase = false; // Boolean which tells if the cursor is in the preparse or output part of the buffer (inside or outside an executable segment) bool codeMode = false; for(int i=0; i < input.Length ;i++) { // Check if at an overgoing to an code block if(codeMode) { if((startCase && input[i] == '?' && input[i+1] == '>') ||( input[i] == '\n' && !startCase)) { codeMode=false; // Jump forward two times if endcase is ?> if(startCase) i++; // Get the final output string codeOutput = executableSegment.ToString(); // If in JSPython mode, convert all row breaks to ; and other syntax elements if (JSPython) { codeOutput = codeOutput.Replace("\n", ";"); /** * Convert statements * */ codeOutput = codeOutput.Replace(":", "{"); codeOutput = codeOutput.Replace("end", "}"); codeOutput = codeOutput.Replace("\nif ", "\nif("); codeOutput = codeOutput.Replace("then:", "){"); codeOutput = codeOutput.Replace("do:", "){"); codeOutput = codeOutput.Replace("endif", "}"); } codeOutput = codeOutput.Replace("lt;", "<"); codeOutput = codeOutput.Replace("lower than", "<"); codeOutput = codeOutput.Replace("lower", "<"); codeOutput = codeOutput.Replace("higher", ">"); codeOutput = codeOutput.Replace("highter than", ">"); codeOutput = codeOutput.Replace("gt;", ">"); // Append the code data to the string buffer finalOutput.Append(" "+ codeOutput + " "); // Clear outputcode buffer executableSegment = new StringBuilder(); continue; } executableSegment.Append(input[i]); } else { // If at end, summarize the string if(i == input.Length - 1) { // Append the last string outputCode.Append(input[i]); // Format output code (Replace " to ¤ and swap back on preprocessing) String OutputCode = outputCode.ToString().Replace("\"", "¤").Replace("\n", "%BR%\");\n__printx(\""); OutputCode = this.HandleToTokens(OutputCode.ToString(),'@'); finalOutput.Append("__printx(\"" + OutputCode + "\");"); } try { if (((input[i] == '\n' && input[i + 1] == '%')) || (input[i] == '<' && input[i + 1] == '?')) { startCase = (input[i] == '<' && input[i + 1] == '?'); codeMode = true; // Convert tokens to interpretable handles String OutputCode = outputCode.ToString().Replace("\"", "¤").Replace("\n", "%BR%\");\n__printx(\""); OutputCode = this.HandleToTokens(OutputCode.ToString(), '@'); finalOutput.Append("__printx(\"" + OutputCode + "\");"); // Clear the output code buffer outputCode = new StringBuilder(); // Skip two tokens forward to not include those tokens to the code buffer i += 1; continue; } } catch { continue; } outputCode.Append(input[i]); } } // if exiting in text mode, append an end of the scalar string if (!parseMode) { CallStack += "\");"; } // Run the code RuntimeMachine.SetFunction("__printx",new Func<String,object>( __printx)); RuntimeMachine.SetFunction("synchronize_data", new Func<String, object>(synchronize_data)); CallStack = finalOutput.ToString(); CallStack = CallStack.Replace("\r", ""); if (!onlyPreprocess) { /*** * Try run the page. If there was error return ERROR: <error> message so the * handler can choose to present it to the user * */ try { RuntimeMachine.Run(CallStack); /** * Check if the result of the preprocessing is the same as before. If nothing * has changed return NONCHANGE. This is only for rendering whole pages, not inflate. * */ if (!inflate) { if (Output == OldOutput) return "NONCHANGE"; OldOutput = Output; } } catch (Exception e) { // clear output this.Output = ""; // Load error page using (System.IO.StreamReader SR = new System.IO.StreamReader("views\\error.xml")) { string errorView = new MakoEngine().Preprocess(SR.ReadToEnd(), "", false, "", true); RuntimeMachine = new JavaScriptEngine(); RuntimeMachine.SetFunction("__printx", new Func<String, object>(__printx)); RuntimeMachine.SetVariable("error", e.ToString() + "\n " ); RuntimeMachine.Run((errorView)); } } return this.Output; } else { return CallStack; } }