Skip to content

Commit

Permalink
Merge branch 'format-existig-code'
Browse files Browse the repository at this point in the history
  • Loading branch information
elvirbrk committed Aug 1, 2017
2 parents 1f44b77 + ad06c06 commit c4f331e
Show file tree
Hide file tree
Showing 7 changed files with 236 additions and 45 deletions.
163 changes: 129 additions & 34 deletions NoteHighlightAddin/AddIn.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,19 @@ private void ShowForm()
//form.ShowDialog();

//TestForm t = new TestForm();
var pageNode = GetPageNode();
string selectedText = "";
XElement outline = null;

MainForm form = new MainForm(tag, outFileName);
if (pageNode != null)
{
var existingPageId = pageNode.Attribute("ID").Value;
selectedText = GetSelectedText(existingPageId);

outline = GetOutline(existingPageId);
}

MainForm form = new MainForm(tag, outFileName, selectedText);

System.Windows.Forms.Application.Run(form);
//}
Expand All @@ -154,7 +165,7 @@ private void ShowForm()

if (File.Exists(fileName))
{
InsertHighLightCodeToCurrentSide(fileName, form.Parameters);
InsertHighLightCodeToCurrentSide(fileName, form.Parameters, outline);
}
}

Expand Down Expand Up @@ -190,11 +201,31 @@ public IStream GetImage(string imageName)
/// 插入 HighLight Code 至滑鼠游標的位置
/// Insert HighLight Code To Mouse Position
/// </summary>
private void InsertHighLightCodeToCurrentSide(string fileName, HighLightParameter parameters)
private void InsertHighLightCodeToCurrentSide(string fileName, HighLightParameter parameters, XElement outline)
{
// 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);
page.Root.SetAttributeValue("ID", existingPageId);

OneNoteApplication.UpdatePageContent(page.ToString(), DateTime.MinValue);
}
}

XElement GetPageNode()
{
string notebookXml;
try
{
Expand All @@ -203,7 +234,7 @@ private void InsertHighLightCodeToCurrentSide(string fileName, HighLightParamete
catch (Exception ex)
{
MessageBox.Show("Exception from onApp.GetHierarchy:" + ex.Message);
return;
return null; ;
}

var doc = XDocument.Parse(notebookXml);
Expand All @@ -212,18 +243,7 @@ private void InsertHighLightCodeToCurrentSide(string fileName, HighLightParamete
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);
}
return pageNode;
}

/// <summary>
Expand Down Expand Up @@ -251,11 +271,69 @@ private string[] GetMousePointPosition(string pageID)
return null;
}

private XElement GetOutline(string pageID)
{
string pageXml;
OneNoteApplication.GetPageContent(pageID, out pageXml, PageInfo.piSelection);

var node = XDocument.Parse(pageXml).Descendants(ns + "Outline")
.Where(n => n.Attribute("selected") != null && n.Attribute("selected").Value == "all")
.FirstOrDefault();
//if (node != null)
//{
// var attrPos = node.Descendants(ns + "Position").FirstOrDefault();
// if (attrPos != null)
// {
// var x = attrPos.Attribute("x").Value;
// var y = attrPos.Attribute("y").Value;
// return new string[] { x, y };
// }
//}
//return null;

return node;
}

private string GetSelectedText(string pageID)
{
string pageXml;
OneNoteApplication.GetPageContent(pageID, out pageXml, PageInfo.piSelection);

var node = XDocument.Parse(pageXml).Descendants(ns + "Outline")
.Where(n => n.Attribute("selected") != null && n.Attribute("selected").Value == "all")
.FirstOrDefault();

StringBuilder sb = new StringBuilder();
if (node != null)
{
var table = node.Descendants(ns + "Table").FirstOrDefault();

System.Collections.Generic.IEnumerable<XElement> attrPos;
if (table == null)
{
attrPos = node.Descendants(ns + "OEChildren").Descendants(ns + "T");
}
else
{
attrPos = table.Descendants(ns + "Cell").LastOrDefault().Descendants(ns + "T");
}

foreach (var line in attrPos)
{
var htmlDocument = new HtmlAgilityPack.HtmlDocument();
htmlDocument.LoadHtml(line.Value);

sb.AppendLine(HttpUtility.HtmlDecode(htmlDocument.DocumentNode.InnerText));
}
}
return sb.ToString();
}

