/// <summary> /// Registers Lua functions found in the specified target. /// </summary> /// <param name="luaFunctions">Global lua function table.</param> /// <param name="target">Object (class,struct) to search in.</param> /// <param name="vm">The Lua virtual machine.</param> public static void RegisterLuaFunctions(Lua vm, ref Dictionary<string, LuaFunctionDescriptor> luaFunctions, object target) { if(vm.IsNull() || luaFunctions.IsNull()) return; var type = target.GetType(); foreach(var method in type.GetMethods()) { foreach(var attribute in Attribute.GetCustomAttributes(method)) { var attr = attribute as LuaFunctionAttribute; if(attr.IsNull()) continue; var parameters = new List<string>(); var paramInfo = method.GetParameters(); if(!attr.FunctionParameters.IsNull() && paramInfo.Length != attr.FunctionParameters.Length) { Log.Error("LuaHelper", sLConsole.GetString("Function {0} (exported as {1}): argument number mismatch. Declared {2}, but requires {3}!"), method.Name, attr.FunctionName, attr.FunctionParameters.Length, paramInfo.Length); break; } // build parameter doc hashtable. if(!attr.FunctionParameters.IsNull()) parameters.AddRange(paramInfo.Select((t, i) => string.Format("{0} - {1}", t.Name, attr.FunctionParameters[i]))); var descriptor = new LuaFunctionDescriptor(attr.FunctionName, attr.FunctionDocumentation, parameters); luaFunctions.Add(attr.FunctionName, descriptor); vm.RegisterFunction(attr.FunctionName, target, method); } } }
public void SetLinkTag(Dictionary<string, string> attributes) { if (attributes.IsNull()) { throw new ArgumentNullException(); } var linkElement = Document.CreateElement(TagNames.LinkTagName); foreach (KeyValuePair<string, string> attribute in attributes) { linkElement.SetAttribute(attribute.Key, attribute.Value); } var bodyElement = GetElementsByTagName(TagNames.BodyTagName)[0]; bodyElement.InsertAdjacentElement(HtmlElementInsertionOrientation.BeforeBegin, linkElement); }
public void SetBaseTag(Dictionary<string, string> attributes) { if (attributes.IsNull()) { throw new ArgumentNullException(); } var baseElement = Document.CreateElement(TagNames.BaseTagName); foreach (KeyValuePair<string, string> attribute in attributes) { baseElement.SetAttribute(attribute.Key, attribute.Value); } var headElement = GetElementsByTagName(TagNames.HeadTagName)[0]; headElement.AppendChild(baseElement); }
private static string ConcatAttributes(Dictionary<string, string> attributes) { if (attributes.IsNull()) { throw new ArgumentNullException(); } var attrs = string.Empty; foreach (var attribute in attributes) { attrs += string.Concat(attribute.Key, "=\"", attribute.Value, "\" "); } return attrs; }
//моя функция для стилей public static void SurroundWithStyleHtml(HtmlEditingTool htmlEditingTool, string tagName, Dictionary<string, string> attributes) { if (htmlEditingTool.IsNull() || tagName.IsNull() || attributes.IsNull()) { throw new ArgumentNullException(); } if (htmlEditingTool.Mode == Enums.HtmlEditingToolMode.Preview) { throw new InvalidOperationException(); } // Обрамление html кода разрешено только при наличии выделения. if (!htmlEditingTool.IsSelection) { return; } var htmlDocument = htmlEditingTool.GetNativeHtmlDocument(); IHTMLTxtRange textRange; try { textRange = htmlDocument.selection.createRange() as IHTMLTxtRange; } catch { throw new InvalidOperationException(); } if (textRange.IsNull()) { throw new NullReferenceException(); } var selection = textRange.htmlText; // var htmlText = textRange.htmlText; selection = string.Copy(selection); // var htmlPaste = htmlText; // var selection = htmlEditingTool.GetSelection(); // selection = string.Copy(selection); if (selection.IsNull()) { throw new NullReferenceException(); } if (selection.StartsWith(" ")) { selection = selection.Remove(0, 1); selection = string.Concat(" ", selection); } var attrs = ConcatAttributes(attributes); selection = string.Concat("<", tagName, " ", attrs, ">", selection, "</", tagName, ">"); try { textRange.pasteHTML(selection); textRange.collapse(false); textRange.select(); } catch { throw new InvalidOperationException(); } }
public static void DeleteSurroundWithHtml(HtmlEditingTool htmlEditingTool, string tagName, Dictionary<string, string> attributes) { if (htmlEditingTool.IsNull() || tagName.IsNull() || attributes.IsNull()) { throw new ArgumentNullException(); } if (htmlEditingTool.Mode == Enums.HtmlEditingToolMode.Preview) { throw new InvalidOperationException(); } // Обрамление html кода разрешено только при наличии выделения. if (!htmlEditingTool.IsSelection) { return; } var htmlDocument = htmlEditingTool.GetNativeHtmlDocument(); IHTMLTxtRange textRange; try { textRange = htmlDocument.selection.createRange() as IHTMLTxtRange; } catch { throw new InvalidOperationException(); } if (textRange.IsNull()) { throw new NullReferenceException(); } var current = textRange.htmlText; var htmlText = textRange.htmlText; htmlText = string.Copy(htmlText); var htmlPaste = htmlText; string searchString1 = "<DIV"; string searchString2 = "</DIV"; while (htmlText.Contains(searchString1) && htmlText.Contains(searchString2)) { int first = htmlText.IndexOf(searchString1); int second = htmlText.IndexOf(searchString2); if (first < second) { int i = htmlPaste.IndexOf(searchString1); string temp=""; string temp2 = ""; while(htmlPaste[i]!='>') { for(int j=0; j<htmlPaste.Length; j++) if (j != i) { temp = string.Concat(temp, htmlPaste[j].ToString()); temp2 = string.Concat(temp2, htmlPaste[j].ToString()); } else { temp2 = string.Concat(temp2, " "); } htmlPaste = temp; htmlText = temp2; temp = ""; temp2 = ""; } for (int j = 0; j < htmlPaste.Length; j++) if (j != i) { temp = string.Concat(temp, htmlPaste[j].ToString()); } htmlPaste = temp; i = htmlPaste.IndexOf(searchString2); temp = ""; for (int j = 0; j < htmlPaste.Length; j++) if (j < i || j>(i+5)) { temp = string.Concat(temp, htmlPaste[j].ToString()); } htmlPaste = temp; i = htmlText.IndexOf(searchString2); temp2 = ""; for (int j = 0; j < htmlText.Length; j++) if (j < i || j > (i + 5)) { temp2 = string.Concat(temp2, htmlText[j].ToString()); } htmlText = temp2; } else { string temp2 = ""; for (int j = 0; j < htmlText.Length; j++) if (j < second || j > (second + 5)) { temp2 = string.Concat(temp2, htmlText[j].ToString()); } htmlText = temp2; } } try { textRange.pasteHTML(htmlPaste); textRange.collapse(false); textRange.select(); } catch { throw new InvalidOperationException(); } }
public static void InsertHtml(HtmlEditingTool htmlEditingTool, string tagName, Dictionary<string, string> attributes) { if (htmlEditingTool.IsNull() || tagName.IsNull() || attributes.IsNull()) { throw new ArgumentNullException(); } if (htmlEditingTool.Mode == Enums.HtmlEditingToolMode.Preview) { throw new InvalidOperationException(); } // Вставка html кода разрешена только при снятом выделении. if (htmlEditingTool.IsSelection) { return; } var htmlDocument = htmlEditingTool.GetNativeHtmlDocument(); IHTMLTxtRange textRange; try { textRange = htmlDocument.selection.createRange() as IHTMLTxtRange; } catch { throw new InvalidOperationException(); } if (textRange.IsNull()) { throw new NullReferenceException(); } var attrs = ConcatAttributes(attributes); var html = string.Concat("<", tagName, " ", attrs, ">"); try { textRange.pasteHTML(html); textRange.collapse(false); textRange.select(); } catch { throw new InvalidOperationException(); } }
/// <summary> /// Basically strips everything between the start marker and the end marker /// The start marker is the Key in the Dictionary<string, string>, the end marker is the Value /// </summary> /// <param name="rules"> /// </param> /// <param name="content"> /// </param> /// <returns> /// </returns> private static string StripByRules(Dictionary<string, string> rules, string content) { if (rules.IsNull() || content.IsNullOrEmpty()) { return content; } foreach (KeyValuePair<string, string> k in rules) { string key = Regex.Escape(k.Key); string value = Regex.Escape(k.Value); string pattern = "({0})(.*?)({1})".FormatWith(key, value); const RegexOptions options = RegexOptions.IgnoreCase | RegexOptions.Singleline | RegexOptions.CultureInvariant | RegexOptions.Compiled; content = Regex.Replace(content, pattern, string.Empty, options); } return content; }