Inheritance: Microsoft.DocAsCode.MarkdownLite.MarkdownEngine
Exemplo n.º 1
0
        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);
            }
        }
Exemplo n.º 2
0
 public string InternalMarkup(string src, IMarkdownContext context)
 {
     DfmEngine engine = new DfmEngine(context, Rewriter, Renderer, Options);
     return Mark(Normalize(src), context).ToString();
 }
Exemplo n.º 3
0
 public string InternalMarkup(string src, ImmutableStack<string> parents)
 {
     DfmEngine engine = new DfmEngine(Context, Rewriter, Renderer, Options);
     return Mark(Normalize(src), Context.SetFilePathStack(parents)).ToString();
 }
Exemplo n.º 4
0
        private string LoadCore(IMarkdownRenderer adapter, string currentPath, SourceInfo sourceInfo, IMarkdownContext context, DfmEngine engine)
        {
            try
            {
                if (!PathUtility.IsRelativePath(currentPath))
                {
                    return(GenerateErrorNodeWithCommentWrapper("INCLUDE", $"Absolute path \"{currentPath}\" is not supported.", 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.OSPlatformSensitiveRelativePathComparer))
                {
                    return(GenerateErrorNodeWithCommentWrapper("INCLUDE", $"Circular dependency found in \"{parent}\"", sourceInfo));
                }

                // Add current file path to chain when entering recursion
                parents = parents.Push(currentPath);
                if (!_dependencyCache.TryGetValue(currentPath, out HashSet <string> dependency) ||
                    !_cache.TryGet(currentPath, out string result))
                {
                    var src = GetIncludedContent(originalRelativePath, context);
                    dependency = new HashSet <string>();
                    src        = new DfmEngine(engine).InternalMarkup(src, context.SetFilePathStack(parents).SetDependency(dependency).SetIsInclude());

                    result = UpdateToHrefFromWorkingFolder(src, currentPath);
                    _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", e.Message, sourceInfo));
            }
        }
Exemplo n.º 5
0
 public virtual string Load(IMarkdownRenderer adapter, string currentPath, SourceInfo sourceInfo, IMarkdownContext context, DfmEngine engine)
 {
     return(LoadCore(adapter, currentPath, sourceInfo, context, engine));
 }
Exemplo n.º 6
0
        public string InternalMarkup(string src, IMarkdownContext context)
        {
            DfmEngine engine = new DfmEngine(context, Rewriter, Renderer, Options);

            return(Mark(Normalize(src), context).ToString());
        }
Exemplo n.º 7
0
        public string InternalMarkup(string src, ImmutableStack <string> parents)
        {
            DfmEngine engine = new DfmEngine(Context, Rewriter, Renderer, Options);

            return(Mark(Normalize(src), Context.SetFilePathStack(parents)).ToString());
        }
Exemplo n.º 8
0
 public string Load(IMarkdownRenderer adapter, string currentPath, string raw, SourceInfo sourceInfo, IMarkdownContext context, DfmEngine engine)
 {
     return LoadCore(adapter, currentPath, raw, sourceInfo, context, engine);
 }