Skip to content

Commit 285871d

Browse files
committed
Fix file overriden issue on export hierarchial files
1 parent 1b6f3a1 commit 285871d

File tree

8 files changed

+74
-14
lines changed

8 files changed

+74
-14
lines changed

NoteWidgetAddIn/Export/AbstractExportor.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public virtual string ExportNodeToHierarchicalFiles(string nodeID, string export
6060
foreach (var node in rootNode.Descendants(n => n.NodeType == NodeType.Page))
6161
{
6262
var filePath = Path.Combine(rootPath, GetFullPathNodeName(node, '_') + FileExtension);
63-
CreatePageFile(node.ID, filePath);
63+
CreatePageFile(node.ID, PathHelper.MakeUniqueFileName(filePath));
6464
}
6565
return rootPath;
6666
}
@@ -73,7 +73,7 @@ private string ExportFileHierarchyRecursively(NoteNode parentNode, string hierar
7373
if (parentNode.NodeType == NodeType.Page)
7474
{
7575
var file = Path.Combine(hierarchyFolderPath, PathHelper.MakeValidFileName(parentNode.Name) + FileExtension);
76-
CreatePageFile(parentNode.ID, file);
76+
CreatePageFile(parentNode.ID, PathHelper.MakeUniqueFileName(file));
7777
}
7878
string currentFolderPath;
7979
if (parentNode.NodeType != NodeType.Page || (parentNode.NodeType == NodeType.Page && parentNode.Children.Count > 0))

NoteWidgetAddIn/Utils/PathHelper.cs

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) Efrey Kong. All Rights Reserved.
22
// Licensed under the Apache License, Version 2.0.
33

4+
using System;
45
using System.IO;
56
using System.Text.RegularExpressions;
67

@@ -30,23 +31,37 @@ public static string MakeValidFolderName(string folderName)
3031

3132
public static string MakeUniqueFolderName(string fullFolderName)
3233
{
33-
var currentPath = fullFolderName;
34-
if (Directory.Exists(currentPath))
34+
return MakeUniquePath(fullFolderName,
35+
d => { return Directory.Exists(d); },
36+
(d, i) => { return d + " (" + i.ToString() + ")"; });
37+
}
38+
39+
public static string MakeUniqueFileName(string filePath)
40+
{
41+
return MakeUniquePath(filePath,
42+
p => { return File.Exists(p); },
43+
(p, i) => { return Path.Combine(Path.GetDirectoryName(filePath), Path.GetFileNameWithoutExtension(p) + " (" + i.ToString() + ")" + Path.GetExtension(p)); });
44+
}
45+
46+
private static string MakeUniquePath(string path, Func<string, bool> checkPathExists, Func<string, int, string> makeNewPath)
47+
{
48+
var currentPath = path;
49+
if (checkPathExists(currentPath))
3550
{
36-
int i = 1;
37-
while (true)
51+
var i = 1;
52+
while(true)
3853
{
39-
currentPath = fullFolderName + " (" +i.ToString() + ")";
40-
if (!Directory.Exists(currentPath))
54+
currentPath = makeNewPath(path, i);
55+
if(!checkPathExists(currentPath))
4156
{
4257
break;
4358
}
4459
i++;
4560
}
4661
}
47-
4862
return currentPath;
4963
}
64+
5065
/// <summary>
5166
/// Returns virtual host root path start with <code>file:///</code> protocol.
5267
/// </summary>

