/// <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 string LoadCore(IMarkdownRenderer adapter, string currentPath, string raw, IMarkdownContext context, DfmEngine engine) { try { if (!PathUtility.IsRelativePath(currentPath)) { return(GenerateErrorNodeWithCommentWrapper("INCLUDE", $"Absolute path \"{currentPath}\" is not supported.", raw)); } var parents = context.GetFilePathStack(); var originalPath = currentPath; context.ReportDependency(originalPath); 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)); } // 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 src = File.ReadAllText(Path.Combine(context.GetBaseFolder(), currentPath)); dependency = new HashSet <string>(); src = engine.InternalMarkup(src, context.SetFilePathStack(parents).SetDependency(dependency)); 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)); } }
/// <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)); } // var currentFileFolder = Path.Combine(context.GetBaseFolder(), Path.Combine(context.GetFilePathStack().Select(path => Path.GetDirectoryName(path)).ToArray())); // var originalFilePath = Path.Combine(context.GetBaseFolder(), orginalRelativePath); RelativePath filePathToDocset = null; string parentFileDirectoryToDocset = context.GetBaseFolder(); var parents = context.GetFilePathStack(); if (parents != null) { var parent = parents.Peek(); filePathToDocset = ((RelativePath)parent + (RelativePath)relativePath).RemoveWorkingFolder(); parentFileDirectoryToDocset = Path.GetDirectoryName(Path.Combine(context.GetBaseFolder(), parent)); } else { filePathToDocset = ((RelativePath)relativePath).RemoveWorkingFolder(); } var originalFullPath = Path.Combine(context.GetBaseFolder(), filePathToDocset); if (EnvironmentContext.FileAbstractLayer.Exists(filePathToDocset)) { return(Tuple.Create((string)filePathToDocset, false)); } else { return(FindInFallbackFolders( context, filePathToDocset, parentFileDirectoryToDocset, originalFullPath)); } }