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 = EnvironmentContext.FileAbstractLayer.ReadAllText(filePathWithStatus.Item1); dependency = new HashSet <string>(); src = new DfmEngine(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, IMarkdownContext context, Func <string, IMarkdownContext, string> resolver) { try { if (!PathUtility.IsRelativePath(currentPath)) { return(GenerateErrorNodeWithCommentWrapper("INCLUDE", $"Absolute path \"{currentPath}\" is not supported.", raw)); } var parents = context.GetFilePathStack(); var originalPath = currentPath; 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; if (!_cache.TryGet(currentPath, out result)) { var src = File.ReadAllText(Path.Combine(context.GetBaseFolder(), currentPath)); src = resolver(src, context.SetFilePathStack(parents)); result = UpdateToHrefFromWorkingFolder(src, currentPath); result = GenerateNodeWithCommentWrapper("INCLUDE", $"Include content from \"{currentPath}\"", result); _cache.Add(currentPath, result); } return(result); } catch (Exception e) { return(GenerateErrorNodeWithCommentWrapper("INCLUDE", $"Unable to resolve {raw}:{e.Message}", raw)); } }
private string LoadCore(IMarkdownRenderer adapter, string currentPath, string raw, IMarkdownContext context, Func <string, IMarkdownContext, string> resolver) { if (!PathUtility.IsRelativePath(currentPath)) { if (!Path.IsPathRooted(currentPath)) { return(GenerateNodeWithCommentWrapper("ERROR INCLUDE", $"Absolute path \"{currentPath}\" is not supported.", raw)); } else { currentPath = PathUtility.MakeRelativePath(Environment.CurrentDirectory, currentPath); } } var parents = context.GetFilePathStack(); var originalPath = currentPath; 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(GenerateNodeWithCommentWrapper("ERROR INCLUDE", $"Unable to resolve {raw}: Circular dependency found in \"{parent}\"", raw)); } string result = string.Empty; // Add current file path to chain when entering recursion parents = parents.Push(currentPath); try { if (!_cache.TryGet(currentPath, out result)) { var src = File.ReadAllText(currentPath); src = resolver(src, context.SetFilePathStack(parents)); HtmlDocument htmlDoc = new HtmlDocument(); htmlDoc.LoadHtml(src); var node = htmlDoc.DocumentNode; // If current content is not the root one, update href to root if (parents.Count() > 1) { UpdateHref(node, originalPath); } result = node.WriteTo(); result = GenerateNodeWithCommentWrapper("INCLUDE", $"Include content from \"{currentPath}\"", result); } } catch (Exception e) { result = GenerateNodeWithCommentWrapper("ERROR INCLUDE", $"Unable to resolve {raw}:{e.Message}", raw); } _cache.Add(currentPath, result); return(result); }