Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sergey-tihon committed Oct 6, 2024
1 parent 598f8b1 commit 18660ea
Show file tree
Hide file tree
Showing 5 changed files with 150 additions and 150 deletions.
2 changes: 1 addition & 1 deletion Clippit/PowerPoint/Fluent/FluentPresentationBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Presentation;
using Path = System.IO.Path;
using PBT = Clippit.PowerPoint.PresentationBuilderTools;
using PBT = Clippit.PowerPoint.Fluent.PresentationBuilderTools;

namespace Clippit.PowerPoint.Fluent;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
using DocumentFormat.OpenXml.Experimental;
using DocumentFormat.OpenXml.Packaging;

namespace Clippit.PowerPoint
namespace Clippit.PowerPoint.Fluent
{
internal static class PresentationBuilderTools
{
Expand Down Expand Up @@ -44,6 +44,15 @@ internal static string GetSlideTitle(XElement slide)
return paragraphText.ToString().Trim();
}

internal static List<string> GetSlideIdsInOrder(PresentationDocument srcDoc)
{
return srcDoc
.PresentationPart.GetXElement()
.Descendants(P.sldId)
.Select(x => x.Attribute(R.id)!.Value)
.ToList();
}

internal static readonly Dictionary<XName, int> OrderPresentation =
new()
{
Expand Down
2 changes: 1 addition & 1 deletion Clippit/PowerPoint/PmlDocument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Clippit.PowerPoint;

public partial class PmlDocument : OpenXmlPowerToolsDocument
public class PmlDocument : OpenXmlPowerToolsDocument
{
private const string NotPresentationExceptionMessage = "The document is not a PresentationML document.";

Expand Down
228 changes: 81 additions & 147 deletions Clippit/PowerPoint/PresentationBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,192 +5,126 @@
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using Clippit.Excel;
using Clippit.PowerPoint.Fluent;
using DocumentFormat.OpenXml.Packaging;
using PBT = Clippit.PowerPoint.Fluent.PresentationBuilderTools;

namespace Clippit.PowerPoint
namespace Clippit.PowerPoint;

public static partial class PresentationBuilder
{
public class SlideSource
public static IFluentPresentationBuilder Create(PresentationDocument document)
{
public PmlDocument PmlDocument { get; set; }
public int Start { get; set; }
public int Count { get; set; }
public bool KeepMaster { get; set; }

public SlideSource(PmlDocument source, bool keepMaster)
{
PmlDocument = source;
Start = 0;
Count = int.MaxValue;
KeepMaster = keepMaster;
}

public SlideSource(string fileName, bool keepMaster)
{
PmlDocument = new PmlDocument(fileName);
Start = 0;
Count = int.MaxValue;
KeepMaster = keepMaster;
}

public SlideSource(PmlDocument source, int start, bool keepMaster)
{
PmlDocument = source;
Start = start;
Count = int.MaxValue;
KeepMaster = keepMaster;
}

public SlideSource(string fileName, int start, bool keepMaster)
{
PmlDocument = new PmlDocument(fileName);
Start = start;
Count = int.MaxValue;
KeepMaster = keepMaster;
}
return new FluentPresentationBuilder(document);
}

public SlideSource(PmlDocument source, int start, int count, bool keepMaster)
public static PmlDocument BuildPresentation(List<SlideSource> sources)
{
using var streamDoc = OpenXmlMemoryStreamDocument.CreatePresentationDocument();
using (var output = streamDoc.GetPresentationDocument(new OpenSettings { AutoSave = false }))
{
PmlDocument = source;
Start = start;
Count = count;
KeepMaster = keepMaster;
BuildPresentation(sources, output);
output.PackageProperties.Modified = DateTime.Now;
}
return streamDoc.GetModifiedPmlDocument();
}

public SlideSource(string fileName, int start, int count, bool keepMaster)
{
PmlDocument = new PmlDocument(fileName);
Start = start;
Count = count;
KeepMaster = keepMaster;
}
public static IList<PmlDocument> PublishSlides(PmlDocument src)
{
using var streamSrcDoc = new OpenXmlMemoryStreamDocument(src);
using var srcDoc = streamSrcDoc.GetPresentationDocument(new OpenSettings { AutoSave = false });
return PublishSlides(srcDoc, src.FileName).ToList();
}

public static partial class PresentationBuilder
public static IEnumerable<PmlDocument> PublishSlides(PresentationDocument srcDoc, string fileName)
{
public static IFluentPresentationBuilder Create(PresentationDocument document)
var slidesIds = PBT.GetSlideIdsInOrder(srcDoc);
var slideNameRegex = SlideNameRegex();
for (var slideNumber = 0; slideNumber < slidesIds.Count; slideNumber++)
{
return new FluentPresentationBuilder(document);
}
var srcSlidePart = (SlidePart)srcDoc.PresentationPart.GetPartById(slidesIds[slideNumber]);

public static PmlDocument BuildPresentation(List<SlideSource> sources)
{
using var streamDoc = OpenXmlMemoryStreamDocument.CreatePresentationDocument();
using (var output = streamDoc.GetPresentationDocument(new OpenSettings { AutoSave = false }))
{
BuildPresentation(sources, output);
output.PackageProperties.Modified = DateTime.Now;
}
return streamDoc.GetModifiedPmlDocument();
}
ExtractSlide(srcSlidePart, output);

public static IList<PmlDocument> PublishSlides(PmlDocument src)
{
using var streamSrcDoc = new OpenXmlMemoryStreamDocument(src);
using var srcDoc = streamSrcDoc.GetPresentationDocument(new OpenSettings { AutoSave = false });
return PublishSlides(srcDoc, src.FileName).ToList();
}
// Set the title of the new presentation to the title of the slide
var title = PBT.GetSlideTitle(srcSlidePart.GetXElement());
output.PackageProperties.Title = title;
}

public static IEnumerable<PmlDocument> PublishSlides(PresentationDocument srcDoc, string fileName)
{
var slidesIds = GetSlidesInOrder(srcDoc);
var slideNameRegex = SlideNameRegex();
for (var slideNumber = 0; slideNumber < slidesIds.Count; slideNumber++)
var slideDoc = streamDoc.GetModifiedPmlDocument();
if (!string.IsNullOrWhiteSpace(fileName))
{
var srcSlidePart = (SlidePart)srcDoc.PresentationPart.GetPartById(slidesIds[slideNumber]);
slideDoc.FileName = slideNameRegex.Replace(fileName, $"_{slideNumber + 1:000}.pptx");
}

using var streamDoc = OpenXmlMemoryStreamDocument.CreatePresentationDocument();
using (var output = streamDoc.GetPresentationDocument(new OpenSettings { AutoSave = false }))
{
ExtractSlide(srcSlidePart, output);
}
yield return slideDoc;
}
}

var slideDoc = streamDoc.GetModifiedPmlDocument();
if (!string.IsNullOrWhiteSpace(fileName))
{
slideDoc.FileName = slideNameRegex.Replace(fileName, $"_{slideNumber + 1:000}.pptx");
}
private static void ExtractSlide(SlidePart slidePart, PresentationDocument output)
{
using var builder = new FluentPresentationBuilder(output);
try
{
var newSlidePart = builder.AddSlide(slidePart);

yield return slideDoc;
}
// Remove the show attribute from the slide element (if it exists)
var slideDocument = newSlidePart.GetXDocument();
slideDocument.Root?.Attribute(NoNamespace.show)?.Remove();
}

private static List<string> GetSlidesInOrder(PresentationDocument srcDoc)
catch (PresentationBuilderInternalException dbie)
{
return srcDoc
.PresentationPart.GetXElement()
.Descendants(P.sldId)
.Select(x => x.Attribute(R.id)!.Value)
.ToList();
if (dbie.Message.Contains("{0}"))
throw new PresentationBuilderException(string.Format(dbie.Message, slidePart.Uri));
throw;
}
}

private static void BuildPresentation(List<SlideSource> sources, PresentationDocument output)
{
using var builder = Create(output);

private static void ExtractSlide(SlidePart slidePart, PresentationDocument output)
var sourceNum = 0;
var openSettings = new OpenSettings { AutoSave = false };
foreach (var source in sources)
{
using var builder = new FluentPresentationBuilder(output);
using var streamDoc = new OpenXmlMemoryStreamDocument(source.PmlDocument);
using var doc = streamDoc.GetPresentationDocument(openSettings);
try
{
var newSlidePart = builder.AddSlide(slidePart);
if (source.KeepMaster)
{
foreach (var slideMasterPart in doc.PresentationPart.SlideMasterParts)
{
builder.AddSlideMaster(slideMasterPart);
}
}

// Remove the show attribute from the slide element (if it exists)
var slideDocument = newSlidePart.GetXDocument();
slideDocument.Root?.Attribute(NoNamespace.show)?.Remove();
var slideIds = PBT.GetSlideIdsInOrder(doc);
var (count, start) = (source.Count, source.Start);
while (count > 0 && start < slideIds.Count)
{
var slidePart = (SlidePart)doc.PresentationPart.GetPartById(slideIds[start]);
builder.AddSlide(slidePart);

// Set the title of the new presentation to the title of the slide
var title = PresentationBuilderTools.GetSlideTitle(newSlidePart.GetXElement());
output.PackageProperties.Title = title;
start++;
count--;
}
}
catch (PresentationBuilderInternalException dbie)
{
if (dbie.Message.Contains("{0}"))
throw new PresentationBuilderException(string.Format(dbie.Message, slidePart.Uri));
throw new PresentationBuilderException(string.Format(dbie.Message, sourceNum));
throw;
}
}

private static void BuildPresentation(List<SlideSource> sources, PresentationDocument output)
{
using var builder = Create(output);

var sourceNum = 0;
var openSettings = new OpenSettings { AutoSave = false };
foreach (var source in sources)
{
using var streamDoc = new OpenXmlMemoryStreamDocument(source.PmlDocument);
using var doc = streamDoc.GetPresentationDocument(openSettings);
try
{
if (source.KeepMaster)
{
foreach (var slideMasterPart in doc.PresentationPart.SlideMasterParts)
{
builder.AddSlideMaster(slideMasterPart);
}
}

var slideIds = GetSlidesInOrder(doc);
var (count, start) = (source.Count, source.Start);
while (count > 0 && start < slideIds.Count)
{
var slidePart = (SlidePart)doc.PresentationPart.GetPartById(slideIds[start]);
builder.AddSlide(slidePart);

start++;
count--;
}
}
catch (PresentationBuilderInternalException dbie)
{
if (dbie.Message.Contains("{0}"))
throw new PresentationBuilderException(string.Format(dbie.Message, sourceNum));
throw;
}

sourceNum++;
}
sourceNum++;
}

[GeneratedRegex(".pptx", RegexOptions.IgnoreCase, "en-US")]
private static partial Regex SlideNameRegex();
}

[GeneratedRegex(".pptx", RegexOptions.IgnoreCase, "en-US")]
private static partial Regex SlideNameRegex();
}
57 changes: 57 additions & 0 deletions Clippit/PowerPoint/SlideSource.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
namespace Clippit.PowerPoint;

public class SlideSource
{
public PmlDocument PmlDocument { get; set; }
public int Start { get; set; }
public int Count { get; set; }
public bool KeepMaster { get; set; }

public SlideSource(PmlDocument source, bool keepMaster)
{
PmlDocument = source;
Start = 0;
Count = int.MaxValue;
KeepMaster = keepMaster;
}

public SlideSource(string fileName, bool keepMaster)
{
PmlDocument = new PmlDocument(fileName);
Start = 0;
Count = int.MaxValue;
KeepMaster = keepMaster;
}

public SlideSource(PmlDocument source, int start, bool keepMaster)
{
PmlDocument = source;
Start = start;
Count = int.MaxValue;
KeepMaster = keepMaster;
}

public SlideSource(string fileName, int start, bool keepMaster)
{
PmlDocument = new PmlDocument(fileName);
Start = start;
Count = int.MaxValue;
KeepMaster = keepMaster;
}

public SlideSource(PmlDocument source, int start, int count, bool keepMaster)
{
PmlDocument = source;
Start = start;
Count = count;
KeepMaster = keepMaster;
}

public SlideSource(string fileName, int start, int count, bool keepMaster)
{
PmlDocument = new PmlDocument(fileName);
Start = start;
Count = count;
KeepMaster = keepMaster;
}
}

0 comments on commit 18660ea

Please sign in to comment.