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); }
private void LoadThemes() { try { HighLightSection section = (new GenerateHighLight()).Config; var workingDirectory = Path.Combine(ProcessHelper.GetDirectoryFromPath(Assembly.GetCallingAssembly().Location), section.FolderName, section.ThemeFolder); string[] files = Directory.GetFiles(workingDirectory, "*.theme"); foreach (var item in files) { cbx_style.Items.Add(Path.GetFileNameWithoutExtension(item)); } } catch (Exception e) { MessageBox.Show("Exception from MainForm.LoadThemes:" + e.Message); return; } }
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); }
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> /// 產生 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; //Change outline width outline.Element(ns + "Size").Attribute("width").Value = "1600"; 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(); outline.Descendants(ns + "OEChildren").Where(n => n.HasElements == false && n.Attribute("selected") != null && (n.Attribute("selected").Value == "partial")).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); } }