/// <inheritdoc /> public IShortcodeResult Execute(KeyValuePair <string, string>[] args, string content, IDocument document, IExecutionContext context) { ConvertingDictionary dictionary = args.ToDictionary( context, "Language", "Element", "HighlightJsFile"); HtmlParser parser = new HtmlParser(); using (IJavaScriptEnginePool enginePool = context.GetJavaScriptEnginePool(x => { if (dictionary.ContainsKey("HighlightJsFile")) { x.ExecuteFile(dictionary.String("HighlightJsFile")); } else { x.ExecuteResource("highlight-all.js", typeof(Wyam.Highlight.Highlight)); } })) { AngleSharp.Dom.IDocument htmlDocument = parser.Parse(string.Empty); AngleSharp.Dom.IElement element = htmlDocument.CreateElement(dictionary.String("Element", "code")); element.InnerHtml = content.Trim(); if (dictionary.ContainsKey("Language")) { element.SetAttribute("class", $"language-{dictionary.String("Language")}"); } Wyam.Highlight.Highlight.HighlightElement(enginePool, element); return(context.GetShortcodeResult(element.OuterHtml)); } }
/// <summary> /// Converts the shortcode arguments into a dictionary of named parameters. /// This will match un-named positional parameters with their expected position /// after which named parameters will be included. If an un-named positional /// parameter follows named parameters and exception will be thrown. /// </summary> /// <param name="args">The original shortcode arguments.</param> /// <param name="context">The current execution context.</param> /// <param name="keys">The parameter names in expected order.</param> /// <returns>A dictionary containing the parameters and their values.</returns> public static ConvertingDictionary ToDictionary(this KeyValuePair <string, string>[] args, IExecutionContext context, params string[] keys) { ConvertingDictionary dictionary = new ConvertingDictionary(context); bool nullKeyAllowed = true; for (int c = 0; c < args.Length; c++) { if (string.IsNullOrWhiteSpace(args[c].Key)) { if (!nullKeyAllowed) { throw new ShortcodeArgumentException("Unexpected positional shortcode argument"); } dictionary.Add(keys[c], args[c].Value); } else { if (dictionary.ContainsKey(args[c].Key)) { throw new ShortcodeArgumentException("Duplicate shortcode parameter name", args[c].Key); } dictionary.Add(args[c].Key, args[c].Value); nullKeyAllowed = false; } } return(dictionary); }
public void MatchesPositionalAndNamedArguments() { // Given TestExecutionContext context = new TestExecutionContext(); KeyValuePair <string, string>[] args = new KeyValuePair <string, string>[] { new KeyValuePair <string, string>(null, "1"), new KeyValuePair <string, string>("C", "3"), new KeyValuePair <string, string>("B", "2") }; // When ConvertingDictionary dictionary = args.ToDictionary(context, "A", "B", "C"); // Then dictionary.ShouldBe( new KeyValuePair <string, object>[] { new KeyValuePair <string, object>("A", "1"), new KeyValuePair <string, object>("B", "2"), new KeyValuePair <string, object>("C", "3") }, true); }
/// <inheritdoc /> public IShortcodeResult Execute(KeyValuePair <string, string>[] args, string content, IDocument document, IExecutionContext context) { ConvertingDictionary arguments = args.ToDictionary( context, "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.GetShortcodeResult(figure.ToString())); }
/// <inheritdoc /> public IShortcodeResult Execute(KeyValuePair <string, string>[] args, string content, IDocument document, IExecutionContext context) { ConvertingDictionary arguments = args.ToDictionary( context, "Path", "IncludeHost", "Host", "Root", "Scheme", "UseHttps", "HideIndexPages", "HideExtensions", "Lowercase"); arguments.RequireKeys("Path"); string path = arguments.String("Path"); if (LinkGenerator.TryGetAbsoluteHttpUri(path, out string absoluteUri)) { return(context.GetShortcodeResult(absoluteUri)); } FilePath filePath = new FilePath(path); // Use "Host" if it's provided, otherwise use Host setting if "IncludeHost" is true string host = arguments.String("Host", arguments.Bool("IncludeHost") ? context.String(Keys.Host) : null); // Use "Root" if it's provided, otherwise LinkRoot setting DirectoryPath root = arguments.DirectoryPath("Root", context.DirectoryPath(Keys.LinkRoot)); // Use "Scheme" if it's provided, otherwise if "UseHttps" is true use "https" or use LinksUseHttps setting string scheme = arguments.String("Scheme", arguments.ContainsKey("UseHttps") ? (arguments.Bool("UseHttps") ? "https" : null) : (context.Bool(Keys.LinksUseHttps) ? "https" : null)); // If "HideIndexPages" is provided and true use default hide pages, otherwise use default hide pages if LinkHideIndexPages is true string[] hidePages = arguments.ContainsKey("HideIndexPages") ? (arguments.Bool("HideIndexPages") ? LinkGenerator.DefaultHidePages : null) : (context.Bool(Keys.LinkHideIndexPages) ? LinkGenerator.DefaultHidePages : null); // If "HideExtensions" is provided and true use default hide extensions, otherwise use default hide extensions if LinkHideExtensions is true string[] hideExtensions = arguments.ContainsKey("HideExtensions") ? (arguments.Bool("HideExtensions") ? LinkGenerator.DefaultHideExtensions : null) : (context.Bool(Keys.LinkHideExtensions) ? LinkGenerator.DefaultHideExtensions : null); // If "Lowercase" is provided use that, otherwise use LinkLowercase setting bool lowercase = arguments.ContainsKey("Lowercase") ? arguments.Bool("Lowercase") : context.Bool(Keys.LinkLowercase); return(context.GetShortcodeResult(LinkGenerator.GetLink(filePath, host, root, scheme, hidePages, hideExtensions, lowercase))); }
/// <inheritdoc /> public IShortcodeResult Execute(KeyValuePair <string, string>[] args, string content, IDocument document, IExecutionContext context) { ConvertingDictionary arguments = args.ToDictionary( context, "Id", "Username", "File"); arguments.RequireKeys("Id"); return(context.GetShortcodeResult( $"<script src=\"//gist.github.com/{arguments.String("Username", x => x + "/")}{arguments.String("Id")}.js" + $"{arguments.String("File", x => "?file=" + x)}\" type=\"text/javascript\"></script>")); }
/// <inheritdoc /> public virtual IShortcodeResult Execute(KeyValuePair <string, string>[] args, string content, IDocument document, IExecutionContext context) { ConvertingDictionary arguments = args.ToDictionary( context, "Endpoint", "Url", "Format"); arguments.RequireKeys("Endpoint", "Url"); return(Execute( arguments.String("Endpoint"), arguments.String("Url"), arguments.ContainsKey("Format") ? new string[] { $"format={arguments.String("Format")}" } : null, context)); }
/// <inheritdoc /> public override IShortcodeResult Execute(KeyValuePair <string, string>[] args, string content, IDocument document, IExecutionContext context) { ConvertingDictionary arguments = args.ToDictionary( context, "Id", "HideMedia", "HideThread", "Theme", "OmitScript"); arguments.RequireKeys("Id"); // Create the url List <string> query = new List <string>(); if (arguments.Bool("HideMedia")) { query.Add("hide_media=true"); } if (arguments.Bool("HideThread")) { query.Add("hide_thread=true"); } if (arguments.ContainsKey("Theme")) { query.Add($"theme={arguments.String("theme")}"); } if (_omitScript || arguments.Bool("OmitScript")) { query.Add("omit_script=true"); } // Omit the script on the next Twitter embed _omitScript = true; return(Execute("https://publish.twitter.com/oembed", $"https://twitter.com/username/status/{arguments.String("Id")}", query, context)); }
/// <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())); }