/// <inheritdoc /> public override async Task <IDocument> ExecuteAsync(KeyValuePair <string, string>[] args, string content, IDocument document, IExecutionContext context) { IMetadataDictionary dictionary = args.ToDictionary( "Language", "Element", "HighlightJsFile"); HtmlParser parser = new HtmlParser(); using (IJavaScriptEnginePool enginePool = context.GetJavaScriptEnginePool(x => { if (dictionary.ContainsKey("HighlightJsFile")) { x.ExecuteFile(dictionary.GetString("HighlightJsFile")); } else { x.ExecuteResource("highlight-all.js", typeof(Statiq.Highlight.HighlightCode)); } })) { AngleSharp.Dom.IDocument htmlDocument = parser.Parse(string.Empty); AngleSharp.Dom.IElement element = htmlDocument.CreateElement(dictionary.GetString("Element", "code")); element.InnerHtml = content.Trim(); if (dictionary.ContainsKey("Language")) { element.SetAttribute("class", $"language-{dictionary.GetString("Language")}"); } Statiq.Highlight.HighlightCode.HighlightElement(enginePool, element); return(context.CreateDocument(await context.GetContentProviderAsync(element.OuterHtml))); } }
/// <inheritdoc /> public override ShortcodeResult Execute(KeyValuePair <string, string>[] args, string content, IDocument document, IExecutionContext context) { IMetadataDictionary arguments = args.ToDictionary(Id, Username, File); arguments.RequireKeys(Id); return($"<script src=\"//gist.github.com/{arguments.GetString(Username, x => x + "/")}{arguments.GetString(Id)}.js" + $"{arguments.GetString(File, x => "?file=" + x)}\" type=\"text/javascript\"></script>"); }
public override ShortcodeResult Execute(KeyValuePair <string, string>[] args, string content, IDocument document, IExecutionContext context) { IMetadataDictionary arguments = args.ToDictionary(Owner, Repo, PathFileName, RegionName); arguments.RequireKeys(Owner, Repo, PathFileName); content = GetContent(arguments.GetString(Owner), arguments.GetString(Repo), arguments.GetString(PathFileName), arguments.GetString(RegionName)).Result; return(content); }
/// <inheritdoc /> public override async Task <IDocument> ExecuteAsync(KeyValuePair <string, string>[] args, string content, IDocument document, IExecutionContext context) { IMetadataDictionary arguments = args.ToDictionary( "Id", "Username", "File"); arguments.RequireKeys("Id"); return(context.CreateDocument( await context.GetContentProviderAsync( $"<script src=\"//gist.github.com/{arguments.GetString("Username", x => x + "/")}{arguments.GetString("Id")}.js" + $"{arguments.GetString("File", x => "?file=" + x)}\" type=\"text/javascript\"></script>"))); }
/// <summary> /// Shortcodes for specific oEmbed services should override this method and call one of the other execute helper methods. /// </summary> public virtual async Task <ShortcodeResult> ExecuteAsync(KeyValuePair <string, string>[] args, IDocument document, IExecutionContext context) { IMetadataDictionary arguments = args.ToDictionary(Endpoint, Url, Format); arguments.RequireKeys(Endpoint, Url); return(await GetEmbedResultAsync( arguments.GetString(Endpoint), arguments.GetString(Url), arguments.ContainsKey(Format) ?new string[] { $"format={arguments.GetString(Format)}" } : null, context)); }
/// <inheritdoc /> public override async Task <IDocument> ExecuteAsync(KeyValuePair <string, string>[] args, string content, IDocument document, IExecutionContext context) { IMetadataDictionary arguments = args.ToDictionary( "Path", "IncludeHost", "Host", "Root", "Scheme", "UseHttps", "HideIndexPages", "HideExtensions", "Lowercase"); arguments.RequireKeys("Path"); string path = arguments.GetString("Path"); if (LinkGenerator.TryGetAbsoluteHttpUri(path, out string absoluteUri)) { return(context.CreateDocument(await context.GetContentProviderAsync(absoluteUri))); } FilePath filePath = new FilePath(path); // Use "Host" if it's provided, otherwise use Host setting if "IncludeHost" is true string host = arguments.GetString("Host", arguments.GetBool("IncludeHost") ? context.Settings.GetString(Keys.Host) : null); // Use "Root" if it's provided, otherwise LinkRoot setting DirectoryPath root = arguments.GetDirectoryPath("Root", context.Settings.GetDirectoryPath(Keys.LinkRoot)); // Use "Scheme" if it's provided, otherwise if "UseHttps" is true use "https" or use LinksUseHttps setting string scheme = arguments.GetString("Scheme", arguments.ContainsKey("UseHttps") ? (arguments.GetBool("UseHttps") ? "https" : null) : (context.Settings.GetBool(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.GetBool("HideIndexPages") ? LinkGenerator.DefaultHidePages : null) : (context.Settings.GetBool(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.GetBool("HideExtensions") ? LinkGenerator.DefaultHideExtensions : null) : (context.Settings.GetBool(Keys.LinkHideExtensions) ? LinkGenerator.DefaultHideExtensions : null); // If "Lowercase" is provided use that, otherwise use LinkLowercase setting bool lowercase = arguments.ContainsKey("Lowercase") ? arguments.GetBool("Lowercase") : context.Settings.GetBool(Keys.LinkLowercase); return(context.CreateDocument(await context.GetContentProviderAsync(LinkGenerator.GetLink(filePath, host, root, scheme, hidePages, hideExtensions, lowercase)))); }
/// <inheritdoc /> public override ShortcodeResult Execute(KeyValuePair <string, string>[] args, string content, IDocument document, IExecutionContext context) { IMetadataDictionary arguments = args.ToDictionary( Path, IncludeHost, Host, Root, Scheme, UseHttps, HideIndexPages, HideExtensions, Lowercase); arguments.RequireKeys(Path); string path = arguments.GetString(Path); if (LinkGenerator.TryGetAbsoluteHttpUri(path, out string absoluteUri)) { return(absoluteUri); } NormalizedPath filePath = new NormalizedPath(path); // Use "Host" if it's provided, otherwise use Host setting if "IncludeHost" is true string host = arguments.GetString(Host, arguments.GetBool(IncludeHost) ? context.Settings.GetString(Keys.Host) : null); // Use "Root" if it's provided, otherwise LinkRoot setting NormalizedPath root = arguments.GetPath(Root, context.Settings.GetPath(Keys.LinkRoot)); // Use "Scheme" if it's provided, otherwise if "UseHttps" is true use "https" or use LinksUseHttps setting string scheme = arguments.GetString(Scheme, arguments.ContainsKey(UseHttps) ? (arguments.GetBool(UseHttps) ? "https" : null) : (context.Settings.GetBool(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.GetBool(HideIndexPages) ? LinkGenerator.DefaultHidePages : null) : (context.Settings.GetBool(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.GetBool(HideExtensions) ? LinkGenerator.DefaultHideExtensions : null) : (context.Settings.GetBool(Keys.LinkHideExtensions) ? LinkGenerator.DefaultHideExtensions : null); // If "Lowercase" is provided use that, otherwise use LinkLowercase setting bool lowercase = arguments.ContainsKey(Lowercase) ? arguments.GetBool(Lowercase) : context.Settings.GetBool(Keys.LinkLowercase); return(LinkGenerator.GetLink(filePath, host, root, scheme, hidePages, hideExtensions, lowercase)); }
/// <summary> /// Shortcodes for specific oEmbed services should override this method and call one of the other execute helper methods. /// </summary> public override async Task <IDocument> ExecuteAsync(KeyValuePair <string, string>[] args, string content, IDocument document, IExecutionContext context) { IMetadataDictionary arguments = args.ToDictionary( "Endpoint", "Url", "Format"); arguments.RequireKeys("Endpoint", "Url"); return(await ExecuteAsync( arguments.GetString("Endpoint"), arguments.GetString("Url"), arguments.ContainsKey("Format") ?new string[] { $"format={arguments.GetString("Format")}" } : null, context)); }
public override async Task <IDocument> ExecuteAsync( KeyValuePair <string, string>[] args, string content, IDocument document, IExecutionContext context) { IMetadataDictionary dictionary = args.ToDictionary( "Key", "Value"); dictionary.RequireKeys("Key"); object keyValue = document.Get(dictionary.GetString("Key")); if (dictionary.ContainsKey("Value")) { return(TypeHelper.TryConvert(dictionary.Get("Value"), keyValue.GetType(), out object value) && (keyValue?.Equals(value) ?? (keyValue == null && value == null)) ? await document.CloneAsync(content) : null); } return(TypeHelper.TryConvert(keyValue, out bool result) && result ? await document.CloneAsync(content) : null); }
public override ShortcodeResult Execute(KeyValuePair <string, string>[] args, string content, IDocument document, IExecutionContext context) { IMetadataDictionary arguments = args.ToDictionary( Src, Style); var style = arguments.GetString(Style) ?? "container"; XElement container = new XElement( "div", new XAttribute("class", style)); XElement figure = new XElement( "figure"); // Image XElement image = arguments.XElement("img", Src, x => new[] { new XAttribute("src", context.GetLink(x)) }); if (image is object) { figure.Add(image); } // Caption if (content is object) { figure.Add(new XElement("figcaption", content)); } container.Add(figure); return(container.ToString()); }
public override async Task <IEnumerable <IDocument> > ExecuteAsync( KeyValuePair <string, string>[] args, string content, IDocument document, IExecutionContext context) { IMetadataDictionary dictionary = args.ToDictionary( "Key", "ValueKey", "IndexKey"); dictionary.RequireKeys("Key", "ValueKey"); string valueKey = dictionary.GetString("ValueKey"); if (string.IsNullOrEmpty(valueKey)) { throw new ShortcodeArgumentException("Invalid ValueKey"); } string indexKey = dictionary.GetString("IndexKey"); IReadOnlyList <object> items = document.GetList <object>(dictionary.GetString("Key")); if (items != null) { List <IDocument> results = new List <IDocument>(); int index = 0; foreach (object item in items) { MetadataItems metadata = new MetadataItems() { { valueKey, item } }; if (!string.IsNullOrEmpty(indexKey)) { metadata.Add(indexKey, index); } results.Add(await document.CloneAsync(metadata, content)); index++; } return(results); } return(null); }
public override IEnumerable <ShortcodeResult> Execute( KeyValuePair <string, string>[] args, string content, IDocument document, IExecutionContext context) { IMetadataDictionary dictionary = args.ToDictionary(Key, ValueKey, IndexKey); dictionary.RequireKeys(Key, ValueKey); string valueKey = dictionary.GetString(ValueKey); if (string.IsNullOrEmpty(valueKey)) { throw new ShortcodeArgumentException(ValueKey); } string indexKey = dictionary.GetString(IndexKey); IReadOnlyList <object> items = document.GetList <object>(dictionary.GetString(Key)); if (items != null) { List <ShortcodeResult> results = new List <ShortcodeResult>(); int index = 0; foreach (object item in items) { MetadataItems metadata = new MetadataItems() { { valueKey, item } }; if (!string.IsNullOrEmpty(indexKey)) { metadata.Add(indexKey, index); } results.Add(new ShortcodeResult(content, metadata)); index++; } return(results); } return(null); }
/// <inheritdoc /> public override async Task <IDocument> ExecuteAsync(KeyValuePair <string, string>[] args, string content, IDocument document, IExecutionContext context) { IMetadataDictionary arguments = args.ToDictionary( "Id", "HideMedia", "HideThread", "Theme", "OmitScript"); arguments.RequireKeys("Id"); // Create the url List <string> query = new List <string>(); if (arguments.GetBool("HideMedia")) { query.Add("hide_media=true"); } if (arguments.GetBool("HideThread")) { query.Add("hide_thread=true"); } if (arguments.ContainsKey("Theme")) { query.Add($"theme={arguments.GetString("theme")}"); } if (_omitScript || arguments.GetBool("OmitScript")) { query.Add("omit_script=true"); } // Omit the script on the next Twitter embed _omitScript = true; return(await ExecuteAsync("https://publish.twitter.com/oembed", $"https://twitter.com/username/status/{arguments.GetString("Id")}", query, context)); }
public override ShortcodeResult Execute( KeyValuePair <string, string>[] args, string content, IDocument document, IExecutionContext context) { IMetadataDictionary dictionary = args.ToDictionary(Key, Value); dictionary.RequireKeys(Key); object keyValue = document.Get(dictionary.GetString(Key)); if (dictionary.ContainsKey(Value)) { return(TypeHelper.TryConvert(dictionary.Get(Value), keyValue.GetType(), out object value) && (keyValue?.Equals(value) ?? (keyValue == null && value == null)) ? content : null); } return(TypeHelper.TryConvert(keyValue, out bool result) && result ? content : null); }
/// <inheritdoc /> public override async Task <ShortcodeResult> ExecuteAsync(KeyValuePair <string, string>[] args, IDocument document, IExecutionContext context) { IMetadataDictionary arguments = args.ToDictionary( Id, HideMedia, HideThread, Theme, OmitScript); arguments.RequireKeys(Id); // Create the url List <string> query = new List <string>(); if (arguments.GetBool(HideMedia)) { query.Add("hide_media=true"); } if (arguments.GetBool(HideThread)) { query.Add("hide_thread=true"); } if (arguments.ContainsKey(Theme)) { query.Add($"theme={arguments.GetString(Theme)}"); } if (_omitScript || arguments.GetBool(OmitScript)) { query.Add("omit_script=true"); } // Omit the script on the next Twitter embed _omitScript = true; return(await GetEmbedResultAsync("https://publish.twitter.com/oembed", $"https://twitter.com/username/status/{arguments.GetString(Id)}", query, context)); }
public override ShortcodeResult Execute(KeyValuePair <string, string>[] args, string content, IDocument document, IExecutionContext context) { IMetadataDictionary dictionary = args.ToDictionary(Configuration, PrependLinkRoot); return(RenderMarkdown.Render(context, dictionary.GetString(Configuration, RenderMarkdown.DefaultConfiguration), null, dictionary.GetBool(PrependLinkRoot), content)); }