/// <summary>
/// 產生 XML 插入至 OneNote
/// Generate XML Insert To OneNote
/// </summary>
public XDocument InsertHighLightCode(string htmlContent, string[] position, HighLightParameter parameters)
public XDocument InsertHighLightCode(string htmlContent, string[] position, HighLightParameter parameters, XElement outline)
{
XElement children = new XElement(ns + "OEChildren");

Expand Down Expand Up @@ -364,29 +442,46 @@ public XDocument InsertHighLightCode(string htmlContent, string[] position, High
children.Add(new XElement(ns + "OE",
table));

XElement outline = new XElement(ns + "Outline");
bool update = false;
if (outline == null)
{
outline = new XElement(ns + "Outline");

if (position != null && position.Length == 2)
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);
}
}
else
{
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);
update = true;
outline.RemoveNodes();
}
outline.Add(children);

XElement page = new XElement(ns + "Page");
page.Add(outline);
outline.Add(children);
if (update)
{
return outline.Parent.Document;
}
else
{
XElement page = new XElement(ns + "Page");
page.Add(outline);

XDocument doc = new XDocument();
doc.Add(page);
XDocument doc = new XDocument();
doc.Add(page);
return doc;
}

return doc;

}

}
Expand Down
3 changes: 2 additions & 1 deletion NoteHighlightAddin/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,13 @@ public partial class MainForm : Form

#region -- Constructor --

public MainForm(string codeType, string fileName)
public MainForm(string codeType, string fileName, string selectedText)
{
_codeType = codeType;
_fileName = fileName;
InitializeComponent();
LoadThemes();
txtCode.Text = selectedText;

}

