Exemplo n.º 1
0
        public virtual StringBuffer RenderFencesCode(DfmFencesToken token,
                                                     Options options,
                                                     string errorMessage,
                                                     string[] codeLines = null,
                                                     IDfmFencesBlockPathQueryOption pathQueryOption = null)
        {
            StringBuffer result;
            string       renderedErrorMessage = string.Empty;
            string       renderedCodeLines    = string.Empty;

            if (!string.IsNullOrEmpty(errorMessage))
            {
                result = RenderCodeErrorString(errorMessage);
            }
            else
            {
                result = StringBuffer.Empty;
            }

            if (codeLines != null)
            {
                result = RenderOpenPreTag(result, token, options);
                result = RenderOpenCodeTag(result, token, options, pathQueryOption);
                foreach (var line in codeLines)
                {
                    result += StringHelper.HtmlEncode(line);
                    result += "\n";
                }
                result = RenderCloseCodeTag(result, token, options);
                result = RenderClosePreTag(result, token, options);
            }

            return(result);
        }
Exemplo n.º 2
0
 public DfmFencesToken(IMarkdownRule rule, IMarkdownContext context, string name, string path, SourceInfo sourceInfo, string lang, string title, IDfmFencesBlockPathQueryOption pathQueryOption)
 {
     Rule = rule;
     Context = context;
     Path = path;
     Lang = lang;
     Name = name;
     Title = title;
     PathQueryOption = pathQueryOption;
     SourceInfo = sourceInfo;
 }
Exemplo n.º 3
0
 public DfmFencesBlockToken(IMarkdownRule rule, IMarkdownContext context, string name, string path, string rawMarkdown, string lang = null, string title = null, IDfmFencesBlockPathQueryOption pathQueryOption = null)
 {
     Rule            = rule;
     Context         = context;
     Path            = path;
     Lang            = lang;
     Name            = name;
     Title           = title;
     PathQueryOption = pathQueryOption;
     RawMarkdown     = rawMarkdown;
 }
Exemplo n.º 4
0
 public DfmFencesToken(IMarkdownRule rule, IMarkdownContext context, string name, string path, SourceInfo sourceInfo, string lang, string title, IDfmFencesBlockPathQueryOption pathQueryOption)
 {
     Rule            = rule;
     Context         = context;
     Path            = path;
     Lang            = lang;
     Name            = name;
     Title           = title;
     PathQueryOption = pathQueryOption;
     SourceInfo      = sourceInfo;
 }
Exemplo n.º 5
0
        public DfmExtractCodeResult ExtractFencesCode(DfmFencesToken token, string[] fencesCode, IDfmFencesBlockPathQueryOption pathQueryOption)
        {
            if (token == null)
            {
                throw new ArgumentNullException(nameof(token));
            }

            if (fencesCode == null)
            {
                throw new ArgumentNullException(nameof(fencesCode));
            }

            using (new LoggerPhaseScope("Extract Dfm Code"))
            {
                if (pathQueryOption == null)
                {
                    // Add the full file when no query option is given
                    return(new DfmExtractCodeResult
                    {
                        IsSuccessful = true,
                        CodeLines = Dedent(fencesCode),
                    });
                }

                if (!pathQueryOption.ValidateAndPrepare(fencesCode, token))
                {
                    Logger.LogWarning(GenerateErrorMessage(token, pathQueryOption), line: token.SourceInfo.LineNumber.ToString(), code: WarningCodes.Markdown.InvalidCodeSnippet);
                    return(new DfmExtractCodeResult {
                        IsSuccessful = false, ErrorMessage = pathQueryOption.ErrorMessage, CodeLines = fencesCode
                    });
                }
                if (!string.IsNullOrEmpty(pathQueryOption.ErrorMessage))
                {
                    Logger.LogWarning(GenerateErrorMessage(token, pathQueryOption), line: token.SourceInfo.LineNumber.ToString());
                }

                var includedLines = pathQueryOption.GetQueryLines(fencesCode).ToList();

                if (!pathQueryOption.ValidateHighlightLinesAndDedentLength(includedLines.Count))
                {
                    Logger.LogWarning(GenerateErrorMessage(token, pathQueryOption), line: token.SourceInfo.LineNumber.ToString());
                }

                return(new DfmExtractCodeResult
                {
                    IsSuccessful = true,
                    ErrorMessage = pathQueryOption.ErrorMessage,
                    CodeLines = Dedent(includedLines, pathQueryOption.DedentLength)
                });
            }
        }
Exemplo n.º 6
0
        public DfmExtractCodeResult ExtractFencesCode(DfmFencesToken token, string fencesPath, IDfmFencesBlockPathQueryOption pathQueryOption)
        {
            if (token == null)
            {
                throw new ArgumentNullException(nameof(token));
            }

            if (string.IsNullOrEmpty(fencesPath))
            {
                throw new ArgumentNullException(nameof(fencesPath));
            }

            using (new LoggerPhaseScope("Extract Dfm Code From Path"))
            {
                var fencesCode = EnvironmentContext.FileAbstractLayer.ReadAllLines(fencesPath);

                return(ExtractFencesCode(token, fencesCode, pathQueryOption));
            }
        }
Exemplo n.º 7
0
 private static string GenerateErrorMessage(DfmFencesToken token, IDfmFencesBlockPathQueryOption option)
 {
     return($"{option.ErrorMessage} when resolving \"{token.SourceInfo.Markdown.Trim()}\"");
 }
Exemplo n.º 8
0
 public DfmFencesToken(IMarkdownRule rule, IMarkdownContext context, string name, string path, SourceInfo sourceInfo, string lang, string title, IDfmFencesBlockPathQueryOption pathQueryOption)
     : this(rule, context, name, path, sourceInfo, lang, title, pathQueryOption, null)
 {
 }
