-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathMarkdownCodec.cs
More file actions
232 lines (207 loc) · 6.91 KB
/
MarkdownCodec.cs
File metadata and controls
232 lines (207 loc) · 6.91 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
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Waher.Runtime.Inventory;
using Waher.Runtime.IO;
namespace Waher.Content.Markdown
{
/// <summary>
/// Markdown encoder.
/// </summary>
public class MarkdownCodec : IContentDecoder, IContentEncoder
{
private static bool allowEncoding = true;
private static bool locked = false;
/// <summary>
/// If raw encoding of web script should be allowed.
/// </summary>
/// <param name="Allow">If Raw encoding should be allowed.</param>
/// <param name="Lock">If settings should be locked.</param>
public static void AllowRawEncoding(bool Allow, bool Lock)
{
if (locked)
throw new InvalidOperationException("Setting has been locked.");
allowEncoding = Allow;
locked = Lock;
}
/// <summary>
/// If Raw encoding is allowed. Can be changed calling <see cref="AllowRawEncoding(bool, bool)"/>.
/// </summary>
public static bool IsRawEncodingAllowed => allowEncoding;
/// <summary>
/// If the <see cref="IsRawEncodingAllowed"/> setting is locked.
/// </summary>
public static bool IsRawEncodingAllowedLocked => locked;
/// <summary>
/// Markdown encoder/decoder.
/// </summary>
public MarkdownCodec()
{
}
/// <summary>
/// Markdown content type.
/// </summary>
public const string ContentType = "text/markdown";
/// <summary>
/// Supported content types.
/// </summary>
public string[] ContentTypes => contentTypes;
private static readonly string[] contentTypes = new string[] { ContentType };
/// <summary>
/// Supported file extensions.
/// </summary>
public string[] FileExtensions
{
get
{
return new string[]
{
"md",
"markdown"
};
}
}
/// <summary>
/// If the decoder decodes an object with a given content type.
/// </summary>
/// <param name="ContentType">Content type to decode.</param>
/// <param name="Grade">How well the decoder decodes the object.</param>
/// <returns>If the decoder can decode an object with the given type.</returns>
public bool Decodes(string ContentType, out Grade Grade)
{
if (Array.IndexOf(this.ContentTypes, ContentType) >= 0)
{
Grade = Grade.Excellent;
return true;
}
else
{
Grade = Grade.NotAtAll;
return false;
}
}
/// <summary>
/// Decodes an object.
/// </summary>
/// <param name="ContentType">Internet Content Type.</param>
/// <param name="Data">Encoded object.</param>
/// <param name="Encoding">Any encoding specified. Can be null if no encoding specified.</param>
/// <param name="Fields">Any content-type related fields and their corresponding values.</param>
/// <param name="BaseUri">Base URI, if any. If not available, value is null.</param>
/// <param name="Progress">Optional progress reporting of encoding/decoding. Can be null.</param>
/// <returns>Decoded object.</returns>
public async Task<ContentResponse> DecodeAsync(string ContentType, byte[] Data, Encoding Encoding,
KeyValuePair<string, string>[] Fields, Uri BaseUri, ICodecProgress Progress)
{
string s = Strings.GetString(Data, Encoding ?? Encoding.UTF8);
MarkdownSettings Settings = new MarkdownSettings()
{
Progress = Progress
};
MarkdownDocument Doc;
if (BaseUri is null)
Doc = await MarkdownDocument.CreateAsync(s, Settings);
else
{
Doc = await MarkdownDocument.CreateAsync(s, Settings,
string.Empty, string.Empty, BaseUri.ToString());
}
return new ContentResponse(ContentType, Doc, Data);
}
/// <summary>
/// If the encoder encodes a given object.
/// </summary>
/// <param name="Object">Object to encode.</param>
/// <param name="Grade">How well the encoder encodes the object.</param>
/// <param name="AcceptedContentTypes">Optional array of accepted content types. If array is empty, all content types are accepted.</param>
/// <returns>If the encoder can encode the given object.</returns>
public bool Encodes(object Object, out Grade Grade, params string[] AcceptedContentTypes)
{
if (Object is MarkdownContent && InternetContent.IsAccepted(contentTypes, AcceptedContentTypes))
{
Grade = Grade.Excellent;
return true;
}
else if (allowEncoding && Object is MarkdownDocument &&
InternetContent.IsAccepted(contentTypes, AcceptedContentTypes))
{
Grade = Grade.Excellent;
return true;
}
else
{
Grade = Grade.NotAtAll;
return false;
}
}
/// <summary>
/// Encodes an object.
/// </summary>
/// <param name="Object">Object to encode.</param>
/// <param name="Encoding">Desired encoding of text. Can be null if no desired encoding is speified.</param>
/// <param name="Progress">Optional progress reporting of encoding/decoding. Can be null.</param>
/// <param name="AcceptedContentTypes">Optional array of accepted content types. If array is empty, all content types are accepted.</param>
/// <returns>Encoded object, as well as Content Type of encoding. Includes information about any text encodings used.</returns>
public async Task<ContentResponse> EncodeAsync(object Object, Encoding Encoding, ICodecProgress Progress, params string[] AcceptedContentTypes)
{
string Markdown;
if (Object is MarkdownContent Content)
Markdown = Content.Markdown;
else if (allowEncoding && Object is MarkdownDocument MarkdownDocument)
Markdown = await MarkdownDocument.GenerateMarkdown(true);
else
return new ContentResponse(new ArgumentException("Object not a markdown document.", nameof(Object)));
string ContentType;
byte[] Bin;
if (Encoding is null)
{
ContentType = "text/markdown; charset=utf-8";
Bin = Encoding.UTF8.GetBytes(Markdown);
}
else
{
ContentType = "text/markdown; charset=" + Encoding.WebName;
Bin = Encoding.GetBytes(Markdown);
}
return new ContentResponse(ContentType, Object, Bin);
}
/// <summary>
/// Tries to get the content type of an item, given its file extension.
/// </summary>
/// <param name="FileExtension">File extension.</param>
/// <param name="ContentType">Content type.</param>
/// <returns>If the extension was recognized.</returns>
public bool TryGetContentType(string FileExtension, out string ContentType)
{
switch (FileExtension.ToLower())
{
case "md":
case "markdown":
ContentType = MarkdownCodec.ContentType;
return true;
default:
ContentType = string.Empty;
return false;
}
}
/// <summary>
/// Tries to get the file extension of an item, given its Content-Type.
/// </summary>
/// <param name="ContentType">Content type.</param>
/// <param name="FileExtension">File extension.</param>
/// <returns>If the Content-Type was recognized.</returns>
public bool TryGetFileExtension(string ContentType, out string FileExtension)
{
switch (ContentType.ToLower())
{
case MarkdownCodec.ContentType:
FileExtension = "md";
return true;
default:
FileExtension = string.Empty;
return false;
}
}
}
}