protected override void Write(HtmlRenderer renderer, CodeSnippet codeSnippet) { var(content, codeSnippetPath) = _context.ReadFile(codeSnippet.CodePath, codeSnippet); if (content == null) { _context.LogWarning("codesnippet-not-found", $"Invalid code snippet link: '{codeSnippet.CodePath}'.", codeSnippet); renderer.Write(GetWarning()); return; } codeSnippet.SetAttributeString(); renderer.Write("<pre><code").WriteAttributes(codeSnippet).Write(">"); renderer.WriteEscape(GetContent(content, codeSnippet)); renderer.Write("</code></pre>"); }
protected override void Write(HtmlRenderer renderer, CodeSnippet codeSnippet) { var(content, codeSnippetPath) = _context.ReadFile(codeSnippet.CodePath, InclusionContext.File, codeSnippet); if (content == null) { _context.LogWarning("codesnippet-not-found", $"Cannot resolve '{codeSnippet.CodePath}' relative to '{InclusionContext.File}'.", codeSnippet); renderer.Write(GetWarning()); return; } codeSnippet.SetAttributeString(); renderer.Write("<pre><code").WriteAttributes(codeSnippet).Write(">"); renderer.WriteEscape(GetContent(content, codeSnippet)); renderer.Write("</code></pre>"); }
protected override void Write(HtmlRenderer renderer, InclusionBlock inclusion) { var(content, includeFilePath) = _context.ReadFile(inclusion.IncludedFilePath, InclusionContext.File); if (content == null) { _context.LogWarning("include-not-found", $"Cannot resolve '{inclusion.IncludedFilePath}' relative to '{InclusionContext.File}'."); renderer.Write(inclusion.GetRawToken()); return; } if (InclusionContext.IsCircularReference(includeFilePath, out var dependencyChain)) { _context.LogWarning("circular-reference", $"Found circular reference: {string.Join(" --> ", dependencyChain)} --> {includeFilePath}"); renderer.Write(inclusion.GetRawToken()); return; } using (InclusionContext.PushFile(includeFilePath)) { renderer.Write(Markdown.ToHtml(content, _pipeline)); } }
protected override void Write(HtmlRenderer renderer, InclusionBlock inclusion) { var(content, includeFilePath) = _context.ReadFile(inclusion.IncludedFilePath, InclusionContext.File, inclusion); if (content == null) { _context.LogWarning("include-not-found", $"Cannot resolve '{inclusion.IncludedFilePath}' relative to '{InclusionContext.File}'.", inclusion); renderer.Write(inclusion.GetRawToken()); return; } if (InclusionContext.IsCircularReference(includeFilePath, out var dependencyChain)) { _context.LogWarning("circular-reference", $"Build has identified file(s) referencing each other: {string.Join(" --> ", dependencyChain.Select(file => $"'{file}'"))} --> '{includeFilePath}'", inclusion); renderer.Write(inclusion.GetRawToken()); return; } using (InclusionContext.PushInclusion(includeFilePath)) { renderer.Write(Markdown.ToHtml(content, _pipeline)); } }
public bool Render(HtmlRenderer renderer, MarkdownObject markdownObject, Action <string> logWarning) { var block = (TripleColonBlock)markdownObject; block.Attributes.TryGetValue("id", out var currentId); //it's okay if this is null block.Attributes.TryGetValue("range", out var currentRange); //it's okay if this is null block.Attributes.TryGetValue("source", out var currentSource); //source has already been checked above var(code, codePath) = _context.ReadFile(currentSource, block); if (string.IsNullOrEmpty(code)) { logWarning($"The code snippet \"{currentSource}\" could not be found."); return(false); } //var updatedCode = GetCodeSnippet(currentRange, currentId, code, logError).TrimEnd(); var htmlCodeSnippetRenderer = new HtmlCodeSnippetRenderer(_context); var snippet = new CodeSnippet(null); snippet.CodePath = currentSource; snippet.TagName = currentId; HtmlCodeSnippetRenderer.TryGetLineRanges(currentRange, out var ranges); snippet.CodeRanges = ranges; var updatedCode = htmlCodeSnippetRenderer.GetContent(code, snippet); updatedCode = ExtensionsHelper.Escape(updatedCode).TrimEnd(); if (updatedCode == string.Empty) { logWarning($"It looks like your code snippet was not rendered. Try range instead."); return(false); } renderer.WriteLine("<pre>"); renderer.Write("<code").WriteAttributes(block).Write(">"); renderer.WriteLine(updatedCode); renderer.WriteLine("</code></pre>"); return(true); }
public bool TryProcessAttributes(IDictionary <string, string> attributes, out HtmlAttributes htmlAttributes, out IDictionary <string, string> renderProperties, Action <string> logError, BlockProcessor processor) { htmlAttributes = null; renderProperties = new Dictionary <string, string>(); var source = string.Empty; var range = string.Empty; var id = string.Empty; var highlight = string.Empty; var language = string.Empty; var interactive = string.Empty; foreach (var attribute in attributes) { var name = attribute.Key; var value = attribute.Value; switch (name) { case "source": source = value; break; case "range": range = value; break; case "id": id = value; break; case "highlight": highlight = value; break; case "language": language = value; break; case "interactive": interactive = value; break; default: logError($"Unexpected attribute \"{name}\"."); return(false); } } if (string.IsNullOrEmpty(source)) { logError("source is a required attribute. Please ensure you have specified a source attribute"); return(false); } if (string.IsNullOrEmpty(language)) { language = InferLanguageFromFile(source, logError); } htmlAttributes = new HtmlAttributes(); htmlAttributes.AddProperty("class", $"lang-{language}"); if (!string.IsNullOrEmpty(interactive)) { htmlAttributes.AddProperty("data-interactive", language); htmlAttributes.AddProperty("data-interactive-mode", interactive); } if (!string.IsNullOrEmpty(highlight)) { htmlAttributes.AddProperty("highlight-lines", highlight); } RenderDelegate = (renderer, obj) => { var currentId = string.Empty; var currentRange = string.Empty; var currentSource = string.Empty; obj.Attributes.TryGetValue("id", out currentId); //it's okay if this is null obj.Attributes.TryGetValue("range", out currentRange); //it's okay if this is null obj.Attributes.TryGetValue("source", out currentSource); //source has already been checked above var(code, codePath) = _context.ReadFile(currentSource, InclusionContext.File, obj); if (string.IsNullOrEmpty(code)) { logError($"The code snippet \"{currentSource}\" could not be found."); return(false); } var updatedCode = GetCodeSnippet(currentRange, currentId, code, logError).TrimEnd(); if (updatedCode == string.Empty) { return(false); } renderer.WriteLine("<pre>"); renderer.Write("<code").WriteAttributes(obj).Write(">"); renderer.WriteLine(updatedCode); renderer.WriteLine("</code></pre>"); return(true); }; return(true); }