2using System.Collections.Generic;
5using System.Threading.Tasks;
28 internal class GraphInfo
30 public string FileName;
33 public byte[] DynamicContent;
44 internal const SKEncodedImageFormat DefaultFormat = SKEncodedImageFormat.Png;
45 internal const int DefaultQuality = 100;
47 private static readonly Random rnd =
new Random();
48 private static Scheduler scheduler =
null;
49 private static string layoutFolder =
null;
50 private static string contentRootFolder =
null;
64 public static void Init(
string ContentRootFolder)
66 contentRootFolder = ContentRootFolder;
67 layoutFolder = Path.Combine(contentRootFolder,
"Layout");
69 if (scheduler is
null)
77 Log.Terminating += (Sender, e) =>
85 if (!Directory.Exists(layoutFolder))
86 Directory.CreateDirectory(layoutFolder);
88 DeleteOldFiles(TimeSpan.FromDays(7));
91 private static void DeleteOldFiles(
object P)
93 if (P is TimeSpan MaxAge)
94 DeleteOldFiles(MaxAge,
true);
104 if (
string.IsNullOrEmpty(layoutFolder))
107 DateTime Limit = DateTime.Now - MaxAge;
110 DirectoryInfo LayoutFolder =
new DirectoryInfo(layoutFolder);
111 FileInfo[] Files = LayoutFolder.GetFiles(
"*.*");
113 foreach (FileInfo FileInfo
in Files)
115 if (FileInfo.LastAccessTime < Limit)
119 File.Delete(FileInfo.FullName);
124 Log.
Error(
"Unable to delete old file: " + ex.Message, FileInfo.FullName);
130 Log.
Informational(Count.ToString() +
" old file(s) deleted.", layoutFolder);
136 scheduler.
Add(DateTime.Now.AddDays(rnd.NextDouble() * 2), DeleteOldFiles, MaxAge);
148 int i = Language.IndexOf(
':');
150 Language = Language.Substring(0, i).TrimEnd();
152 switch (Language.ToLower())
155 return Grade.Excellent;
158 return Grade.NotAtAll;
186 GraphInfo Info = await GetFileName(Language, Rows, Document.
Settings.
Variables, DefaultFormat, DefaultQuality, DefaultFileExtension);
187 if (Info?.FileName is
null)
190 string FileName = Info.FileName.Substring(contentRootFolder.Length).Replace(Path.DirectorySeparatorChar,
'/');
191 if (!FileName.StartsWith(
"/"))
192 FileName =
"/" + FileName;
196 Output.Append(
"<figure>");
197 Output.Append(
"<img src=\"");
203 if (!
string.IsNullOrEmpty(Info.Title))
205 Output.Append(
"\" alt=\"");
208 Output.Append(
"\" title=\"");
212 Output.Append(
"\" alt=\"2D-layout");
214 Output.Append(
"\" class=\"aloneUnsized\"/>");
216 if (!
string.IsNullOrEmpty(Info.Title))
218 Output.Append(
"<figcaption>");
220 Output.Append(
"</figcaption>");
223 Output.AppendLine(
"</figure>");
238 internal static Task<GraphInfo> GetFileName(
string Language,
string[] Rows,
Variables Session,
239 SKEncodedImageFormat ImageFormat,
int Quality,
string FileExtension)
254 internal static async Task<GraphInfo> GetFileName(
string Language,
string Xml,
Variables Session,
255 SKEncodedImageFormat ImageFormat,
int Quality,
string FileExtension)
257 GraphInfo Result =
new GraphInfo();
258 int i = Language.IndexOf(
':');
262 Result.Title = Language.Substring(i + 1).Trim();
263 Language = Language.Substring(0, i).TrimEnd();
266 Result.Title =
string.Empty;
273 string LayoutFolder = Path.Combine(contentRootFolder,
"Layout");
274 string FileName = Path.Combine(LayoutFolder, Hash);
275 Result.FileName = FileName +
"." + FileExtension;
277 if (!File.Exists(Result.FileName))
279 XmlDocument Doc =
new XmlDocument();
285 KeyValuePair<SKImage, Map[]> P = await LayoutDoc.
Render(Settings);
286 using (SKImage Img = P.Key)
288 using (SKData Data = Img.Encode(ImageFormat, Quality))
290 Result.DynamicContent = Data.ToArray();
291 Result.Dynamic = LayoutDoc.
Dynamic;
310 if (v.ValueObject is SKColor Color)
312 else if (v.ValueObject is
string s)
329 GraphInfo Info = await GetFileName(Language, Rows, Document.
Settings.
Variables, DefaultFormat, DefaultQuality, DefaultFileExtension);
330 if (Info?.FileName is
null)
353 GraphInfo Info = await GetFileName(Language, Rows, Document.
Settings.
Variables, DefaultFormat, DefaultQuality, DefaultFileExtension);
370 GraphInfo Info = await GetFileName(Language, Rows, Document.
Settings.
Variables, DefaultFormat, DefaultQuality, DefaultFileExtension);
371 if (Info?.FileName is
null)
374 XmlWriter Output =
Renderer.XmlOutput;
378 await Wpf.Multimedia.ImageContent.OutputWpf(Output,
new ImageSource()
385 Output.WriteStartElement(
"Image");
386 Output.WriteAttributeString(
"Source", Info.FileName);
387 Output.WriteAttributeString(
"Stretch",
"None");
389 if (!
string.IsNullOrEmpty(Info.Title))
390 Output.WriteAttributeString(
"ToolTip", Info.Title);
392 Output.WriteEndElement();
409 GraphInfo Info = await GetFileName(Language, Rows, Document.
Settings.
Variables, DefaultFormat, DefaultQuality, DefaultFileExtension);
410 if (Info?.FileName is
null)
413 XmlWriter Output =
Renderer.XmlOutput;
417 await Xamarin.Multimedia.ImageContent.OutputXamarinForms(Output,
new ImageSource()
424 Output.WriteStartElement(
"Image");
425 Output.WriteAttributeString(
"Source", Info.FileName);
426 Output.WriteEndElement();
443 GraphInfo Info = await GetFileName(Language, Rows, Document.
Settings.
Variables, DefaultFormat, DefaultQuality, DefaultFileExtension);
444 if (Info?.FileName is
null)
448 Info.FileName = await Model.Multimedia.ImageContent.GetTemporaryFile(Info.DynamicContent, DefaultFileExtension.Substring(1));
452 Output.AppendLine(
"\\begin{figure}[h]");
453 Output.AppendLine(
"\\centering");
455 Output.Append(
"\\fbox{\\includegraphics{");
456 Output.Append(Info.FileName.Replace(
'\\',
'/'));
457 Output.AppendLine(
"}}");
459 if (!
string.IsNullOrEmpty(Info.Title))
461 Output.Append(
"\\caption{");
463 Output.AppendLine(
"}");
466 Output.AppendLine(
"\\end{figure}");
481 GraphInfo Info = await GetFileName(Language, Rows, Document.
Settings.
Variables, DefaultFormat, DefaultQuality, DefaultFileExtension);
482 if (Info?.FileName is
null)
487 using (SKBitmap Bitmap = SKBitmap.Decode(Data))
500 return Xml.DocumentElement.LocalName == Layout2DDocument.LocalName &&
501 Xml.DocumentElement.NamespaceURI == Layout2DDocument.Namespace ? Grade.Excellent :
Grade.NotAtAll;
515 KeyValuePair<SKImage, Map[]> P = await LayoutDoc.
Render(Settings);
516 using (SKImage Img = P.Key)
538 int i = Language.IndexOf(
':');
542 Title = Language.Substring(i + 1).Trim();
543 Language = Language.Substring(0, i).TrimEnd();
546 Title =
string.Empty;
548 XmlDocument Doc =
new XmlDocument();
554 XmlWriter Output =
Renderer.XmlOutput;
556 KeyValuePair<SKImage, Map[]> P = await LayoutDoc.
Render(Settings);
558 using (SKImage Img = P.Key)
560 Output.WriteStartElement(
"imageStandalone");
562 Output.WriteAttributeString(
"contentType", DefaultContentType);
563 Output.WriteAttributeString(
"width", Img.Width.ToString());
564 Output.WriteAttributeString(
"height", Img.Height.ToString());
566 using (SKData Data = Img.Encode(DefaultFormat, DefaultQuality))
568 byte[] Bin = Data.ToArray();
570 Output.WriteStartElement(
"binary");
571 Output.WriteValue(Convert.ToBase64String(Bin));
572 Output.WriteEndElement();
575 Output.WriteStartElement(
"caption");
576 if (
string.IsNullOrEmpty(Title))
577 Output.WriteElementString(
"text",
"Layout");
579 Output.WriteElementString(
"text", Title);
581 Output.WriteEndElement();
582 Output.WriteEndElement();
587 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.
Static class managing loading of resources stored as embedded resources or in content files.
static async Task< byte[]> ReadAllBytesAsync(string FileName)
Reads a binary file asynchronously.
static Task WriteAllBytesAsync(string FileName, byte[] Data)
Creates a binary file asynchronously.
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 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 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 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, ScheduledEventCallback 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.