Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
GraphVizDocumentToImageConverter.cs
1using System;
2using System.IO;
3using System.Threading.Tasks;
4using Waher.Content;
7using Waher.Script;
8
10{
15 {
20 {
21 }
22
26 public string[] FromContentTypes => new string[] { GraphVizCodec.DefaultContentType };
27
31 public virtual string[] ToContentTypes
32 {
33 get
34 {
35 return new string[]
36 {
38 ImageCodec.ContentTypeSvg
39 };
40 }
41 }
42
46 public virtual Grade ConversionGrade => Grade.Excellent;
47
53 public async Task<bool> ConvertAsync(ConversionState State)
54 {
55 string GraphDescription;
56
57 using (StreamReader rd = new StreamReader(State.From))
58 {
59 GraphDescription = rd.ReadToEnd();
60 }
61
62 string s = State.ToContentType;
63 int i;
64
65 i = s.IndexOf(';');
66 if (i > 0)
67 s = s.Substring(0, i);
68
69 bool Png = string.Compare(s, ImageCodec.ContentTypePng, true) == 0;
70 bool Svg = string.Compare(s, ImageCodec.ContentTypeSvg, true) == 0;
71
72 if (!(State.PossibleContentTypes is null))
73 {
74 foreach (string ContentType in State.PossibleContentTypes)
75 {
76 s = ContentType;
77 i = s.IndexOf(';');
78 if (i > 0)
79 s = s.Substring(0, i);
80
81 Png |= string.Compare(s, ImageCodec.ContentTypePng, true) == 0;
82 Svg |= string.Compare(s, ImageCodec.ContentTypeSvg, true) == 0;
83 }
84 }
85
86 string Language;
87
88 if (string.IsNullOrEmpty(State.FromFileName))
89 Language = "dot";
90 else
91 {
92 Language = Path.GetExtension(State.FromFileName).ToLower();
93 if (Language.StartsWith("."))
94 Language = Language.Substring(1);
95
96 switch (Language)
97 {
98 case "dot":
99 case "neato":
100 case "fdp":
101 case "sfdp":
102 case "twopi":
103 case "circo":
104 break;
105
106 default:
107 Language = "dot";
108 break;
109 }
110 }
111
113 GraphInfo Graph;
114
115 if (Svg)
116 {
117 Graph = await GraphViz.GetFileName(Language, GraphDescription, ResultType.Svg, true, Variables);
118 State.ToContentType = ImageCodec.ContentTypeSvg;
119 }
120 else if (Png)
121 {
122 Graph = await GraphViz.GetFileName(Language, GraphDescription, ResultType.Png, true, Variables);
123 State.ToContentType = ImageCodec.ContentTypePng;
124 }
125 else
126 throw new Exception("Unable to convert document from " + State.FromContentType + " to " + State.ToContentType);
127
128 byte[] Data = await Resources.ReadAllBytesAsync(Graph.FileName);
129
130 await State.To.WriteAsync(Data, 0, Data.Length);
131
132 return false;
133 }
134
135 }
136}
Contains the state of a content conversion process.
string ToContentType
Content type of the content to convert to. This value might be changed, in case the converter finds a...
string[] PossibleContentTypes
Possible content types the converter is allowed to convert to. Can be null if there are no alternativ...
string FromContentType
Content type of the content to convert from.
Stream To
Stream pointing to where binary representation of content is to be sent.
string FromFileName
If the content is coming from a file, this parameter contains the name of that file....
Stream From
Stream pointing to binary representation of content.
Image encoder/decoder.
Definition: ImageCodec.cs:14
const string ContentTypePng
image/png
Definition: ImageCodec.cs:30
const string ContentTypeSvg
image/svg+xml
Definition: ImageCodec.cs:45
virtual string[] ToContentTypes
Converts content to these content types.
async Task< bool > ConvertAsync(ConversionState State)
Performs the actual conversion.
Class managing GraphViz integration into Markdown documents.
Definition: GraphViz.cs:47
Static class managing loading of resources stored as embedded resources or in content files.
Definition: Resources.cs:15
static async Task< byte[]> ReadAllBytesAsync(string FileName)
Reads a binary file asynchronously.
Definition: Resources.cs:183
Collection of variables.
Definition: Variables.cs:25
Basic interface for Internet Content encoders. A class implementing this interface and having a defau...
Grade
Grade enumeration
Definition: Grade.cs:7