public string ToStringDebug(int indent = 0) => MFormat.ToIndented(indent, "Typograph Data", Elements);
        /// <summary>
        /// Parses raw markup text into a fully formed TypographData.
        /// </summary>
        /// <exception cref="MarkupSyntaxError" />
        public static TypographData Load(string markupText, Localization localization = null, bool showLog = false)
        {
            string plainText = MatchAny.Replace(markupText, "");

            if (showLog)
            {
                Console.WriteLine("\nParsing from Markup\n");
                MFormat.PrintIndented(1, $"PlainText: {TextUtil.GetRepresentation(plainText)}");
                Console.WriteLine();
            }


            var elements = new List <Tuple <int, IElement> >();



            Dictionary <string, Stack <Match> > openSpans = new Dictionary <string, Stack <Match> >();

            if (showLog)
            {
                Console.WriteLine("  Span Matches");
            }

            foreach (Match m in MatchSpan.Matches(markupText))
            {
                if (!m.Groups["isclose"].Success)
                {
                    string type = m.Groups["otype"].Value;

                    if (openSpans.TryGetValue(type, out Stack <Match> stack))
                    {
                        openSpans[type].Push(m);
                    }
                    else
                    {
                        openSpans.Add(type, new Stack <Match>());
                        openSpans[type].Push(m);
                    }
                }
                else
                {
                    string type = m.Groups["ctype"].Value;

                    if (openSpans.TryGetValue(type, out Stack <Match> stack))
                    {
                        if (stack.Count > 0)
                        {
                            Match open = stack.Pop();

                            Span span = GetSpan(type,
                                                MatchAny.Replace(markupText.Substring(0, open.Index), "").Length,
                                                MatchAny.Replace(markupText.Substring(open.Index + open.Length, m.Index - open.Index - open.Length), "").Length,
                                                open.Groups["parameter"].Value);

                            if (span != null)
                            {
                                if (showLog)
                                {
                                    MFormat.PrintIndented(2, $"[{open.Index}] {span}");
                                }
                                elements.Add(new Tuple <int, IElement>(open.Index, span));
                            }

                            continue;
                        }
                    }

                    Console.WriteLine($"  [Warning] Unmatched close spans '{m.Value}'. Ignoring span.");
                }
            }

            if (openSpans.Any(x => x.Value.Count > 0))
            {
                Console.WriteLine("  [Warning] Unmatched open spans. Listing...");

                foreach (var item in openSpans)
                {
                    foreach (var m in item.Value)
                    {
                        Console.WriteLine($"    [{m.Index}] {item.Key}: {m.Value}");
                    }
                }
            }



            if (showLog)
            {
                Console.WriteLine("  Token Matches");
            }

            foreach (Match m in MatchToken.Matches(markupText))
            {
                Token token = GetToken(m.Groups["type"].Value,
                                       MatchAny.Replace(markupText.Substring(0, m.Index), "").Length,
                                       m.Groups["parameter"].Value);
                if (token != null)
                {
                    if (showLog)
                    {
                        MFormat.PrintIndented(2, $"[{m.Index}] {token}");
                    }
                    elements.Add(new Tuple <int, IElement>(m.Index, token));
                }
            }
            if (showLog)
            {
                Console.WriteLine();
            }

            elements.Sort();

            int digits = elements.Last().Item1.ToString().Length;

            if (showLog)
            {
                MFormat.PrintIndented(1, "Items", elements, x => $"({x.Item1.ToString().PadLeft(digits)}, {x.Item2})");
            }

            if (showLog)
            {
                Console.WriteLine("\nFinished Parsing Markup\n");
            }

            return(new TypographData(plainText, localization, elements.Select(x => x.Item2)));
        }
Exemplo n.º 3
0
 public string ListAllLocalizations()
 {
     return(MFormat.ToIndented(0, $"Library '{Name}' Localizations", localizations, x => $"{x.Value.Name.PadRight(12)}: parent -> '{x.Value.Parent}'"));
 }