public override ShortcodeResult Execute(KeyValuePair <string, string>[] args, string content, IDocument document, IExecutionContext context) { IMetadataDictionary arguments = args.ToDictionary( Src, Link, Target, Rel, Alt, Class, Height, Width); XElement figure = new XElement( "figure", arguments.XAttribute(Class)); // Image link XElement imageLink = arguments.XElement("a", Link, x => new[] { new XAttribute("href", context.GetLink(x)), arguments.XAttribute(Target), arguments.XAttribute(Rel) }); // Image XElement image = arguments.XElement("img", Src, x => new[] { new XAttribute("src", context.GetLink(x)), arguments.XAttribute(Alt), arguments.XAttribute(Height), arguments.XAttribute(Width) }); if (imageLink != null && image != null) { imageLink.Add(image); figure.Add(imageLink); } else if (image != null) { figure.Add(image); } // Caption if (content != null) { figure.Add(new XElement("figcaption", content)); } return(figure.ToString()); }
/// <inheritdoc /> public override async Task <IDocument> ExecuteAsync(KeyValuePair <string, string>[] args, string content, IDocument document, IExecutionContext context) { IMetadataDictionary arguments = args.ToDictionary( "Src", "Link", "Target", "Rel", "Alt", "Class", "Height", "Width"); XElement figure = new XElement( "figure", arguments.XAttribute("class")); // Image link XElement imageLink = arguments.XElement("a", "link", x => new[] { new XAttribute("href", context.GetLink(x)), arguments.XAttribute("target"), arguments.XAttribute("rel") }); // Image XElement image = arguments.XElement("img", "src", x => new[] { new XAttribute("src", context.GetLink(x)), arguments.XAttribute("alt"), arguments.XAttribute("height"), arguments.XAttribute("width") }); if (imageLink != null && image != null) { imageLink.Add(image); figure.Add(imageLink); } else if (image != null) { figure.Add(image); } // Caption if (content != null) { figure.Add(new XElement("figcaption", content)); } return(context.CreateDocument(await context.GetContentProviderAsync(figure.ToString()))); }
/// <inheritdoc /> public override async Task <IDocument> ExecuteAsync(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 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.CreateDocument(await context.GetContentProviderAsync(table.ToString()))); }