Skip to content

Commit

Permalink
Merge pull request OpenXmlDev#8 from Codeuctivity/SymbolFontToUnicode
Browse files Browse the repository at this point in the history
Enabled image handler and text transform handler injection
  • Loading branch information
stesee committed Jan 10, 2021
2 parents d5879a7 + 37b79a2 commit 8e943a6
Show file tree
Hide file tree
Showing 91 changed files with 901 additions and 1,440 deletions.
3 changes: 3 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -199,3 +199,6 @@ dotnet_naming_style.begins_with_i.required_prefix = I
dotnet_naming_style.begins_with_i.required_suffix =
dotnet_naming_style.begins_with_i.word_separator =
dotnet_naming_style.begins_with_i.capitalization = pascal_case

# CS1591: Missing XML comment for publicly visible type or member
dotnet_diagnostic.CS1591.severity = suggestion
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"request": "launch",
"preLaunchTask": "build",
// If you have changed target frameworks, make sure to update the program path.
"program": "${workspaceFolder}/OpenXmlPowerToolsExamples/DocumentAssembler/bin/Debug/netcoreapp3.1/DocumentAssembler.dll",
"program": "${workspaceFolder}/OpenXmlPowerToolsExamples/DocumentAssembler/bin/Debug/net5.0/DocumentAssembler.dll",
"args": [],
"cwd": "${workspaceFolder}/OpenXmlPowerToolsExamples/DocumentAssembler",
// For more information about the 'console' field, see https://aka.ms/VSCode-CS-LaunchJson-Console
Expand Down
1 change: 1 addition & 0 deletions OpenXmlPowerTools.Tests/ChartUpdaterTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using DocumentFormat.OpenXml.Packaging;
using OpenXmlPowerTools;
using OpenXmlPowerTools.Tests;
using System;
using System.IO;
using Xunit;
Expand Down
1 change: 1 addition & 0 deletions OpenXmlPowerTools.Tests/DocumentAssemblerTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Validation;
using OpenXmlPowerTools;
using OpenXmlPowerTools.Tests;
using System;
using System.Collections.Generic;
using System.IO;
Expand Down
1 change: 1 addition & 0 deletions OpenXmlPowerTools.Tests/DocumentBuilderTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Validation;
using OpenXmlPowerTools;
using OpenXmlPowerTools.Tests;
using System;
using System.Collections.Generic;
using System.IO;
Expand Down
1 change: 1 addition & 0 deletions OpenXmlPowerTools.Tests/FormattingAssemblerTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Validation;
using OpenXmlPowerTools;
using OpenXmlPowerTools.Tests;
using System;
using System.Collections.Generic;
using System.IO;
Expand Down
356 changes: 0 additions & 356 deletions OpenXmlPowerTools.Tests/HtmlConverterTests.cs

This file was deleted.

18 changes: 9 additions & 9 deletions OpenXmlPowerTools.Tests/HtmlToWmlConverterTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Validation;
using OpenXmlPowerTools;
using OpenXmlPowerTools.Tests;
using System;
using System.IO;
using System.Linq;
Expand Down Expand Up @@ -360,10 +361,7 @@ public void HW001(string name)

var doc = HtmlToWmlConverter.ConvertHtmlToWml(defaultCss, usedAuthorCss, userCss, html, settings, null, s_ProduceAnnotatedHtml ? annotatedHtmlFi.FullName : null);
Assert.NotNull(doc);
if (doc != null)
{
SaveValidateAndFormatMainDocPart(destDocxFi, doc);
}
SaveValidateAndFormatMainDocPart(destDocxFi, doc);
}

