WebEdit.NET allows ad-hoc execution of code with the .NET Console, an interactive C# interpreter.
Conceptually, the code interpreter lies between the command model and the WebEdit.NET API: in the command model, its task is executing commands of the "code:" protocol; as a bridge to the API, it is suited for ad-hoc interactions.
The code interpreter is also employed when evaluating parameters anywhere Token Expansion occurs (for example, when processing a parameterized template).
WebEdit's code interpreter is implemented by the .NET Console component, which is part of the Gregor.NET application framework. It's an interactive interpreter (using C#-like syntax). It can execute single statements, like calling methods or properties, setting and reading variables, and so on. It's useful for highly interactive scenarios, and allows storing results in variables that persist through the current session.
The Code Interpreter can be useful in the following situations:
To get help, type "code:show help" into the Console Window.
By default, the code interpreter imports all of the namespaces declared by WebEdit.NET, some Gregor.NET namespaces, and a selection of .NET framework namespaces.
The Gregor.WebEdit.Vars module defines static properties for returing often used information, such as the active document's full path:
code:Vars.DocName code:Vars.TextSelection
For evaluating complex expressions, blocks, or user functions, you can use an editor window in WebEdit, and run the interpreter with the following command:
code:Vars.Eval(Vars.TextAll)
To show the app's command line using the .NET Console code interpreter (in the Console tool window):
code:WebEditApp.StartOptions
To set a code variable (Tip: variables can be used for token expansion):
code:StyleSheet = "format.css"
To generate a new GUID with the code interpreter (in the Console):
code:System.Guid.NewGuid()
To convert a hex number to decimal and back (in the Console):
code:n = 0x47
code:string.Format("{0:x2}", n)
code:Convert.ToString(n, 16)
To get information about some Unicode character (in the Console):
code:Gregor.Core.Parse.GetCharacterInformation('\u221e')
code:Gregor.Core.Parse.GetCharacterInformation('$Char%')
To read some storage (like a file or URL) into a variable:
s = WebEditApp.DocumentManager.ReadStorage("http://localhost/")
Note that you can evaluate several code statements separated by semicolons. The value of the last statement will be output in this case:
s = Dev.ReadTextFile("C:\\AutoExec.bat"); s.Length
To extract lines from the current document that match certain criteria:
foreach(sLine in Vars.TextLines){if(sLine.StartsWith("!")){WebEditApp.Trace(sLine);}}
To remove line breaks in the current document:
Vars.TextAll = string.Join(string.Empty, Vars.TextLines)
The code interpreter also supports defining simple functions, called user functions. Here's a utility that converts lines into HTML paragraphs:
paragraphLines(asLines)
{
sb = new System.Text.StringBuilder();
foreach(sLine in asLines){
if(Flow.IsString(sLine)){
sb.Append("<p>");
sb.Append(Environment.NewLine);
sb.Append(sLine);
sb.Append(Environment.NewLine);
sb.Append("</p>");
sb.Append(Environment.NewLine);
sb.Append(Environment.NewLine);
}
}
sb.ToString();
}
Use it like this to convert a text file, or its selected lines, into HTML:
Vars.TextAll = paragraphLines(Vars.TextLines) Vars.PasteToNewDocument(paragraphLines(Vars.TextSelectionLines))
Here's a utility that builds an HTML table from lines with tab-separated values:
tableLines(asLines)
{
sb = new StringBuilder();
sb.Append("<table>");
sb.Append(Environment.NewLine);
foreach(sLine in asLines){
if(Flow.IsString(sLine)){
sb.Append("<tr>");
aStr = sLine.Split(Chars.Tab);
foreach(sCol in aStr){
sb.Append("<td>");
sb.Append(sCol);
sb.Append("</td>");
}
sb.Append("</tr>");
}
// keep cosmetic blank lines
sb.Append(Environment.NewLine);
}
sb.Append("</table>");
sb.Append(Environment.NewLine);
sb.ToString();
}
This function converts non-blank lines into XML elements with id attributes, supporting line continuation with backslashes:
markupLines(asLines, sElementName)
{
// all
sbAll = new StringBuilder(16384);
sbAll.AppendFormat("<{0:s}-list>", sElementName);
sbAll.Append(Environment.NewLine);
sbAll.Append(Environment.NewLine);
// items
sbItem = new StringBuilder(512);
id = 0;
loop(iLine in asLines.Length){
sLine = asLines[iLine];
if(Flow.IsString(sLine)){
sbItem.Remove(0, sbItem.Length);
// element begin
sbItem.AppendFormat("<{0:s} id=\"{1:d}\">", sElementName, id = Mat.Increment(id));
sbItem.Append(Environment.NewLine);
// content (text child)
sbItem.Append(sLine);
while(sLine.EndsWith("\\")){
sbItem.Replace("\\", " ", Mat.Substract(sbItem.Length, 1), 1);
iLine = Mat.Add(iLine, 1);
sLine = asLines[iLine];
sbItem.Append(sLine);
}
sbItem.Append(Environment.NewLine);
// element end
sbItem.AppendFormat("</{0:s}>", sElementName);
sbItem.Append(Environment.NewLine);
sbItem.Append(Environment.NewLine);
// done with item
sItem = Parse.Tighten(sbItem.ToString(), ' ', StringTightenOptions.All);
sbAll.Append(sItem);
}
}
// finish
sbAll.AppendFormat("</{0:s}-list>", sElementName)
sbAll.Append(Environment.NewLine);
sbAll.Append(Environment.NewLine);
sbAll.ToString();
}
For more details and examples, please refer to the .NET Console project page (there is a link at the bottom of this page).
See Also: Command Model | WebEdit.NET API | .NET Console On The Web..