Пример #1
0
        protected override void Write(HtmlRenderer renderer, InclusionInline inclusion)
        {
            if (string.IsNullOrEmpty(inclusion.Context.IncludedFilePath))
            {
                Logger.LogError("file path can't be empty or null in IncludeFile");
                renderer.Write(inclusion.Context.GetRaw());

                return;
            }

            if (!PathUtility.IsRelativePath(inclusion.Context.IncludedFilePath))
            {
                string tag     = "ERROR INCLUDE";
                string message = $"Unable to resolve {inclusion.Context.GetRaw()}: Absolute path \"{inclusion.Context.IncludedFilePath}\" is not supported.";
                ExtensionsHelper.GenerateNodeWithCommentWrapper(renderer, tag, message, inclusion.Context.GetRaw(), inclusion.Line);

                return;
            }

            var currentFilePath = ((RelativePath)_context.FilePath).GetPathFromWorkingFolder();
            var includeFilePath = ((RelativePath)inclusion.Context.IncludedFilePath).BasedOn(currentFilePath);

            var filePath = Path.Combine(_context.BasePath, includeFilePath.RemoveWorkingFolder());

            if (!File.Exists(filePath))
            {
                Logger.LogWarning($"Can't find {includeFilePath}.");
                renderer.Write(inclusion.Context.GetRaw());

                return;
            }

            var parents = _context.InclusionSet;

            if (parents != null && parents.Contains(includeFilePath))
            {
                string tag     = "ERROR INCLUDE";
                string message = $"Unable to resolve {inclusion.Context.GetRaw()}: Circular dependency found in \"{_context.FilePath}\"";
                ExtensionsHelper.GenerateNodeWithCommentWrapper(renderer, tag, message, inclusion.Context.GetRaw(), inclusion.Line);

                return;
            }

            var content = File.ReadAllText(filePath);
            var context = new MarkdownContextBuilder()
                          .WithContext(_context)
                          .WithFilePath(includeFilePath.RemoveWorkingFolder())
                          .WithContent(content)
                          .WithIsInline(true)
                          .WithAddingIncludedFile(currentFilePath)
                          .Build();

            _engine.ReportDependency(includeFilePath);
            // Do not need to check if content is a single paragragh
            // context.IsInline = true will force it into a single paragragh and render with no <p></p>
            var result = _engine.Markup(context, _parameters);

            renderer.Write(result);
        }
Пример #2
0
        protected override void Write(HtmlRenderer renderer, InclusionBlock inclusion)
        {
            if (string.IsNullOrEmpty(inclusion.Context.IncludedFilePath))
            {
                Logger.LogError("file path can't be empty or null in IncludeFile");
                renderer.Write(inclusion.Context.GetRaw());

                return;
            }

            if (!PathUtility.IsRelativePath(inclusion.Context.IncludedFilePath))
            {
                var tag     = "ERROR INCLUDE";
                var message = $"Unable to resolve {inclusion.Context.GetRaw()}: Absolute path \"{inclusion.Context.IncludedFilePath}\" is not supported.";
                ExtensionsHelper.GenerateNodeWithCommentWrapper(renderer, tag, message, inclusion.Context.GetRaw(), inclusion.Line);

                return;
            }

            var currentFilePath  = ((RelativePath)_context.FilePath).GetPathFromWorkingFolder();
            var includedFilePath = ((RelativePath)inclusion.Context.IncludedFilePath).BasedOn(currentFilePath);

            if (!EnvironmentContext.FileAbstractLayer.Exists(includedFilePath))
            {
                Logger.LogWarning($"Can't find {includedFilePath}.");
                renderer.Write(inclusion.Context.GetRaw());

                return;
            }

            var parents = _context.InclusionSet;

            if (parents != null && parents.Contains(includedFilePath))
            {
                string tag     = "ERROR INCLUDE";
                string message = $"Unable to resolve {inclusion.Context.GetRaw()}: Circular dependency found in \"{_context.FilePath}\"";
                ExtensionsHelper.GenerateNodeWithCommentWrapper(renderer, tag, message, inclusion.Context.GetRaw(), inclusion.Line);

                return;
            }

            var content = EnvironmentContext.FileAbstractLayer.ReadAllText(includedFilePath);
            var context = new MarkdownContextBuilder()
                          .WithContext(_context)
                          .WithFilePath(includedFilePath.RemoveWorkingFolder())
                          .WithContent(content)
                          .WithAddingIncludedFile(currentFilePath)
                          .Build();

            _engine.ReportDependency(includedFilePath);
            var result = _engine.Markup(context, _parameters);

            result = SkipYamlHeader(result);

            renderer.Write(result);
        }
Пример #3
0
        private string GetContent(CodeSnippet obj)
        {
            var currentFilePath     = ((RelativePath)_context.FilePath).GetPathFromWorkingFolder();
            var refFileRelativePath = ((RelativePath)obj.CodePath).BasedOn(currentFilePath);

            _engine.ReportDependency(refFileRelativePath);

            var refPath  = Path.Combine(_context.BasePath, refFileRelativePath.RemoveWorkingFolder());
            var allLines = EnvironmentContext.FileAbstractLayer.ReadAllLines(refFileRelativePath);

            // code range priority: tag > #L1 > start/end > range > default
            if (!string.IsNullOrEmpty(obj.TagName))
            {
                var lang = obj.Language ?? Path.GetExtension(refPath);
                if (!CodeLanguageExtractors.TryGetValue(lang, out List <CodeSnippetExtrator> extrators))
                {
                    Logger.LogError($"{lang} is not supported languaging name, alias or extension for parsing code snippet with tag name, you can use line numbers instead");
                }

                if (extrators != null)
                {
                    var tagWithPrefix = tagPrefix + obj.TagName;
                    foreach (var extrator in extrators)
                    {
                        HashSet <int> tagLines = new HashSet <int>();
                        var           tagToCoderangeMapping = extrator.GetAllTags(allLines, ref tagLines);
                        CodeRange     cr;
                        if (tagToCoderangeMapping.TryGetValue(obj.TagName, out cr) ||
                            tagToCoderangeMapping.TryGetValue(tagWithPrefix, out cr))
                        {
                            return(GetCodeLines(allLines, obj, new List <CodeRange> {
                                cr
                            }, tagLines));
                        }
                    }
                }
            }
            else if (obj.BookMarkRange != null)
            {
                return(GetCodeLines(allLines, obj, new List <CodeRange> {
                    obj.BookMarkRange
                }));
            }
            else if (obj.StartEndRange != null)
            {
                return(GetCodeLines(allLines, obj, new List <CodeRange> {
                    obj.StartEndRange
                }));
            }
            else if (obj.CodeRanges != null)
            {
                return(GetCodeLines(allLines, obj, obj.CodeRanges));
            }
            else
            {
                return(GetCodeLines(allLines, obj, new List <CodeRange> {
                    new CodeRange()
                    {
                        Start = 0, End = allLines.Length
                    }
                }));
            }

            return(string.Empty);
        }