public void HighlightWords(IRibbonControl control) { Window context = control.Context as Window; try { WordMatcher matcher = new WordMatcher(AppRegKey); matcher.LoadHightlightDefineFile(null); string pageId = OneNoteHelper.HierarchyHelper.GetActiveObjectID(context.Application, OneNoteHelper.HierarchyHelper._ObjectType.Page); HighlightPage(context.Application, pageId, matcher); //OneNoteHelper.HierarchyHelper.GoThoughHierarchy(context.Application, (application, pageId) => HighlightPage(application, pageId, matcher)); } catch (Exception ex) { Debug.WriteLine(ex.ToString(), "Error"); EventLog.WriteEntry(EventLogSource, ex.ToString()); } finally { context = null; GC.Collect(); GC.WaitForPendingFinalizers(); GC.Collect(); } }
private void HighlightPage(Application application, string pageId, WordMatcher matcher) { string pageXml; application.GetPageContent(pageId, out pageXml); Debug.WriteLine(pageXml); XElement page = XElement.Parse(pageXml); XNamespace oneNS = page.Name.Namespace; XElement title = page.Element(oneNS + "Title"); XElement titleOE = title.Element(oneNS + "OE"); Debug.WriteLine("page:{0} by {1}", titleOE.Element(oneNS + "T").Value, titleOE.Attribute("author")); StatisticsDataSet.HighlightStatisticsRow statRow = null; // frmStatistics.BeforeProcessing(); foreach (XElement outline in page.Elements(oneNS + "Outline")) { foreach (XElement oeChildren in outline.Elements(oneNS + "OEChildren")) { foreach (XElement oe in oeChildren.Elements(oneNS + "OE")) { foreach (XElement t in oe.Elements(oneNS + "T")) { //Debug.WriteLine("before: "); //Debug.WriteLine(t.Value); string process = ProcessingWords(t.Value, matcher, statRow); t.RemoveNodes(); t.Add(new XCData(process)); } } } } //frmStatistics.AfterProcessing(); application.UpdatePageContent(page.ToString(SaveOptions.None)); }
private string ProcessingWords(string textContent, WordMatcher matcher, StatisticsDataSet.HighlightStatisticsRow statisticsRow = null) { StringBuilder word = new StringBuilder(); HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument(); doc.LoadHtml(textContent); HtmlAgilityPack.HtmlDocument newDoc = new HtmlAgilityPack.HtmlDocument(); foreach (HtmlNode htmlNode in doc.DocumentNode.ChildNodes) { if (htmlNode.Name.Equals("#text", StringComparison.OrdinalIgnoreCase)) { foreach (char c in htmlNode.InnerText) { if (char.IsLetterOrDigit(c)) { word.Append(c); } else { if (word.Length > 0) { AppendNewWord(ref newDoc, word.ToString(), matcher, statisticsRow); word.Clear(); } AppendSimpleWord(ref newDoc, c.ToString()); } } } else { if (word.Length > 0) { AppendNewWord(ref newDoc, word.ToString(), matcher, statisticsRow); word.Clear(); } HtmlNode otherNode = htmlNode.CloneNode(true); newDoc.DocumentNode.ChildNodes.Add(otherNode); } } if (word.Length > 0) { AppendNewWord(ref newDoc, word.ToString(), matcher); } return GetDocContent(newDoc); }
private void AppendNewWord(ref HtmlAgilityPack.HtmlDocument newDoc, string word, WordMatcher matcher, StatisticsDataSet.HighlightStatisticsRow statisticsRow = null) { HighlightWord matchedWord = null; if (matcher.Match(word, out matchedWord)) { HtmlNode span = newDoc.CreateElement("span"); span.SetAttributeValue("style", matchedWord.Style); span.InnerHtml = word; newDoc.DocumentNode.ChildNodes.Append(span); //if (statisticsRow != null) //{ // frmStatistics.HighlightStaticsticIncrease(statisticsRow, matchedWord); //} } else { AppendSimpleWord(ref newDoc, word); } }