Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<PropertyGroup>
<Copyright>Copyright 2009-$([System.DateTime]::Today.Year) Olivier Nizet</Copyright>
<Nullable>enable</Nullable>
<LangVersion>latest</LangVersion>
<LangVersion>preview</LangVersion>
<ImplicitUsings>enable</ImplicitUsings>
<Version>4.2.0</Version>
</PropertyGroup>
Expand Down
64 changes: 37 additions & 27 deletions src/Html2OpenXml/Expressions/BlockElementExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ namespace HtmlToOpenXml.Expressions;
/// </summary>
class BlockElementExpression: PhrasingElementExpression
{
// 100% A4 page width expressed in Pct
internal const string FullPageWidthPct = "5000";
private readonly OpenXmlLeafElement[]? defaultStyleProperties;
protected readonly ParagraphProperties paraProperties = new();
protected TableProperties? tableProperties;
Expand Down Expand Up @@ -89,25 +91,8 @@ protected override IEnumerable<OpenXmlElement> Interpret (
ParsingContext context, IEnumerable<AngleSharp.Dom.INode> childNodes)
{
return ComposeChildren(context, childNodes, paraProperties,
(runs) => {
if (styleAttributes.RequiresPageBreakBefore())
{
runs.Add(
new Run(
new Break() { Type = BreakValues.Page })
);
runs.Add(new Run(
new LastRenderedPageBreak())
);
}
},
(runs) => {
if (styleAttributes.RequiresPageBreakAfter())
{
runs.Add(new Run(
new Break() { Type = BreakValues.Page }));
}
});
prefixRuns: CreatePageBreakBeforeRuns(),
suffiRuns: CreatePageBreakAfterRuns());
}

public override void CascadeStyles(OpenXmlElement element)
Expand Down Expand Up @@ -301,18 +286,19 @@ protected override void ComposeStyles (ParsingContext context)
/// <param name="context">The child parsing context.</param>
/// <param name="childNodes">The list of child nodes.</param>
/// <param name="paragraphProperties">The parent paragraph properties to apply.</param>
/// <param name="preAction">Optionally insert new runs at the beginning of the processing.</param>
/// <param name="postAction">Optionally insert new runs at the end of the processing.</param>
/// <param name="prefixRuns">Prepend runs at the beginning of the processing (optional).</param>
/// <param name="suffiRuns">Append runs at the end of the processing (optional).</param>
internal static IEnumerable<OpenXmlElement> ComposeChildren(ParsingContext context,
IEnumerable<AngleSharp.Dom.INode> childNodes,
ParagraphProperties paragraphProperties,
Action<IList<OpenXmlElement>>? preAction = null,
Action<IList<OpenXmlElement>>? postAction = null)
IEnumerable<OpenXmlElement>? prefixRuns = null,
IEnumerable<OpenXmlElement>? suffiRuns = null)
{
var runs = new List<OpenXmlElement>();
var flowElements = new List<OpenXmlElement>();

preAction?.Invoke(runs);
if (prefixRuns != null)
runs.AddRange(prefixRuns);

OpenXmlElement? previousElement = null;
foreach (var child in childNodes)
Expand Down Expand Up @@ -346,7 +332,8 @@ internal static IEnumerable<OpenXmlElement> ComposeChildren(ParsingContext conte
}
}

postAction?.Invoke(runs);
if (suffiRuns != null)
runs.AddRange(suffiRuns);

if (runs.Count > 0)
flowElements.Add(CreateParagraph(context, runs, paragraphProperties));
Expand Down Expand Up @@ -379,7 +366,6 @@ private static Paragraph CreateParagraph(ParsingContext context, IList<OpenXmlEl
return p;
}


/// <summary>
/// Group all the paragraph inside a framed table.
/// </summary>
Expand All @@ -393,7 +379,10 @@ protected Table CreateFrame(IEnumerable<OpenXmlElement> childElements)
TableProperties tableProperties;
Table framedTable = new(
tableProperties = new TableProperties {
TableWidth = new() { Type = TableWidthUnitValues.Pct, Width = "5000" } // 100%
TableWidth = new() {
Type = TableWidthUnitValues.Pct,
Width = FullPageWidthPct
}
},
new TableGrid(
new GridColumn() { Width = "9442" }),
Expand Down Expand Up @@ -442,4 +431,25 @@ protected static int IncrementBookmarkId(ParsingContext context)
context.Properties("bookmarkRef", bookmarkRef);
return bookmarkRef.Value;
}

private IEnumerable<OpenXmlElement> CreatePageBreakBeforeRuns()
{
if (!styleAttributes.RequiresPageBreakBefore())
yield break;

yield return new Run(
new Break() { Type = BreakValues.Page });

yield return new Run(
new LastRenderedPageBreak());
}

private IEnumerable<OpenXmlElement> CreatePageBreakAfterRuns()
{
if (!styleAttributes.RequiresPageBreakAfter())
yield break;

yield return new Run(
new Break() { Type = BreakValues.Page });
}
}
28 changes: 28 additions & 0 deletions src/Html2OpenXml/Expressions/Numbering/ListContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/* Copyright (C) Olivier Nizet https://github.com/onizet/html2openxml - All Rights Reserved
*
* This source is subject to the Microsoft Permissive License.
* Please see the License.txt file for more information.
* All other rights reserved.
*
* THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
* KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
* PARTICULAR PURPOSE.
*/
using AngleSharp.Dom;

namespace HtmlToOpenXml.Expressions;

/// <summary>
/// Represents the current numbering state while traversing nested HTML lists.
/// </summary>
readonly record struct ListContext(string Name, int AbsNumId, int InstanceId,
int Level, DirectionMode? Dir, long MarginLeftDxa)
{
public ListContext NextLevel(DirectionMode? dir, long extraMargin)
=> this with {
Level = Level+1,
Dir = dir ?? Dir,
MarginLeftDxa = MarginLeftDxa + extraMargin
};
}
Loading
Loading