aboutsummaryrefslogtreecommitdiff
path: root/Mammoth
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 /Mammoth
parentc70ed0148f584d5fa23f1c553cd618bc3368dd3c (diff)
Add support for image converters
Diffstat (limited to 'Mammoth')
-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
4 files changed, 44 insertions, 0 deletions
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" />