NoteWidgetTests/DummyData/NotebookHierarchy.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
</one:Section>
3333
<one:SectionGroup name="DotNet" ID="{6F8F683A-D9FE-4CDD-8605-8BAA00E742EA}{1}{B0}" path="C:\OneNote\UnitTestNote\Backend\DotNet" lastModifiedTime="2021-12-21T07:57:54.000Z">
3434
<one:Section name="CSharp" ID="{443F89FE-0C2F-41D2-8FCA-B54E1F45349B}{1}{B0}" path="C:\OneNote\UnitTestNote\Backend\DotNet\CSharp.one" lastModifiedTime="2021-12-21T07:56:53.000Z" color="#91BAAE">
35+
<one:Page ID="{443F89FE-0C2F-41D2-8FCA-B54E1F45349B}{1}{E19554024442055462406520137364240064166667971}" name="Office Addin" dateTime="2021-12-21T07:52:40.000Z" lastModifiedTime="2022-04-11T07:56:29.000Z" pageLevel="1" />
3536
<one:Page ID="{443F89FE-0C2F-41D2-8FCA-B54E1F45349B}{1}{E19554024442055462406520137364240064103627971}" name="Office Addin" dateTime="2021-12-21T07:52:40.000Z" lastModifiedTime="2021-12-21T07:56:29.000Z" pageLevel="1" />
3637
<one:Page ID="{443F89FE-0C2F-41D2-8FCA-B54E1F45349B}{1}{E19524986248993555224420161066687775526316971}" name="OneNote add-in" dateTime="2021-12-21T07:56:34.000Z" lastModifiedTime="2021-12-21T07:56:47.000Z" pageLevel="2" />
3738
</one:Section>

