示例#1
0
        /// <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));
            }
        }
示例#2
0
        /// <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)));
            }
        }