Пример #1
0
        public void DDocMacros()
        {
            var frstParam  = "a\"s\"";
            var scndParam  = " ((d)('\\'')) ";
            var thirdParam = " hehe";

            var ddoc = "asdf $(NAME) yeah $(D " + frstParam + "," + scndParam + "," + thirdParam + ")";

            int    macroStart;
            int    macroLength;
            string macroName;
            Dictionary <string, string> parameters;

            DDocParser.FindNextMacro(ddoc, 0, out macroStart, out macroLength, out macroName, out parameters);

            Assert.That(macroStart, Is.EqualTo(5));
            Assert.That(macroLength, Is.EqualTo(7));
            Assert.That(macroName, Is.EqualTo("NAME"));
            Assert.That(parameters, Is.Null);

            DDocParser.FindNextMacro(ddoc, macroStart + macroLength, out macroStart, out macroLength, out macroName, out parameters);

            Assert.That(macroName, Is.EqualTo("D"));
            Assert.That(parameters, Is.Not.Null);

            Assert.That(parameters["$0"], Is.EqualTo(frstParam + "," + scndParam + "," + thirdParam));
            Assert.That(parameters["$1"], Is.EqualTo(frstParam));
            Assert.That(parameters["$2"], Is.EqualTo(scndParam));
            Assert.That(parameters["$3"], Is.EqualTo(thirdParam));
            Assert.That(parameters["$+"], Is.EqualTo(scndParam + "," + thirdParam));
        }
        public string DDocToMarkup(string ddoc)
        {
            if (ddoc == null)
            {
                return(string.Empty);
            }

            var sb = new StringBuilder(ddoc.Length);
            int i = 0, len = 0;

            while (i < ddoc.Length)
            {
                string macroName;
                Dictionary <string, string> parameters;
                var k = i + len;

                DDocParser.FindNextMacro(ddoc, i + len, out i, out len, out macroName, out parameters);

                if (i < 0)
                {
                    i = k;
                    break;
                }

                while (k < i)
                {
                    sb.Append(ddoc[k++]);
                }

                var firstParam = parameters != null ? parameters["$0"] : null;

                //TODO: Have proper macro infrastructure
                switch (macroName)
                {
                case "I":
                    if (firstParam != null)
                    {
                        AppendFormat(DDocToMarkup(firstParam), sb, FormatFlags.Italic);
                    }
                    break;

                case "U":
                    if (firstParam != null)
                    {
                        AppendFormat(DDocToMarkup(firstParam), sb, FormatFlags.Underline);
                    }
                    break;

                case "B":
                    if (firstParam != null)
                    {
                        AppendFormat(DDocToMarkup(firstParam), sb, FormatFlags.Bold);
                    }
                    break;

                case "D_CODE":
                case "D":
                    if (firstParam != null)
                    {
                        sb.Append(DCodeToMarkup(DDocToMarkup(firstParam)));
                    }
                    break;

                case "BR":
                    sb.AppendLine();
                    break;

                case "RED":
                    if (firstParam != null)
                    {
                        AppendFormat(DDocToMarkup(firstParam), sb, FormatFlags.Color, 1.0);
                    }
                    break;

                case "BLUE":
                    if (firstParam != null)
                    {
                        AppendFormat(DDocToMarkup(firstParam), sb, FormatFlags.Color, 0, 0, 1.0);
                    }
                    break;

                case "GREEN":
                    if (firstParam != null)
                    {
                        AppendFormat(DDocToMarkup(firstParam), sb, FormatFlags.Color, 0, 1, 0);
                    }
                    break;

                case "YELLOW":
                    if (firstParam != null)
                    {
                        AppendFormat(DDocToMarkup(firstParam), sb, FormatFlags.Color, 1, 1, 0);
                    }
                    break;

                case "BLACK":
                    if (firstParam != null)
                    {
                        AppendFormat(DDocToMarkup(firstParam), sb, FormatFlags.Color);
                    }
                    break;

                case "WHITE":
                    if (firstParam != null)
                    {
                        AppendFormat(DDocToMarkup(firstParam), sb, FormatFlags.Color, 1, 1, 1);
                    }
                    break;

                default:
                    if (firstParam != null)
                    {
                        sb.Append(DDocToMarkup(firstParam));
                    }
                    break;
                }
            }

            while (i < ddoc.Length)
            {
                sb.Append(ddoc[i++]);
            }

            return(sb.ToString());
        }