Exemplo n.º 9
0
 public DfmFencesInlineToken(IMarkdownRule rule, IMarkdownContext context, string name, string path, SourceInfo sourceInfo, string lang = null, string title = null, IDfmFencesBlockPathQueryOption pathQueryOption = null, string queryStringAndFragment = null)
     : base(rule, context, name, path, sourceInfo, lang, title, pathQueryOption, queryStringAndFragment)
 {
 }
Exemplo n.º 10
0
 public virtual StringBuffer RenderOpenCodeTag(StringBuffer result, DfmFencesToken token, Options options, IDfmFencesBlockPathQueryOption pathQueryOption)
 {
     result += "<code";
     if (!string.IsNullOrEmpty(token.Lang))
     {
         result = result + " class=\"" + options.LangPrefix + token.Lang + "\"";
     }
     if (!string.IsNullOrEmpty(token.Name))
     {
         result = result + " name=\"" + StringHelper.HtmlEncode(token.Name) + "\"";
     }
     if (!string.IsNullOrEmpty(token.Title))
     {
         result = result + " title=\"" + StringHelper.HtmlEncode(token.Title) + "\"";
     }
     if (!string.IsNullOrEmpty(pathQueryOption?.HighlightLines))
     {
         result = result + " highlight-lines=\"" + StringHelper.HtmlEncode(pathQueryOption.HighlightLines) + "\"";
     }
     result += ">";
     return(result);
 }
Exemplo n.º 11
0
 public virtual DfmExtractCodeResult ExtractCode(DfmFencesToken token, string[] fencesCode, IDfmFencesBlockPathQueryOption option)
 {
     return(_dfmCodeExtractor.ExtractFencesCode(token, fencesCode, option));
 }
Exemplo n.º 12
0
 public DfmFencesInlineToken(IMarkdownRule rule, IMarkdownContext context, string name, string path, SourceInfo sourceInfo, string lang = null, string title = null, IDfmFencesBlockPathQueryOption pathQueryOption = null)
     : base(rule, context, name, path, sourceInfo, lang, title, pathQueryOption) { }
        public override StringBuffer RenderOpenCodeTag(StringBuffer result, DfmFencesToken token, Options options, IDfmFencesBlockPathQueryOption pathQueryOption)
        {
            var lang = GetLang(token, out bool isInteractive);

            var gitUrl = GetGitUrl(token);

            result += "<code";
            if (!string.IsNullOrEmpty(lang))
            {
                result = result + " class=\"" + options.LangPrefix + lang + "\"";
                if (isInteractive)
                {
                    result += " data-interactive=\"";
                    result += StringHelper.HtmlEncode(lang);
                    result += "\"";
                }
            }
            if (gitUrl != null && gitUrl.StartsWith("https://github.com/"))
            {
                result += " data-src=\"";
                result += StringHelper.HtmlEncode(gitUrl);
                result += "\"";
            }
            if (!string.IsNullOrEmpty(token.Name))
            {
                result = result + " name=\"" + StringHelper.HtmlEncode(token.Name) + "\"";
            }
            if (!string.IsNullOrEmpty(token.Title))
            {
                result = result + " title=\"" + StringHelper.HtmlEncode(token.Title) + "\"";
            }
            if (!string.IsNullOrEmpty(pathQueryOption?.HighlightLines))
            {
                result = result + " highlight-lines=\"" + StringHelper.HtmlEncode(pathQueryOption.HighlightLines) + "\"";
            }
            result += ">";
            return(result);
        }
Exemplo n.º 14
0
 public DfmFencesToken(IMarkdownRule rule, IMarkdownContext context, string name, string path, SourceInfo sourceInfo, string lang, string title, IDfmFencesBlockPathQueryOption pathQueryOption, string queryStringAndFragment)
     : this(rule, context, name, path, sourceInfo, lang, title, queryStringAndFragment)
 {
     PathQueryOption = pathQueryOption;
 }
Exemplo n.º 15
0
        public DfmExtractCodeResult ExtractFencesCode(DfmFencesToken token, string[] fencesCode, IDfmFencesBlockPathQueryOption pathQueryOption)
        {
            if (token == null)
            {
                throw new ArgumentNullException(nameof(token));
            }

            if (fencesCode == null)
            {
                throw new ArgumentNullException(nameof(fencesCode));
            }

            using (new LoggerPhaseScope("Extract Dfm Code"))
            {
                if (pathQueryOption == null)
                {
                    // Add the full file when no query option is given
                    return(new DfmExtractCodeResult
                    {
                        IsSuccessful = true,
                        CodeLines = Dedent(fencesCode),
                    });
                }

                string[] includedLines;
                try
                {
                    includedLines = pathQueryOption.GetQueryLines(fencesCode, token).ToArray();
                }
                catch (Exception e)
                {
                    Logger.LogWarning(
                        GenerateErrorMessage(token, e.Message),
                        line: token.SourceInfo.LineNumber.ToString(),
                        code: WarningCodes.Markdown.InvalidCodeSnippet);
                    return(new DfmExtractCodeResult {
                        IsSuccessful = false, ErrorMessage = e.Message, CodeLines = fencesCode
                    });
                }

                return(new DfmExtractCodeResult
                {
                    IsSuccessful = true,
                    ErrorMessage = pathQueryOption.ErrorMessage,
                    CodeLines = includedLines,
                });
            }
        }
Exemplo n.º 16
0
 public static string GenerateErrorMessage(DfmFencesToken token, IDfmFencesBlockPathQueryOption option)
 {
     return(GenerateErrorMessage(token, option.ErrorMessage));
 }