private ShortcodeLocation GetShortcodeLocation(int firstIndex, string tagContent) { // Trim whitespace tagContent = tagContent.Trim(); if (tagContent.Length < 1) { throw new ShortcodeParserException("Shortcode must have a name"); } // Get the name and arguments string name; KeyValuePair <string, string>[] arguments; int nameLength = tagContent.IndexOf(' '); if (nameLength < 0) { name = tagContent; arguments = Array.Empty <KeyValuePair <string, string> >(); } else { name = tagContent.Substring(0, nameLength); arguments = ShortcodeHelper.SplitArguments(tagContent, nameLength + 1).ToArray(); } // Try to get the shortcode if (!_shortcodes.Contains(name)) { throw new ShortcodeParserException($"A shortcode with the name {name} was not found"); } return(new ShortcodeLocation(firstIndex, name, arguments)); }
public void ShouldSplitArguments(string arguments, string expectedKey, string expectedValue) { // Given, When KeyValuePair <string, string>[] result = ShortcodeHelper.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 = ShortcodeHelper.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 = ShortcodeHelper.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") }); }
/// <inheritdoc /> public override ShortcodeResult Execute(KeyValuePair <string, string>[] args, string content, IDocument document, IExecutionContext context) { IMetadataDictionary dictionary = args.ToDictionary( 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 ShortcodeHelper.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 ShortcodeHelper.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 ShortcodeHelper.SplitArguments(lines[line], 0).ToValueArray()) { row.Add(new XElement(th-- > 0 ? "th" : "td", col)); } } return(table.ToString()); }