forked from PeterWaher/IoTGateway
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMarkdownToHtmlConverter.cs
More file actions
387 lines (335 loc) · 10.2 KB
/
Copy pathMarkdownToHtmlConverter.cs
File metadata and controls
387 lines (335 loc) · 10.2 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
using System;
using System.IO;
using System.Collections.Generic;
using System.Text;
using Waher.Content.Emoji;
using Waher.Networking.HTTP;
using Waher.Networking.HTTP.ScriptExtensions;
using Waher.Runtime.Inventory;
using Waher.Script;
using Waher.Security;
namespace Waher.Content.Markdown.Web
{
/// <summary>
/// Converts Markdown documents to HTML documents.
/// </summary>
public class MarkdownToHtmlConverter : IContentConverter
{
private static IEmojiSource emojiSource = null;
private static string bareJid = string.Empty;
private static string rootFolder = string.Empty;
private static HtmlSettings htmlSettings = new HtmlSettings();
/// <summary>
/// Converts Markdown documents to HTML documents.
/// </summary>
public MarkdownToHtmlConverter()
{
}
/// <summary>
/// Converts content from these content types.
/// </summary>
public string[] FromContentTypes
{
get
{
return new string[]
{
MarkdownCodec.ContentType
};
}
}
/// <summary>
/// Converts content to these content types.
/// </summary>
public string[] ToContentTypes
{
get
{
return new string[]
{
"text/html",
"application/xhtml+xml"
};
}
}
/// <summary>
/// How well the content is converted.
/// </summary>
public Grade ConversionGrade
{
get { return Grade.Excellent; }
}
/// <summary>
/// Bare JID used, if the HTTPX URI scheme is supported.
/// </summary>
public static string BareJID
{
get { return bareJid; }
set { bareJid = value; }
}
/// <summary>
/// Root folder used for web content.
/// </summary>
public static string RootFolder
{
get { return rootFolder; }
set { rootFolder = value; }
}
/// <summary>
/// Emoji source to use when converting Markdown documents to HTML.
/// </summary>
public static IEmojiSource EmojiSource
{
get { return emojiSource; }
set { emojiSource = value; }
}
/// <summary>
/// Performs the actual conversion.
/// </summary>
/// <param name="FromContentType">Content type of the content to convert from.</param>
/// <param name="From">Stream pointing to binary representation of content.</param>
/// <param name="FromFileName">If the content is coming from a file, this parameter contains the name of that file.
/// Otherwise, the parameter is the empty string.</param>
/// <param name="ResourceName">Local resource name of file, if accessed from a web server.</param>
/// <param name="URL">URL of resource, if accessed from a web server.</param>
/// <param name="ToContentType">Content type of the content to convert to. This value might be changed, in case
/// the converter finds a better option.</param>
/// <param name="To">Stream pointing to where binary representation of content is to be sent.</param>
/// <param name="Session">Session states.</param>
/// <param name="PossibleContentTypes">Possible content types the converter is allowed to convert to.
/// Can be null if there are no alternatives.</param>
/// <returns>If the result is dynamic (true), or only depends on the source (false).</returns>
public bool Convert(string FromContentType, Stream From, string FromFileName, string ResourceName, string URL,
ref string ToContentType, Stream To, Variables Session, params string[] PossibleContentTypes)
{
HttpRequest Request = null;
string Markdown;
bool b;
using (StreamReader rd = new StreamReader(From))
{
Markdown = rd.ReadToEnd();
}
if (!(Session is null) && Session.TryGetVariable("Request", out Variable v))
{
Request = v.ValueObject as HttpRequest;
if (!(Request is null))
{
Page.GetPageVariables(Session, Request.Header.ResourcePart);
int i = Markdown.IndexOf("\r\n\r\n");
if (i < 0)
i = Markdown.IndexOf("\n\n");
if (i > 0)
{
string Header = Markdown.Substring(0, i);
string Parameter;
foreach (string Row in Header.Split(CommonTypes.CRLF, StringSplitOptions.RemoveEmptyEntries))
{
if (!Row.StartsWith("Parameter:", StringComparison.OrdinalIgnoreCase))
continue;
Parameter = Row.Substring(10).Trim();
if (Request.Header.TryGetQueryParameter(Parameter, out string Value))
{
Value = System.Net.WebUtility.UrlDecode(Value);
if (CommonTypes.TryParse(Value, out double d))
Session[Parameter] = d;
else if (bool.TryParse(Value, out b))
Session[Parameter] = b;
else
Session[Parameter] = Value;
}
else
Session[Parameter] = string.Empty;
}
}
}
}
MarkdownSettings Settings = new MarkdownSettings(emojiSource, true, Session)
{
RootFolder = rootFolder,
HtmlSettings = htmlSettings
};
if (!string.IsNullOrEmpty(bareJid))
{
Settings.HttpxProxy = "/HttpxProxy/%URL%";
Settings.LocalHttpxResourcePath = "httpx://" + bareJid + "/";
}
MarkdownDocument Doc = new MarkdownDocument(Markdown, Settings, FromFileName, ResourceName, URL, typeof(HttpException));
if (Doc.TryGetMetaData("UserVariable", out KeyValuePair<string, bool>[] MetaValues))
{
object User = null;
bool Authorized = true;
if (!Doc.TryGetMetaData("Login", out KeyValuePair<string, bool>[] Login))
Login = null;
if (!Doc.TryGetMetaData("Privilege", out KeyValuePair<string, bool>[] Privilege))
Privilege = null;
foreach (KeyValuePair<string, bool> P in MetaValues)
{
if (Session is null)
{
Authorized = false;
break;
}
if (Session.TryGetVariable(P.Key, out v) && !(v.ValueObject is null))
User = v.ValueObject;
else
{
Uri LoginUrl = null;
string LoginFileName = null;
string FromFolder = Path.GetDirectoryName(FromFileName);
if (!(Login is null))
{
foreach (KeyValuePair<string, bool> P2 in Login)
{
LoginFileName = Path.Combine(FromFolder, P2.Key.Replace('/', Path.DirectorySeparatorChar));
LoginUrl = new Uri(new Uri(URL), P2.Key.Replace(Path.DirectorySeparatorChar, '/'));
if (File.Exists(LoginFileName))
break;
else
LoginFileName = null;
}
}
if (!(LoginFileName is null))
{
string LoginMarkdown = File.ReadAllText(LoginFileName);
MarkdownDocument LoginDoc = new MarkdownDocument(LoginMarkdown, Settings, LoginFileName, LoginUrl.AbsolutePath,
LoginUrl.ToString(), typeof(HttpException));
if (!Session.TryGetVariable(P.Key, out v))
{
Authorized = false;
break;
}
}
else
{
Authorized = false;
break;
}
}
if (!(Privilege is null))
{
if (!(v.ValueObject is IUser User2))
{
Authorized = false;
break;
}
foreach (KeyValuePair<string, bool> P2 in Privilege)
{
if (!User2.HasPrivilege(P2.Key))
{
Authorized = false;
break;
}
}
}
if (!Authorized)
break;
}
if (!Authorized)
{
if (!(Login is null))
{
foreach (KeyValuePair<string, bool> P in Login)
{
StringBuilder Location = new StringBuilder(P.Key);
if (P.Key.IndexOf('?') >= 0)
Location.Append('&');
else
Location.Append('?');
Location.Append("from=");
if (!(Request is null))
Location.Append(System.Net.WebUtility.UrlEncode(Request.Header.GetURL(true, true)));
else
Location.Append(System.Net.WebUtility.UrlEncode(URL));
throw new TemporaryRedirectException(Location.ToString());
}
}
throw new ForbiddenException("Access denied.");
}
if (User is null)
throw new ForbiddenException("Access denied.");
Session[" User "] = User;
}
if (Doc.TryGetMetaData("AudioControls", out MetaValues))
{
foreach (KeyValuePair<string, bool> P in MetaValues)
{
if (CommonTypes.TryParse(P.Key, out b))
Settings.AudioControls = b;
}
}
if (Doc.TryGetMetaData("AudioAutoplay", out MetaValues))
{
foreach (KeyValuePair<string, bool> P in MetaValues)
{
if (CommonTypes.TryParse(P.Key, out b))
Settings.AudioAutoplay = b;
}
}
if (Doc.TryGetMetaData("VideoControls", out MetaValues))
{
foreach (KeyValuePair<string, bool> P in MetaValues)
{
if (CommonTypes.TryParse(P.Key, out b))
Settings.VideoControls = b;
}
}
if (Doc.TryGetMetaData("VideoAutoplay", out MetaValues))
{
foreach (KeyValuePair<string, bool> P in MetaValues)
{
if (CommonTypes.TryParse(P.Key, out b))
Settings.VideoAutoplay = b;
}
}
if (!(Session is null) && Session.TryGetVariable("Response", out v))
{
if (v.ValueObject is HttpResponse Response)
{
if (Response.ResponseSent)
return Doc.IsDynamic;
Response.SetHeader("X-Content-Type-Options", "nosniff");
if (!this.CopyHttpHeader("Cache-Control", Doc, Response))
{
if (Doc.IsDynamic)
Response.SetHeader("Cache-Control", "max-age=0, no-cache, no-store");
else
Response.SetHeader("Cache-Control", "no-transform,public,max-age=60,s-maxage=60,stale-while-revalidate=604800");
}
this.CopyHttpHeader("Access-Control-Allow-Origin", Doc, Response);
this.CopyHttpHeader("Content-Security-Policy", Doc, Response);
this.CopyHttpHeader("Public-Key-Pins", Doc, Response);
this.CopyHttpHeader("Strict-Transport-Security", Doc, Response);
this.CopyHttpHeader("Sunset", Doc, Response);
this.CopyHttpHeader("Vary", Doc, Response);
}
}
string HTML = Doc.GenerateHTML();
byte[] Data = Utf8WithBOM.GetBytes(HTML);
To.Write(Data, 0, Data.Length);
return Doc.IsDynamic;
}
private bool CopyHttpHeader(string Name, MarkdownDocument Doc, HttpResponse Response)
{
if (Doc.TryGetMetaData(Name, out KeyValuePair<string, bool>[] Value))
{
foreach (KeyValuePair<string, bool> P in Value)
{
Response.SetHeader(Name, P.Key);
break;
}
return true;
}
else
return false;
}
internal static readonly Encoding Utf8WithBOM = new UTF8Encoding(true);
/// <summary>
/// HTML settings for automatically converted content.
/// </summary>
public static HtmlSettings HtmlSettings
{
get => htmlSettings;
set => htmlSettings = value;
}
}
}