NoteWidgetTests/DummyData/NotebookHierarchy_Expected.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
</one:Section>
1212
<one:SectionGroup name="DotNet" ID="{6F8F683A-D9FE-4CDD-8605-8BAA00E742EA}{1}{B0}" path="C:\OneNote\UnitTestNote\Backend\DotNet" lastModifiedTime="2021-12-21T07:57:54.000Z">
1313
<one:Section name="CSharp" ID="{443F89FE-0C2F-41D2-8FCA-B54E1F45349B}{1}{B0}" path="C:\OneNote\UnitTestNote\Backend\DotNet\CSharp.one" lastModifiedTime="2021-12-21T07:56:53.000Z" color="#91BAAE">
14-
<one:Page ID="{443F89FE-0C2F-41D2-8FCA-B54E1F45349B}{1}{E19554024442055462406520137364240064103627971}" name="Office Addin" dateTime="2021-12-21T07:52:40.000Z" lastModifiedTime="2021-12-21T07:56:29.000Z" pageLevel="1" >
14+
<one:Page ID="{443F89FE-0C2F-41D2-8FCA-B54E1F45349B}{1}{E19554024442055462406520137364240064166667971}" name="Office Addin" dateTime="2021-12-21T07:52:40.000Z" lastModifiedTime="2022-04-11T07:56:29.000Z" pageLevel="1" />
15+
<one:Page ID="{443F89FE-0C2F-41D2-8FCA-B54E1F45349B}{1}{E19554024442055462406520137364240064103627971}" name="Office Addin (1)" dateTime="2021-12-21T07:52:40.000Z" lastModifiedTime="2021-12-21T07:56:29.000Z" pageLevel="1" >
1516
<one:Page ID="{443F89FE-0C2F-41D2-8FCA-B54E1F45349B}{1}{E19524986248993555224420161066687775526316971}" name="OneNote add-in" dateTime="2021-12-21T07:56:34.000Z" lastModifiedTime="2021-12-21T07:56:47.000Z" pageLevel="2" />
1617
</one:Page>
1718
</one:Section>
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<one:Page xmlns:one="http://schemas.microsoft.com/office/onenote/2013/onenote" ID="{443F89FE-0C2F-41D2-8FCA-B54E1F45349B}{1}{E19554024442055462406520137364240064103627971}" name="Office Addin" dateTime="2021-12-21T07:52:40.000Z" lastModifiedTime="2021-12-23T14:58:44.000Z" pageLevel="1" isCurrentlyViewed="true" lang="en-US">
2+
<one:QuickStyleDef index="0" name="PageTitle" fontColor="automatic" highlightColor="automatic" font="Microsoft YaHei UI" fontSize="20.0" spaceBefore="0.0" spaceAfter="0.0" />
3+
<one:QuickStyleDef index="1" name="p" fontColor="automatic" highlightColor="automatic" font="Microsoft YaHei UI" fontSize="12.0" spaceBefore="0.0" spaceAfter="0.0" />
4+
<one:PageSettings RTL="false" color="automatic">
5+
<one:PageSize>
6+
<one:Automatic />
7+
</one:PageSize>
8+
<one:RuleLines visible="false" />
9+
</one:PageSettings>
10+
<one:Title lang="en-US">
11+
<one:OE author="efrey kong" authorInitials="EK" lastModifiedBy="efrey kong" lastModifiedByInitials="EK" creationTime="2021-12-21T07:55:39.000Z" lastModifiedTime="2021-12-21T07:56:30.000Z" objectID="{CF3178B8-782D-4FE2-BC94-763867512B51}{15}{B0}" alignment="left" quickStyleIndex="0" style="font-family:Calibri;font-size:20.0pt">
12+
<one:T><![CDATA[Office Addin]]></one:T>
13+
</one:OE>
14+
</one:Title>
15+
<one:Outline author="efrey kong" authorInitials="EK" lastModifiedBy="efrey kong" lastModifiedByInitials="EK" lastModifiedTime="2021-12-23T09:56:19.000Z" objectID="{697E4E52-2C7D-4F4F-9EEE-E5506ABA16E5}{16}{B0}">
16+
<one:Position x="36.0" y="104.400001525879" z="0" />
17+
<one:Size width="205.9984283447265" height="87.89054107666016" />
18+
<one:OEChildren>
19+
<one:OE creationTime="2021-12-21T07:55:46.000Z" lastModifiedTime="2021-12-21T07:56:19.000Z" objectID="{697E4E52-2C7D-4F4F-9EEE-E5506ABA16E5}{17}{B0}" alignment="left" quickStyleIndex="1" style="font-family:Calibri;font-size:12.0pt">
20+
<one:T><![CDATA[# Office add-ins overview]]></one:T>
21+
</one:OE>
22+
<one:OE creationTime="2021-12-21T07:55:53.000Z" lastModifiedTime="2021-12-21T07:55:53.000Z" objectID="{697E4E52-2C7D-4F4F-9EEE-E5506ABA16E5}{22}{B0}" alignment="left" quickStyleIndex="1" style="font-family:Calibri;font-size:12.0pt">
23+
<one:T><![CDATA[]]></one:T>
24+
</one:OE>
25+
<one:OE creationTime="2021-12-21T07:55:53.000Z" lastModifiedTime="2021-12-21T07:55:53.000Z" objectID="{697E4E52-2C7D-4F4F-9EEE-E5506ABA16E5}{25}{B0}" alignment="left" quickStyleIndex="1" style="font-family:Calibri;font-size:12.0pt">
26+
<one:T><![CDATA[]]></one:T>
27+
</one:OE>
28+
<one:OE creationTime="2021-12-21T07:55:54.000Z" lastModifiedTime="2021-12-23T09:56:19.000Z" objectID="{697E4E52-2C7D-4F4F-9EEE-E5506ABA16E5}{19}{B0}" alignment="left" quickStyleIndex="1" style="font-family:Calibri;font-size:12.0pt">
29+
<one:T><![CDATA[# How to develop an Office add-in]]></one:T>
30+
</one:OE>
31+
<one:OE creationTime="2021-12-23T09:56:19.000Z" lastModifiedTime="2021-12-23T09:56:19.000Z" objectID="{1EFB87A6-5C23-4D52-9208-BBA3AC553CE1}{24}{B0}" alignment="left" quickStyleIndex="1" style="font-family:Calibri;font-size:12.0pt" lang="zh-CN">
32+
<one:T><![CDATA[]]></one:T>
33+
</one:OE>
34+
<one:OE creationTime="2021-12-23T09:56:19.000Z" lastModifiedTime="2021-12-23T09:56:19.000Z" objectID="{1EFB87A6-5C23-4D52-9208-BBA3AC553CE1}{28}{B0}" alignment="left" quickStyleIndex="1" style="font-family:Calibri;font-size:12.0pt" lang="zh-CN">
35+
<one:T><![CDATA[]]></one:T>
36+
</one:OE>
37+
</one:OEChildren>
38+
</one:Outline>
39+
</one:Page>

NoteWidgetTests/DummyDataExportTest.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,19 +60,19 @@ private void DoTestExportToPath(ExportFormat fileFormat)
6060
Console.WriteLine(path);
6161
var di = new DirectoryInfo(path);
6262

63-
var xe = GetDummyExpectedNotebookXDoc();
63+
var xeExpected = GetDummyExpectedNotebookXDoc();
6464
FileInfo[] fs;
6565
fs = di.GetFiles("*.*", SearchOption.AllDirectories);
6666
DirectoryInfo[] ds = di.GetDirectories("*", SearchOption.AllDirectories);
67-
var dlist = xe.Descendants().Where(e => e.Name.LocalName != "Page" && e.Name.LocalName != "Notebook").Select(e => e.Attribute("name").Value).ToList();
67+
var dlist = xeExpected.Descendants().Where(e => e.Name.LocalName != "Page" && e.Name.LocalName != "Notebook").Select(e => e.Attribute("name").Value).ToList();
6868
foreach (var d in dlist)
6969
{
7070
Assert.IsTrue(ds.Any(e => e.Name == d));
7171
}
7272

7373
string fileExtension = ExportHelper.GetExportFormatFileExtension(fileFormat);
7474

75-
var plist = xe.Descendants(xe.Root.Name.Namespace + "Page").Select(e => e.Attribute("name").Value + fileExtension).ToList();
75+
var plist = xeExpected.Descendants(xeExpected.Root.Name.Namespace + "Page").Select(e => e.Attribute("name").Value + fileExtension).ToList();
7676
Assert.AreEqual(fs.Length, plist.Count);
7777
foreach (var f in fs)
7878
{

NoteWidgetTests/MarkdownTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public void TestTemplateToHtml()
5555
Assert.IsTrue(System.IO.File.Exists(filePath));
5656
}
5757
[TestMethod]
58-
public async Task TestWebView2()
58+
public void TestWebView2()
5959
{
6060
var browser = new WebBrowserWindow();
6161
browser.BrowserHtmlContent = "<html><head></head><body><div id=\"content\"><span>Hello world!</span></div></body></html>";

NoteWidgetTests/NoteWidgetTests.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
<ItemGroup>
1818
<None Remove="DummyData\{443F89FE-0C2F-41D2-8FCA-B54E1F45349B}{1}{E19524986248993555224420161066687775526316971}.xml" />
1919
<None Remove="DummyData\{443F89FE-0C2F-41D2-8FCA-B54E1F45349B}{1}{E19554024442055462406520137364240064103627971}.xml" />
20+
<None Remove="DummyData\{443F89FE-0C2F-41D2-8FCA-B54E1F45349B}{1}{E19554024442055462406520137364240064166667971}.xml" />
2021
<None Remove="DummyData\{5E3A0919-1331-41D5-B19C-4AE7F96D7626}{1}{E19529340608983392449520151492106384070007221}.xml" />
2122
<None Remove="DummyData\{842240F6-F77A-4421-B2F6-44510E03ABAB}{1}{E19483954550601460158520165992657368372596901}.xml" />
2223
<None Remove="DummyData\{9CA0EF3E-A634-41E8-A1B7-821A01E657E2}{1}{E1949968395446988629731972422109116520132061}.xml" />
@@ -39,6 +40,9 @@
3940
<Content Include="DummyData\{443F89FE-0C2F-41D2-8FCA-B54E1F45349B}{1}{E19524986248993555224420161066687775526316971}.xml">
4041
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
4142
</Content>
43+
<Content Include="DummyData\{443F89FE-0C2F-41D2-8FCA-B54E1F45349B}{1}{E19554024442055462406520137364240064166667971}.xml">
44+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
45+
</Content>
4246
<Content Include="DummyData\{443F89FE-0C2F-41D2-8FCA-B54E1F45349B}{1}{E19554024442055462406520137364240064103627971}.xml">
4347
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
4448
</Content>

0 commit comments

Comments
 (0)