Skip to content

Commit

Permalink
special chars (#1622)
Browse files Browse the repository at this point in the history
  • Loading branch information
stevencohn authored Oct 20, 2024
1 parent ae0f8d5 commit 40125c7
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 40 deletions.
22 changes: 10 additions & 12 deletions OneMore/Commands/File/ArchiveCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -216,8 +216,11 @@ private async Task Archive(ProgressDialog progress, XElement root, string path)
progress.SetMessage($"Archiving {page.Title ?? Resx.phrase_QuickNote}");
progress.Increment();

await ArchivePage(element, page, path);
order.Add(page.Title.Trim());
var name = await ArchivePage(element, page, path);
if (name is not null)
{
order.Add(name);
}

CleanupTemp();
}
Expand All @@ -235,7 +238,6 @@ private async Task Archive(ProgressDialog progress, XElement root, string path)
var name = element.Attribute("name").Value.Trim();

await Archive(progress, element, Path.Combine(path, name));
//order.Add(name);
}
}

Expand All @@ -246,7 +248,7 @@ private async Task Archive(ProgressDialog progress, XElement root, string path)
}


private async Task ArchivePage(XElement element, Page page, string path)
private async Task<string> ArchivePage(XElement element, Page page, string path)
{
if (page.Title == null)
{
Expand All @@ -256,10 +258,6 @@ private async Task ArchivePage(XElement element, Page page, string path)

quickCount++;
}
else
{
page.SetTitle(page.Title.Trim());
}

var name = PathHelper.CleanFileName(page.Title).Trim();
if (string.IsNullOrEmpty(name))
Expand Down Expand Up @@ -287,11 +285,11 @@ private async Task ArchivePage(XElement element, Page page, string path)
filename = await archivist.ExportHTML(page, filename, path, bookScope);
await ArchiveAssets(Path.GetDirectoryName(filename), path);
pageCount++;
return name;
}
else
{
logger.WriteLine($"archive path too long [{tpath}\\{name}.htm]");
}

logger.WriteLine($"archive path too long [{tpath}\\{name}.htm]");
return null;
}


Expand Down
2 changes: 2 additions & 0 deletions OneMore/Commands/Images/PlantUmlCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,8 @@ private byte[] Render(string text)
errorMessage = renderer.ErrorMessages;
logger.WriteLine("rendering messages:");
logger.WriteLine(renderer.ErrorMessages);
logger.WriteLine("text ---");
logger.WriteLine(text);
}
return false;
Expand Down
17 changes: 13 additions & 4 deletions OneMore/Helpers/Extensions/StringExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -342,10 +342,8 @@ public static (string, string) SplitAtLastWord(this string s)
/// </summary>
/// <param name="s"></param>
/// <returns></returns>
public static XElement ToXmlWrapper(this string s)
public static XElement ToXmlWrapper(this string s, string name = "wrapper")
{
// ensure proper XML

// OneNote doesn't like &nbsp; inside CDATAs but &#160; is OK
// and is the same as \u00A0 but 1-byte
var value = s.Replace("&nbsp;", "&#160;");
Expand All @@ -356,7 +354,18 @@ public static XElement ToXmlWrapper(this string s)
// quote unquote language attribute, e.g., lang=yo to lang="yo" (or two part en-US)
value = Regex.Replace(value, @"(\s)lang=([\w\-]+)([\s/>])", "$1lang=\"$2\"$3");

return XElement.Parse($"<wrapper>{value}</wrapper>");
// escape &
//value = System.Security.SecurityElement.Escape(value);

try
{
return XElement.Parse($"<{name}>{value}</{name}>", LoadOptions.PreserveWhitespace);
}
catch
{
Logger.Current.WriteLine($"error wrapping /{value}/");
throw;
}
}


Expand Down
25 changes: 1 addition & 24 deletions OneMore/Helpers/Extensions/XCDataExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,30 +50,7 @@ public static bool IsEmpty(this XCData cdata)
/// </remarks>
public static XElement GetWrapper(this XCData cdata)
{
// ensure proper XML

// OneNote doesn't like &nbsp; but &#160; is ok and is the same as \u00A0 but 1-byte
var value = cdata.Value.Replace("&nbsp;", "&#160;");

// XElement doesn't like <br> so replace with <br/>
value = Regex.Replace(value, @"\<\s*br\s*\>", "<br/>");

// quote unquoted language attribute, e.g., lang=yo to lang="yo" (or two part en-US)
value = Regex.Replace(value, @"(\s)lang=([\w\-]+)([\s/>])", "$1lang=\"$2\"$3");

// replace non-printable characters (0-31) that break XML validation with "�"
// this will change ("fix") user data but there's nothing we can do about it
value = Regex.Replace(value, @"(&#(?:0?[0-9]|[12][0-9]|3[01]);)", UnicodeReplacementChar);

try
{
return XElement.Parse("<cdata>" + value + "</cdata>", LoadOptions.PreserveWhitespace);
}
catch
{
Logger.Current.WriteLine($"error wrapping /{value}/");
throw;
}
return cdata.Value.ToXmlWrapper("cdata");
}


Expand Down

0 comments on commit 40125c7

Please sign in to comment.