[Theory]
Expand Down Expand Up @@ -397,6 +395,11 @@ private static void SaveValidateAndFormatMainDocPart(FileInfo destDocxFi, WmlDoc
{
WmlDocument formattedDoc;

if (File.Exists(destDocxFi.FullName))
{
File.Delete(destDocxFi.FullName);
}

doc.SaveAs(destDocxFi.FullName);
using (var ms = new MemoryStream())
{
Expand All @@ -407,12 +410,9 @@ private static void SaveValidateAndFormatMainDocPart(FileInfo destDocxFi, WmlDoc
document.MainDocumentPart.PutXDocumentWithFormatting();
var validator = new OpenXmlValidator();
var errors = validator.Validate(document);
var errorsString = errors
.Select(e => e.Description + Environment.NewLine)
.StringConcatenate();
var errorsString = errors.Select(e => e.Description + Environment.NewLine).StringConcatenate();

// Assert that there were no errors in the generated document.
Assert.Equal("", errorsString);
Assert.True(errorsString.Length == 0, $"Error in {destDocxFi.FullName}\n{errorsString}");
}
formattedDoc = new WmlDocument(destDocxFi.FullName, ms.ToArray());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
using DocumentFormat.OpenXml.Packaging;
using OpenXmlPowerTools;
using OpenXmlPowerTools.OpenXMLWordprocessingMLToHtmlConverter;
using OpenXmlPowerTools.Tests;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using Xunit;

namespace OxPt
{
public class WmlToHtmlConverterTests
{
// PowerShell oneliner that generates InlineData for all files in a directory
// dir | % { '[InlineData("' + $_.Name + '")]' } | clip

[Theory]
[InlineData("HC001-5DayTourPlanTemplate.docx")]
[InlineData("HC002-Hebrew-01.docx")]
[InlineData("HC003-Hebrew-02.docx")]
[InlineData("HC004-ResumeTemplate.docx")]
[InlineData("HC005-TaskPlanTemplate.docx")]
[InlineData("HC006-Test-01.docx")]
[InlineData("HC007-Test-02.docx")]
[InlineData("HC008-Test-03.docx")]
[InlineData("HC009-Test-04.docx")]
[InlineData("HC010-Test-05.docx")]
[InlineData("HC011-Test-06.docx")]
[InlineData("HC012-Test-07.docx")]
[InlineData("HC013-Test-08.docx")]
[InlineData("HC014-RTL-Table-01.docx")]
[InlineData("HC015-Vertical-Spacing-atLeast.docx")]
[InlineData("HC016-Horizontal-Spacing-firstLine.docx")]
[InlineData("HC017-Vertical-Alignment-Cell-01.docx")]
[InlineData("HC018-Vertical-Alignment-Para-01.docx")]
[InlineData("HC019-Hidden-Run.docx")]
[InlineData("HC020-Small-Caps.docx")]
[InlineData("HC021-Symbols.docx")]
[InlineData("HC022-Table-Of-Contents.docx")]
[InlineData("HC023-Hyperlink.docx")]
[InlineData("HC024-Tabs-01.docx")]
[InlineData("HC025-Tabs-02.docx")]
[InlineData("HC026-Tabs-03.docx")]
[InlineData("HC027-Tabs-04.docx")]
[InlineData("HC028-No-Break-Hyphen.docx")]
[InlineData("HC029-Table-Merged-Cells.docx")]
[InlineData("HC030-Content-Controls.docx")]
[InlineData("HC031-Complicated-Document.docx")]
[InlineData("HC032-Named-Color.docx")]
[InlineData("HC033-Run-With-Border.docx")]
[InlineData("HC034-Run-With-Position.docx")]
[InlineData("HC035-Strike-Through.docx")]
[InlineData("HC036-Super-Script.docx")]
[InlineData("HC037-Sub-Script.docx")]
[InlineData("HC038-Conflicting-Border-Weight.docx")]
[InlineData("HC039-Bold.docx")]
[InlineData("HC040-Hyperlink-Fieldcode-01.docx")]
[InlineData("HC041-Hyperlink-Fieldcode-02.docx")]
[InlineData("HC042-Image-Png.docx")]
[InlineData("HC043-Chart.docx")]
[InlineData("HC044-Embedded-Workbook.docx")]
[InlineData("HC045-Italic.docx")]
[InlineData("HC046-BoldAndItalic.docx")]
[InlineData("HC047-No-Section.docx")]
[InlineData("HC048-Excerpt.docx")]
[InlineData("HC049-Borders.docx")]
[InlineData("HC050-Shaded-Text-01.docx")]
[InlineData("HC051-Shaded-Text-02.docx")]
[InlineData("HC060-Image-with-Hyperlink.docx")]
[InlineData("HC061-Hyperlink-in-Field.docx")]
public void HC001(string name)
{
var sourceDir = new DirectoryInfo("../../../../TestFiles/");
var sourceDocx = new FileInfo(Path.Combine(sourceDir.FullName, name));

var oxPtConvertedDestHtml = new FileInfo(Path.Combine(TestUtil.TempDir.FullName, sourceDocx.Name.Replace(".docx", "-3-OxPt.html")));
ConvertToHtml(sourceDocx, oxPtConvertedDestHtml, false);
}

[Theory]
[InlineData("HC006-Test-01.docx")]
public void HC002_NoCssClasses(string name)
{
var sourceDir = new DirectoryInfo("../../../../TestFiles/");
var sourceDocx = new FileInfo(Path.Combine(sourceDir.FullName, name));

var oxPtConvertedDestHtml = new FileInfo(Path.Combine(TestUtil.TempDir.FullName, sourceDocx.Name.Replace(".docx", "-5-OxPt-No-CSS-Classes.html")));
ConvertToHtml(sourceDocx, oxPtConvertedDestHtml, true);
}

private static void CopyFormattingAssembledDocx(FileInfo source, FileInfo dest)
{
var ba = File.ReadAllBytes(source.FullName);
using var ms = new MemoryStream();
ms.Write(ba, 0, ba.Length);
using (var wordDoc = WordprocessingDocument.Open(ms, true))
{
RevisionAccepter.AcceptRevisions(wordDoc);
var simplifyMarkupSettings = new SimplifyMarkupSettings
{
RemoveComments = true,
RemoveContentControls = true,
RemoveEndAndFootNotes = true,
RemoveFieldCodes = false,
RemoveLastRenderedPageBreak = true,

RemovePermissions = true,
RemoveProof = true,
RemoveRsidInfo = true,
RemoveSmartTags = true,
RemoveSoftHyphens = true,
RemoveGoBackBookmark = true,
ReplaceTabsWithSpaces = false,
};
MarkupSimplifier.SimplifyMarkup(wordDoc, simplifyMarkupSettings);

var formattingAssemblerSettings = new FormattingAssemblerSettings
{
RemoveStyleNamesFromParagraphAndRunProperties = false,
ClearStyles = false,
RestrictToSupportedLanguages = false,
RestrictToSupportedNumberingFormats = false,
CreateHtmlConverterAnnotationAttributes = true,
OrderElementsPerStandard = false,
ListItemRetrieverSettings =
new ListItemRetrieverSettings()
{
ListItemTextImplementations = ListItemRetrieverSettings.DefaultListItemTextImplementations,
},
};

FormattingAssembler.AssembleFormatting(wordDoc, formattingAssemblerSettings);
}
var newBa = ms.ToArray();
File.WriteAllBytes(dest.FullName, newBa);
}

private static void ConvertToHtml(FileInfo sourceDocx, FileInfo destFileName, bool fabricateCssClasses)
{
var byteArray = File.ReadAllBytes(sourceDocx.FullName);
using var memoryStream = new MemoryStream();
memoryStream.Write(byteArray, 0, byteArray.Length);
using var wDoc = WordprocessingDocument.Open(memoryStream, true);
var outputDirectory = destFileName.Directory;
destFileName = new FileInfo(Path.Combine(outputDirectory.FullName, destFileName.Name));
var imageDirectoryName = destFileName.FullName.Substring(0, destFileName.FullName.Length - 5) + "_files";
var pageTitle = (string)wDoc.CoreFilePropertiesPart.GetXDocument().Descendants(DC.title).FirstOrDefault();
if (pageTitle == null)
{
pageTitle = sourceDocx.FullName;
}

var settings = new WmlToHtmlConverterSettings()
{
PageTitle = pageTitle,
FabricateCssClasses = fabricateCssClasses,
CssClassPrefix = fabricateCssClasses ? "pt-" : null,
RestrictToSupportedLanguages = false,
RestrictToSupportedNumberingFormats = false
};

var html = WmlToHtmlConverter.ConvertToHtml(wDoc, settings);

// Note: the xhtml returned by ConvertToHtmlTransform contains objects of type XEntity. PtOpenXmlUtil.cs define the XEntity class. See http://blogs.msdn.com/ericwhite/archive/2010/01/21/writing-entity-references-using-linq-to-xml.aspx for detailed explanation.
// If you further transform the XML tree returned by ConvertToHtmlTransform, you must do it correctly, or entities will not be serialized properly.

var htmlString = html.ToString(SaveOptions.DisableFormatting);
File.WriteAllText(destFileName.FullName, htmlString, Encoding.UTF8);
}
}
}
2 changes: 1 addition & 1 deletion OpenXmlPowerTools.Tests/OpenXmlPowerTools.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks Condition="'$(OS)' == 'Windows_NT' ">net472;net5.0</TargetFrameworks>
<TargetFrameworks Condition="'$(OS)' == 'Windows_NT' ">net472;net5.0;netcoreapp3.1</TargetFrameworks>
<TargetFrameworks Condition="'$(OS)' != 'Windows_NT' ">net5.0</TargetFrameworks>
<LangVersion>8.0</LangVersion>
<EnableNETAnalyzers>true</EnableNETAnalyzers>
Expand Down
1 change: 1 addition & 0 deletions OpenXmlPowerTools.Tests/PresentationBuilderTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using DocumentFormat.OpenXml.Packaging;
using OpenXmlPowerTools;
using OpenXmlPowerTools.Tests;
using System.Collections.Generic;
using System.IO;
using System.Linq;
Expand Down
1 change: 1 addition & 0 deletions OpenXmlPowerTools.Tests/RevisionAccepterTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using OpenXmlPowerTools;
using OpenXmlPowerTools.Tests;
using System.IO;
using Xunit;

Expand Down
1 change: 1 addition & 0 deletions OpenXmlPowerTools.Tests/RevisionProcessorTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using OpenXmlPowerTools;
using OpenXmlPowerTools.Tests;
using System;
using System.IO;
using System.Linq;
Expand Down
3 changes: 2 additions & 1 deletion OpenXmlPowerTools.Tests/SmlCellFormatterTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using DocumentFormat.OpenXml.Packaging;
using OpenXmlPowerTools;
using OpenXmlPowerTools.Tests;
using System.IO;
using System.Linq;
using Xunit;
Expand Down Expand Up @@ -119,7 +120,7 @@ public void CF002(string name, string sheetName, string range, string expected,
File.Copy(sourceXlsx.FullName, sourceCopiedToDestXlsx.FullName);
}

using var sDoc = SpreadsheetDocument.Open(sourceXlsx.FullName, true);
using var sDoc = SpreadsheetDocument.Open(sourceCopiedToDestXlsx.FullName, true);
var rangeXml = SmlDataRetriever.RetrieveRange(sDoc, sheetName, range);
var displayValue = (string)rangeXml.Descendants("DisplayValue").FirstOrDefault();
var displayColor = (string)rangeXml.Descendants("DisplayColor").FirstOrDefault();
Expand Down
17 changes: 9 additions & 8 deletions OpenXmlPowerTools.Tests/SmlToHtmlConverterTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using DocumentFormat.OpenXml.Packaging;
using OpenXmlPowerTools;
using OpenXmlPowerTools.Tests;
using System.IO;
using Xunit;

Expand Down Expand Up @@ -53,8 +54,8 @@ public void SH005_ConvertSheet(string name, string sheetName)
}

var dataTemplateFileNameSuffix = "-2-Generated-XmlData-Entire-Sheet.xml";
var dataXmlFi = new FileInfo(Path.Combine(TestUtil.TempDir.FullName, sourceXlsx.Name.Replace(".xlsx", dataTemplateFileNameSuffix)));
using var sDoc = SpreadsheetDocument.Open(sourceXlsx.FullName, true);
var dataXmlFi = new FileInfo(Path.Combine(TestUtil.TempDir.FullName, sourceCopiedToDestXlsx.Name.Replace(".xlsx", dataTemplateFileNameSuffix)));
using var sDoc = SpreadsheetDocument.Open(sourceCopiedToDestXlsx.FullName, true);
var settings = new SmlToHtmlConverterSettings();
var rangeXml = SmlDataRetriever.RetrieveSheet(sDoc, sheetName);
rangeXml.Save(dataXmlFi.FullName);
Expand Down Expand Up @@ -111,8 +112,8 @@ public void SH004_ConvertRange(string name, string sheetName, string range)
}

var dataTemplateFileNameSuffix = string.Format("-2-Generated-XmlData-{0}.xml", range.Replace(":", ""));
var dataXmlFi = new FileInfo(Path.Combine(TestUtil.TempDir.FullName, sourceXlsx.Name.Replace(".xlsx", dataTemplateFileNameSuffix)));
using var sDoc = SpreadsheetDocument.Open(sourceXlsx.FullName, true);
var dataXmlFi = new FileInfo(Path.Combine(TestUtil.TempDir.FullName, sourceCopiedToDestXlsx.Name.Replace(".xlsx", dataTemplateFileNameSuffix)));
using var sDoc = SpreadsheetDocument.Open(sourceCopiedToDestXlsx.FullName, true);
var settings = new SmlToHtmlConverterSettings();
var rangeXml = SmlDataRetriever.RetrieveRange(sDoc, sheetName, range);
rangeXml.Save(dataXmlFi.FullName);
Expand All @@ -139,8 +140,8 @@ public void SH003_ConvertTable(string name, string tableName)
File.Copy(sourceXlsx.FullName, sourceCopiedToDestXlsx.FullName);
}