Expand Down
5 changes: 5 additions & 0 deletions NoteHighlightAddin/NoteHighlightAddin.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@
<Reference Include="extensibility, Version=7.0.3300.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<EmbedInteropTypes>True</EmbedInteropTypes>
</Reference>
<Reference Include="HtmlAgilityPack, Version=1.5.1.0, Culture=neutral, PublicKeyToken=bd319b19eaf3b43a, processorArchitecture=MSIL">
<HintPath>..\packages\HtmlAgilityPack.1.5.1\lib\Net45\HtmlAgilityPack.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="ICSharpCode.TextEditor, Version=3.0.0.3437, Culture=neutral, PublicKeyToken=4d61825e8dd49f1a, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>Lib\ICSharpCode.TextEditor.dll</HintPath>
Expand Down Expand Up @@ -159,6 +163,7 @@
<Content Include="Resources\SQL.png" />
<Content Include="Resources\XHTML.png" />
<Content Include="Resources\XML.png" />
<None Include="packages.config" />
<None Include="ribbon.xml">
<SubType>Designer</SubType>
</None>
Expand Down
4 changes: 2 additions & 2 deletions NoteHighlightAddin/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,5 @@
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
[assembly: AssemblyVersion("2.3.*")]
[assembly: AssemblyFileVersion("2.3.*")]
[assembly: AssemblyVersion("2.4.*")]
[assembly: AssemblyFileVersion("2.4.*")]
4 changes: 4 additions & 0 deletions NoteHighlightAddin/packages.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="HtmlAgilityPack" version="1.5.1" targetFramework="net45" />
</packages>
51 changes: 47 additions & 4 deletions Setup/Setup.vdproj
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@
}
"Entry"
{
"MsmKey" = "8:_5F7DADAECA95B8A8EECB662A9FCF5BB5"
"OwnerKey" = "8:_692D7E391D074D36AD2530720FB8D6C4"
"MsmSig" = "8:_UNDEFINED"
}
"Entry"
{
"MsmKey" = "8:_692D7E391D074D36AD2530720FB8D6C4"
"OwnerKey" = "8:_UNDEFINED"
"MsmSig" = "8:_UNDEFINED"
Expand Down Expand Up @@ -94,6 +100,12 @@
"Entry"
{
"MsmKey" = "8:_UNDEFINED"
"OwnerKey" = "8:_5F7DADAECA95B8A8EECB662A9FCF5BB5"
"MsmSig" = "8:_UNDEFINED"
}
"Entry"
{
"MsmKey" = "8:_UNDEFINED"
"OwnerKey" = "8:_CBF51C0C38434D629FC3271C5390E247"
"MsmSig" = "8:_UNDEFINED"
}
Expand Down Expand Up @@ -278,7 +290,7 @@
{
"AssemblyRegister" = "3:1"
"AssemblyIsInGAC" = "11:FALSE"
"AssemblyAsmDisplayName" = "8:GenerateHighlightContent, Version=2.0.0.0, Culture=neutral, PublicKeyToken=77d9ec1ac4fb0cdc, processorArchitecture=MSIL"
"AssemblyAsmDisplayName" = "8:GenerateHighlightContent, Version=2.3.0.0, Culture=neutral, PublicKeyToken=77d9ec1ac4fb0cdc, processorArchitecture=MSIL"
"ScatterAssemblies"
{
"_0FFDD9474C2B804E2E2B2C7C6360577B"
Expand Down Expand Up @@ -345,6 +357,37 @@
"IsDependency" = "11:FALSE"
"IsolateTo" = "8:"
}
"{9F6F8455-1EF1-4B85-886A-4223BCC8E7F7}:_5F7DADAECA95B8A8EECB662A9FCF5BB5"
{
"AssemblyRegister" = "3:1"
"AssemblyIsInGAC" = "11:FALSE"
"AssemblyAsmDisplayName" = "8:HtmlAgilityPack, Version=1.5.1.0, Culture=neutral, PublicKeyToken=bd319b19eaf3b43a, processorArchitecture=MSIL"
"ScatterAssemblies"
{
"_5F7DADAECA95B8A8EECB662A9FCF5BB5"
{
"Name" = "8:HtmlAgilityPack.dll"
"Attributes" = "3:512"
}
}
"SourcePath" = "8:HtmlAgilityPack.dll"
"TargetName" = "8:"
"Tag" = "8:"
"Folder" = "8:_76DE0C3E4C3C41398EBF2960C9F81C36"
"Condition" = "8:"
"Transitive" = "11:FALSE"
"Vital" = "11:TRUE"
"ReadOnly" = "11:FALSE"
"Hidden" = "11:FALSE"
"System" = "11:FALSE"
"Permanent" = "11:FALSE"
"SharedLegacy" = "11:FALSE"
"PackageAs" = "3:1"
"Register" = "3:1"
"Exclude" = "11:FALSE"
"IsDependency" = "11:TRUE"
"IsolateTo" = "8:"
}
"{9F6F8455-1EF1-4B85-886A-4223BCC8E7F7}:_805E0317B9E0B1539EEBB800C533393B"
{
"AssemblyRegister" = "3:1"
Expand Down Expand Up @@ -571,15 +614,15 @@
{
"Name" = "8:NoteHighlightAddin"
"ProductName" = "8:NoteHighlight2016"
"ProductCode" = "8:{60F24B03-C32C-4E84-8E4C-DF696910E5C2}"
"PackageCode" = "8:{17A94C9D-5142-4CC1-93D4-3C8896C40A95}"
"ProductCode" = "8:{C35EA8DF-ED92-4011-B998-B66F28C01EAF}"
"PackageCode" = "8:{CB51AF31-9D66-464C-8F45-70CE7425C909}"
"UpgradeCode" = "8:{0025873C-20C5-48D6-A93A-FBD3891A9233}"
"AspNetVersion" = "8:4.0.30319.0"
"RestartWWWService" = "11:FALSE"
"RemovePreviousVersions" = "11:TRUE"
"DetectNewerInstalledVersion" = "11:TRUE"
"InstallAllUsers" = "11:TRUE"
"ProductVersion" = "8:2.3"
"ProductVersion" = "8:2.4"
"Manufacturer" = "8:CodingRoad"
"ARPHELPTELEPHONE" = "8:"
"ARPHELPLINK" = "8:"
Expand Down
Loading

0 comments on commit c4f331e

Please sign in to comment.