6using System.Threading.Tasks;
29 internal class GraphInfo
31 public XmlDocument Xml;
32 public string FileName;
35 public byte[] DynamicContent;
36 public bool Converted;
47 internal const SKEncodedImageFormat DefaultFormat = SKEncodedImageFormat.Png;
48 internal const int DefaultQuality = 100;
50 private static readonly Random rnd =
new Random();
51 private static Scheduler scheduler =
null;
52 private static string layoutFolder =
null;
53 private static string contentRootFolder =
null;
67 public static void Init(
string ContentRootFolder)
69 contentRootFolder = ContentRootFolder;
70 layoutFolder = Path.Combine(contentRootFolder,
"Layout");
72 if (scheduler is
null)
80 Log.Terminating += (Sender, e) =>
84 return Task.CompletedTask;
89 if (!Directory.Exists(layoutFolder))
90 Directory.CreateDirectory(layoutFolder);
92 DeleteOldFiles(TimeSpan.FromDays(7));
95 private static void DeleteOldFiles(
object P)
97 if (P is TimeSpan MaxAge)
98 DeleteOldFiles(MaxAge,
true);
108 if (
string.IsNullOrEmpty(layoutFolder))
111 DateTime Limit = DateTime.Now - MaxAge;
114 DirectoryInfo LayoutFolder =
new DirectoryInfo(layoutFolder);
115 FileInfo[]
Files = LayoutFolder.GetFiles(
"*.*");
117 foreach (FileInfo FileInfo
in Files)
119 if (FileInfo.LastAccessTime < Limit)
123 File.Delete(FileInfo.FullName);
128 Log.
Error(
"Unable to delete old file: " + ex.Message, FileInfo.FullName);
134 Log.
Informational(Count.ToString() +
" old file(s) deleted.", layoutFolder);
140 scheduler.
Add(DateTime.Now.AddDays(rnd.NextDouble() * 2), DeleteOldFiles, MaxAge);
152 int i = Language.IndexOf(
':');
154 Language = Language.Substring(0, i).TrimEnd();
156 switch (Language.ToLower())
159 return Grade.Excellent;
162 return Grade.NotAtAll;
190 GraphInfo Info = await GetFileName(Language, Rows, Document.
Settings.
Variables, DefaultFormat, DefaultQuality, DefaultFileExtension);
191 if (Info?.FileName is
null || !Info.Converted)
194 string FileName = Info.FileName.Substring(contentRootFolder.Length).Replace(Path.DirectorySeparatorChar,
'/');
195 if (!FileName.StartsWith(
"/"))
196 FileName =
"/" + FileName;
200 Output.Append(
"<figure>");
201 Output.Append(
"<img src=\"");
207 if (!
string.IsNullOrEmpty(Info.Title))
209 Output.Append(
"\" alt=\"");
212 Output.Append(
"\" title=\"");
216 Output.Append(
"\" alt=\"2D-layout");
218 Output.Append(
"\" class=\"aloneUnsized\"/>");
220 if (!
string.IsNullOrEmpty(Info.Title))
222 Output.Append(
"<figcaption>");
224 Output.Append(
"</figcaption>");
227 Output.AppendLine(
"</figure>");
242 internal static Task<GraphInfo> GetFileName(
string Language,
string[] Rows,
Variables Session,
243 SKEncodedImageFormat ImageFormat,
int Quality,
string FileExtension)
258 internal static async Task<GraphInfo> GetFileName(
string Language,
string Xml,
Variables Session,
259 SKEncodedImageFormat ImageFormat,
int Quality,
string FileExtension)
261 GraphInfo Result =
new GraphInfo();
262 int i = Language.IndexOf(
':');
266 Result.Title = Language.Substring(i + 1).Trim();
267 Language = Language.Substring(0, i).TrimEnd();
270 Result.Title =
string.Empty;
277 string LayoutFolder = Path.Combine(contentRootFolder,
"Layout");
278 string FileName = Path.Combine(LayoutFolder, Hash);
279 Result.FileName = FileName +
"." + FileExtension;
281 if (File.Exists(Result.FileName))
282 Result.Converted =
true;
292 KeyValuePair<SKImage, Map[]> P = await LayoutDoc.
Render(Settings);
293 using (SKImage Img = P.Key)
295 using (SKData Data = Img.Encode(ImageFormat, Quality))
297 Result.DynamicContent = Data.ToArray();
298 Result.Dynamic = LayoutDoc.
Dynamic;
305 Result.Converted =
true;
308 Result.Converted =
false;
322 if (v.ValueObject is SKColor Color)
324 else if (v.ValueObject is
string s)
341 GraphInfo Info = await GetFileName(Language, Rows, Document.
Settings.
Variables, DefaultFormat, DefaultQuality, DefaultFileExtension);
342 if (Info?.FileName is
null || !Info.Converted)
365 GraphInfo Info = await GetFileName(Language, Rows, Document.
Settings.
Variables, DefaultFormat, DefaultQuality, DefaultFileExtension);
366 if (Info?.FileName is
null || !Info.Converted)
385 GraphInfo Info = await GetFileName(Language, Rows, Document.
Settings.
Variables, DefaultFormat, DefaultQuality, DefaultFileExtension);
386 if (Info?.FileName is
null || !Info.Converted)
389 XmlWriter Output =
Renderer.XmlOutput;
393 await Wpf.Multimedia.ImageContent.OutputWpf(Output,
new ImageSource()
400 Output.WriteStartElement(
"Image");
401 Output.WriteAttributeString(
"Source", Info.FileName);
402 Output.WriteAttributeString(
"Stretch",
"None");
404 if (!
string.IsNullOrEmpty(Info.Title))
405 Output.WriteAttributeString(
"ToolTip", Info.Title);
407 Output.WriteEndElement();
424 GraphInfo Info = await GetFileName(Language, Rows, Document.
Settings.
Variables, DefaultFormat, DefaultQuality, DefaultFileExtension);
425 if (Info?.FileName is
null || !Info.Converted)
428 XmlWriter Output =
Renderer.XmlOutput;
432 await Xamarin.Multimedia.ImageContent.OutputXamarinForms(Output,
new ImageSource()
439 Output.WriteStartElement(
"Image");
440 Output.WriteAttributeString(
"Source", Info.FileName);
441 Output.WriteEndElement();
458 GraphInfo Info = await GetFileName(Language, Rows, Document.
Settings.
Variables, DefaultFormat, DefaultQuality, DefaultFileExtension);
459 if (Info?.FileName is
null || !Info.Converted)
463 Info.FileName = await Model.Multimedia.ImageContent.GetTemporaryFile(Info.DynamicContent, DefaultFileExtension.Substring(1));
467 Output.AppendLine(
"\\begin{figure}[h]");
468 Output.AppendLine(
"\\centering");
470 Output.Append(
"\\fbox{\\includegraphics{");
471 Output.Append(Info.FileName.Replace(
'\\',
'/'));
472 Output.AppendLine(
"}}");
474 if (!
string.IsNullOrEmpty(Info.Title))
476 Output.Append(
"\\caption{");
478 Output.AppendLine(
"}");
481 Output.AppendLine(
"\\end{figure}");
496 GraphInfo Info = await GetFileName(Language, Rows, Document.
Settings.
Variables, DefaultFormat, DefaultQuality, DefaultFileExtension);
497 if (Info?.FileName is
null || !Info.Converted)
502 using (SKBitmap Bitmap = SKBitmap.Decode(Data))
529 KeyValuePair<SKImage, Map[]> P = await LayoutDoc.
Render(Settings);
530 using (SKImage Img = P.Key)
552 int i = Language.IndexOf(
':');
556 Title = Language.Substring(i + 1).Trim();
557 Language = Language.Substring(0, i).TrimEnd();
560 Title =
string.Empty;
567 XmlWriter Output =
Renderer.XmlOutput;
569 KeyValuePair<SKImage, Map[]> P = await LayoutDoc.
Render(Settings);
571 using (SKImage Img = P.Key)
573 Output.WriteStartElement(
"imageStandalone");
575 Output.WriteAttributeString(
"contentType", DefaultContentType);
576 Output.WriteAttributeString(
"width", Img.Width.ToString());
577 Output.WriteAttributeString(
"height", Img.Height.ToString());
579 using (SKData Data = Img.Encode(DefaultFormat, DefaultQuality))
581 byte[] Bin = Data.ToArray();
583 Output.WriteStartElement(
"binary");
584 Output.WriteValue(Convert.ToBase64String(Bin));
585 Output.WriteEndElement();
588 Output.WriteStartElement(
"caption");
589 if (
string.IsNullOrEmpty(Title))
590 Output.WriteElementString(
"text",
"Layout");
592 Output.WriteElementString(
"text", Title);
594 Output.WriteEndElement();
595 Output.WriteEndElement();
600 catch (XmlException ex)
Contains information about an emoji image.
const string FileExtensionPng
png
const string ContentTypePng
image/png
Renders Contracts XML from a Markdown document.
Renders LaTeX from a Markdown document.
static string EscapeLaTeX(string s)
Escapes text for output in a LaTeX document.
Class managing 2D XML Layout integration into Markdown documents.
Grade Supports(XmlDocument Xml)
Checks how well the handler supports multimedia content of a given type.
Grade Supports(string Language)
Checks how well the handler supports multimedia content of a given type.
async Task< object > TransformXml(XmlDocument Xml, Variables Session)
Transforms the XML document before visualizing it.
static void Init(string ContentRootFolder)
Initializes the Layout2D-Markdown integration.
async Task< bool > RenderHtml(HtmlRenderer Renderer, string[] Rows, string Language, int Indent, MarkdownDocument Document)
Generates HTML for the code content.
async Task< bool > RenderText(TextRenderer Renderer, string[] Rows, string Language, int Indent, MarkdownDocument Document)
Generates plain text for the code content.
XmlLayout()
Class managing 2D XML Layout integration into Markdown documents.
async Task< bool > RenderMarkdown(MarkdownRenderer Renderer, string[] Rows, string Language, int Indent, MarkdownDocument Document)
Generates Markdown for the code content.
static void DeleteOldFiles(TimeSpan MaxAge, bool Reschedule)
Deletes generated files older than MaxAge .
void Register(MarkdownDocument Document)
Is called on the object when an instance of the element has been created in a document.
async Task< bool > RenderContractXml(ContractsRenderer Renderer, string[] Rows, string Language, int Indent, MarkdownDocument Document)
Generates smart contract XML for the code content.
async Task< bool > RenderLatex(LatexRenderer Renderer, string[] Rows, string Language, int Indent, MarkdownDocument Document)
Generates LaTeX for the code content.
bool EvaluatesScript
If script is evaluated for this type of code block.
async Task< PixelInformation > GenerateImage(string[] Rows, string Language, MarkdownDocument Document)
Generates an image of the contents.
async Task< bool > RenderWpfXaml(WpfXamlRenderer Renderer, string[] Rows, string Language, int Indent, MarkdownDocument Document)
Generates WPF XAML for the code content.
async Task< bool > RenderXamarinFormsXaml(XamarinFormsXamlRenderer Renderer, string[] Rows, string Language, int Indent, MarkdownDocument Document)
Generates Xamarin.Forms XAML for the code content.
Contains a markdown document. This markdown document class supports original markdown,...
static string AppendRows(string[] Rows)
Appends a set of rows into a single string with newlines between rows.
MarkdownSettings Settings
Markdown settings.
Variables Variables
Collection of variables. Providing such a collection enables script execution inside markdown documen...
Base64-encoded image content.
static async Task< bool > GenerateMarkdownFromFile(StringBuilder Output, string FileName, string Title)
Generates Markdown embedding an image available in a file.
static string GenerateUrl(string Language, string[] Rows, out string ContentType, out string Title)
Generates a data URL of an encoded image.
static void GenerateMarkdown(StringBuilder Output, byte[] Bin, string ContentType, string Title)
Generates Markdown embedding an encoded image.
Renders HTML from a Markdown document.
Renders portable Markdown from a Markdown document.
Abstract base class for Markdown renderers.
readonly StringBuilder Output
Renderer output.
Renders plain text from a Markdown document.
Renders XAML (WPF flavour) from a Markdown document.
Renders XAML (Xamarin.Forms flavour) from a Markdown document.
Helps with common XML-related tasks.
static string HtmlValueEncode(string s)
Differs from Encode(String), in that it does not encode the aposotrophe or the quote.
static XmlException AnnotateException(XmlException ex)
Creates a new XML Exception object, with reference to the source XML file, for information.
static string HtmlAttributeEncode(string s)
Differs from Encode(String), in that it does not encode the aposotrophe.
static XmlDocument ParseXml(string Xml)
Parses an XML Document from its string representation.
Static class managing the application event log. Applications and services log events on this static ...
static void Exception(Exception Exception, string Object, string Actor, string EventId, EventLevel Level, string Facility, string Module, params KeyValuePair< string, object >[] Tags)
Logs an exception. Event type will be determined by the severity of the exception.
static void Error(string Message, string Object, string Actor, string EventId, EventLevel Level, string Facility, string Module, string StackTrace, params KeyValuePair< string, object >[] Tags)
Logs an error event.
static void Informational(string Message, string Object, string Actor, string EventId, EventLevel Level, string Facility, string Module, string StackTrace, params KeyValuePair< string, object >[] Tags)
Logs an informational event.
Contains a 2D layout document.
bool Dynamic
If the layout is dynamic (i.e. contains script).
async Task< KeyValuePair< SKImage, Map[]> > Render(RenderSettings Settings)
Renders the layout to an image
static bool IsLayoutXml(XmlDocument Xml)
Checks if an XML document contains a layout document.
static Task< Layout2DDocument > FromXml(string Xml, params KeyValuePair< string, object >[] Attachments)
Parses a 2D layout document from its XML definition.
async Task< RenderSettings > GetRenderSettings(Variables Session)
Creates a render settings object.
static Task WriteAllBytesAsync(string FileName, byte[] Data)
Creates a binary file asynchronously.
static async Task< byte[]> ReadAllBytesAsync(string FileName)
Reads a binary file asynchronously.
Static class that dynamically manages types and interfaces available in the runtime environment.
static bool TryGetModuleParameter(string Name, out object Value)
Tries to get a module parameter value.
Class that can be used to schedule events in time. It uses a timer to execute tasks at the appointed ...
void Dispose()
IDisposable.Dispose
DateTime Add(DateTime When, Action< object > Callback, object State)
Adds an event.
const string GraphFgColorVariableName
Variable name for graph foreground color.
const string GraphBgColorVariableName
Variable name for graph background color.
static string ToRGBAStyle(SKColor Color)
Converts a color to an RGB(A) style string.
Contains information about a variable.
virtual bool TryGetVariable(string Name, out Variable Variable)
Tries to get a variable object, given its name.
Contains methods for simple hash calculations.
static string ComputeSHA256HashString(byte[] Data)
Computes the SHA-256 hash of a block of binary data.
Interface for code content contract renderers.
Interface for code content LaTeX renderers.
Interface for all markdown handlers of code content that generates an image output.
Interface for all XML visalizers.
Interface for code content HTML renderers.
Interface for code content Markdown renderers.
Interface for code content plain text renderers.
Interface for code content WPF XAML renderers.
Interface for code content Xamarin.Forms XAML renderers.