private string LoadCore(IMarkdownRenderer adapter, string currentPath, string raw, SourceInfo sourceInfo, IMarkdownContext context, DfmEngine engine) { try { if (!PathUtility.IsRelativePath(currentPath)) { return(GenerateErrorNodeWithCommentWrapper("INCLUDE", $"Absolute path \"{currentPath}\" is not supported.", raw, sourceInfo)); } // Always report original include file dependency var originalRelativePath = currentPath; context.ReportDependency(currentPath); var parents = context.GetFilePathStack(); string parent = string.Empty; if (parents == null) { parents = ImmutableStack <string> .Empty; } // Update currentPath to be referencing to sourcePath else if (!parents.IsEmpty) { parent = parents.Peek(); currentPath = ((RelativePath)currentPath).BasedOn((RelativePath)parent); } if (parents.Contains(currentPath, FilePathComparer.OSPlatformSensitiveComparer)) { return(GenerateErrorNodeWithCommentWrapper("INCLUDE", $"Unable to resolve {raw}: Circular dependency found in \"{parent}\"", raw, sourceInfo)); } // Add current file path to chain when entering recursion parents = parents.Push(currentPath); string result; HashSet <string> dependency; if (!_dependencyCache.TryGetValue(currentPath, out dependency) || !_cache.TryGet(currentPath, out result)) { var filePathWithStatus = DfmFallbackHelper.GetFilePathWithFallback(originalRelativePath, context); var src = File.ReadAllText(filePathWithStatus.Item1); dependency = new HashSet <string>(); src = engine.InternalMarkup(src, context.SetFilePathStack(parents).SetDependency(dependency).SetIsInclude()); result = UpdateToHrefFromWorkingFolder(src, currentPath); result = GenerateNodeWithCommentWrapper("INCLUDE", $"Include content from \"{currentPath}\"", result); _cache.Add(currentPath, result); _dependencyCache[currentPath] = dependency; } context.ReportDependency( from d in dependency select(string)((RelativePath)currentPath + (RelativePath)d - (RelativePath)parent)); return(result); } catch (Exception e) { return(GenerateErrorNodeWithCommentWrapper("INCLUDE", $"Unable to resolve {raw}:{e.Message}", raw, sourceInfo)); } }
private string LoadCore(IMarkdownRenderer adapter, string currentPath, string raw, SourceInfo sourceInfo, IMarkdownContext context, DfmEngine engine) { try { if (!PathUtility.IsRelativePath(currentPath)) { return GenerateErrorNodeWithCommentWrapper("INCLUDE", $"Absolute path \"{currentPath}\" is not supported.", raw, sourceInfo); } // Always report original include file dependency var originalRelativePath = currentPath; context.ReportDependency(currentPath); var parents = context.GetFilePathStack(); string parent = string.Empty; if (parents == null) parents = ImmutableStack<string>.Empty; // Update currentPath to be referencing to sourcePath else if (!parents.IsEmpty) { parent = parents.Peek(); currentPath = ((RelativePath)currentPath).BasedOn((RelativePath)parent); } if (parents.Contains(currentPath, FilePathComparer.OSPlatformSensitiveComparer)) { return GenerateErrorNodeWithCommentWrapper("INCLUDE", $"Unable to resolve {raw}: Circular dependency found in \"{parent}\"", raw, sourceInfo); } // Add current file path to chain when entering recursion parents = parents.Push(currentPath); string result; HashSet<string> dependency; if (!_dependencyCache.TryGetValue(currentPath, out dependency) || !_cache.TryGet(currentPath, out result)) { var filePathWithStatus = DfmFallbackHelper.GetFilePathWithFallback(originalRelativePath, context); var src = File.ReadAllText(filePathWithStatus.Item1); dependency = new HashSet<string>(); src = engine.InternalMarkup(src, context.SetFilePathStack(parents).SetDependency(dependency).SetIsInclude()); result = UpdateToHrefFromWorkingFolder(src, currentPath); result = GenerateNodeWithCommentWrapper("INCLUDE", $"Include content from \"{currentPath}\"", result); _cache.Add(currentPath, result); _dependencyCache[currentPath] = dependency; } context.ReportDependency( from d in dependency select (string)((RelativePath)currentPath + (RelativePath)d - (RelativePath)parent)); return result; } catch (Exception e) { return GenerateErrorNodeWithCommentWrapper("INCLUDE", $"Unable to resolve {raw}:{e.Message}", raw, sourceInfo); } }
public virtual StringBuffer Render(IMarkdownRenderer renderer, DfmFencesToken token, IMarkdownContext 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(DfmFencesBlockHelper.GetRenderedFencesBlockString(token, renderer.Options, errorMessage)); } try { // Always report original dependency context.ReportDependency(token.Path); var filePathWithStatus = DfmFallbackHelper.GetFilePathWithFallback(token.Path, context); var extractResult = _dfmCodeExtractor.ExtractFencesCode(token, filePathWithStatus.Item1); var result = DfmFencesBlockHelper.GetRenderedFencesBlockString(token, renderer.Options, extractResult.ErrorMessage, extractResult.FencesCodeLines); return(result); } catch (DirectoryNotFoundException) { return(DfmFencesBlockHelper.GenerateReferenceNotFoundErrorMessage(renderer, token)); } catch (FileNotFoundException) { return(DfmFencesBlockHelper.GenerateReferenceNotFoundErrorMessage(renderer, token)); } }
public virtual StringBuffer Render(IMarkdownRenderer renderer, DfmFencesToken token, IMarkdownContext 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(RenderFencesCode(token, renderer.Options, errorMessage)); } try { // Always report original dependency context.ReportDependency(token.Path); var filePath = FindFile(token, context); var code = ExtractCode(token, filePath); return(RenderFencesCode(token, renderer.Options, code.ErrorMessage, code.CodeLines)); } catch (DirectoryNotFoundException) { return(RenderReferenceNotFoundErrorMessage(renderer, token)); } catch (FileNotFoundException) { return(RenderReferenceNotFoundErrorMessage(renderer, token)); } }
public virtual StringBuffer Render(IMarkdownRenderer renderer, DfmFencesToken token, IMarkdownContext 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(RenderFencesCode(token, renderer.Options, errorMessage)); } try { // Always report original dependency when path is valid if (PathUtility.IsVaildFilePath(token.Path)) { context.ReportDependency(token.Path); } var pathQueryOption = !string.IsNullOrEmpty(token.QueryStringAndFragment) ? _dfmCodeExtractor.ParsePathQueryString(token.QueryStringAndFragment) : null; var filePath = FindFile(token, context); var code = ExtractCode(token, filePath, pathQueryOption); return(RenderFencesCode(token, renderer.Options, code.ErrorMessage, code.CodeLines, pathQueryOption)); } catch (DirectoryNotFoundException) { return(RenderReferenceNotFoundErrorMessage(renderer, token)); } catch (FileNotFoundException) { return(RenderReferenceNotFoundErrorMessage(renderer, token)); } }
/// <summary> /// Get file path with fallback /// </summary> /// <param name="relativePath">original relative path in markdown.</param> /// <param name="context">markdown context</param> /// <returns>item1: acutal file path. item: true if it hit fallback file. Otherwise false</returns> public static Tuple <string, bool> GetFilePathWithFallback(string relativePath, IMarkdownContext context) { if (context == null) { throw new ArgumentNullException(nameof(context)); } if (relativePath == null) { throw new ArgumentNullException(nameof(relativePath)); throw new FileNotFoundException($"Couldn't resolve path {relativePath}."); } // var currentFileFolder = Path.Combine(context.GetBaseFolder(), Path.Combine(context.GetFilePathStack().Select(path => Path.GetDirectoryName(path)).ToArray())); // var originalFilePath = Path.Combine(context.GetBaseFolder(), orginalRelativePath); var filePathToDocset = relativePath; string parentFileDirectoryToDocset = context.GetBaseFolder(); var parents = context.GetFilePathStack(); if (parents != null) { var parent = parents.Peek(); filePathToDocset = (RelativePath)parent + (RelativePath)filePathToDocset; parentFileDirectoryToDocset = Path.GetDirectoryName(Path.Combine(context.GetBaseFolder(), parent)); } var originalFilePath = Path.Combine(context.GetBaseFolder(), filePathToDocset); var actualFilePath = originalFilePath; bool hitFallback = false; if (!File.Exists(originalFilePath)) { var fallbackFolders = context.GetFallbackFolders(); foreach (var folder in fallbackFolders) { var fallbackFilePath = Path.Combine(folder, filePathToDocset); var fallbackFileRelativePath = PathUtility.MakeRelativePath(parentFileDirectoryToDocset, fallbackFilePath); context.ReportDependency(fallbackFileRelativePath); // All the high priority fallback files should be reported to the dependency. if (File.Exists(fallbackFilePath)) { actualFilePath = fallbackFilePath; hitFallback = true; break; } } if (!hitFallback) { if (fallbackFolders.Count > 0) { throw new FileNotFoundException($"Couldn't find file {filePathToDocset}. Fallback folders: {string.Join(",", fallbackFolders)}", filePathToDocset); } throw new FileNotFoundException($"Couldn't find file {filePathToDocset}.", originalFilePath); } } return(Tuple.Create(actualFilePath, hitFallback)); }
/// <summary> /// Get file path with fallback /// </summary> /// <param name="relativePath">original relative path in markdown.</param> /// <param name="context">markdown context</param> /// <returns>item1: acutal file path. item: true if it hit fallback file. Otherwise false</returns> public static Tuple<string, bool> GetFilePathWithFallback(string relativePath, IMarkdownContext context) { if (context == null) { throw new ArgumentNullException(nameof(context)); } if (relativePath == null) { throw new ArgumentNullException(nameof(relativePath)); throw new FileNotFoundException($"Couldn't resolve path {relativePath}."); } // var currentFileFolder = Path.Combine(context.GetBaseFolder(), Path.Combine(context.GetFilePathStack().Select(path => Path.GetDirectoryName(path)).ToArray())); // var originalFilePath = Path.Combine(context.GetBaseFolder(), orginalRelativePath); var filePathToDocset = relativePath; string parentFileDirectoryToDocset = context.GetBaseFolder(); var parents = context.GetFilePathStack(); if(parents != null) { var parent = parents.Peek(); filePathToDocset = (RelativePath)parent + (RelativePath)filePathToDocset; parentFileDirectoryToDocset = Path.GetDirectoryName(Path.Combine(context.GetBaseFolder(), parent)); } var originalFilePath = Path.Combine(context.GetBaseFolder(), filePathToDocset); var actualFilePath = originalFilePath; bool hitFallback = false; if (!File.Exists(originalFilePath)) { var fallbackFolders = context.GetFallbackFolders(); foreach (var folder in fallbackFolders) { var fallbackFilePath = Path.Combine(folder, filePathToDocset); var fallbackFileRelativePath = PathUtility.MakeRelativePath(parentFileDirectoryToDocset, fallbackFilePath); context.ReportDependency(fallbackFileRelativePath); // All the high priority fallback files should be reported to the dependency. if (File.Exists(fallbackFilePath)) { actualFilePath = fallbackFilePath; hitFallback = true; break; } } if (!hitFallback) { if (fallbackFolders.Count > 0) { throw new FileNotFoundException($"Couldn't find file {filePathToDocset}. Fallback folders: {string.Join(",", fallbackFolders)}", filePathToDocset); } throw new FileNotFoundException($"Couldn't find file {filePathToDocset}.", originalFilePath); } } return Tuple.Create(actualFilePath, hitFallback); }
private static Tuple <string, bool> FindInFallbackFolders(IMarkdownContext context, RelativePath filePathToDocset, string parentFileDirectoryToDocset, string originalFullPath) { var fallbackFolders = context.GetFallbackFolders(); foreach (var folder in fallbackFolders) { var fallbackFilePath = Path.Combine(folder, filePathToDocset); var fallbackFileRelativePath = PathUtility.MakeRelativePath(parentFileDirectoryToDocset, fallbackFilePath); context.ReportDependency(fallbackFileRelativePath); // All the high priority fallback files should be reported to the dependency. if (EnvironmentContext.FileAbstractLayer.Exists(fallbackFilePath)) { return(Tuple.Create(fallbackFilePath, true)); } } if (fallbackFolders.Count > 0) { throw new FileNotFoundException($"Couldn't find file {filePathToDocset}. Fallback folders: {string.Join(",", fallbackFolders)}", filePathToDocset); } throw new FileNotFoundException($"Couldn't find file {filePathToDocset}.", originalFullPath); }
public virtual StringBuffer Render(IMarkdownRenderer renderer, DfmFencesToken token, IMarkdownContext context) { if (!TypeForwardedToPathUtility.IsRelativePath(token.Path)) { string errorMessage = $"Code absolute path: {token.Path} is not supported in file {context.GetFilePathStack().Peek()}"; Logger.LogError(errorMessage); return DfmFencesBlockHelper.GetRenderedFencesBlockString(token, renderer.Options, errorMessage); } try { // Always report original dependency context.ReportDependency(token.Path); var filePathWithStatus = DfmFallbackHelper.GetFilePathWithFallback(token.Path, context); var extractResult = _dfmCodeExtractor.ExtractFencesCode(token, filePathWithStatus.Item1); var result = DfmFencesBlockHelper.GetRenderedFencesBlockString(token, renderer.Options, extractResult.ErrorMessage, extractResult.FencesCodeLines); return result; } catch (DirectoryNotFoundException) { return DfmFencesBlockHelper.GenerateReferenceNotFoundErrorMessage(renderer, token); } catch (FileNotFoundException) { return DfmFencesBlockHelper.GenerateReferenceNotFoundErrorMessage(renderer, token); } }