Exemplo n.º 1
0
        /// <summary>
        /// Returns the markup representation of this style.
        /// </summary>
        /// <returns>The markup representation of this style.</returns>
        public string ToMarkup()
        {
            var builder = new List <string>();

            if (Decoration != Decoration.None)
            {
                var result = DecorationTable.GetMarkupNames(Decoration);
                if (result.Count != 0)
                {
                    builder.AddRange(result);
                }
            }

            if (Foreground != Color.Default)
            {
                builder.Add(Foreground.ToMarkup());
            }

            if (Background != Color.Default)
            {
                if (builder.Count == 0)
                {
                    builder.Add("default");
                }

                builder.Add("on " + Background.ToMarkup());
            }

            return(string.Join(" ", builder));
        }
Exemplo n.º 2
0
        private static Style?Parse(string text, out string?error)
        {
            var effectiveDecoration = (Decoration?)null;
            var effectiveForeground = (Color?)null;
            var effectiveBackground = (Color?)null;
            var effectiveLink       = (string?)null;

            var parts      = text.Split(new[] { ' ' });
            var foreground = true;

            foreach (var part in parts)
            {
                if (part.Equals("default", StringComparison.OrdinalIgnoreCase))
                {
                    continue;
                }

                if (part.Equals("on", StringComparison.OrdinalIgnoreCase))
                {
                    foreground = false;
                    continue;
                }

                if (part.StartsWith("link=", StringComparison.OrdinalIgnoreCase))
                {
                    if (effectiveLink != null)
                    {
                        error = "A link has already been set.";
                        return(null);
                    }

                    effectiveLink = part.Substring(5);
                    continue;
                }
                else if (part.StartsWith("link", StringComparison.OrdinalIgnoreCase))
                {
                    effectiveLink = Constants.EmptyLink;
                    continue;
                }

                var decoration = DecorationTable.GetDecoration(part);
                if (decoration != null)
                {
                    effectiveDecoration ??= Decoration.None;

                    effectiveDecoration |= decoration.Value;
                }
                else
                {
                    var color = ColorTable.GetColor(part);
                    if (color == null)
                    {
                        if (part.StartsWith("#", StringComparison.OrdinalIgnoreCase))
                        {
                            color = ParseHexColor(part, out error);
                            if (!string.IsNullOrWhiteSpace(error))
                            {
                                return(null);
                            }
                        }
                        else if (part.StartsWith("rgb", StringComparison.OrdinalIgnoreCase))
                        {
                            color = ParseRgbColor(part, out error);
                            if (!string.IsNullOrWhiteSpace(error))
                            {
                                return(null);
                            }
                        }
                        else
                        {
                            error = !foreground
                                ? $"Could not find color '{part}'."
                                : $"Could not find color or style '{part}'.";

                            return(null);
                        }
                    }

                    if (foreground)
                    {
                        if (effectiveForeground != null)
                        {
                            error = "A foreground color has already been set.";
                            return(null);
                        }

                        effectiveForeground = color;
                    }
                    else
                    {
                        if (effectiveBackground != null)
                        {
                            error = "A background color has already been set.";
                            return(null);
                        }

                        effectiveBackground = color;
                    }
                }
            }

            error = null;
            return(new Style(
                       effectiveForeground,
                       effectiveBackground,
                       effectiveDecoration,
                       effectiveLink));
        }