var dataXmlFi = new FileInfo(Path.Combine(TestUtil.TempDir.FullName, sourceXlsx.Name.Replace(".xlsx", "-2-Generated-XmlData.xml")));
using var sDoc = SpreadsheetDocument.Open(sourceXlsx.FullName, true);
var dataXmlFi = new FileInfo(Path.Combine(TestUtil.TempDir.FullName, sourceCopiedToDestXlsx.Name.Replace(".xlsx", "-2-Generated-XmlData.xml")));
using var sDoc = SpreadsheetDocument.Open(sourceCopiedToDestXlsx.FullName, true);
var settings = new SmlToHtmlConverterSettings();
var rangeXml = SmlDataRetriever.RetrieveTable(sDoc, tableName);
rangeXml.Save(dataXmlFi.FullName);
Expand All @@ -152,7 +153,7 @@ public void SH002_SheetNames(string name, int numberOfSheets)
{
var sourceDir = new DirectoryInfo("../../../../TestFiles/");
var sourceXlsx = new FileInfo(Path.Combine(sourceDir.FullName, name));
using var sDoc = SpreadsheetDocument.Open(sourceXlsx.FullName, true);
using var sDoc = SpreadsheetDocument.Open(sourceXlsx.FullName, false);
var sheetNames = SmlDataRetriever.SheetNames(sDoc);
Assert.Equal(numberOfSheets, sheetNames.Length);
}
Expand All @@ -164,7 +165,7 @@ public void SH001_TableNames(string name, int numberOfTables)
{
var sourceDir = new DirectoryInfo("../../../../TestFiles/");
var sourceXlsx = new FileInfo(Path.Combine(sourceDir.FullName, name));
using var sDoc = SpreadsheetDocument.Open(sourceXlsx.FullName, true);
using var sDoc = SpreadsheetDocument.Open(sourceXlsx.FullName, false);
var table = SmlDataRetriever.TableNames(sDoc);
Assert.Equal(numberOfTables, table.Length);
}
Expand Down
Loading

0 comments on commit 8e943a6

Please sign in to comment.