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);
        }
Esempio n. 2
0
        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);
        }
Esempio n. 4
0
 public void Register(ContentFormatType contentFormat, string resourceName)
 {
     _registeredHighlighting.Add(contentFormat, new Lazy <IHighlightingDefinition>(() => LoadHighlighting(resourceName)));
 }
Esempio n. 5
0
 public ContentFormat(ContentFormatType type) : this()
 {
     MediaType = type;
 }
Esempio n. 6
0
 public ContentFormat() : base(optionNumber: RegisteredOptionNumber.ContentFormat, maxLength: 2, type: OptionType.UInt)
 {
     MediaType = ContentFormatType.TextPlain;
 }