Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
MarkdownCodec.cs
1using System;
3using System.Text;
4using System.Threading.Tasks;
8
10{
15 {
16 private static bool allowEncoding = true;
17 private static bool locked = false;
18
24 public static void AllowRawEncoding(bool Allow, bool Lock)
25 {
26 if (locked)
27 throw new InvalidOperationException("Setting has been locked.");
28
29 allowEncoding = Allow;
30 locked = Lock;
31 }
32
36 public static bool IsRawEncodingAllowed => allowEncoding;
37
41 public static bool IsRawEncodingAllowedLocked => locked;
42
47 {
48 }
49
53 public const string ContentType = "text/markdown";
54
58 public string[] ContentTypes => contentTypes;
59
60 private static readonly string[] contentTypes = new string[] { ContentType };
61
65 public string[] FileExtensions
66 {
67 get
68 {
69 return new string[]
70 {
71 "md",
72 "markdown"
73 };
74 }
75 }
76
83 public bool Decodes(string ContentType, out Grade Grade)
84 {
85 if (Array.IndexOf(this.ContentTypes, ContentType) >= 0)
86 {
87 Grade = Grade.Excellent;
88 return true;
89 }
90 else
91 {
92 Grade = Grade.NotAtAll;
93 return false;
94 }
95 }
96
107 public async Task<ContentResponse> DecodeAsync(string ContentType, byte[] Data, Encoding Encoding,
108 KeyValuePair<string, string>[] Fields, Uri BaseUri, ICodecProgress Progress)
109 {
110 string s = Strings.GetString(Data, Encoding ?? Encoding.UTF8);
111 MarkdownSettings Settings = new MarkdownSettings()
112 {
113 Progress = Progress
114 };
116
117 if (BaseUri is null)
118 Doc = await MarkdownDocument.CreateAsync(s, Settings);
119 else
120 {
121 Doc = await MarkdownDocument.CreateAsync(s, Settings,
122 string.Empty, string.Empty, BaseUri.ToString());
123 }
124
125 return new ContentResponse(ContentType, Doc, Data);
126 }
127
135 public bool Encodes(object Object, out Grade Grade, params string[] AcceptedContentTypes)
136 {
137 if (Object is MarkdownContent && InternetContent.IsAccepted(contentTypes, AcceptedContentTypes))
138 {
139 Grade = Grade.Excellent;
140 return true;
141 }
142 else if (allowEncoding && Object is MarkdownDocument &&
143 InternetContent.IsAccepted(contentTypes, AcceptedContentTypes))
144 {
145 Grade = Grade.Excellent;
146 return true;
147 }
148 else
149 {
150 Grade = Grade.NotAtAll;
151 return false;
152 }
153 }
154
163 public async Task<ContentResponse> EncodeAsync(object Object, Encoding Encoding, ICodecProgress Progress, params string[] AcceptedContentTypes)
164 {
165 string Markdown;
166
167 if (Object is MarkdownContent Content)
168 Markdown = Content.Markdown;
169 else if (allowEncoding && Object is MarkdownDocument MarkdownDocument)
170 Markdown = await MarkdownDocument.GenerateMarkdown(true);
171 else
172 return new ContentResponse(new ArgumentException("Object not a markdown document.", nameof(Object)));
173
174 string ContentType;
175 byte[] Bin;
176
177 if (Encoding is null)
178 {
179 ContentType = "text/markdown; charset=utf-8";
180 Bin = Encoding.UTF8.GetBytes(Markdown);
181 }
182 else
183 {
184 ContentType = "text/markdown; charset=" + Encoding.WebName;
185 Bin = Encoding.GetBytes(Markdown);
186 }
187
188 return new ContentResponse(ContentType, Object, Bin);
189 }
190
197 public bool TryGetContentType(string FileExtension, out string ContentType)
198 {
199 switch (FileExtension.ToLower())
200 {
201 case "md":
202 case "markdown":
204 return true;
205
206 default:
207 ContentType = string.Empty;
208 return false;
209 }
210 }
211
218 public bool TryGetFileExtension(string ContentType, out string FileExtension)
219 {
220 switch (ContentType.ToLower())
221 {
223 FileExtension = "md";
224 return true;
225
226 default:
227 FileExtension = string.Empty;
228 return false;
229 }
230 }
231
237 public Grade Supports(Type ObjectType)
238 {
239 return ObjectType == typeof(MarkdownDocument) || ObjectType == typeof(MarkdownContent)
240 ? Grade.Excellent : Grade.NotAtAll;
241 }
242
249 public void Encode(object Object, int? Indent, StringBuilder Json)
250 {
251 string s;
252
254 s = MarkdownDocument.GenerateMarkdown(false).Result;
255 else if (Object is MarkdownContent MarkdownContent)
257 else
258 s = string.Empty;
259
260 Json.Append('"');
261 Json.Append(JSON.Encode(s));
262 Json.Append('"');
263 }
264
265 }
266}
Contains information about a response to a content request.
Static class managing encoding and decoding of internet content.
static bool IsAccepted(string ContentType, params string[] AcceptedContentTypes)
Checks if a given content type is acceptable.
Helps with common JSON-related tasks.
Definition: JSON.cs:16
static string Encode(string s)
Encodes a string for inclusion in JSON.
Definition: JSON.cs:537
bool Encodes(object Object, out Grade Grade, params string[] AcceptedContentTypes)
If the encoder encodes a given object.
static bool IsRawEncodingAllowed
If Raw encoding is allowed. Can be changed calling AllowRawEncoding(bool, bool).
static bool IsRawEncodingAllowedLocked
If the IsRawEncodingAllowed setting is locked.
bool TryGetContentType(string FileExtension, out string ContentType)
Tries to get the content type of an item, given its file extension.
bool TryGetFileExtension(string ContentType, out string FileExtension)
Tries to get the file extension of an item, given its Content-Type.
Grade Supports(Type ObjectType)
How well the JSON encoder encodes objects of type ObjectType .
bool Decodes(string ContentType, out Grade Grade)
If the decoder decodes an object with a given content type.
const string ContentType
Markdown content type.
string[] ContentTypes
Supported content types.
void Encode(object Object, int? Indent, StringBuilder Json)
Encodes the Object to JSON.
async Task< ContentResponse > EncodeAsync(object Object, Encoding Encoding, ICodecProgress Progress, params string[] AcceptedContentTypes)
Encodes an object.
static void AllowRawEncoding(bool Allow, bool Lock)
If raw encoding of web script should be allowed.
async Task< ContentResponse > DecodeAsync(string ContentType, byte[] Data, Encoding Encoding, KeyValuePair< string, string >[] Fields, Uri BaseUri, ICodecProgress Progress)
Decodes an object.
string[] FileExtensions
Supported file extensions.
MarkdownCodec()
Markdown encoder/decoder.
Class that can be used to encapsulate Markdown to be returned from a Web Service, bypassing any encod...
Contains a markdown document. This markdown document class supports original markdown,...
Task< string > GenerateMarkdown()
Generates Markdown from the markdown text.
static Task< MarkdownDocument > CreateAsync(string MarkdownText, params Type[] TransparentExceptionTypes)
Contains a markdown document. This markdown document class supports original markdown,...
Contains settings that the Markdown parser uses to customize its behavior.
Static class managing binary representations of strings.
Definition: Strings.cs:10
static string GetString(byte[] Data, int Offset, int Count, Encoding DefaultEncoding)
Gets a string from its binary representation, taking any Byte Order Mark (BOM) into account.
Definition: Strings.cs:148
Interface for reporting progress about an encoding or decoding.
Basic interface for Internet Content decoders. A class implementing this interface and having a defau...
Basic interface for Internet Content encoders. A class implementing this interface and having a defau...
Interface for encoding objects of certain types to JSON.
Definition: IJsonEncoder.cs:11
Grade
Grade enumeration
Definition: Grade.cs:7