public void ShouldSplitArguments(string arguments, string expectedKey, string expectedValue)
            {
                // Given, When
                KeyValuePair <string, string>[] result = ShortcodeParser.SplitArguments(arguments, 0).ToArray();

                // Then
                result.ShouldBe(new KeyValuePair <string, string>[]
                {
                    new KeyValuePair <string, string>(expectedKey, expectedValue)
                });
            }
            public void ShouldIgnoreLeadingAndTrailingWhiteSpace()
            {
                // Given, When
                KeyValuePair <string, string>[] result = ShortcodeParser.SplitArguments("  foo  fizz=buzz  ", 0).ToArray();

                // Then
                result.ShouldBe(new KeyValuePair <string, string>[]
                {
                    new KeyValuePair <string, string>(null, "foo"),
                    new KeyValuePair <string, string>("fizz", "buzz")
                });
            }
            public void ShouldSplitComplexArguments()
            {
                // Given, When
                KeyValuePair <string, string>[] result = ShortcodeParser.SplitArguments("foo \"abc 123\" fizz=buzz  \"qwe\"=\"try\"\r\nxyz=\"zyx\"  \"678=987\" goo=boo", 0).ToArray();

                // Then
                result.ShouldBe(new KeyValuePair <string, string>[]
                {
                    new KeyValuePair <string, string>(null, "foo"),
                    new KeyValuePair <string, string>(null, "abc 123"),
                    new KeyValuePair <string, string>("fizz", "buzz"),
                    new KeyValuePair <string, string>("qwe", "try"),
                    new KeyValuePair <string, string>("xyz", "zyx"),
                    new KeyValuePair <string, string>(null, "678=987"),
                    new KeyValuePair <string, string>("goo", "boo")
                });
            }
Esempio n. 4
0
        /// <inheritdoc />
        public IShortcodeResult Execute(KeyValuePair <string, string>[] args, string content, IDocument document, IExecutionContext context)
        {
            ConvertingDictionary dictionary = args.ToDictionary(
                context,
                "Class",
                "HeaderRows",
                "FooterRows",
                "HeaderCols",
                "HeaderClass",
                "BodyClass",
                "FooterClass");

            string[] lines = content
                             .Trim()
                             .Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries)
                             .Select(x => x.Trim())
                             .ToArray();

            // Table
            XElement table = new XElement(
                "table",
                dictionary.XAttribute("class"));
            int line = 0;

            // Header
            int      headerRows = dictionary.Get("HeaderRows", 0);
            XElement header     = null;

            for (int c = 0; c < headerRows && line < lines.Length; c++, line++)
            {
                // Create the section
                if (c == 0)
                {
                    header = new XElement(
                        "thead",
                        dictionary.XAttribute("class", "HeaderClass"));
                    table.Add(header);
                }

                // Create the current row
                XElement row = new XElement("tr");
                header.Add(row);

                // Add the columns
                foreach (string col in ShortcodeParser.SplitArguments(lines[line], 0).ToValueArray())
                {
                    row.Add(new XElement("th", col));
                }
            }

            // Body
            int      bodyRows = lines.Length - line - dictionary.Get("FooterRows", 0);
            XElement body     = null;

            for (int c = 0; c < bodyRows && line < lines.Length; c++, line++)
            {
                // Create the section
                if (c == 0)
                {
                    body = new XElement(
                        "tbody",
                        dictionary.XAttribute("class", "BodyClass"));
                    table.Add(body);
                }

                // Create the current row
                XElement row = new XElement("tr");
                body.Add(row);

                // Add the columns
                int th = dictionary.Get("HeaderCols", 0);
                foreach (string col in ShortcodeParser.SplitArguments(lines[line], 0).ToValueArray())
                {
                    row.Add(new XElement(th-- > 0 ? "th" : "td", col));
                }
            }

            // Footer
            XElement footer = null;

            for (int c = 0; line < lines.Length; c++, line++)
            {
                // Create the section
                if (c == 0)
                {
                    footer = new XElement(
                        "tfoot",
                        dictionary.XAttribute("class", "FooterClass"));
                    table.Add(footer);
                }

                // Create the current row
                XElement row = new XElement("tr");
                footer.Add(row);

                // Add the columns
                int th = dictionary.Get("HeaderCols", 0);
                foreach (string col in ShortcodeParser.SplitArguments(lines[line], 0).ToValueArray())
                {
                    row.Add(new XElement(th-- > 0 ? "th" : "td", col));
                }
            }

            return(context.GetShortcodeResult(table.ToString()));
        }