summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2017-05-10 20:11:39 +0100
committerMichael Williamson <mike@zwobble.org>2017-05-10 20:11:39 +0100
commitd46578f9c125e4700f0c8e1f4db3d896edb9cd92 (patch)
tree63fe2045daadce4d5d769173edc79f6efc52853c
parentc70ed0148f584d5fa23f1c553cd618bc3368dd3c (diff)
Add support for image converters
-rw-r--r--Mammoth.Tests/DocumentConverterTests.cs23
-rw-r--r--Mammoth/Couscous/Adapters.cs4
-rw-r--r--Mammoth/DocumentConverter.cs30
-rw-r--r--Mammoth/IImage.cs9
-rw-r--r--Mammoth/Mammoth.csproj1
5 files changed, 67 insertions, 0 deletions
diff --git a/Mammoth.Tests/DocumentConverterTests.cs b/Mammoth.Tests/DocumentConverterTests.cs
index 416893f..5469e08 100644
--- a/Mammoth.Tests/DocumentConverterTests.cs
+++ b/Mammoth.Tests/DocumentConverterTests.cs
@@ -2,6 +2,7 @@
using System.IO;
using Xunit.Sdk;
using System;
+using System.Collections.Generic;
using System.Linq;
namespace Mammoth.Tests {
@@ -97,6 +98,28 @@ namespace Mammoth.Tests {
Directory.Delete(tempDirectory, recursive: true);
}
}
+
+ [Fact]
+ public void ImageConversionCanBeCustomised() {
+ AssertSuccessfulConversion(
+ ConvertToHtml("tiny-picture.docx", mammoth => mammoth.ImageConverter(ConvertImage)),
+ "<p><img src=\"iV,image/png\" /></p>"
+ );
+ }
+
+ private IDictionary<string, string> ConvertImage(IImage image) {
+ using (var stream = image.GetStream()) {
+ var base64 = StreamToBase64(stream);
+ var src = base64.Substring(0, 2) + "," + image.ContentType;
+ return new Dictionary<string, string> { { "src", src } };
+ }
+ }
+
+ private static string StreamToBase64(System.IO.Stream stream) {
+ var memoryStream = new System.IO.MemoryStream();
+ stream.CopyTo(memoryStream);
+ return System.Convert.ToBase64String(memoryStream.ToArray());
+ }
[Fact]
public void ContentTypesAreRead() {
diff --git a/Mammoth/Couscous/Adapters.cs b/Mammoth/Couscous/Adapters.cs
index 85fe77a..20d5f4f 100644
--- a/Mammoth/Couscous/Adapters.cs
+++ b/Mammoth/Couscous/Adapters.cs
@@ -76,6 +76,10 @@ namespace Mammoth.Couscous {
}
}
+ internal static Map<TKey, TValue> DictionaryToMap<TKey, TValue>(IDictionary<TKey, TValue> dictionary) {
+ return new HashMap<TKey, TValue>(new Dictionary<TKey, TValue>(dictionary));
+ }
+
internal static Map<TKey, TValue> DictionaryToMap<TKey, TValue>(Dictionary<TKey, TValue> dictionary) {
return new HashMap<TKey, TValue>(dictionary);
}
diff --git a/Mammoth/DocumentConverter.cs b/Mammoth/DocumentConverter.cs
index 59b09e1..4995e54 100644
--- a/Mammoth/DocumentConverter.cs
+++ b/Mammoth/DocumentConverter.cs
@@ -1,3 +1,5 @@
+using System;
+using System.Collections.Generic;
using System.IO;
using Mammoth.Couscous;
using Mammoth.Couscous.org.zwobble.mammoth.@internal;
@@ -33,6 +35,34 @@ namespace Mammoth {
public DocumentConverter DisableEmbeddedStyleMap() {
return new DocumentConverter(options.disableEmbeddedStyleMap());
}
+
+ public DocumentConverter ImageConverter(Func<IImage, IDictionary<string, string>> imageConverter) {
+ return new DocumentConverter(options.imageConverter(new ImageConverterShim(imageConverter)));
+ }
+
+ internal class ImageConverterShim : Mammoth.Couscous.org.zwobble.mammoth.images.ImageConverter__ImgElement {
+ private readonly Func<IImage, IDictionary<string, string>> func;
+
+ internal ImageConverterShim(Func<IImage, IDictionary<string, string>> func) {
+ this.func = func;
+ }
+
+ public Mammoth.Couscous.java.util.Map<string, string> convert(Mammoth.Couscous.org.zwobble.mammoth.images.Image image) {
+ return ToJava.DictionaryToMap(func(new Image(image)));
+ }
+ }
+
+ internal class Image : IImage {
+ private readonly Mammoth.Couscous.org.zwobble.mammoth.images.Image image;
+
+ internal Image(Mammoth.Couscous.org.zwobble.mammoth.images.Image image) {
+ this.image = image;
+ }
+
+ public string AltText { get { return image.getAltText().orElse(null); } }
+ public string ContentType { get { return image.getContentType(); } }
+ public Stream GetStream() { return image.getInputStream().Stream; }
+ }
public IResult<string> ConvertToHtml(Stream stream) {
return new InternalDocumentConverter(options)
diff --git a/Mammoth/IImage.cs b/Mammoth/IImage.cs
new file mode 100644
index 0000000..0a52ee6
--- /dev/null
+++ b/Mammoth/IImage.cs
@@ -0,0 +1,9 @@
+using System.IO;
+
+namespace Mammoth {
+ public interface IImage {
+ string AltText { get; }
+ string ContentType { get; }
+ Stream GetStream();
+ }
+}
diff --git a/Mammoth/Mammoth.csproj b/Mammoth/Mammoth.csproj
index da7bd36..5d30d2a 100644
--- a/Mammoth/Mammoth.csproj
+++ b/Mammoth/Mammoth.csproj
@@ -36,6 +36,7 @@
<ItemGroup>
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="DocumentConverter.cs" />
+ <Compile Include="IImage.cs" />
<Compile Include="IResult.cs" />
<Compile Include="SimpleResult.cs" />
<Compile Include="generated.cs" />