Exemplo n.º 1
0
        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));
            }
        }
Exemplo n.º 2
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.º 3
0
 public virtual string FindFile(DfmFencesToken token, IMarkdownContext context)
 {
     return(DfmFallbackHelper.GetFilePathWithFallback(token.Path, context).Item1);
 }
Exemplo n.º 4
0
        protected virtual string GetIncludedContent(string filePath, IMarkdownContext context)
        {
            var filePathWithStatus = DfmFallbackHelper.GetFilePathWithFallback(filePath, context);

            return(EnvironmentContext.FileAbstractLayer.ReadAllText(filePathWithStatus.Item1));
        }