public virtual StringBuffer Render(DfmEngine engine, DfmFencesBlockToken token, MarkdownBlockContext context) { var lang = string.IsNullOrEmpty(token.Lang) ? null : $" class=\"language-{token.Lang}\""; var name = string.IsNullOrEmpty(token.Name) ? null : $" name=\"{StringHelper.HtmlEncode(token.Name)}\""; var title = string.IsNullOrEmpty(token.Title) ? null : $" title=\"{StringHelper.HtmlEncode(token.Title)}\""; if (!PathUtility.IsRelativePath(token.Path)) { string errorMessage = $"Code absolute path: {token.Path} is not supported in file {engine.Parents.Peek()}"; Logger.LogError(errorMessage); return($"<!-- {StringHelper.HtmlEncode(errorMessage)} -->"); } try { // TODO: Valid REST and REST-i script. var fencesPath = ((RelativePath)token.Path).BasedOn((RelativePath)engine.Parents.Peek()); var fencesCode = File.ReadAllText(fencesPath); return($"<pre><code{lang}{name}{title}>{StringHelper.HtmlEncode(fencesCode)}\n</code></pre>"); } catch (FileNotFoundException) { string errorMessage = $"Can not find reference {token.Path}"; Logger.LogError(errorMessage); return($"<!-- {StringHelper.HtmlEncode(errorMessage)} -->"); } }
public virtual StringBuffer Render(DfmEngine engine, DfmFencesBlockToken token, MarkdownBlockContext context) { var lang = string.IsNullOrEmpty(token.Lang) ? null : $" class=\"language-{token.Lang}\""; var name = string.IsNullOrEmpty(token.Name) ? null : $" name=\"{StringHelper.HtmlEncode(token.Name)}\""; var title = string.IsNullOrEmpty(token.Title) ? null : $" title=\"{StringHelper.HtmlEncode(token.Title)}\""; if (!PathUtility.IsRelativePath(token.Path)) { string errorMessage = $"Code absolute path: {token.Path} is not supported in file {engine.Parents.Peek()}"; Logger.LogError(errorMessage); return $"<!-- {StringHelper.HtmlEncode(errorMessage)} -->"; } try { // TODO: Valid REST and REST-i script. var fencesPath = ((RelativePath)token.Path).BasedOn((RelativePath)engine.Parents.Peek()); var fencesCode = File.ReadAllText(fencesPath); return $"<pre><code{lang}{name}{title}>{StringHelper.HtmlEncode(fencesCode)}\n</code></pre>"; } catch(FileNotFoundException) { string errorMessage = $"Can not find reference {token.Path}"; Logger.LogError(errorMessage); return $"<!-- {StringHelper.HtmlEncode(errorMessage)} -->"; } }
public DfmExtractCodeResult ExtractFencesCode(DfmFencesBlockToken token, string fencesPath) { if (token == null) { throw new ArgumentNullException(nameof(token)); } if (string.IsNullOrEmpty(fencesPath)) { throw new ArgumentNullException(nameof(fencesPath)); } var fencesCode = File.ReadAllLines(fencesPath); // NOTE: Parsing language and removing comment lines only do for tag name representation if (token.PathQueryOption?.TagName != null) { var lang = GetCodeLanguageOrExtension(token); Regex regex; if (!CodeLanguageRegexes.TryGetValue(lang, out regex)) { string errorMessage = $"{lang} is not supported languaging name, alias or extension for parsing code snippet with tag name, you can use line numbers instead"; Logger.LogError(errorMessage); return(new DfmExtractCodeResult { IsSuccessful = false, ErrorMessage = errorMessage, FencesCodeLines = fencesCode }); } var resolveResult = ResolveTagNamesFromPath(fencesPath, fencesCode, token.PathQueryOption.TagName, regex); if (!resolveResult.IsSuccessful) { Logger.LogError(resolveResult.ErrorMessage); return(new DfmExtractCodeResult { IsSuccessful = false, ErrorMessage = resolveResult.ErrorMessage, FencesCodeLines = fencesCode }); } return(GetFencesCodeCore(fencesCode, resolveResult.StartLine, resolveResult.EndLine, resolveResult.ExcludesLines)); } else { // line range check only need to be done for line number representation string errorMessage; if (!CheckLineRange(fencesCode.Length, token.PathQueryOption?.StartLine, token.PathQueryOption?.EndLine, out errorMessage)) { Logger.LogError(errorMessage); return(new DfmExtractCodeResult { IsSuccessful = false, ErrorMessage = errorMessage, FencesCodeLines = fencesCode }); } return(GetFencesCodeCore(fencesCode, token.PathQueryOption?.StartLine, token.PathQueryOption?.EndLine)); } }
public DfmExtractCodeResult ExtractFencesCode(DfmFencesBlockToken token, string fencesPath) { if (token == null) { throw new ArgumentNullException(nameof(token)); } if (string.IsNullOrEmpty(fencesPath)) { throw new ArgumentNullException(nameof(fencesPath)); } var fencesCode = File.ReadAllLines(fencesPath); // NOTE: Parsing language and removing comment lines only do for tag name representation if (token.PathQueryOption?.TagName != null) { var lang = GetCodeLanguageOrExtension(token); Regex regex; if (!CodeLanguageRegexes.TryGetValue(lang, out regex)) { string errorMessage = $"{lang} is not supported languaging name, alias or extension for parsing code snippet with tag name, you can use line numbers instead"; Logger.LogError(errorMessage); return new DfmExtractCodeResult { IsSuccessful = false, ErrorMessage = errorMessage, FencesCodeLines = fencesCode }; } var resolveResult = ResolveTagNamesFromPath(fencesPath, fencesCode, token.PathQueryOption.TagName, regex); if (!resolveResult.IsSuccessful) { Logger.LogError(resolveResult.ErrorMessage); return new DfmExtractCodeResult { IsSuccessful = false, ErrorMessage = resolveResult.ErrorMessage, FencesCodeLines = fencesCode }; } return GetFencesCodeCore(fencesCode, resolveResult.StartLine, resolveResult.EndLine, resolveResult.ExcludesLines); } else { // line range check only need to be done for line number representation string errorMessage; if (!CheckLineRange(fencesCode.Length, token.PathQueryOption?.StartLine, token.PathQueryOption?.EndLine, out errorMessage)) { Logger.LogError(errorMessage); return new DfmExtractCodeResult { IsSuccessful = false, ErrorMessage = errorMessage, FencesCodeLines = fencesCode }; } return GetFencesCodeCore(fencesCode, token.PathQueryOption?.StartLine, token.PathQueryOption?.EndLine); } }
public static string GetRenderedFencesBlockString(DfmFencesBlockToken token, Options options, string errorMessage, string[] codeLines = null) { string renderedErrorMessage = string.Empty; string renderedCodeLines = string.Empty; if (!string.IsNullOrEmpty(errorMessage)) { renderedErrorMessage = $@"<!-- {StringHelper.HtmlEncode(errorMessage)} -->\n"; } if (codeLines != null) { var lang = string.IsNullOrEmpty(token.Lang) ? null : $" class=\"{options.LangPrefix}{token.Lang}\""; var name = string.IsNullOrEmpty(token.Name) ? null : $" name=\"{StringHelper.HtmlEncode(token.Name)}\""; var title = string.IsNullOrEmpty(token.Title) ? null : $" title=\"{StringHelper.HtmlEncode(token.Title)}\""; renderedCodeLines = $"<pre><code{lang}{name}{title}>{StringHelper.HtmlEncode(string.Join("\n", codeLines))}\n</code></pre>"; } return $"{renderedErrorMessage}{renderedCodeLines}"; }
public static string GetRenderedFencesBlockString(DfmFencesBlockToken token, Options options, string errorMessage, string[] codeLines = null) { string renderedErrorMessage = string.Empty; string renderedCodeLines = string.Empty; if (!string.IsNullOrEmpty(errorMessage)) { renderedErrorMessage = $@"<!-- {StringHelper.HtmlEncode(errorMessage)} -->\n"; } if (codeLines != null) { var lang = string.IsNullOrEmpty(token.Lang) ? null : $" class=\"{options.LangPrefix}{token.Lang}\""; var name = string.IsNullOrEmpty(token.Name) ? null : $" name=\"{StringHelper.HtmlEncode(token.Name)}\""; var title = string.IsNullOrEmpty(token.Title) ? null : $" title=\"{StringHelper.HtmlEncode(token.Title)}\""; renderedCodeLines = $"<pre><code{lang}{name}{title}>{StringHelper.HtmlEncode(string.Join("\n", codeLines))}\n</code></pre>"; } return($"{renderedErrorMessage}{renderedCodeLines}"); }
public virtual StringBuffer Render(IMarkdownRenderer engine, DfmFencesBlockToken token, MarkdownBlockContext context) { if (!PathUtility.IsRelativePath(token.Path)) { string errorMessage = $"Code absolute path: {token.Path} is not supported in file {context.GetFilePathStack().Peek()}"; Logger.LogError(errorMessage); return DfmRendererHelper.GetRenderedFencesBlockString(token, engine.Options, errorMessage); } try { // TODO: Valid REST and REST-i script. var fencesPath = ((RelativePath)token.Path).BasedOn((RelativePath)context.GetFilePathStack().Peek()); var extractResult = _dfmCodeExtractor.ExtractFencesCode(token, fencesPath); return DfmRendererHelper.GetRenderedFencesBlockString(token, engine.Options, extractResult.ErrorMessage, extractResult.FencesCodeLines); } catch (FileNotFoundException) { string errorMessage = $"Can not find reference {token.Path}"; Logger.LogError(errorMessage); return DfmRendererHelper.GetRenderedFencesBlockString(token, engine.Options, errorMessage); } }
public virtual StringBuffer Render(IMarkdownRenderer engine, DfmFencesBlockToken token, MarkdownBlockContext context) { if (!PathUtility.IsRelativePath(token.Path)) { string errorMessage = $"Code absolute path: {token.Path} is not supported in file {context.GetFilePathStack().Peek()}"; Logger.LogError(errorMessage); return(DfmRendererHelper.GetRenderedFencesBlockString(token, engine.Options, errorMessage)); } try { // TODO: Valid REST and REST-i script. var fencesPath = ((RelativePath)token.Path).BasedOn((RelativePath)context.GetFilePathStack().Peek()); var extractResult = _dfmCodeExtractor.ExtractFencesCode(token, fencesPath); return(DfmRendererHelper.GetRenderedFencesBlockString(token, engine.Options, extractResult.ErrorMessage, extractResult.FencesCodeLines)); } catch (FileNotFoundException) { string errorMessage = $"Can not find reference {token.Path}"; Logger.LogError(errorMessage); return(DfmRendererHelper.GetRenderedFencesBlockString(token, engine.Options, errorMessage)); } }
private static string GetCodeLanguageOrExtension(DfmFencesBlockToken token) { return(!string.IsNullOrEmpty(token.Lang) ? token.Lang : Path.GetExtension(token.Path)); }
private static string GetCodeLanguageOrExtension(DfmFencesBlockToken token) { return !string.IsNullOrEmpty(token.Lang) ? token.Lang : Path.GetExtension(token.Path); }