-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathGraphViz.cs
More file actions
1033 lines (878 loc) · 29.8 KB
/
GraphViz.cs
File metadata and controls
1033 lines (878 loc) · 29.8 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Xml;
using SkiaSharp;
using Waher.Content.Images;
using Waher.Content.Markdown.Contracts;
using Waher.Content.Markdown.Latex;
using Waher.Content.Markdown.Model;
using Waher.Content.Markdown.Model.CodeContent;
using Waher.Content.Markdown.Rendering;
using Waher.Content.Markdown.Wpf;
using Waher.Content.Markdown.Xamarin;
using Waher.Content.SystemFiles;
using Waher.Content.Xml;
using Waher.Events;
using Waher.Runtime.Inventory;
using Waher.Runtime.IO;
using Waher.Runtime.Queue;
using Waher.Runtime.Timing;
using Waher.Script;
using Waher.Script.Graphs;
using Waher.Security;
namespace Waher.Content.Markdown.GraphViz
{
/// <summary>
/// Image types supported by GraphViz
/// </summary>
public enum ResultType
{
/// <summary>
/// SVG
/// </summary>
Svg,
/// <summary>
/// PNG
/// </summary>
Png
}
internal class GraphInfo
{
public string FileName;
public string TextFileName;
public string Title;
public string MapFileName;
public string Hash;
}
/// <summary>
/// Class managing GraphViz integration into Markdown documents.
/// </summary>
public class GraphViz : IImageCodeContent, ICodeContentMarkdownRenderer, ICodeContentHtmlRenderer, ICodeContentTextRenderer,
ICodeContentContractsRenderer, ICodeContentLatexRenderer, ICodeContentWpfXamlRenderer, ICodeContentXamarinFormsXamlRenderer
{
private const int chartGenerationTimeout = 30000;
private static readonly AsyncProcessor<ChartRecord> queue = new AsyncProcessor<ChartRecord>(1, "GraphViz processor");
private static readonly Random rnd = new Random();
private static Scheduler scheduler = null;
private static string installationFolder = null;
private static string binFolder = null;
private static string graphVizFolder = null;
private static string contentRootFolder = null;
private static bool supportsDot = false;
private static bool supportsNeato = false;
private static bool supportsFdp = false;
private static bool supportsSfdp = false;
private static bool supportsTwopi = false;
private static bool supportsCirco = false;
private static IMarkdownAsynchronousOutput asyncHtmlOutput = null;
/// <summary>
/// Class managing GraphViz integration into Markdown documents.
/// </summary>
public GraphViz()
{
}
/// <summary>
/// Initializes the GraphViz-Markdown integration.
/// </summary>
/// <param name="ContentRootFolder">Content root folder. If hosting markdown under a web server, this would correspond
/// to the roof folder for the web content.</param>
public static void Init(string ContentRootFolder)
{
try
{
contentRootFolder = ContentRootFolder;
if (scheduler is null)
{
if (Types.TryGetModuleParameter("Scheduler", out Scheduler Scheduler))
scheduler = Scheduler;
else
{
scheduler = new Scheduler();
Log.Terminating += (Sender, e) =>
{
scheduler?.Dispose();
scheduler = null;
return Task.CompletedTask;
};
}
}
string Folder = SearchForInstallationFolder();
if (string.IsNullOrEmpty(Folder))
Log.Warning("GraphViz not found. GraphViz support will not be available in Markdown.");
else
{
SetInstallationFolder(Folder);
Log.Informational("GraphViz found. Integration with Markdown added.",
new KeyValuePair<string, object>("Installation Folder", installationFolder),
new KeyValuePair<string, object>("Binary Folder", binFolder),
new KeyValuePair<string, object>("dot", supportsDot),
new KeyValuePair<string, object>("neato", supportsNeato),
new KeyValuePair<string, object>("fdp", supportsFdp),
new KeyValuePair<string, object>("sfdp", supportsSfdp),
new KeyValuePair<string, object>("twopi", supportsTwopi),
new KeyValuePair<string, object>("circo", supportsCirco));
asyncHtmlOutput = Types.FindBest<IMarkdownAsynchronousOutput, MarkdownOutputType>(MarkdownOutputType.Html);
}
}
catch (Exception ex)
{
Log.Exception(ex);
}
}
/// <summary>
/// Terminates GraphViz processing.
/// </summary>
/// <returns></returns>
public static async Task Terminate()
{
try
{
await queue.CloseForTermination(true, chartGenerationTimeout);
await queue.DisposeAsync();
}
catch (Exception ex)
{
Log.Exception(ex);
}
}
/// <summary>
/// Sets the installation folder of GraphViz.
/// </summary>
/// <param name="Folder">Installation folder.</param>
/// <exception cref="Exception">If trying to set the installation folder to a different folder than the one set previously.
/// The folder can only be set once, for security reasons.</exception>
public static void SetInstallationFolder(string Folder)
{
if (!string.IsNullOrEmpty(installationFolder) && Folder != installationFolder)
throw new Exception("GraphViz installation folder has already been set.");
string Suffix = FileSystem.ExecutableExtension;
installationFolder = Folder;
switch (Environment.OSVersion.Platform)
{
case PlatformID.Win32S:
case PlatformID.Win32Windows:
case PlatformID.Win32NT:
case PlatformID.WinCE:
default:
binFolder = Path.Combine(installationFolder, "bin");
break;
case PlatformID.Unix:
case PlatformID.MacOSX:
binFolder = installationFolder;
break;
}
supportsDot = File.Exists(Path.Combine(binFolder, "dot" + Suffix));
supportsNeato = File.Exists(Path.Combine(binFolder, "neato" + Suffix));
supportsFdp = File.Exists(Path.Combine(binFolder, "fdp" + Suffix));
supportsSfdp = File.Exists(Path.Combine(binFolder, "sfdp" + Suffix));
supportsTwopi = File.Exists(Path.Combine(binFolder, "twopi" + Suffix));
supportsCirco = File.Exists(Path.Combine(binFolder, "circo" + Suffix));
graphVizFolder = Path.Combine(contentRootFolder, "GraphViz");
if (!Directory.Exists(graphVizFolder))
Directory.CreateDirectory(graphVizFolder);
DeleteOldFiles(TimeSpan.FromDays(7));
}
private static void DeleteOldFiles(object P)
{
if (P is TimeSpan MaxAge)
DeleteOldFiles(MaxAge, true);
}
/// <summary>
/// Deletes generated files older than <paramref name="MaxAge"/>.
/// </summary>
/// <param name="MaxAge">Age limit.</param>
/// <param name="Reschedule">If rescheduling should be done.</param>
public static void DeleteOldFiles(TimeSpan MaxAge, bool Reschedule)
{
if (string.IsNullOrEmpty(graphVizFolder))
return;
DateTime Limit = DateTime.Now - MaxAge;
int Count = 0;
DirectoryInfo GraphVizFolder = new DirectoryInfo(graphVizFolder);
FileInfo[] Files = GraphVizFolder.GetFiles("*.*");
foreach (FileInfo FileInfo in Files)
{
if (FileInfo.LastAccessTime < Limit)
{
try
{
File.Delete(FileInfo.FullName);
Count++;
}
catch (Exception ex)
{
Log.Error("Unable to delete old file: " + ex.Message, FileInfo.FullName);
}
}
}
if (Count > 0)
Log.Informational(Count.ToString() + " old file(s) deleted.", graphVizFolder);
if (Reschedule)
{
lock (rnd)
{
scheduler.Add(DateTime.Now.AddDays(rnd.NextDouble() * 2), DeleteOldFiles, MaxAge);
}
}
}
/// <summary>
/// Searches for the installation folder on the local machine.
/// </summary>
/// <returns>Installation folder, if found, null otherwise.</returns>
public static string SearchForInstallationFolder()
{
string InstallationFolder;
switch (Environment.OSVersion.Platform)
{
case PlatformID.Win32S:
case PlatformID.Win32Windows:
case PlatformID.Win32NT:
case PlatformID.WinCE:
default:
InstallationFolder = SearchForInstallationFolder(Environment.SpecialFolder.ProgramFilesX86);
if (string.IsNullOrEmpty(InstallationFolder))
{
InstallationFolder = SearchForInstallationFolder(Environment.SpecialFolder.ProgramFiles);
if (string.IsNullOrEmpty(InstallationFolder))
{
InstallationFolder = SearchForInstallationFolder(Environment.SpecialFolder.Programs);
if (string.IsNullOrEmpty(InstallationFolder))
{
InstallationFolder = SearchForInstallationFolder(Environment.SpecialFolder.CommonProgramFilesX86);
if (string.IsNullOrEmpty(InstallationFolder))
{
InstallationFolder = SearchForInstallationFolder(Environment.SpecialFolder.CommonProgramFiles);
if (string.IsNullOrEmpty(InstallationFolder))
InstallationFolder = SearchForInstallationFolder(Environment.SpecialFolder.CommonPrograms);
}
}
}
}
break;
case PlatformID.Unix:
case PlatformID.MacOSX:
InstallationFolder = "/opt/local/bin";
if (!Directory.Exists(InstallationFolder))
InstallationFolder = null;
break;
}
return InstallationFolder;
}
private static string SearchForInstallationFolder(Environment.SpecialFolder SpecialFolder)
{
string Folder;
try
{
Folder = Environment.GetFolderPath(SpecialFolder);
}
catch (Exception)
{
return null; // Folder not defined for the operating system.
}
string Result = SearchForInstallationFolder(Folder);
if (string.IsNullOrEmpty(Result) && Types.TryGetModuleParameter("Runtime", out string RuntimeFolder))
Result = SearchForInstallationFolder(Path.Combine(RuntimeFolder, SpecialFolder.ToString()));
return Result;
}
private static string SearchForInstallationFolder(string Folder)
{
if (string.IsNullOrEmpty(Folder))
return null;
if (!Directory.Exists(Folder))
return null;
string FolderName;
string BestFolder = null;
double BestVersion = 0;
string[] SubFolders;
try
{
SubFolders = Directory.GetDirectories(Folder);
}
catch (UnauthorizedAccessException)
{
return null;
}
catch (Exception ex)
{
Log.Exception(ex);
return null;
}
foreach (string SubFolder in SubFolders)
{
FolderName = Path.GetFileName(SubFolder);
if (!FolderName.StartsWith("Graphviz", StringComparison.CurrentCultureIgnoreCase))
continue;
if (!CommonTypes.TryParse(FolderName.Substring(8), out double Version))
Version = 1.0;
if (BestFolder is null || Version > BestVersion)
{
BestFolder = SubFolder;
BestVersion = Version;
}
}
return BestFolder;
}
/// <summary>
/// Checks how well the handler supports multimedia content of a given type.
/// </summary>
/// <param name="Language">Language.</param>
/// <returns>How well the handler supports the content.</returns>
public Grade Supports(string Language)
{
int i = Language.IndexOf(':');
if (i > 0)
Language = Language.Substring(0, i).TrimEnd();
switch (Language.ToLower())
{
case "dot":
if (supportsDot)
return Grade.Excellent;
break;
case "neato":
if (supportsNeato)
return Grade.Excellent;
break;
case "fdp":
if (supportsFdp)
return Grade.Excellent;
break;
case "sfdp":
if (supportsSfdp)
return Grade.Excellent;
break;
case "twopi":
if (supportsTwopi)
return Grade.Excellent;
break;
case "circo":
if (supportsCirco)
return Grade.Excellent;
break;
}
return Grade.NotAtAll;
}
/// <summary>
/// If script is evaluated for this type of code block.
/// </summary>
public bool EvaluatesScript => false;
/// <summary>
/// Is called on the object when an instance of the element has been created in a document.
/// </summary>
/// <param name="Document">Document containing the instance.</param>
public void Register(MarkdownDocument Document)
{
// Do nothing.
}
/// <summary>
/// Generates HTML for the code content.
/// </summary>
/// <param name="Renderer">Renderer.</param>
/// <param name="Rows">Code rows.</param>
/// <param name="Language">Language.</param>
/// <param name="Indent">Code block indentation.</param>
/// <param name="Document">Markdown document containing element.</param>
/// <returns>If renderer was able to generate output.</returns>
public async Task<bool> RenderHtml(HtmlRenderer Renderer, string[] Rows, string Language, int Indent, MarkdownDocument Document)
{
GraphInfo Info = await GetFileName(Language, Rows, ResultType.Svg, asyncHtmlOutput is null, Document.Settings?.Variables);
if (!(Info is null))
{
await this.GenerateHTML(Renderer.Output, Info);
return true;
}
string Title;
int i = Language.IndexOf(':');
if (i > 0)
Title = Language.Substring(i + 1).Trim();
else
Title = null;
string Id = await asyncHtmlOutput.GenerateStub(MarkdownOutputType.Html, Renderer.Output, Title, Document);
Document.QueueAsyncTask(this.ExecuteGraphViz, new AsyncState()
{
Id = Id,
Language = Language,
Rows = Rows,
Document = Document
});
return true;
}
private class AsyncState
{
public string Id;
public string Language;
public string[] Rows;
public MarkdownDocument Document;
}
private async Task ExecuteGraphViz(object State)
{
AsyncState AsyncState = (AsyncState)State;
StringBuilder Output = new StringBuilder();
try
{
GraphInfo Info = await GetFileName(AsyncState.Language, AsyncState.Rows, ResultType.Svg, true,
AsyncState.Document.Settings?.Variables);
if (!(Info is null))
await this.GenerateHTML(Output, Info);
}
catch (Exception ex)
{
using (HtmlRenderer Renderer = new HtmlRenderer(Output, new HtmlSettings()
{
XmlEntitiesOnly = true
}))
{
await Renderer.RenderObject(ex, true, new Variables());
}
}
await asyncHtmlOutput.ReportResult(MarkdownOutputType.Html, AsyncState.Id, Output.ToString());
}
private async Task GenerateHTML(StringBuilder Output, GraphInfo Info)
{
Info.FileName = Info.FileName.Substring(contentRootFolder.Length).Replace(Path.DirectorySeparatorChar, '/');
if (!Info.FileName.StartsWith("/"))
Info.FileName = "/" + Info.FileName;
Output.Append("<figure>");
Output.Append("<img src=\"");
Output.Append(XML.HtmlAttributeEncode(Info.FileName));
if (!string.IsNullOrEmpty(Info.Title))
{
Output.Append("\" alt=\"");
Output.Append(XML.HtmlAttributeEncode(Info.Title));
Output.Append("\" title=\"");
Output.Append(XML.HtmlAttributeEncode(Info.Title));
}
else
Output.Append("\" alt=\"GraphViz graph");
if (!string.IsNullOrEmpty(Info.MapFileName))
{
Output.Append("\" usemap=\"#Map");
Output.Append(Info.Hash);
}
Output.Append("\" class=\"aloneUnsized\"/>");
if (!string.IsNullOrEmpty(Info.Title))
{
Output.Append("<figcaption>");
Output.Append(XML.HtmlValueEncode(Info.Title));
Output.Append("</figcaption>");
}
Output.AppendLine("</figure>");
if (!string.IsNullOrEmpty(Info.MapFileName))
{
Output.Append("<map id=\"Map");
Output.Append(Info.Hash);
Output.Append("\" name=\"Map");
Output.Append(Info.Hash);
Output.AppendLine("\">");
string Map = await Files.ReadAllTextAsync(Info.MapFileName);
string[] MapRows = Map.Split(CommonTypes.CRLF, StringSplitOptions.RemoveEmptyEntries);
int i, c;
for (i = 1, c = MapRows.Length - 1; i < c; i++)
Output.AppendLine(MapRows[i]);
Output.AppendLine("</map>");
}
}
internal static Task<GraphInfo> GetFileName(string Language, string[] Rows, ResultType Type, bool GenerateIfNotExists, Variables Variables)
{
return GetFileName(Language, MarkdownDocument.AppendRows(Rows), Type, GenerateIfNotExists, Variables);
}
internal static async Task<GraphInfo> GetFileName(string Language, string GraphText, ResultType Type, bool GenerateIfNotExists, Variables Variables)
{
GraphInfo Result = new GraphInfo();
int i = Language.IndexOf(':');
if (i > 0)
{
Result.Title = Language.Substring(i + 1).Trim();
Language = Language.Substring(0, i).TrimEnd();
}
else
Result.Title = string.Empty;
string GraphBgColor = GetColor(Graph.GraphBgColorVariableName, Variables);
string GraphFgColor = GetColor(Graph.GraphFgColorVariableName, Variables);
Result.Hash = Hashes.ComputeSHA256HashString(Encoding.UTF8.GetBytes(GraphText + Language + GraphBgColor + GraphFgColor));
string GraphVizFolder = Path.Combine(contentRootFolder, "GraphViz");
string FileName = Path.Combine(GraphVizFolder, Result.Hash);
switch (Type)
{
case ResultType.Svg:
default:
Result.FileName = FileName + "." + ImageCodec.FileExtensionSvg;
break;
case ResultType.Png:
Result.FileName = FileName + "." + ImageCodec.FileExtensionPng;
break;
}
Result.TextFileName = FileName + ".txt";
Result.MapFileName = FileName + ".map";
if (File.Exists(Result.FileName))
{
FileInfo Info = new FileInfo(Result.FileName);
if (Info.Length > 0)
{
if (!File.Exists(Result.MapFileName))
Result.MapFileName = null;
return Result;
}
}
if (!GenerateIfNotExists)
return null;
ChartRecord Rec = new ChartRecord(Result, Language, GraphText, GraphFgColor, GraphBgColor, Type);
queue.Queue(Rec);
if (!await Rec.Wait())
throw new Exception("Unable to process GraphViz chart.");
return Result;
}
private class ChartRecord : IWorkItem
{
private readonly GraphInfo info;
private readonly TaskCompletionSource<bool> result;
private readonly string language;
private readonly string graphText;
private readonly string graphFgColor;
private readonly string graphBgColor;
private readonly ResultType type;
public ChartRecord(GraphInfo Result, string Language, string GraphText,
string GraphFgColor, string GraphBgColor, ResultType Type)
{
this.result = new TaskCompletionSource<bool>();
this.info = Result;
this.language = Language;
this.graphText = GraphText;
this.graphFgColor = GraphFgColor;
this.graphBgColor = GraphBgColor;
this.type = Type;
}
/// <summary>
/// Executes the operation.
/// </summary>
public Task Execute()
{
return this.Execute(CancellationToken.None);
}
/// <summary>
/// Executes the operation.
/// </summary>
/// <param name="Cancel">Cancellation token.</param>
public async Task Execute(CancellationToken Cancel)
{
await Files.WriteAllTextAsync(this.info.TextFileName, this.graphText, Encoding.Default); // Use UTF-8 ?
StringBuilder Arguments = new StringBuilder();
Arguments.Append("-Tcmapx -o\"");
Arguments.Append(this.info.MapFileName);
Arguments.Append("\" -T");
Arguments.Append(this.type.ToString().ToLower());
if (!string.IsNullOrEmpty(this.graphBgColor))
{
Arguments.Append(" -Gbgcolor=\"");
Arguments.Append(this.graphBgColor);
Arguments.Append('"');
}
if (!string.IsNullOrEmpty(this.graphFgColor))
{
Arguments.Append(" -Gcolor=\"");
Arguments.Append(this.graphFgColor);
//Arguments.Append("\" -Nfillcolor=\"");
//Arguments.Append(defaultFgColor);
Arguments.Append("\" -Nfontcolor=\"");
Arguments.Append(this.graphFgColor);
Arguments.Append("\" -Nlabelfontcolor=\"");
Arguments.Append(this.graphFgColor);
Arguments.Append("\" -Npencolor=\"");
Arguments.Append(this.graphFgColor);
Arguments.Append("\" -Efontcolor=\"");
Arguments.Append(this.graphFgColor);
Arguments.Append("\" -Elabelfontcolor=\"");
Arguments.Append(this.graphFgColor);
Arguments.Append("\" -Epencolor=\"");
Arguments.Append(this.graphFgColor);
Arguments.Append("\"");
}
Arguments.Append(" -q -o\"");
Arguments.Append(this.info.FileName);
Arguments.Append("\" \"");
Arguments.Append(this.info.TextFileName + "\"");
ProcessStartInfo ProcessInformation = new ProcessStartInfo()
{
FileName = Path.Combine(binFolder, this.language.ToLower() + FileSystem.ExecutableExtension),
Arguments = Arguments.ToString(),
UseShellExecute = false,
RedirectStandardError = true,
RedirectStandardOutput = true,
RedirectStandardInput = false,
WorkingDirectory = graphVizFolder,
CreateNoWindow = true,
WindowStyle = ProcessWindowStyle.Hidden
};
Process P = new Process();
TaskCompletionSource<int> ExitSource = new TaskCompletionSource<int>();
P.Exited += (Sender, e) =>
{
ExitSource.TrySetResult(P.ExitCode);
};
Task _ = Task.Delay(chartGenerationTimeout).ContinueWith(Prev =>
{
try
{
P.Kill();
if (File.Exists(this.info.FileName))
File.Delete(this.info.FileName);
if (!string.IsNullOrEmpty(this.info.MapFileName) &&
File.Exists(this.info.MapFileName))
{
File.Delete(this.info.MapFileName);
}
}
catch (Exception ex)
{
Log.Exception(ex);
}
finally
{
ExitSource.TrySetException(new TimeoutException("GraphViz process did not terminate properly."));
}
return Task.CompletedTask;
});
P.StartInfo = ProcessInformation;
P.EnableRaisingEvents = true;
P.Start();
int ExitCode = await ExitSource.Task;
if (ExitCode != 0)
{
string Error = P.StandardError.ReadToEnd();
this.result.TrySetException(new Exception(Error));
}
try
{
if (File.Exists(this.info.MapFileName))
{
string Map = await Files.ReadAllTextAsync(this.info.MapFileName);
string[] MapRows = Map.Split(CommonTypes.CRLF, StringSplitOptions.RemoveEmptyEntries);
if (MapRows.Length <= 2)
{
File.Delete(this.info.MapFileName);
this.info.MapFileName = null;
}
}
else
this.info.MapFileName = null;
}
catch (Exception ex)
{
Log.Exception(ex);
}
}
/// <summary>
/// Flags the item as processed.
/// </summary>
/// <returns>If item was processed successfully.</returns>
public void Processed(bool Result)
{
this.result.TrySetResult(Result);
}
/// <summary>
/// Waits for the item to be processed.
/// </summary>
/// <returns>If item was processed successfully.</returns>
public Task<bool> Wait()
{
return this.result.Task;
}
/// <summary>
/// Waits for the item to be processed.
/// </summary>
/// <param name="Cancel">Cancellation token.</param>
/// <returns>If item was processed successfully.</returns>
public Task<bool> Wait(CancellationToken Cancel)
{
if (Cancel.CanBeCanceled)
Cancel.Register(() => this.result.TrySetException(new OperationCanceledException(Cancel)));
return this.result.Task;
}
}
private static string GetColor(string VariableName, Variables Variables)
{
if (Variables is null)
return null;
if (!Variables.TryGetVariable(VariableName, out Variable v))
return null;
if (v.ValueObject is SKColor Color)
return Graph.ToRGBAStyle(Color);
else if (v.ValueObject is string s)
return s;
else
return null;
}
/// <summary>
/// Generates plain text for the code content.
/// </summary>
/// <param name="Renderer">Renderer.</param>
/// <param name="Rows">Code rows.</param>
/// <param name="Language">Language.</param>
/// <param name="Indent">Code block indentation.</param>
/// <param name="Document">Markdown document containing element.</param>
/// <returns>If renderer was able to generate output.</returns>
public async Task<bool> RenderText(TextRenderer Renderer, string[] Rows, string Language, int Indent, MarkdownDocument Document)
{
GraphInfo Info = await GetFileName(Language, Rows, ResultType.Svg, true, Document.Settings?.Variables);
if (Info is null)
return false;
Renderer.Output.AppendLine(Info.Title);
return true;
}
/// <summary>
/// Generates Markdown for the code content.
/// </summary>
/// <param name="Renderer">Renderer.</param>
/// <param name="Rows">Code rows.</param>
/// <param name="Language">Language.</param>
/// <param name="Indent">Code block indentation.</param>
/// <param name="Document">Markdown document containing element.</param>
/// <returns>If renderer was able to generate output.</returns>
public async Task<bool> RenderMarkdown(MarkdownRenderer Renderer, string[] Rows, string Language, int Indent, MarkdownDocument Document)
{
GraphInfo Info = await GetFileName(Language, Rows, ResultType.Png, true, Document.Settings?.Variables);
if (Info is null)
return false;
return await ImageContent.GenerateMarkdownFromFile(Renderer.Output, Info.FileName, Info.Title);
}
/// <summary>
/// Generates WPF XAML for the code content.
/// </summary>
/// <param name="Renderer">Renderer.</param>
/// <param name="Rows">Code rows.</param>
/// <param name="Language">Language.</param>
/// <param name="Indent">Code block indentation.</param>
/// <param name="Document">Markdown document containing element.</param>
/// <returns>If renderer was able to generate output.</returns>
public async Task<bool> RenderWpfXaml(WpfXamlRenderer Renderer, string[] Rows, string Language, int Indent, MarkdownDocument Document)
{
GraphInfo Info = await GetFileName(Language, Rows, ResultType.Png, true, Document.Settings?.Variables);
if (Info is null)
return false;
XmlWriter Output = Renderer.XmlOutput;
Output.WriteStartElement("Image");
Output.WriteAttributeString("Source", Info.FileName);
Output.WriteAttributeString("Stretch", "None");
if (!string.IsNullOrEmpty(Info.Title))
Output.WriteAttributeString("ToolTip", Info.Title);
Output.WriteEndElement();
return true;
}
/// <summary>
/// Generates Xamarin.Forms XAML for the code content.
/// </summary>
/// <param name="Renderer">Renderer.</param>
/// <param name="Rows">Code rows.</param>
/// <param name="Language">Language.</param>
/// <param name="Indent">Code block indentation.</param>
/// <param name="Document">Markdown document containing element.</param>
/// <returns>If renderer was able to generate output.</returns>
public async Task<bool> RenderXamarinFormsXaml(XamarinFormsXamlRenderer Renderer, string[] Rows, string Language, int Indent, MarkdownDocument Document)
{
GraphInfo Info = await GetFileName(Language, Rows, ResultType.Png, true, Document.Settings?.Variables);
if (Info is null)
return false;
XmlWriter Output = Renderer.XmlOutput;
Output.WriteStartElement("Image");
Output.WriteAttributeString("Source", Info.FileName);
Output.WriteEndElement();
return true;
}
/// <summary>
/// Generates LaTeX for the code content.
/// </summary>
/// <param name="Renderer">Renderer.</param>
/// <param name="Rows">Code rows.</param>
/// <param name="Language">Language.</param>
/// <param name="Indent">Code block indentation.</param>
/// <param name="Document">Markdown document containing element.</param>
/// <returns>If renderer was able to generate output.</returns>
public async Task<bool> RenderLatex(LatexRenderer Renderer, string[] Rows, string Language, int Indent, MarkdownDocument Document)
{
GraphInfo Info = await GetFileName(Language, Rows, ResultType.Png, true, Document.Settings?.Variables);
StringBuilder Output = Renderer.Output;
Output.AppendLine("\\begin{figure}[h]");
Output.AppendLine("\\centering");
Output.Append("\\fbox{\\includegraphics{");
Output.Append(Info.FileName.Replace('\\', '/'));
Output.AppendLine("}}");
if (!string.IsNullOrEmpty(Info.Title))
{
Output.Append("\\caption{");
Output.Append(LatexRenderer.EscapeLaTeX(Info.Title));
Output.AppendLine("}");
}
Output.AppendLine("\\end{figure}");
Output.AppendLine();
return true;
}
/// <summary>
/// Generates an image of the contents.
/// </summary>
/// <param name="Rows">Code rows.</param>
/// <param name="Language">Language used.</param>
/// <param name="Document">Markdown document containing element.</param>
/// <returns>Image, if successful, null otherwise.</returns>
public async Task<PixelInformation> GenerateImage(string[] Rows, string Language, MarkdownDocument Document)
{
GraphInfo Info = await GetFileName(Language, Rows, ResultType.Png, true, Document.Settings?.Variables);
if (Info is null)
return null;
byte[] Data = await Runtime.IO.Files.ReadAllBytesAsync(Info.FileName);
using (SKBitmap Bitmap = SKBitmap.Decode(Data))
{
return new PixelInformationPng(Data, Bitmap.Width, Bitmap.Height);
}
}
/// <summary>
/// Generates smart contract XML for the code content.
/// </summary>
/// <param name="Renderer">Renderer.</param>
/// <param name="Rows">Code rows.</param>
/// <param name="Language">Language.</param>
/// <param name="Indent">Code block indentation.</param>
/// <param name="Document">Markdown document containing element.</param>
/// <returns>If renderer was able to generate output.</returns>
public async Task<bool> RenderContractXml(ContractsRenderer Renderer, string[] Rows, string Language, int Indent, MarkdownDocument Document)
{
try
{
GraphInfo Info = await GetFileName(Language, Rows, ResultType.Png, true, Document.Settings?.Variables);
if (Info is null)
return false;
byte[] Data = await Runtime.IO.Files.ReadAllBytesAsync(Info.FileName);
string ContentType = ImageCodec.ContentTypePng;
ContentResponse Content = await InternetContent.DecodeAsync(ContentType, Data, null);
if (Content.HasError || !(Content.Decoded is SKImage Image))
return false;