Skip to content

Commit c8597ed

Browse files
nefariussergey-tihonCopilot
authored
Extends DocumentAssembler to also apply HTML formatting on content in table cells (#101)
* Extends #70 and #86 to also apply HTML formatting on content in table cells * fix: format * fix: remove code duplication * fix: small refactoring * Update Clippit/Word/DocumentAssembler.cs Co-authored-by: Copilot <[email protected]> * Revert "Update Clippit/Word/DocumentAssembler.cs" This reverts commit 4b8fac3. * fix: standartize error handling --------- Co-authored-by: Sergey Tihon <[email protected]> Co-authored-by: Copilot <[email protected]>
1 parent 61f2e8d commit c8597ed

File tree

2 files changed

+43
-6
lines changed

2 files changed

+43
-6
lines changed

Clippit/Word/Assembler/HtmlConverter.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,15 @@ ref OpenXmlPart part
5151
}
5252

5353
// otherwise split the values if there are new line characters
54+
return ConvertTextToRunsWithMarkupSupport(values, part, templateError);
55+
}
56+
57+
internal static List<XElement> ConvertTextToRunsWithMarkupSupport(
58+
string[] values,
59+
OpenXmlPart part,
60+
TemplateError templateError
61+
)
62+
{
5463
List<XElement> results = new List<XElement>();
5564
for (int i = 0; i < values.Length; i++)
5665
{

Clippit/Word/DocumentAssembler.cs

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1632,6 +1632,19 @@ OpenXmlPart part
16321632
}
16331633
}
16341634

1635+
if (paragraph is null)
1636+
{
1637+
var errorCell = new XElement(
1638+
W.tc,
1639+
tc.Elements().Where(z => z.Name != W.p),
1640+
ErrorHandler.CreateRunErrorMessage(
1641+
"Error: No paragraph found in table cell",
1642+
templateError
1643+
)
1644+
);
1645+
return errorCell;
1646+
}
1647+
16351648
var cellRun = paragraph.Elements(W.r).FirstOrDefault();
16361649
var xPath = paragraph.Value;
16371650
string[] newValues;
@@ -1654,15 +1667,30 @@ OpenXmlPart part
16541667
}
16551668

16561669
var pPr = paragraph.Element(W.pPr);
1657-
var rPr = cellRun != null ? cellRun.Element(W.rPr) : new XElement(W.rPr); //if the cell was empty there is no cellRun
1670+
var rPr = cellRun?.Element(W.rPr) ?? new XElement(W.rPr);
1671+
var runs = Assembler.HtmlConverter.ConvertTextToRunsWithMarkupSupport(
1672+
newValues,
1673+
part,
1674+
templateError
1675+
);
1676+
1677+
// Build paragraphs (you can keep all runs in one paragraph, or split on <w:br/> to multiple <w:p>)
1678+
var paragraphContent = new XElement(
1679+
W.p,
1680+
pPr,
1681+
runs.Select(r =>
1682+
{
1683+
// Inject formatting if not present
1684+
if (r.Name == W.r && r.Element(W.rPr) == null)
1685+
r.AddFirst(new XElement(rPr));
1686+
return r;
1687+
})
1688+
);
1689+
16581690
var newCell = new XElement(
16591691
W.tc,
16601692
tc.Elements().Where(z => z.Name != W.p),
1661-
newValues.Select(text => new XElement(
1662-
W.p,
1663-
pPr,
1664-
new XElement(W.r, rPr, new XElement(W.t, text))
1665-
))
1693+
paragraphContent
16661694
);
16671695
return newCell;
16681696
})

0 commit comments

Comments
 (0)