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;
2using System.Collections.Generic;
3using System.Text;
4using System.Threading.Tasks;
6
8{
13 {
14 private static bool allowEncoding = true;
15 private static bool locked = false;
16
22 public static void AllowRawEncoding(bool Allow, bool Lock)
23 {
24 if (locked)
25 throw new InvalidOperationException("Setting has been locked.");
26
27 allowEncoding = Allow;
28 locked = Lock;
29 }
30
34 public static bool IsRawEncodingAllowed => allowEncoding;
35
39 public static bool IsRawEncodingAllowedLocked => locked;
40
45 {
46 }
47
51 public const string ContentType = "text/markdown";
52
56 public string[] ContentTypes => new string[] { ContentType };
57
61 public string[] FileExtensions
62 {
63 get
64 {
65 return new string[]
66 {
67 "md",
68 "markdown"
69 };
70 }
71 }
72
79 public bool Decodes(string ContentType, out Grade Grade)
80 {
81 if (Array.IndexOf(this.ContentTypes, ContentType) >= 0)
82 {
83 Grade = Grade.Excellent;
84 return true;
85 }
86 else
87 {
88 Grade = Grade.NotAtAll;
89 return false;
90 }
91 }
92
103 public async Task<object> DecodeAsync(string ContentType, byte[] Data, Encoding Encoding, KeyValuePair<string, string>[] Fields, Uri BaseUri)
104 {
105 string s = CommonTypes.GetString(Data, Encoding ?? Encoding.UTF8);
106
107 if (BaseUri is null)
108 return await MarkdownDocument.CreateAsync(s);
109 else
110 return await MarkdownDocument.CreateAsync(s, new MarkdownSettings(), string.Empty, string.Empty, BaseUri.ToString());
111 }
112
120 public bool Encodes(object Object, out Grade Grade, params string[] AcceptedContentTypes)
121 {
122 if (Object is MarkdownContent && InternetContent.IsAccepted(this.ContentTypes, AcceptedContentTypes))
123 {
124 Grade = Grade.Excellent;
125 return true;
126 }
127 else if (allowEncoding && Object is MarkdownDocument && InternetContent.IsAccepted(this.ContentTypes, AcceptedContentTypes))
128 {
129 Grade = Grade.Excellent;
130 return true;
131 }
132 else
133 {
134 Grade = Grade.NotAtAll;
135 return false;
136 }
137 }
138
147 public async Task<KeyValuePair<byte[], string>> EncodeAsync(object Object, Encoding Encoding, params string[] AcceptedContentTypes)
148 {
149 string Markdown;
150
151 if (Object is MarkdownContent Content)
152 Markdown = Content.Markdown;
153 else if (allowEncoding && Object is MarkdownDocument MarkdownDocument)
154 Markdown = await MarkdownDocument.GenerateMarkdown(true);
155 else
156 throw new ArgumentException("Object not a markdown document.", nameof(Object));
157
158 string ContentType;
159 byte[] Bin;
160
161 if (Encoding is null)
162 {
163 ContentType = "text/markdown; charset=utf-8";
164 Bin = Encoding.UTF8.GetBytes(Markdown);
165 }
166 else
167 {
168 ContentType = "text/markdown; charset=" + Encoding.WebName;
169 Bin = Encoding.GetBytes(Markdown);
170 }
171
172 return new KeyValuePair<byte[], string>(Bin, ContentType);
173 }
174
181 public bool TryGetContentType(string FileExtension, out string ContentType)
182 {
183 switch (FileExtension.ToLower())
184 {
185 case "md":
186 case "markdown":
188 return true;
189
190 default:
191 ContentType = string.Empty;
192 return false;
193 }
194 }
195
202 public bool TryGetFileExtension(string ContentType, out string FileExtension)
203 {
204 switch (ContentType.ToLower())
205 {
207 FileExtension = "md";
208 return true;
209
210 default:
211 FileExtension = string.Empty;
212 return false;
213 }
214 }
215
216 }
217}
Helps with parsing of commong data types.
Definition: CommonTypes.cs:13
static string GetString(byte[] Data, Encoding DefaultEncoding)
Gets a string from its binary representation, taking any Byte Order Mark (BOM) into account.
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.
bool Encodes(object Object, out Grade Grade, params string[] AcceptedContentTypes)
If the encoder encodes a given object.
async Task< object > DecodeAsync(string ContentType, byte[] Data, Encoding Encoding, KeyValuePair< string, string >[] Fields, Uri BaseUri)
Decodes an 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.
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.
async Task< KeyValuePair< byte[], string > > EncodeAsync(object Object, Encoding Encoding, params string[] AcceptedContentTypes)
Encodes an object.
static void AllowRawEncoding(bool Allow, bool Lock)
If raw encoding of web script should be allowed.
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.
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...
Grade
Grade enumeration
Definition: Grade.cs:7