-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathPlantUmlDocumentToImageConverter.cs
More file actions
110 lines (93 loc) · 3.34 KB
/
PlantUmlDocumentToImageConverter.cs
File metadata and controls
110 lines (93 loc) · 3.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
using System;
using System.IO;
using System.Threading.Tasks;
using Waher.Content.Images;
using Waher.Runtime.Inventory;
namespace Waher.Content.Markdown.PlantUml
{
/// <summary>
/// Converts PlantUML documents to images.
/// </summary>
public class PlantUmlDocumentToImageConverter : IContentConverter
{
/// <summary>
/// Converts PlantUML documents to images.
/// </summary>
public PlantUmlDocumentToImageConverter()
{
}
/// <summary>
/// Converts content from these content types.
/// </summary>
public string[] FromContentTypes => new string[] { PlantUmlCodec.DefaultContentType };
/// <summary>
/// Converts content to these content types.
/// </summary>
public virtual string[] ToContentTypes
{
get
{
return new string[]
{
ImageCodec.ContentTypePng,
ImageCodec.ContentTypeSvg
};
}
}
/// <summary>
/// How well the content is converted.
/// </summary>
public virtual Grade ConversionGrade => Grade.Excellent;
/// <summary>
/// Performs the actual conversion.
/// </summary>
/// <param name="State">State of the current conversion.</param>
/// <returns>If the result is dynamic (true), or only depends on the source (false).</returns>
public async Task<bool> ConvertAsync(ConversionState State)
{
string GraphDescription;
using (StreamReader rd = new StreamReader(State.From))
{
GraphDescription = rd.ReadToEnd();
}
string s = State.ToContentType;
int i;
i = s.IndexOf(';');
if (i > 0)
s = s.Substring(0, i);
bool Png = string.Compare(s, ImageCodec.ContentTypePng, true) == 0;
bool Svg = string.Compare(s, ImageCodec.ContentTypeSvg, true) == 0;
if (!(State.PossibleContentTypes is null))
{
foreach (string ContentType in State.PossibleContentTypes)
{
s = ContentType;
i = s.IndexOf(';');
if (i > 0)
s = s.Substring(0, i);
Png |= string.Compare(s, ImageCodec.ContentTypePng, true) == 0;
Svg |= string.Compare(s, ImageCodec.ContentTypeSvg, true) == 0;
}
}
GraphInfo Graph;
if (Svg)
{
Graph = await PlantUml.GetGraphInfo("uml", GraphDescription, ResultType.Svg, true);
State.ToContentType = ImageCodec.ContentTypeSvg;
}
else if (Png)
{
Graph = await PlantUml.GetGraphInfo("uml", GraphDescription, ResultType.Png, true);
State.ToContentType = ImageCodec.ContentTypePng;
}
else
{
State.Error = new Exception("Unable to convert document from " + State.FromContentType + " to " + State.ToContentType);
return false;
}
byte[] Data = await Runtime.IO.Files.ReadAllBytesAsync(Graph.ImageFileName);
await State.To.WriteAsync(Data, 0, Data.Length);
return false;
}
}
}