Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
LoadMarkdown.cs
1using System.Collections.Generic;
2using System.Text.RegularExpressions;
3using System.Threading.Tasks;
4using Waher.Script;
9
11{
16 {
25 : base(new ScriptNode[] { FileName }, argumentTypes1Scalar, Start, Length, Expression)
26 {
27 }
28
37 public LoadMarkdown(ScriptNode FileName, ScriptNode Headers, int Start, int Length, Expression Expression)
38 : base(new ScriptNode[] { FileName, Headers }, argumentTypes2Scalar, Start, Length, Expression)
39 {
40 }
41
45 public override string FunctionName => nameof(LoadMarkdown);
46
50 public override string[] DefaultArgumentNames => new string[] { "FileName", "Headers" };
51
56 public override bool IsAsynchronous => true;
57
65 {
66 return this.EvaluateAsync(Arguments, Variables).Result;
67 }
68
75 public override async Task<IElement> EvaluateAsync(IElement[] Arguments, Variables Variables)
76 {
77 if (!(Arguments[0].AssociatedObjectValue is string FileName))
78 throw new ScriptRuntimeException("Expected first argument to be a string file name.", this);
79
80 bool IncludeHeaders;
81
82 if (Arguments.Length > 1)
83 {
84 if (!(Arguments[1].AssociatedObjectValue is bool b))
85 throw new ScriptRuntimeException("Expected second argument to be a Boolean value.", this);
86
87 IncludeHeaders = b;
88 }
89 else
90 IncludeHeaders = false;
91
92 string Markdown = await Resources.ReadAllTextAsync(FileName);
93 MarkdownSettings Settings = new MarkdownSettings()
94 {
96 ParseMetaData = true
97 };
98
100 v.ValueObject is MarkdownSettings ParentSettings)
101 {
102 Settings.AllowScriptTag = ParentSettings.AllowScriptTag;
103 Settings.AudioAutoplay = ParentSettings.AudioAutoplay;
104 Settings.AudioControls = ParentSettings.AudioControls;
105 Settings.EmbedEmojis = ParentSettings.EmbedEmojis;
106 Settings.EmojiSource = ParentSettings.EmojiSource;
107 Settings.HttpxProxy = ParentSettings.HttpxProxy;
108 Settings.LocalHttpxResourcePath = ParentSettings.LocalHttpxResourcePath;
109 Settings.RootFolder = ParentSettings.RootFolder;
110 Settings.VideoAutoplay = ParentSettings.VideoAutoplay;
111 Settings.VideoControls = ParentSettings.VideoControls;
112 }
113
114 KeyValuePair<string, bool> P = await MarkdownDocument.Preprocess(Markdown, Settings, FileName, true);
115 Markdown = P.Key;
116
117 if (!IncludeHeaders)
118 {
119 int? Pos = MarkdownDocument.HeaderEndPosition(Markdown);
120
121 if (Pos.HasValue)
122 Markdown = Markdown.Substring(Pos.Value).TrimStart();
123 }
124
125 return new StringValue(Markdown);
126 }
127 }
128}
LoadMarkdown(FileName[,Headers])
Definition: LoadMarkdown.cs:16
override bool IsAsynchronous
If the node (or its decendants) include asynchronous evaluation. Asynchronous nodes should be evaluat...
Definition: LoadMarkdown.cs:56
override IElement Evaluate(IElement[] Arguments, Variables Variables)
Evaluates the function.
Definition: LoadMarkdown.cs:64
override string[] DefaultArgumentNames
Default Argument names
Definition: LoadMarkdown.cs:50
LoadMarkdown(ScriptNode FileName, int Start, int Length, Expression Expression)
LoadMarkdown(FileName[,Headers])
Definition: LoadMarkdown.cs:24
override async Task< IElement > EvaluateAsync(IElement[] Arguments, Variables Variables)
Evaluates the function.
Definition: LoadMarkdown.cs:75
LoadMarkdown(ScriptNode FileName, ScriptNode Headers, int Start, int Length, Expression Expression)
LoadMarkdown(FileName[,Headers])
Definition: LoadMarkdown.cs:37
override string FunctionName
Name of the function
Definition: LoadMarkdown.cs:45
Contains a markdown document. This markdown document class supports original markdown,...
static ? int HeaderEndPosition(string Markdown)
Gets the end position of the header, if one is found, null otherwise.
const string MarkdownSettingsVariableName
Variable name used for storing Markdown settings.
static async Task< string > Preprocess(string Markdown, MarkdownSettings Settings, params Type[] TransparentExceptionTypes)
Preprocesses markdown text.
Contains settings that the Markdown parser uses to customize its behavior.
Static class managing loading of resources stored as embedded resources or in content files.
Definition: Resources.cs:15
static async Task< string > ReadAllTextAsync(string FileName)
Reads a text file asynchronously.
Definition: Resources.cs:205
Class managing a script expression.
Definition: Expression.cs:39
Base class for multivariate funcions.
ScriptNode[] Arguments
Function arguments.
static readonly ArgumentType[] argumentTypes2Scalar
Two scalar parameters.
static readonly ArgumentType[] argumentTypes1Scalar
One scalar parameter.
Base class for all nodes in a parsed script tree.
Definition: ScriptNode.cs:69
int Length
Length of expression covered by node.
Definition: ScriptNode.cs:101
Expression Expression
Expression of which the node is a part.
Definition: ScriptNode.cs:177
int Start
Start position in script expression.
Definition: ScriptNode.cs:92
Contains information about a variable.
Definition: Variable.cs:10
Collection of variables.
Definition: Variables.cs:25
virtual bool TryGetVariable(string Name, out Variable Variable)
Tries to get a variable object, given its name.
Definition: Variables.cs:52
Basic interface for all types of elements.
Definition: IElement.cs:20