/// <summary> /// 插入 HighLight Code 至滑鼠游標的位置 /// Insert HighLight Code To Mouse Position /// </summary> private void InsertHighLightCodeToCurrentSide(string fileName, HighLightParameter parameters, XElement outline, bool selectedTextFormated) { try { // Trace.TraceInformation(System.Reflection.MethodBase.GetCurrentMethod().Name); string htmlContent = File.ReadAllText(fileName, new UTF8Encoding(false)); string byteOrderMarkUtf8 = Encoding.UTF8.GetString(Encoding.UTF8.GetPreamble()); htmlContent = htmlContent.Replace(byteOrderMarkUtf8, ""); var pageNode = GetPageNode(); if (pageNode != null) { var existingPageId = pageNode.Attribute("ID").Value; string[] position = null; if (outline == null) { position = GetMousePointPosition(existingPageId); } var page = InsertHighLightCode(htmlContent, position, parameters, outline, selectedTextFormated, IsSelectedTextInline(existingPageId)); page.Root.SetAttributeValue("ID", existingPageId); OneNoteApplication.UpdatePageContent(page.ToString(), DateTime.MinValue); } } catch (Exception e) { MessageBox.Show("Exception from InsertHighLightCodeToCurrentSide: " + e.ToString()); } }
public void FormatNewCode() { string htmlCode = Resource1.HTMLContent1; string[] pos = new string[] { "198.0", "950.3999633789062" }; HighLightParameter param = new HighLightParameter(); param.ShowLineNumber = true; param.HighlightColor = System.Drawing.Color.FromArgb(240, 240, 240); XElement outline = null; AddIn addIn = new AddIn(); addIn.ns = @"http://schemas.microsoft.com/office/onenote/2013/onenote"; //Arrange ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap { ExeConfigFilename = "Test.config" }; Configuration configuration = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None); HighLightSection config = configuration.GetSection("HighLightSection") as HighLightSection; XDocument output = addIn.InsertHighLightCode(htmlCode, pos, param, outline, config, false, false); Assert.AreEqual(Resource1.Output1, output.ToString(), false); }
/// <summary> /// 產生 XML 插入至 OneNote /// Generate XML Insert To OneNote /// </summary> public XDocument InsertHighLightCode(string htmlContent, string[] position, HighLightParameter parameters, XElement outline, HighLightSection config, bool selectedTextFormated, bool isInline) { XElement children = PrepareFormatedContent(htmlContent, parameters, config, isInline); bool update = false; if (outline == null) { outline = CreateOutline(position, children); } else // Update exiting outline { update = true; if (selectedTextFormated) { outline.Descendants(ns + "Table").Where(n => n.Attribute("selected") != null && (n.Attribute("selected").Value == "all" || n.Attribute("selected").Value == "partial")).FirstOrDefault().ReplaceWith(children.Descendants(ns + "Table").FirstOrDefault()); //outline.Descendants().Where(n => n.Attribute("selected") != null && n.Attribute("selected").Value == "all").Remove(); } else { if (isInline) { int j = 0; for (int i = 0; i < outline.Descendants(ns + "OE").Count(); i++) { XElement oeNode = outline.Descendants(ns + "OE").ElementAt(i); if (oeNode.Descendants(ns + "T").Where(n => n.Attribute("selected") != null && n.Attribute("selected").Value == "all").Count() > 0) { oeNode.Descendants(ns + "T").Where(n => n.Attribute("selected") != null && n.Attribute("selected").Value == "all").FirstOrDefault().ReplaceWith(children.Descendants(ns + "Table").Descendants(ns + "OEChildren").Descendants(ns + "OE").ElementAt(j).Descendants(ns + "T")); j++; } } outline.Descendants(ns + "OE").Where(t => t.Elements(ns + "T").Any(n => n.Attribute("selected") != null && n.Attribute("selected").Value == "all")).Remove(); } else { outline.Descendants(ns + "T").Where(n => n.Attribute("selected") != null && n.Attribute("selected").Value == "all").FirstOrDefault().ReplaceWith(children.Descendants(ns + "Table").FirstOrDefault()); outline.Descendants(ns + "OE").Where(t => t.Elements(ns + "T").Any(n => n.Attribute("selected") != null && n.Attribute("selected").Value == "all")).Remove(); } } } if (update) { return(outline.Parent.Document); } else { XElement page = new XElement(ns + "Page"); page.Add(outline); XDocument doc = new XDocument(); doc.Add(page); return(doc); } }
/// <summary> /// Generate HighLight File /// </summary> private void btnCodeHighLight_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(CodeStyle)) { MessageBox.Show("Please select code Style!"); return; } IGenerateHighLight generate = new GenerateHighLight(); string outputFileName = String.Empty; _parameters = new HighLightParameter() { FileName = _fileName, Content = CodeContent, CodeType = _codeType, HighLightStyle = CodeStyle, ShowLineNumber = IsShowLineNumber, HighlightColor = BackgroundColor, Font = NoteHighlightForm.Properties.Settings.Default.Font, FontSize = NoteHighlightForm.Properties.Settings.Default.FontSize }; try { outputFileName = generate.GenerateHighLightCode(_parameters); } catch (Exception ex) { MessageBox.Show(ex.ToString()); this.Dispose(); this.Close(); return; } if (IsClipboard && !String.IsNullOrEmpty(outputFileName)) { InsertToClipboard(outputFileName); } SaveSetting(); this.Dispose(); this.Close(); }
/// <summary> /// 插入 HighLight Code 至滑鼠游標的位置 /// Insert HighLight Code To Mouse Position /// </summary> private void InsertHighLightCodeToCurrentSide(string fileName, HighLightParameter parameters, XElement outline, bool selectedTextFormated) { // Trace.TraceInformation(System.Reflection.MethodBase.GetCurrentMethod().Name); string htmlContent = File.ReadAllText(fileName, Encoding.UTF8); var pageNode = GetPageNode(); if (pageNode != null) { var existingPageId = pageNode.Attribute("ID").Value; string[] position = null; if (outline == null) { position = GetMousePointPosition(existingPageId); } var page = InsertHighLightCode(htmlContent, position, parameters, outline, selectedTextFormated); page.Root.SetAttributeValue("ID", existingPageId); OneNoteApplication.UpdatePageContent(page.ToString(), DateTime.MinValue); } }
private void bt_insert_Click(object sender, EventArgs e) { if (this.txtCode.Text == "") { this.Close(); return; } IGenerateHighLight generate = new HighLightEngine.HighLightEngine(); string outPutFileName = String.Empty; var v = Properties.Settings.Default; HighLightParameter paramer = new HighLightParameter() { Content = this.txtCode.Text, lang = v.default_lang, theme = v.default_theme, font = v.default_font, size = v.default_size, showLineNumber = v.default_showLineNum, fileName = this.fileName }; try { outPutFileName = generate.GenerateHighLightCode(paramer); } catch (Exception ex) { MessageBox.Show(ex.ToString()); this.Dispose(); this.Close(); return; } this.Dispose(); this.Close(); }
/// <summary> /// 插入 HighLight Code 至滑鼠游標的位置 /// Insert HighLight Code To Mouse Position /// </summary> private void InsertHighLightCodeToCurrentSide(string fileName, HighLightParameter parameters) { // Trace.TraceInformation(System.Reflection.MethodBase.GetCurrentMethod().Name); string htmlContent = File.ReadAllText(fileName, Encoding.UTF8); string notebookXml; try { OneNoteApplication.GetHierarchy(null, HierarchyScope.hsPages, out notebookXml, XMLSchema.xs2013); } catch (Exception ex) { MessageBox.Show("Exception from onApp.GetHierarchy:" + ex.Message); return; } var doc = XDocument.Parse(notebookXml); ns = doc.Root.Name.Namespace; var pageNode = doc.Descendants(ns + "Page") .Where(n => n.Attribute("isCurrentlyViewed") != null && n.Attribute("isCurrentlyViewed").Value == "true") .FirstOrDefault(); if (pageNode != null) { var existingPageId = pageNode.Attribute("ID").Value; string[] position = GetMousePointPosition(existingPageId); var page = InsertHighLightCode(htmlContent, position, parameters); page.Root.SetAttributeValue("ID", existingPageId); OneNoteApplication.UpdatePageContent(page.ToString(), DateTime.MinValue); } }
/// <summary> /// Generate HighLight File /// </summary> private void btnCodeHighLight_Click(object sender, EventArgs e) { IGenerateHighLight generate = new GenerateHighLight(); string outputFileName = String.Empty; HighLightParameter parameter = new HighLightParameter() { FileName = _fileName, Content = CodeContent, CodeType = _codeType, HighLightStyle = CodeStyle, ShowLineNumber = IsShowLineNumber }; try { outputFileName = generate.GenerateHighLightCode(parameter); } catch (Exception ex) { MessageBox.Show(ex.ToString()); this.Dispose(); this.Close(); return; } if (IsClipboard && !String.IsNullOrEmpty(outputFileName)) { InsertToClipboard(outputFileName); } SaveSetting(); this.Dispose(); this.Close(); }
public void FormatSelectedCode_WordSelected() { string htmlCode = Resource1.HTMLContent5; string[] pos = null; HighLightParameter param = new HighLightParameter(); param.ShowLineNumber = true; param.HighlightColor = System.Drawing.Color.FromArgb(240, 240, 240); AddIn addIn = new AddIn(); addIn.ns = @"http://schemas.microsoft.com/office/onenote/2013/onenote"; var outline = XDocument.Parse(Resource1.Page5).Descendants(addIn.ns + "Outline") .Where(n => n.Attribute("selected") != null && (n.Attribute("selected").Value == "all" || n.Attribute("selected").Value == "partial")) .FirstOrDefault(); //Arrange ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap { ExeConfigFilename = "Test.config" }; Configuration configuration = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None); HighLightSection config = configuration.GetSection("HighLightSection") as HighLightSection; bool selectedTextFormated = false; addIn.GetSelectedText(Resource1.Page5, out selectedTextFormated); XDocument output = addIn.InsertHighLightCode(htmlCode, pos, param, outline, config, selectedTextFormated, addIn.IsSelectedTextInline(Resource1.Page5)); Assert.AreEqual(Resource1.Output5, output.ToString(), false); }
/// <summary> /// 產生 XML 插入至 OneNote /// Generate XML Insert To OneNote /// </summary> public XDocument InsertHighLightCode(string htmlContent, string[] position, HighLightParameter parameters) { XElement children = new XElement(ns + "OEChildren"); XElement table = new XElement(ns + "Table"); table.Add(new XAttribute("bordersVisible", "false")); XElement columns = new XElement(ns + "Columns"); XElement column1 = new XElement(ns + "Column"); column1.Add(new XAttribute("index", "0")); column1.Add(new XAttribute("width", "40")); if (parameters.ShowLineNumber) { columns.Add(column1); } XElement column2 = new XElement(ns + "Column"); if (parameters.ShowLineNumber) { column2.Add(new XAttribute("index", "1")); } else { column2.Add(new XAttribute("index", "0")); } column2.Add(new XAttribute("width", "1400")); columns.Add(column2); table.Add(columns); Color color = parameters.HighlightColor; string colorString = string.Format("#{0:X2}{1:X2}{2:X2}", color.R, color.G, color.B); XElement row = new XElement(ns + "Row"); XElement cell1 = new XElement(ns + "Cell"); cell1.Add(new XAttribute("shadingColor", colorString)); XElement cell2 = new XElement(ns + "Cell"); cell2.Add(new XAttribute("shadingColor", colorString)); var arrayLine = htmlContent.Split(new string[] { Environment.NewLine }, StringSplitOptions.None); foreach (var item in arrayLine) { var itemNr = ""; var itemLine = ""; if (parameters.ShowLineNumber) { if (item.Contains("</span>")) { int ind = item.IndexOf("</span>"); itemNr = item.Substring(0, ind + ("</span>").Length); itemLine = item.Substring(ind); } else { itemNr = ""; itemLine = item; } string nr = string.Format(@"<body style=""font-family:{0}"">", GenerateHighlightContent.GenerateHighLight.Config.OutputArguments["Font"].Value) + itemNr.Replace("'", "'") + "</body>"; cell1.Add(new XElement(ns + "OEChildren", new XElement(ns + "OE", new XElement(ns + "T", new XCData(nr))))); } else { itemLine = item; } //string s = item.Replace(@"style=""", string.Format(@"style=""font-family:{0}; ", GenerateHighlightContent.GenerateHighLight.Config.OutputArguments["Font"].Value)); string s = string.Format(@"<body style=""font-family:{0}"">", GenerateHighlightContent.GenerateHighLight.Config.OutputArguments["Font"].Value) + itemLine.Replace("'", "'") + "</body>"; cell2.Add(new XElement(ns + "OEChildren", new XElement(ns + "OE", new XElement(ns + "T", new XCData(s))))); } if (parameters.ShowLineNumber) { row.Add(cell1); } row.Add(cell2); table.Add(row); children.Add(new XElement(ns + "OE", table)); XElement outline = new XElement(ns + "Outline"); if (position != null && position.Length == 2) { XElement pos = new XElement(ns + "Position"); pos.Add(new XAttribute("x", position[0])); pos.Add(new XAttribute("y", position[1])); outline.Add(pos); XElement size = new XElement(ns + "Size"); size.Add(new XAttribute("width", "1600")); size.Add(new XAttribute("height", "200")); outline.Add(size); } outline.Add(children); XElement page = new XElement(ns + "Page"); page.Add(outline); XDocument doc = new XDocument(); doc.Add(page); return(doc); }
private XElement PrepareFormatedContent(string htmlContent, HighLightParameter parameters, HighLightSection config, bool isInline) { XElement children = new XElement(ns + "OEChildren"); XElement table = new XElement(ns + "Table"); table.Add(new XAttribute("bordersVisible", NoteHighlightForm.Properties.Settings.Default.ShowTableBorder)); XElement columns = new XElement(ns + "Columns"); XElement column1 = new XElement(ns + "Column"); column1.Add(new XAttribute("index", "0")); column1.Add(new XAttribute("width", "40")); if (parameters.ShowLineNumber && !isInline) { columns.Add(column1); } XElement column2 = new XElement(ns + "Column"); if (parameters.ShowLineNumber && !isInline) { column2.Add(new XAttribute("index", "1")); } else { column2.Add(new XAttribute("index", "0")); } column2.Add(new XAttribute("width", "1400")); columns.Add(column2); table.Add(columns); Color color = parameters.HighlightColor; string colorString = color.A == 0 ? "none" : string.Format("#{0:X2}{1:X2}{2:X2}", color.R, color.G, color.B); XElement row = new XElement(ns + "Row"); XElement cell1 = new XElement(ns + "Cell"); cell1.Add(new XAttribute("shadingColor", colorString)); XElement cell2 = new XElement(ns + "Cell"); cell2.Add(new XAttribute("shadingColor", colorString)); string defaultStyle = ""; var arrayLine = htmlContent.Split(new string[] { Environment.NewLine }, StringSplitOptions.None); foreach (var it in arrayLine) { string item = it; if (item.StartsWith("<pre")) { defaultStyle = item.Substring(0, item.IndexOf(">") + 1); //Sets language to Latin to disable spell check defaultStyle = defaultStyle.Insert(defaultStyle.Length - 1, " lang=la"); item = item.Substring(item.IndexOf(">") + 1); } if (item == "</pre>") { continue; } var itemNr = ""; var itemLine = ""; if (parameters.ShowLineNumber) { if (item.Contains("</span>")) { int ind = item.IndexOf("</span>"); itemNr = item.Substring(0, ind + ("</span>").Length); itemLine = item.Substring(ind); } else { itemNr = ""; itemLine = item; } //string nr = string.Format(@"<body style=""font-family:{0}"">", GenerateHighlightContent.GenerateHighLight.Config.OutputArguments["Font"].Value) + // itemNr.Replace("'", "'") + "</body>"; string nr = ""; if (string.IsNullOrEmpty(config.LineNrReplaceCh)) { nr = defaultStyle + itemNr.Replace("'", "'") + "</pre>"; } else { nr = defaultStyle + config.LineNrReplaceCh.PadLeft(5) + "</pre>"; } XElement oeElement = new XElement(ns + "OE", new XElement(ns + "T", new XCData(nr))); if (ContainsAsianCharacter(itemLine)) { oeElement.Add(new XAttribute("spaceBefore", config.AsianBeforeSpace)); oeElement.Add(new XAttribute("spaceAfter", config.AsianAfterSpace)); } cell1.Add(new XElement(ns + "OEChildren", oeElement)); } else { itemLine = item; } //string s = item.Replace(@"style=""", string.Format(@"style=""font-family:{0}; ", GenerateHighlightContent.GenerateHighLight.Config.OutputArguments["Font"].Value)); //string s = string.Format(@"<body style=""font-family:{0}"">", GenerateHighlightContent.GenerateHighLight.Config.OutputArguments["Font"].Value) + // itemLine.Replace("'", "'") + "</body>"; string s = defaultStyle + itemLine.Replace("'", "'") + "</pre>"; cell2.Add(new XElement(ns + "OEChildren", new XElement(ns + "OE", new XElement(ns + "T", new XCData(s))))); } if (parameters.ShowLineNumber && !isInline) { row.Add(cell1); } row.Add(cell2); table.Add(row); children.Add(new XElement(ns + "OE", table)); return(children); }
/// <summary> /// 插入 HighLight Code 至滑鼠游標的位置 /// Insert HighLight Code To Mouse Position /// </summary> private void InsertHighLightCodeToCurrentSide(string fileName, string pageXml, HighLightParameter parameters, XElement outline, bool selectedTextFormated) { try { // Trace.TraceInformation(System.Reflection.MethodBase.GetCurrentMethod().Name); string htmlContent = File.ReadAllText(fileName, new UTF8Encoding(false)); string byteOrderMarkUtf8 = Encoding.UTF8.GetString(Encoding.UTF8.GetPreamble()); htmlContent = htmlContent.Replace(byteOrderMarkUtf8, ""); var pageNode = GetPageNode(); if (pageNode != null) { var existingPageId = pageNode.Attribute("ID").Value; string[] position = null; if (outline == null) { position = GetMousePointPosition(pageXml); } var page = InsertHighLightCode(htmlContent, position, parameters, outline, (new GenerateHighLight()).Config, selectedTextFormated, IsSelectedTextInline(pageXml)); page.Root.SetAttributeValue("ID", existingPageId); //Bug fix - remove overflow value for Indents foreach (var el in page.Descendants(ns + "Indent").Where(n => double.Parse(n.Attribute("indent").Value, new CultureInfo(page.Root.Attribute("lang").Value)) > 1000000)) { el.Attribute("indent").Value = "0"; } OneNoteApplication.UpdatePageContent(page.ToString(), DateTime.MinValue); } } catch (Exception e) { MessageBox.Show("Exception from InsertHighLightCodeToCurrentSide: " + e.ToString()); } }
/// <summary> /// 產生 XML 插入至 OneNote /// Generate XML Insert To OneNote /// </summary> public XDocument InsertHighLightCode(string htmlContent, string[] position, HighLightParameter parameters, XElement outline, bool selectedTextFormated) { XElement children = new XElement(ns + "OEChildren"); XElement table = new XElement(ns + "Table"); table.Add(new XAttribute("bordersVisible", "false")); XElement columns = new XElement(ns + "Columns"); XElement column1 = new XElement(ns + "Column"); column1.Add(new XAttribute("index", "0")); column1.Add(new XAttribute("width", "40")); if (parameters.ShowLineNumber) { columns.Add(column1); } XElement column2 = new XElement(ns + "Column"); if (parameters.ShowLineNumber) { column2.Add(new XAttribute("index", "1")); } else { column2.Add(new XAttribute("index", "0")); } column2.Add(new XAttribute("width", "1400")); columns.Add(column2); table.Add(columns); Color color = parameters.HighlightColor; string colorString = string.Format("#{0:X2}{1:X2}{2:X2}", color.R, color.G, color.B); XElement row = new XElement(ns + "Row"); XElement cell1 = new XElement(ns + "Cell"); cell1.Add(new XAttribute("shadingColor", colorString)); XElement cell2 = new XElement(ns + "Cell"); cell2.Add(new XAttribute("shadingColor", colorString)); string defaultStyle = ""; var arrayLine = htmlContent.Split(new string[] { Environment.NewLine }, StringSplitOptions.None); foreach (var it in arrayLine) { string item = it; if (item.StartsWith("<pre")) { defaultStyle = item.Substring(0, item.IndexOf("<span")); item = item.Substring(item.IndexOf("<span")); } if (item == "</pre>") { continue; } var itemNr = ""; var itemLine = ""; if (parameters.ShowLineNumber) { if (item.Contains("</span>")) { int ind = item.IndexOf("</span>"); itemNr = item.Substring(0, ind + ("</span>").Length); itemLine = item.Substring(ind); } else { itemNr = ""; itemLine = item; } //string nr = string.Format(@"<body style=""font-family:{0}"">", GenerateHighlightContent.GenerateHighLight.Config.OutputArguments["Font"].Value) + // itemNr.Replace("'", "'") + "</body>"; string nr = defaultStyle + itemNr.Replace("'", "'") + "</pre>"; cell1.Add(new XElement(ns + "OEChildren", new XElement(ns + "OE", new XElement(ns + "T", new XCData(nr))))); } else { itemLine = item; } //string s = item.Replace(@"style=""", string.Format(@"style=""font-family:{0}; ", GenerateHighlightContent.GenerateHighLight.Config.OutputArguments["Font"].Value)); //string s = string.Format(@"<body style=""font-family:{0}"">", GenerateHighlightContent.GenerateHighLight.Config.OutputArguments["Font"].Value) + // itemLine.Replace("'", "'") + "</body>"; string s = defaultStyle + itemLine.Replace("'", "'") + "</body>"; cell2.Add(new XElement(ns + "OEChildren", new XElement(ns + "OE", new XElement(ns + "T", new XCData(s))))); } if (parameters.ShowLineNumber) { row.Add(cell1); } row.Add(cell2); table.Add(row); children.Add(new XElement(ns + "OE", table)); bool update = false; if (outline == null) { outline = new XElement(ns + "Outline"); if (position != null && position.Length == 2) { XElement pos = new XElement(ns + "Position"); pos.Add(new XAttribute("x", position[0])); pos.Add(new XAttribute("y", position[1])); outline.Add(pos); XElement size = new XElement(ns + "Size"); size.Add(new XAttribute("width", "1600")); size.Add(new XAttribute("height", "200")); outline.Add(size); } outline.Add(children); } else { update = true; if (selectedTextFormated) { outline.Descendants(ns + "Table").Where(n => n.Attribute("selected") != null && (n.Attribute("selected").Value == "all" || n.Attribute("selected").Value == "partial")).FirstOrDefault().ReplaceWith(children.Descendants(ns + "Table").FirstOrDefault()); //outline.Descendants().Where(n => n.Attribute("selected") != null && n.Attribute("selected").Value == "all").Remove(); } else { outline.Descendants(ns + "T").Where(n => n.Attribute("selected") != null && n.Attribute("selected").Value == "all").FirstOrDefault().ReplaceWith(children.Descendants(ns + "Table").FirstOrDefault()); outline.Descendants(ns + "OE").Where(t => t.Elements(ns + "T").Any(n => n.Attribute("selected") != null && n.Attribute("selected").Value == "all")).Remove(); } } if (update) { return(outline.Parent.Document); } else { XElement page = new XElement(ns + "Page"); page.Add(outline); XDocument doc = new XDocument(); doc.Add(page); return(doc); } }