public static byte[] RemoveFormat(string payload, ContentFormatType contentFormat) { if (string.IsNullOrEmpty(payload) || contentFormat == null) { return(null); } try { if (contentFormat.Value == ContentFormatType.TextPlain.Value) { return(Encoding.UTF8.GetBytes(payload)); } if (contentFormat.Value == ContentFormatType.ApplicationJson.Value) { return(Encoding.UTF8.GetBytes(JToken.Parse(payload).ToString())); } if (contentFormat.Value == ContentFormatType.ApplicationLinkFormat.Value) { return(Encoding.UTF8.GetBytes(payload.Replace("\n,", ",").Replace("\r", ""))); } } catch (JsonReaderException ex) { throw new FormattedTextException(ex.Message, ex.LineNumber, ex.LinePosition, ex); } return(null); }
public IHighlightingDefinition GetDefinition(ContentFormatType contentFormat) { if (contentFormat is null) { return(null); } if (!_registeredHighlighting.TryGetValue(contentFormat, out var definition)) { return(null); } try { return(definition.Value); } catch (HighlightingDefinitionInvalidException ex) { throw new InvalidOperationException($"The highlighting for '{contentFormat}' is invalid.", ex); } }
public static string Format(byte[] payload, ContentFormatType contentFormat) { if (payload == null || contentFormat == null) { return(string.Empty); } if (payload.Length == 0) { return(string.Empty); } try { if (contentFormat.Value == ContentFormatType.TextPlain.Value) { return(Encoding.UTF8.GetString(payload)); } if (contentFormat.Value == ContentFormatType.ApplicationJson.Value) { return(JToken.Parse(Encoding.UTF8.GetString(payload)).ToString(Newtonsoft.Json.Formatting.Indented)); } // TODO: Don't simply replace all commas with new line. Need to be context aware to ensure we're not splitting a link format in two. if (contentFormat.Value == ContentFormatType.ApplicationLinkFormat.Value) { return(Encoding.UTF8.GetString(payload).Replace(",", ",\r\n")); } } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex); #if DEBUG return(ex.ToString()); #endif } return(string.Empty); }
public void Register(ContentFormatType contentFormat, string resourceName) { _registeredHighlighting.Add(contentFormat, new Lazy <IHighlightingDefinition>(() => LoadHighlighting(resourceName))); }
public ContentFormat(ContentFormatType type) : this() { MediaType = type; }
public ContentFormat() : base(optionNumber: RegisteredOptionNumber.ContentFormat, maxLength: 2, type: OptionType.UInt) { MediaType = ContentFormatType.TextPlain; }