/// <summary> /// Applies Syntax Highlighting to the specified source according to its language. /// </summary> /// <param name="source">The text to transform</param> /// <param name="language">The language used to highlight the text.</param> /// <param name="linesToMark">The line numbers to apply bold emphasizing.</param> /// <param name="linesToStrikethrough">The line numbers to apply strikethrough emphasizing.</param> /// <returns>The result.</returns> public string Highlight(string source, string language, int[] linesToMark, int[] linesToStrikethrough) { if (string.IsNullOrEmpty(language)) { language = "C#"; } HighlighterBase highlighter = this.GetHighlighter(language); string parsedText = source; if (highlighter != null) { source = HttpUtility.HtmlDecode(source); parsedText = highlighter.Parse(source); } if (linesToMark != null) { parsedText = this.MarkLines(linesToMark, parsedText); } if (linesToStrikethrough != null) { parsedText = this.StrikethroughLines(linesToStrikethrough, parsedText); } return(parsedText); }
/// <summary> /// Parses source code. /// </summary> /// <param name="language">The language used to highlight the text.</param> /// <returns>The highlighter.</returns> protected HighlighterBase GetHighlighter(string language) { Register register = Register.Instance; HighlighterBase highlighter = register.Highlighters[language]; if (highlighter == null) { return(null); } this.EnsureParser(); highlighter = highlighter.Create(); highlighter.Parser = this.htmlParser; highlighter.ForceReset(); return(highlighter); }
/// <summary> /// Gets the highlighter with the specified name. /// </summary> public HighlighterBase this[string name] { get { HighlighterBase highlighter = null; for (int i = 0; i < this.List.Count; i++) { highlighter = (HighlighterBase)this.List[i]; if (highlighter.TagValues.Contains(name.ToLower())) { return(highlighter); } } return(null); } }
/// <summary> /// Handles the match event. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void xmlScanner_Match(object sender, MatchEventArgs e) { if (e.Tag != null && e.Tag.ToLower() == "script") { if (e.Type == MatchType.StartTag) { this.language = e.Attributes["lang"]; if (this.language == null) { this.language = e.Attributes["language"]; } if (this.language != null) { this.language = this.language.Replace("\"", "").Replace("'", ""); if (this.language.Length == 0) { this.language = null; } } if (this.language != null) { HighlighterBase highlighter = Register.Instance.Highlighters[this.language]; if (highlighter != null) { highlighter = highlighter.Create(); highlighter.Parser = this.parser; this.Child = highlighter.BuildEntryPointScanner(this.Tokenizer, this.ScannerResult); } else { this.language = null; } } } else if (e.Type == MatchType.EndTag) { this.language = null; } } }
/// <summary> /// Adds a highlighter to the register. /// </summary> /// <param name="highlighter">The highlighter to add.</param> private void AddHighlighter(HighlighterBase highlighter) { this.highlighters.Add(highlighter); }
/// <summary> /// Saves a highlighter's configuration in the specified file. /// </summary> /// <param name="fileName">The name of the file which will store the information about the highlighter.</param> /// <param name="highlighter">An <see cref="Wilco.SyntaxHighlighting.HighlighterBase"/> object.</param> public static void ToFile(string fileName, HighlighterBase highlighter) { XmlDocument document = new XmlDocument(); XmlElement languageRootElement, languageElement; if (File.Exists(fileName)) { document.Load(fileName); languageRootElement = (XmlElement)document.SelectSingleNode("//highlighter/languages"); if (languageRootElement != null) { languageElement = (XmlElement)document.SelectSingleNode(String.Format("//highlighter/languages/language[@name='{0}']", highlighter.Name)); if (languageElement != null) { languageRootElement.RemoveChild(languageElement); } } } else { document.AppendChild(document.CreateXmlDeclaration("1.0", "utf-8", "yes")); XmlElement rootElement = document.CreateElement("highlighter"); document.AppendChild(rootElement); languageRootElement = document.CreateElement("languages"); rootElement.AppendChild(languageRootElement); } // Store highlighter settings. languageElement = document.CreateElement("language"); languageRootElement.AppendChild(languageElement); languageElement.SetAttribute("name", highlighter.Name); languageElement.SetAttribute("fullName", highlighter.FullName); XmlElement tagRootElement = document.CreateElement("tagValues"); languageElement.AppendChild(tagRootElement); XmlElement tagElement; foreach (string tag in highlighter.TagValues) { tagElement = document.CreateElement("tag"); tagElement.InnerText = tag; tagRootElement.AppendChild(tagElement); } XmlElement fileExtensionRootElement = document.CreateElement("fileExtensions"); languageElement.AppendChild(fileExtensionRootElement); XmlElement fileExtensionElement; foreach (string fileExtension in highlighter.FileExtensions) { fileExtensionElement = document.CreateElement("tag"); fileExtensionElement.InnerText = fileExtension; fileExtensionRootElement.AppendChild(fileExtensionElement); } // Store scanner settings. XmlElement scannerRootElement = document.CreateElement("scanners"); languageElement.AppendChild(scannerRootElement); int scannerCounter = 0; foreach (IScanner scanner in highlighter.Scanners) { SaveScanner(document, scannerRootElement, scanner, scannerCounter++, true); } document.Save(fileName); }
/// <summary> /// Removes an highlighter. /// </summary> /// <param name="highlighter">The highlighter to remove.</param> public void Remove(HighlighterBase highlighter) { this.List.Remove(highlighter); }
/// <summary> /// Adds an highlighter. /// </summary> /// <param name="highlighter">The highlighter to add.</param> /// <returns>Index of the added highlighter.</returns> public int Add(HighlighterBase highlighter) { return(this.List.Add(highlighter)); }