Ejemplo n.º 1
0
        public virtual IMarkdownToken TryMatch(IMarkdownParser parser, IMarkdownParsingContext context)
        {
            var match = NpTable.Match(context.CurrentMarkdown);

            if (match.Length == 0)
            {
                return(null);
            }
            var sourceInfo = context.Consume(match.Length);
            var header     = match.Groups[1].Value.ReplaceRegex(Regexes.Lexers.UselessTableHeader, string.Empty).SplitRegex(Regexes.Lexers.TableSplitter);
            var align      = ParseAligns(match.Groups[2].Value.ReplaceRegex(Regexes.Lexers.UselessTableAlign, string.Empty).SplitRegex(Regexes.Lexers.TableSplitter));
            var cells      = match.Groups[3].Value.ReplaceRegex(Regexes.Lexers.EndWithNewLine, string.Empty).Split('\n').Select(x => new string[] { x }).ToArray();

            for (int i = 0; i < cells.Length; i++)
            {
                cells[i] = cells[i][0].SplitRegex(Regexes.Lexers.TableSplitter);
            }
            return(new TwoPhaseBlockToken(
                       this,
                       parser.Context,
                       sourceInfo,
                       (p, t) => new MarkdownTableBlockToken(
                           t.Rule,
                           t.Context,
                           (from text in header
                            select p.TokenizeInline(t.SourceInfo.Copy(text))).ToImmutableArray(),
                           align.ToImmutableArray(),
                           (from row in cells
                            select(from col in row
                                   select p.TokenizeInline(t.SourceInfo.Copy(col))).ToImmutableArray()).ToImmutableArray(),
                           t.SourceInfo)));
        }
Ejemplo n.º 2
0
        public override IMarkdownToken TryMatch(IMarkdownParser parser, IMarkdownParsingContext context)
        {
            if (parser.Options.LegacyMode)
            {
                return(TryMatchOld(parser, context));
            }
            var match = context.Match(DfmFencesMatcher);

            if (match?.Length > 0)
            {
                var sourceInfo = context.Consume(match.Length);

                // [!code-lang[name](href "optionalTitle")]
                var name  = StringHelper.UnescapeMarkdown(match["name"].GetValue());
                var href  = StringHelper.UnescapeMarkdown(match["href"].GetValue());
                var lang  = match.GetGroup("lang")?.GetValue() ?? string.Empty;
                var title = StringHelper.UnescapeMarkdown(match.GetGroup("title")?.GetValue() ?? string.Empty);
                var queryStringAndFragment = UriUtility.GetQueryStringAndFragment(href);
                var path            = UriUtility.GetPath(href);
                var pathQueryOption =
                    !string.IsNullOrEmpty(queryStringAndFragment) ?
                    ParsePathQueryString(queryStringAndFragment.Remove(1), queryStringAndFragment.Substring(1)) :
                    null;
                return(new DfmFencesBlockToken(this, parser.Context, name, path, sourceInfo, lang, title, pathQueryOption));
            }
            return(null);
        }
        public virtual IMarkdownToken TryMatch(IMarkdownParser parser, IMarkdownParsingContext context)
        {
            if (Heading != Regexes.Block.Heading || parser.Options.LegacyMode)
            {
                return(OldMatch(parser, context));
            }
            var match = context.Match(HeadingMatcher);

            if (match?.Length > 0)
            {
                var sourceInfo = context.Consume(match.Length);
                return(new TwoPhaseBlockToken(
                           this,
                           parser.Context,
                           sourceInfo,
                           (p, t) => new MarkdownHeadingBlockToken(
                               t.Rule,
                               t.Context,
                               p.TokenizeInline(t.SourceInfo.Copy(match["text"].GetValue())),
                               Regex.Replace(match["text"].GetValue().ToLower(), @"[^\p{L}\p{N}\- ]+", "").Replace(' ', '-'),
                               match["level"].Count,
                               t.SourceInfo)));
            }
            return(null);
        }
Ejemplo n.º 4
0
 public override IMarkdownToken TryMatch(IMarkdownParser parser, IMarkdownParsingContext context)
 {
     var c = parser.SwitchContext(MarkdownBlockContext.IsBlockQuote, true);
     var result = base.TryMatch(parser, context);
     parser.SwitchContext(c);
     return result;
 }
Ejemplo n.º 5
0
        public IMarkdownToken TryMatch(IMarkdownParser parser, IMarkdownParsingContext context)
        {
            if (YamlHeader != YamlHeaderRegex)
            {
                return(TryMatchOld(parser, context));
            }
            var match = context.Match(YamlHeaderMatcher);

            if (match?.Length > 0)
            {
                // ---
                // a: b
                // ---
                var value = match["yaml"].GetValue();
                try
                {
                    using (StringReader reader = new StringReader(value))
                    {
                        var result = YamlUtility.Deserialize <Dictionary <string, object> >(reader);
                        if (result == null)
                        {
                            return(null);
                        }
                    }
                }
                catch (Exception)
                {
                    Logger.LogInfo("Invalid yaml header.", file: context.File, line: context.LineNumber.ToString());
                    return(null);
                }
                var sourceInfo = context.Consume(match.Length);
                return(new DfmYamlHeaderBlockToken(this, parser.Context, value, sourceInfo));
            }
            return(null);
        }
Ejemplo n.º 6
0
        public virtual IMarkdownToken TryMatch(IMarkdownParser parser, IMarkdownParsingContext context)
        {
            if (!(bool)parser.Context.Variables[MarkdownBlockContext.IsTop])
            {
                return(null);
            }
            var match = Paragraph.Match(context.CurrentMarkdown);

            if (match.Length == 0)
            {
                return(null);
            }
            var sourceInfo = context.Consume(match.Length);
            var content    = match.Groups[1].Value[match.Groups[1].Value.Length - 1] == '\n'
                ? match.Groups[1].Value.Substring(0, match.Groups[1].Value.Length - 1)
                : match.Groups[1].Value;

            return(new TwoPhaseBlockToken(
                       this,
                       parser.Context,
                       sourceInfo,
                       (p, t) => new MarkdownParagraphBlockToken(
                           t.Rule,
                           t.Context,
                           p.TokenizeInline(t.SourceInfo.Copy(content)),
                           t.SourceInfo)));
        }
Ejemplo n.º 7
0
        public override IMarkdownToken TryMatch(IMarkdownParser parser, IMarkdownParsingContext context)
        {
            var match = RefLink.Match(context.CurrentMarkdown);
            if (match.Length == 0)
            {
                return null;
            }

            var linkStr = match.NotEmpty(2, 1).ReplaceRegex(Regexes.Lexers.WhiteSpaces, " ");

            LinkObj link;
            parser.Links.TryGetValue(linkStr.ToLower(), out link);

            if (string.IsNullOrEmpty(link?.Href))
            {
                var sourceInfo = context.Consume(1);
                var text = match.Value.Remove(1);
                return new MarkdownTextToken(this, parser.Context, text, sourceInfo);
            }
            else
            {
                var sourceInfo = context.Consume(match.Length);
                return GenerateToken(parser, link.Href, link.Title, match.Groups[1].Value, match.Value[0] == '!', sourceInfo, MarkdownLinkType.RefLink, linkStr);
            }
        }
Ejemplo n.º 8
0
        public virtual IMarkdownToken TryMatch(IMarkdownParser parser, IMarkdownParsingContext context)
        {
            var match = AutoLink.Match(context.CurrentMarkdown);

            if (match.Length == 0)
            {
                return(null);
            }
            var sourceInfo = context.Consume(match.Length);

            StringBuffer text;
            StringBuffer href;

            if (match.Groups[2].Value == "@")
            {
                text = match.Groups[1].Value[6] == ':'
                  ? Mangle(parser.Options.Mangle, match.Groups[1].Value.Substring(7))
                  : Mangle(parser.Options.Mangle, match.Groups[1].Value);
                href = Mangle(parser.Options.Mangle, "mailto:") + text;
            }
            else
            {
                text = StringHelper.Escape(match.Groups[1].Value);
                href = text;
            }

            return(new MarkdownLinkInlineToken(
                       this,
                       parser.Context,
                       href,
                       null,
                       ImmutableArray.Create <IMarkdownToken>(
                           new MarkdownRawToken(this, parser.Context, sourceInfo.Copy(text))),
                       sourceInfo));
        }
Ejemplo n.º 9
0
        // process the match
        public IMarkdownToken TryMatch(IMarkdownParser parser, IMarkdownParsingContext context)
        {
            // TODO: This does not process this token from within a code block like

            // ```
            // dotnet tool install lucene-cli -g --version [EnvVar: LuceneNetVersion]
            // ```

            //Console.Write(context.CurrentMarkdown);
            //Console.WriteLine("------------------------------------------------------------------------------");

            var match = EnvironmentVariableUtil.EnvVarRegex.Match(context.CurrentMarkdown);

            if (!match.Success)
            {
                return(null);
            }

            var envVar = match.Groups[1].Value;
            var text   = Environment.GetEnvironmentVariable(envVar);

            if (text == null)
            {
                return(null);
            }

            // 'eat' the characters of the current markdown token so they anr
            var sourceInfo = context.Consume(match.Length);

            return(new MarkdownTextToken(this, parser.Context, text, sourceInfo));
        }
Ejemplo n.º 10
0
        private IMarkdownToken TryMatchOld(IMarkdownParser parser, IMarkdownParsingContext context)
        {
            var match = YamlHeader.Match(context.CurrentMarkdown);

            if (match.Length == 0)
            {
                return(null);
            }

            // ---
            // a: b
            // ---
            var value = match.Groups[1].Value;

            try
            {
                using (StringReader reader = new StringReader(value))
                {
                    var result = YamlUtility.Deserialize <Dictionary <string, object> >(reader);
                    if (result == null)
                    {
                        return(null);
                    }
                }
            }
            catch (Exception)
            {
                Logger.LogInfo("Invalid yaml header.", file: context.File, line: context.LineNumber.ToString());
                return(null);
            }
            var sourceInfo = context.Consume(match.Length);

            return(new DfmYamlHeaderBlockToken(this, parser.Context, value, sourceInfo));
        }
Ejemplo n.º 11
0
        public virtual IMarkdownToken TryMatch(IMarkdownParser parser, IMarkdownParsingContext context)
        {
            var match = AutoLink.Match(context.CurrentMarkdown);
            if (match.Length == 0)
            {
                return null;
            }
            var sourceInfo = context.Consume(match.Length);

            StringBuffer text;
            StringBuffer href;
            if (match.Groups[2].Value == "@")
            {
                text = match.Groups[1].Value[6] == ':'
                  ? Mangle(parser.Options.Mangle, match.Groups[1].Value.Substring(7))
                  : Mangle(parser.Options.Mangle, match.Groups[1].Value);
                href = Mangle(parser.Options.Mangle, "mailto:") + text;
            }
            else
            {
                text = StringHelper.Escape(match.Groups[1].Value);
                href = match.Groups[1].Value;
            }

            return new MarkdownLinkInlineToken(
                this, 
                parser.Context, 
                href, 
                null, 
                ImmutableArray.Create<IMarkdownToken>(
                    new MarkdownRawToken(this, parser.Context, sourceInfo.Copy(text))),
                sourceInfo,
                MarkdownLinkType.AutoLink,
                null);
        }
Ejemplo n.º 12
0
        public override IMarkdownToken TryMatch(IMarkdownParser parser, IMarkdownParsingContext context)
        {
            if (!parser.Context.GetIsInTable())
            {
                if (!parser.Options.LegacyMode)
                {
                    return(null);
                }
            }
            var match = _dfmFencesRegex.Match(context.CurrentMarkdown);

            if (match.Length == 0)
            {
                return(null);
            }
            var sourceInfo = context.Consume(match.Length);

            // [!code-REST-i[name](path "optionalTitle")]
            var name  = match.Groups["name"].Value;
            var path  = match.Groups["path"].Value;
            var lang  = match.Groups["lang"]?.Value;
            var title = match.Groups["title"]?.Value;
            var queryStringAndFragment = match.Groups["option"]?.Value + match.Groups["optionValue"]?.Value;

            if (!parser.Context.GetIsInTable())
            {
                Logger.LogWarning("Inline code snippet is only allowed inside tables.", line: sourceInfo.LineNumber.ToString(), code: WarningCodes.Markdown.InvalidInlineCodeSnippet);
            }
            return(new DfmFencesBlockToken(this, parser.Context, name, path, sourceInfo, lang, title, queryStringAndFragment));
        }
Ejemplo n.º 13
0
        public IMarkdownToken TryMatch(IMarkdownParser parser, IMarkdownParsingContext context)
        {
            if (Include != _incRegex)
            {
                return(TryMatchOld(parser, context));
            }
            var match = context.Match(IncludeMatcher);

            if (match?.Length > 0)
            {
                var sourceInfo = context.Consume(match.Length);

                // [!include[name](path "title")]
                var path  = match["path"].GetValue();
                var name  = match["name"].GetValue();
                var title = match.GetGroup("title")?.GetValue() ?? string.Empty;

                return(new DfmIncludeBlockToken(
                           this,
                           parser.Context,
                           StringHelper.UnescapeMarkdown(path),
                           StringHelper.UnescapeMarkdown(name),
                           StringHelper.UnescapeMarkdown(title),
                           sourceInfo));
            }
            return(null);
        }
Ejemplo n.º 14
0
        public IMarkdownToken TryMatch(IMarkdownParser parser, IMarkdownParsingContext context)
        {
            var match = YamlHeader.Match(context.CurrentMarkdown);
            if (match.Length == 0)
            {
                return null;
            }

            // ---
            // a: b
            // ---
            var value = match.Groups[1].Value;
            try
            {
                using (StringReader reader = new StringReader(value))
                {
                    var result = YamlUtility.Deserialize<Dictionary<string, object>>(reader);
                    if (result == null)
                    {
                        return null;
                    }
                }
            }
            catch (Exception)
            {
                return null;
            }
            var sourceInfo = context.Consume(match.Length);
            return new DfmYamlHeaderBlockToken(this, parser.Context, value, sourceInfo);
        }
Ejemplo n.º 15
0
        public virtual IMarkdownToken TryMatch(IMarkdownParser parser, IMarkdownParsingContext context)
        {
            if (context.IsInParagraph)
            {
                return(null);
            }
            if (Code != Regexes.Block.Code)
            {
                return(TryMatchOld(parser, context));
            }
            var match = context.Match(CodeMatcher);

            if (match?.Length > 0)
            {
                var sourceInfo = context.Consume(match.Length);
                var capStr     = Regexes.Lexers.LeadingWhiteSpaces.Replace(sourceInfo.Markdown, string.Empty);
                if (parser.Options.Pedantic)
                {
                    return(new MarkdownCodeBlockToken(this, parser.Context, capStr, null, sourceInfo));
                }
                else
                {
                    return(new MarkdownCodeBlockToken(this, parser.Context, Regexes.Lexers.TailingEmptyLine.Replace(capStr, string.Empty), null, sourceInfo));
                }
            }
            return(null);
        }
Ejemplo n.º 16
0
        public virtual IMarkdownToken TryMatch(IMarkdownParser engine, IMarkdownParsingContext context)
        {
            var match = AzureHtmlMetadataRegex.Match(context.CurrentMarkdown);

            if (match.Length == 0)
            {
                return(null);
            }

            var sourceInfo = context.Consume(match.Length);

            if (!engine.Context.Variables.TryGetValue("path", out object currentFilePath))
            {
                Logger.LogWarning($"Can't get path for setting azure ms.assetid. Won't set it.");
                currentFilePath = string.Empty;
            }

            var metadata = GetAttributesFromHtmlContent(match.Value);

            metadata.Properties["ms.assetid"] = $"{AzureDocumentPrefix}/{Path.GetFileNameWithoutExtension(currentFilePath.ToString())}";
            if (metadata == null)
            {
                return(new MarkdownTextToken(this, engine.Context, match.Value, sourceInfo));
            }
            return(new AzureHtmlMetadataBlockToken(this, engine.Context, metadata.Properties, metadata.Tags, sourceInfo));
        }
Ejemplo n.º 17
0
        public virtual IMarkdownToken TryMatch(IMarkdownParser parser, IMarkdownParsingContext context)
        {
            var match = Emoji.Match(context.CurrentMarkdown);

            if (match.Length == 0)
            {
                return(null);
            }
            var shortCode = match.Groups[1].Value;
            var text      = GetEmoji(shortCode);

            if (text == null)
            {
                return(null);
            }
            else
            {
                var sourceInfo = context.Consume(match.Length);
                return(new GfmEmojiInlineToken(
                           this,
                           parser.Context,
                           shortCode,
                           text,
                           sourceInfo));
            }
        }
Ejemplo n.º 18
0
        public IMarkdownToken TryMatch(IMarkdownParser parser, IMarkdownParsingContext context)
        {
            if (MarkdownInlineContext.GetIsInLink(parser.Context))
            {
                return(null);
            }
            var match = XrefShortcutRegexWithQuote.Match(context.CurrentMarkdown);

            if (match.Length == 0)
            {
                match = XrefShortcutRegex.Match(context.CurrentMarkdown);
                if (match.Length == 0)
                {
                    return(null);
                }
            }

            var sourceInfo = context.Consume(match.Length);

            // @String=>cap[2]=String, @'string'=>cap[2]=string
            // For cross-reference, add ~/ prefix
            var content = match.Groups["uid"].Value;

            return(new DfmXrefInlineToken(this, parser.Context, content, ImmutableArray <IMarkdownToken> .Empty, null, false, sourceInfo));
        }
        public virtual IMarkdownToken TryMatch(IMarkdownParser parser, IMarkdownParsingContext context)
        {
            if (MarkdownInlineContext.GetIsInLink(parser.Context))
            {
                return(null);
            }
            var match = Url.Match(context.CurrentMarkdown);

            if (match.Length == 0)
            {
                return(null);
            }
            var text = StringHelper.Escape(match.Groups[1].Value);

            if (!Uri.IsWellFormedUriString(text, UriKind.RelativeOrAbsolute))
            {
                return(null);
            }

            var sourceInfo = context.Consume(match.Length);

            return(new MarkdownLinkInlineToken(
                       this,
                       parser.Context,
                       text,
                       null,
                       ImmutableArray.Create <IMarkdownToken>(
                           new MarkdownRawToken(this, parser.Context, sourceInfo.Copy(match.Groups[1].Value))),
                       sourceInfo,
                       MarkdownLinkType.UrlLink,
                       null));
        }
Ejemplo n.º 20
0
        public virtual IMarkdownToken TryMatch(IMarkdownParser parser, IMarkdownParsingContext context)
        {
            if ((bool)parser.Context.Variables[MarkdownInlineContext.IsInLink])
            {
                return null;
            }
            var match = Url.Match(context.CurrentMarkdown);
            if (match.Length == 0)
            {
                return null;
            }
            var text = StringHelper.Escape(match.Groups[1].Value);
            if (!Uri.IsWellFormedUriString(text, UriKind.RelativeOrAbsolute))
            {
                return null;
            }

            var sourceInfo = context.Consume(match.Length);
            return new MarkdownLinkInlineToken(
                this,
                parser.Context,
                text,
                null,
                ImmutableArray.Create<IMarkdownToken>(
                    new MarkdownRawToken(this, parser.Context, sourceInfo.Copy(match.Groups[1].Value))),
                sourceInfo,
                MarkdownLinkType.UrlLink,
                null);
        }
Ejemplo n.º 21
0
        public override IMarkdownToken TryMatch(IMarkdownParser parser, IMarkdownParsingContext context)
        {
            var match = NoLink.Match(context.CurrentMarkdown);

            if (match.Length == 0)
            {
                return(null);
            }
            if (MarkdownInlineContext.GetIsInLink(parser.Context) && match.Value[0] != '!')
            {
                return(null);
            }

            var linkStr = match.NotEmpty(2, 1).ReplaceRegex(Regexes.Lexers.WhiteSpaces, " ");

            LinkObj link;

            parser.Links.TryGetValue(linkStr.ToLower(), out link);

            if (string.IsNullOrEmpty(link?.Href))
            {
                var text       = match.Value.Remove(1);
                var sourceInfo = context.Consume(1);
                return(new MarkdownTextToken(
                           this,
                           parser.Context,
                           text,
                           sourceInfo));
            }
            else
            {
                var sourceInfo = context.Consume(match.Length);
                return(GenerateToken(parser, link.Href, link.Title, match.Groups[1].Value, match.Value[0] == '!', sourceInfo, MarkdownLinkType.NumberLink, null));
            }
        }
Ejemplo n.º 22
0
        public override IMarkdownToken TryMatch(IMarkdownParser parser, IMarkdownParsingContext context)
        {
            var c      = parser.SwitchContext(MarkdownBlockContext.IsBlockQuote, true);
            var result = base.TryMatch(parser, context);

            parser.SwitchContext(c);
            return(result);
        }
Ejemplo n.º 23
0
 private static void LogMessage(IMarkdownParsingContext context)
 {
     Logger.Log(
         (context.LineNumber == 1 ? LogLevel.Warning : LogLevel.Info),
         "Invalid yaml header.",
         file: context.File,
         line: context.LineNumber.ToString(),
         code: WarningCodes.Markdown.InvalidYamlHeader);
 }
Ejemplo n.º 24
0
 public virtual IMarkdownToken TryMatch(IMarkdownParser parser, IMarkdownParsingContext context)
 {
     var match = Del.Match(context.CurrentMarkdown);
     if (match.Length == 0)
     {
         return null;
     }
     var sourceInfo = context.Consume(match.Length);
     return new GfmDelInlineToken(this, parser.Context, parser.Tokenize(sourceInfo.Copy(match.Groups[1].Value)), sourceInfo);
 }
Ejemplo n.º 25
0
 public IMarkdownToken TryMatch(IMarkdownParser parser, IMarkdownParsingContext context)
 {
     var match = Fences.Match(context.CurrentMarkdown);
     if (match.Length == 0)
     {
         return null;
     }
     var sourceInfo = context.Consume(match.Length);
     return new MarkdownCodeBlockToken(this, parser.Context, match.Groups[3].Value, match.Groups[2].Value, sourceInfo);
 }
Ejemplo n.º 26
0
        public virtual IMarkdownToken TryMatch(IMarkdownParser parser, IMarkdownParsingContext context)
        {
            var match = context.Match(_sdnsMatcher);

            return(match?.Length > 0 ? new SDNSBlockToken(
                       this,
                       parser.Context,
                       context.Consume(match.Length),
                       match.GetGroup("text").Value.GetValue()) : null);
        }
Ejemplo n.º 27
0
        public override IMarkdownToken TryMatch(IMarkdownParser parser, IMarkdownParsingContext context)
        {
            var token = base.TryMatch(parser, context);

            if (token is TwoPhaseBlockToken tp)
            {
                return(new TwoPhaseBlockToken(tp, tp.Context.SetIsInTable()));
            }
            return(token);
        }
Ejemplo n.º 28
0
 public virtual IMarkdownToken TryMatch(IMarkdownParser parser, IMarkdownParsingContext context)
 {
     var match = Br.Match(context.CurrentMarkdown);
     if (match.Length == 0)
     {
         return null;
     }
     var sourceInfo = context.Consume(match.Length);
     return new MarkdownBrInlineToken(this, parser.Context, sourceInfo);
 }
Ejemplo n.º 29
0
 public virtual IMarkdownToken TryMatch(IMarkdownParser parser, IMarkdownParsingContext context)
 {
     var match = Em.Match(context.CurrentMarkdown);
     if (match.Length == 0)
     {
         return null;
     }
     var sourceInfo = context.Consume(match.Length);
     return new MarkdownEmInlineToken(this, parser.Context, parser.Tokenize(sourceInfo.Copy(match.NotEmpty(2, 1))), sourceInfo);
 }
Ejemplo n.º 30
0
 public virtual IMarkdownToken TryMatch(IMarkdownParser parser, IMarkdownParsingContext context)
 {
     var match = EscapedText.Match(context.CurrentMarkdown);
     if (match.Length == 0)
     {
         return null;
     }
     var sourceInfo = context.Consume(match.Length);
     return new MarkdownTextToken(this, parser.Context, StringHelper.Escape(match.Groups[1].Value), sourceInfo);
 }
Ejemplo n.º 31
0
        public IMarkdownToken TryMatch(IMarkdownParser parser, IMarkdownParsingContext context)
        {
            var match = context.Match(_FencesMatcher);

            if (match?.Length > 0)
            {
                var sourceInfo = context.Consume(match.Length);
                return(new MarkdownCodeBlockToken(this, parser.Context, match["code"].GetValue(), match["lang"].GetValue(), sourceInfo));
            }
            return(null);
        }
Ejemplo n.º 32
0
        public virtual IMarkdownToken TryMatch(IMarkdownParser parser, IMarkdownParsingContext context)
        {
            if (context.IsInParagraph)
            {
                return(null);
            }
            if (Html != Regexes.Block.Html)
            {
                return(TryMatchOld(parser, context));
            }
            var match = context.Match(HtmlMatcher);

            if (match?.Length > 0)
            {
                var sourceInfo = context.Consume(match.Length);

                var  elementName = match.GetGroup("element")?.GetValue();
                bool isPre       = parser.Options.Sanitizer == null &&
                                   ("pre".Equals(elementName, StringComparison.OrdinalIgnoreCase) || "script".Equals(elementName, StringComparison.OrdinalIgnoreCase) || "style".Equals(elementName, StringComparison.OrdinalIgnoreCase));
                if (parser.Options.Sanitize)
                {
                    return(new TwoPhaseBlockToken(
                               this,
                               parser.Context,
                               sourceInfo,
                               (p, t) => new MarkdownParagraphBlockToken(
                                   t.Rule,
                                   t.Context,
                                   p.TokenizeInline(t.SourceInfo),
                                   t.SourceInfo)));
                }
                else
                {
                    return(new TwoPhaseBlockToken(
                               this,
                               parser.Context,
                               sourceInfo,
                               (p, t) => new MarkdownHtmlBlockToken(
                                   t.Rule,
                                   t.Context,
                                   isPre ?
                                   new InlineContent(
                                       ImmutableArray.Create <IMarkdownToken>(
                                           new MarkdownRawToken(
                                               this,
                                               parser.Context,
                                               t.SourceInfo)))
                            :
                                   p.TokenizeInline(t.SourceInfo),
                                   t.SourceInfo)));
                }
            }
            return(null);
        }
Ejemplo n.º 33
0
        public virtual IMarkdownToken TryMatch(IMarkdownParser parser, IMarkdownParsingContext context)
        {
            var match = LabelRegex.Match(context.CurrentMarkdown);

            if (match.Length == 0)
            {
                return(null);
            }
            var sourceInfo = context.Consume(match.Length);

            return(new LuceneNoteBlockToken(this, parser.Context, match.Groups[1].Value, sourceInfo));
        }
Ejemplo n.º 34
0
        public virtual IMarkdownToken TryMatch(IMarkdownParser parser, IMarkdownParsingContext context)
        {
            var match = EscapedText.Match(context.CurrentMarkdown);

            if (match.Length == 0)
            {
                return(null);
            }
            var sourceInfo = context.Consume(match.Length);

            return(new MarkdownTextToken(this, parser.Context, StringHelper.Escape(match.Groups[1].Value), sourceInfo));
        }
Ejemplo n.º 35
0
        public virtual IMarkdownToken TryMatch(IMarkdownParser parser, IMarkdownParsingContext context)
        {
            var match = Code.Match(context.CurrentMarkdown);

            if (match.Length == 0)
            {
                return(null);
            }
            var sourceInfo = context.Consume(match.Length);

            return(new MarkdownCodeInlineToken(this, parser.Context, match.Groups[2].Value, sourceInfo));
        }
Ejemplo n.º 36
0
        private IMarkdownToken TryMatchOld(IMarkdownParser parser, IMarkdownParsingContext context)
        {
            var match = DfmNoteRegex.Match(context.CurrentMarkdown);

            if (match.Length == 0)
            {
                return(null);
            }
            var sourceInfo = context.Consume(match.Groups["rawmarkdown"].Length);

            return(new DfmNoteBlockToken(this, parser.Context, match.Groups["notetype"].Value, match.Groups["rawmarkdown"].Value, sourceInfo));
        }
        private IMarkdownToken TryMatchOld(IMarkdownParser parser, IMarkdownParsingContext context)
        {
            var match = PreElement.Match(context.CurrentMarkdown);

            if (match.Length == 0)
            {
                return(null);
            }
            var sourceInfo = context.Consume(match.Length);

            return(new MarkdownRawToken(this, parser.Context, sourceInfo));
        }
Ejemplo n.º 38
0
        public virtual IMarkdownToken TryMatch(IMarkdownParser parser, IMarkdownParsingContext context)
        {
            var match = Hr.Match(context.CurrentMarkdown);

            if (match.Length == 0)
            {
                return(null);
            }
            var sourceInfo = context.Consume(match.Length);

            return(new MarkdownHrBlockToken(this, parser.Context, sourceInfo));
        }
Ejemplo n.º 39
0
        public IMarkdownToken TryMatch(IMarkdownParser parser, IMarkdownParsingContext context)
        {
            var match = Xref.Match(context.CurrentMarkdown);

            if (match.Length == 0)
            {
                return(null);
            }
            var sourceInfo = context.Consume(match.Length);

            return(new MarkdownTextToken(this, parser.Context, match.Groups[0].Value, sourceInfo));
        }
Ejemplo n.º 40
0
        public virtual IMarkdownToken TryMatch(IMarkdownParser parser, IMarkdownParsingContext context)
        {
            var match = Em.Match(context.CurrentMarkdown);

            if (match.Length == 0)
            {
                return(null);
            }
            var sourceInfo = context.Consume(match.Length);

            return(new MarkdownEmInlineToken(this, parser.Context, parser.Tokenize(sourceInfo.Copy(match.NotEmpty(2, 1))), sourceInfo));
        }
Ejemplo n.º 41
0
 public override IMarkdownToken TryMatch(IMarkdownParser parser, IMarkdownParsingContext context)
 {
     var match = Link.Match(context.CurrentMarkdown);
     if (match.Length == 0)
     {
         return null;
     }
     if (IsEscape(match.Groups[1].Value) || IsEscape(match.Groups[2].Value))
     {
         return null;
     }
     var sourceInfo = context.Consume(match.Length);
     return GenerateToken(parser, match.Groups[2].Value, match.Groups[4].Value, match.Groups[1].Value, match.Value[0] == '!', sourceInfo, MarkdownLinkType.NormalLink, null);
 }
Ejemplo n.º 42
0
 public virtual IMarkdownToken TryMatch(IMarkdownParser engine, IMarkdownParsingContext context)
 {
     if (!engine.Context.Variables.ContainsKey(MarkdownBlockContext.IsBlockQuote) || !(bool)engine.Context.Variables[MarkdownBlockContext.IsBlockQuote])
     {
         return null;
     }
     var match = AzureNoteRegex.Match(context.CurrentMarkdown);
     if (match.Length == 0)
     {
         return null;
     }
     var sourceInfo = context.Consume(match.Groups["rawmarkdown"].Length);
     return new AzureNoteBlockToken(this, engine.Context, match.Groups["notetype"].Value, match.Groups["rawmarkdown"].Value, sourceInfo);
 }
Ejemplo n.º 43
0
        public IMarkdownToken TryMatch(IMarkdownParser engine, IMarkdownParsingContext context)
        {
            var match = AzureMigrationVideoRegex.Match(context.CurrentMarkdown);
            if (match.Length == 0)
            {
                return null;
            }
            var sourceInfo = context.Consume(match.Length);

            // Sample: [AZURE.VIDEO video-id-string]. Get video id here
            var videoId = match.Groups[1].Value;

            return new AzureVideoBlockToken(this, engine.Context, videoId, sourceInfo);
        }
Ejemplo n.º 44
0
 public virtual IMarkdownToken TryMatch(IMarkdownParser parser, IMarkdownParsingContext context)
 {
     if (!parser.Context.Variables.ContainsKey(MarkdownBlockContext.IsBlockQuote) || !(bool)parser.Context.Variables[MarkdownBlockContext.IsBlockQuote])
     {
         return null;
     }
     var match = _sectionRegex.Match(context.CurrentMarkdown);
     if (match.Length == 0)
     {
         return null;
     }
     var sourceInfo = context.Consume(match.Groups["rawmarkdown"].Length);
     var attributes = ExtractAttibutes(match.Groups["attributes"].Value);
     return new DfmSectionBlockToken(this, parser.Context, attributes, sourceInfo);
 }
Ejemplo n.º 45
0
 public virtual IMarkdownToken TryMatch(IMarkdownParser parser, IMarkdownParsingContext context)
 {
     var match = Def.Match(context.CurrentMarkdown);
     if (match.Length == 0)
     {
         return null;
     }
     var sourceInfo = context.Consume(match.Length);
     parser.Links[match.Groups[1].Value.ToLower()] = new LinkObj
     {
         Href = match.Groups[2].Value,
         Title = match.Groups[3].Value
     };
     return new MarkdownIgnoreToken(this, parser.Context, sourceInfo);
 }
Ejemplo n.º 46
0
 public IMarkdownToken TryMatch(IMarkdownParser parser, IMarkdownParsingContext context)
 {
     var match = XrefAutoLinkRegexWithQuote.Match(context.CurrentMarkdown);
     if (match.Length == 0)
     {
         match = XrefAutoLinkRegex.Match(context.CurrentMarkdown);
         if (match.Length == 0)
         {
             return null;
         }
     }
     var sourceInfo = context.Consume(match.Length);
     var content = match.Groups[2].Value;
     return new DfmXrefInlineToken(this, parser.Context, content, ImmutableArray<IMarkdownToken>.Empty, null, true, sourceInfo);
 }
Ejemplo n.º 47
0
 public virtual IMarkdownToken TryMatch(IMarkdownParser parser, IMarkdownParsingContext context)
 {
     var match = Blockquote.Match(context.CurrentMarkdown);
     if (match.Length == 0)
     {
         return null;
     }
     var sourceInfo = context.Consume(match.Length);
     var capStr = LeadingBlockquote.Replace(sourceInfo.Markdown, string.Empty);
     var blockTokens = parser.Tokenize(sourceInfo.Copy(capStr));
     blockTokens = TokenHelper.CreateParagraghs(parser, this, blockTokens, true, sourceInfo);
     return new MarkdownBlockquoteBlockToken(
         this,
         parser.Context,
         blockTokens,
         sourceInfo);
 }
Ejemplo n.º 48
0
        public override IMarkdownToken TryMatch(IMarkdownParser parser, IMarkdownParsingContext context)
        {
            var match = _dfmFencesRegex.Match(context.CurrentMarkdown);
            if (match.Length == 0)
            {
                return null;
            }
            var sourceInfo = context.Consume(match.Length);

            // [!code-REST-i[name](path "optionalTitle")]
            var name = match.Groups["name"].Value;
            var path = match.Groups["path"].Value;
            var lang = match.Groups["lang"]?.Value;
            var title = match.Groups["title"]?.Value;
            var pathQueryOption = ParsePathQueryString(match.Groups["option"]?.Value, match.Groups["optionValue"]?.Value);

            return new DfmFencesBlockToken(this, parser.Context, name, path, sourceInfo, lang, title, pathQueryOption);
        }
Ejemplo n.º 49
0
        public virtual IMarkdownToken TryMatch(IMarkdownParser parser, IMarkdownParsingContext context)
        {
            var match = Html.Match(context.CurrentMarkdown);
            if (match.Length == 0)
            {
                return null;
            }
            var sourceInfo = context.Consume(match.Length);

            bool isPre = parser.Options.Sanitizer == null &&
                (match.Groups[1].Value == "pre" || match.Groups[1].Value == "script" || match.Groups[1].Value == "style");
            if (parser.Options.Sanitize)
            {
                return new TwoPhaseBlockToken(
                    this,
                    parser.Context,
                    sourceInfo,
                    (p, t) => new MarkdownParagraphBlockToken(
                        t.Rule,
                        t.Context,
                        p.TokenizeInline(t.SourceInfo.Copy(match.Value)),
                        t.SourceInfo));
            }
            else
            {
                return new TwoPhaseBlockToken(
                    this,
                    parser.Context,
                    sourceInfo,
                    (p, t) => new MarkdownHtmlBlockToken(
                        t.Rule,
                        t.Context,
                        isPre ?
                            new InlineContent(
                                ImmutableArray.Create<IMarkdownToken>(
                                    new MarkdownRawToken(
                                        this,
                                        parser.Context,
                                        t.SourceInfo)))
                        :
                            p.TokenizeInline(t.SourceInfo),
                        t.SourceInfo));
            }
        }
Ejemplo n.º 50
0
        public IMarkdownToken TryMatch(IMarkdownParser engine, IMarkdownParsingContext context)
        {
            var match = AzureIncludeRegex.Match(context.CurrentMarkdown);
            if (match.Length == 0)
            {
                return null;
            }
            var sourceInfo = context.Consume(match.Length);

            // [!azure.include[title](path "optionalTitle")]
            // 1. Get include file path 
            var path = match.Groups[2].Value;

            // 2. Get title
            var value = match.Groups[1].Value;
            var title = match.Groups[4].Value;

            if (!TypeForwardedToPathUtility.IsRelativePath(path))
            {
                Logger.LogWarning($"Azure inline include path {path} is not a relative path, can't expand it");
                return new MarkdownTextToken(this, engine.Context, match.Value, sourceInfo);
            }

            // 3. Apply inline rules to the included content
            object currentFilePath;
            if (!engine.Context.Variables.TryGetValue("path", out currentFilePath))
            {
                Logger.LogWarning($"Can't get path for the file that ref azure inline include file, return MarkdownTextToken. Raw: {match.Value}");
                return new MarkdownTextToken(this, engine.Context, match.Value, sourceInfo);
            }

            var includeFilePath = TypeForwardedToPathUtility.NormalizePath(Path.Combine(Path.GetDirectoryName(currentFilePath.ToString()), path));
            if (!File.Exists(includeFilePath))
            {
                Logger.LogWarning($"Can't get include file path {includeFilePath} in the file {currentFilePath}, return MarkdownTextToken. Raw: {match.Value}");
                return new MarkdownTextToken(this, engine.Context, match.Value, sourceInfo);
            }

            return new TwoPhaseBlockToken(this, engine.Context, sourceInfo, (p, t) =>
            {
                var inlineTokens = p.Tokenize(SourceInfo.Create(MarkdownEngine.Normalize(File.ReadAllText(includeFilePath)), includeFilePath));
                return new AzureIncludeInlineToken(t.Rule, t.Context, path, value, title, inlineTokens, match.Groups[0].Value, t.SourceInfo);
            });
        }
Ejemplo n.º 51
0
        public IMarkdownToken TryMatch(IMarkdownParser parser, IMarkdownParsingContext context)
        {
            var match = Include.Match(context.CurrentMarkdown);
            if (match.Length == 0)
            {
                return null;
            }
            var sourceInfo = context.Consume(match.Length);

            // [!include[title](path "optionalTitle")]
            // 1. Get include file path 
            var path = match.Groups[2].Value;

            // 2. Get title
            var value = match.Groups[1].Value;
            var title = match.Groups[4].Value;

            return new DfmIncludeBlockToken(this, parser.Context, path, value, title, match.Groups[0].Value, sourceInfo);
        }
Ejemplo n.º 52
0
        public IMarkdownToken TryMatch(IMarkdownParser engine, IMarkdownParsingContext context)
        {
            var match = AzureIncludeRegex.Match(context.CurrentMarkdown);
            if (match.Length == 0)
            {
                return null;
            }
            var sourceInfo = context.Consume(match.Length);

            // [!azure.include[title](path "optionalTitle")]
            // 1. Get include file path 
            var path = match.Groups[2].Value;

            // 2. Get title
            var name = match.Groups[1].Value;
            var title = match.Groups[4].Value;

            return new AzureMigrationIncludeBlockToken(this, engine.Context, name, path, title, sourceInfo);
        }
Ejemplo n.º 53
0
        public IMarkdownToken TryMatch(IMarkdownParser parser, IMarkdownParsingContext context)
        {
            var match = XrefShortcutRegexWithQuote.Match(context.CurrentMarkdown);
            if (match.Length == 0)
            {
                match = XrefShortcutRegex.Match(context.CurrentMarkdown);
                if (match.Length == 0)
                {
                    return null;
                }
            }

            var sourceInfo = context.Consume(match.Length);

            // @String=>cap[2]=String, @'string'=>cap[2]=string
            // For cross-reference, add ~/ prefix
            var content = match.Groups[2].Value;
            return new DfmXrefInlineToken(this, parser.Context, content, ImmutableArray<IMarkdownToken>.Empty, null, false, sourceInfo);
        }
Ejemplo n.º 54
0
 public virtual IMarkdownToken TryMatch(IMarkdownParser parser, IMarkdownParsingContext context)
 {
     var match = Heading.Match(context.CurrentMarkdown);
     if (match.Length == 0)
     {
         return null;
     }
     var sourceInfo = context.Consume(match.Length);
     return new TwoPhaseBlockToken(
         this,
         parser.Context,
         sourceInfo,
         (p, t) => new MarkdownHeadingBlockToken(
             t.Rule,
             t.Context,
             p.TokenizeInline(t.SourceInfo.Copy(match.Groups[2].Value)),
             Regex.Replace(match.Groups[2].Value.ToLower(), @"[^\p{L}\p{N}\- ]+", "").Replace(' ', '-'),
             match.Groups[1].Value.Length,
             t.SourceInfo));
 }
Ejemplo n.º 55
0
        public IMarkdownToken TryMatch(IMarkdownParser parser, IMarkdownParsingContext context)
        {
            var match = AzureIncludeRegex.Match(context.CurrentMarkdown);
            if (match.Length == 0)
            {
                return null;
            }
            var sourceInfo = context.Consume(match.Length);

            // [!azure.include[title](path "optionalTitle")]
            // 1. Get include file path 
            var path = match.Groups[2].Value;

            // 2. Get title
            var value = match.Groups[1].Value;
            var title = match.Groups[4].Value;

            if (!PathUtility.IsRelativePath(path))
            {
                Logger.LogWarning($"Azure inline include path {path} is not a relative path, can't expand it");
                return new MarkdownTextToken(this, parser.Context, match.Value, sourceInfo);
            }

            object currentFilePath;
            if (!parser.Context.Variables.TryGetValue("path", out currentFilePath))
            {
                Logger.LogWarning($"Can't get path for the file that ref azure block include file, return MarkdownTextToken. Raw: {match.Value}");
                return new MarkdownTextToken(this, parser.Context, match.Value, sourceInfo);
            }

            var includeFilePath = PathUtility.NormalizePath(Path.Combine(Path.GetDirectoryName(currentFilePath.ToString()), path));
            if (!File.Exists(includeFilePath))
            {
                Logger.LogWarning($"Can't get include file path {includeFilePath} in the file {currentFilePath}, return MarkdownTextToken. Raw: {match.Value}");
                return new MarkdownTextToken(this, parser.Context, match.Value, sourceInfo);
            }

            var blockTokens = parser.Tokenize(SourceInfo.Create(MarkdownEngine.Normalize(File.ReadAllText(includeFilePath)), includeFilePath));
            blockTokens = TokenHelper.CreateParagraghs(parser, this, blockTokens, true, sourceInfo);
            return new AzureIncludeBlockToken(this, parser.Context, path, value, title, blockTokens, match.Groups[0].Value, sourceInfo);
        }
Ejemplo n.º 56
0
        public virtual IMarkdownToken TryMatch(IMarkdownParser parser, IMarkdownParsingContext context)
        {
            var match = Tag.Match(context.CurrentMarkdown);
            if (match.Length == 0)
            {
                return null;
            }
            var sourceInfo = context.Consume(match.Length);

            var c = parser.Context;
            var inLink = (bool)c.Variables[MarkdownInlineContext.IsInLink];
            if (!inLink && Regexes.Lexers.StartHtmlLink.IsMatch(match.Value))
            {
                parser.SwitchContext(MarkdownInlineContext.IsInLink, true);
            }
            else if (inLink && Regexes.Lexers.EndHtmlLink.IsMatch(match.Value))
            {
                parser.SwitchContext(MarkdownInlineContext.IsInLink, false);
            }
            return new MarkdownTagInlineToken(this, parser.Context, sourceInfo);
        }
Ejemplo n.º 57
0
 public virtual IMarkdownToken TryMatch(IMarkdownParser parser, IMarkdownParsingContext context)
 {
     var match = StrongEm.Match(context.CurrentMarkdown);
     if (match.Length == 0)
     {
         return null;
     }
     if (match.Groups[1].Length > 0)
     {
         var sourceInfo = context.Consume(match.Groups[1].Length);
         return new MarkdownTextToken(this, parser.Context, match.Groups[1].Value, sourceInfo);
     }
     else
     {
         var sourceInfo = context.Consume(match.Length);
         return new MarkdownStrongInlineToken(
             this,
             parser.Context,
             GetContent(parser, match, sourceInfo),
             sourceInfo);
     }
 }
        public virtual IMarkdownToken TryMatch(IMarkdownParser engine, IMarkdownParsingContext context)
        {
            var match = AzureHtmlMetadataRegex.Match(context.CurrentMarkdown);
            if (match.Length == 0)
            {
                return null;
            }

            var sourceInfo = context.Consume(match.Length);
            object currentFilePath;
            if (!engine.Context.Variables.TryGetValue("path", out currentFilePath))
            {
                Logger.LogWarning($"Can't get path for setting azure ms.assetid. Won't set it.");
                currentFilePath = string.Empty;
            }

            var metadata = GetAttributesFromHtmlContent(match.Value);
            if (metadata == null)
            {
                return new MarkdownTextToken(this, engine.Context, match.Value, sourceInfo);
            }
            return new AzureHtmlMetadataBlockToken(this, engine.Context, metadata.Properties, metadata.Tags, sourceInfo);
        }
Ejemplo n.º 59
0
 public virtual IMarkdownToken TryMatch(IMarkdownParser parser, IMarkdownParsingContext context)
 {
     var match = Emoji.Match(context.CurrentMarkdown);
     if (match.Length == 0)
     {
         return null;
     }
     var shortCode = match.Groups[1].Value;
     var text = GetEmoji(shortCode);
     if (text == null)
     {
         return null;
     }
     else
     {
         var sourceInfo = context.Consume(match.Length);
         return new GfmEmojiInlineToken(
             this,
             parser.Context,
             shortCode,
             text,
             sourceInfo);
     }
 }
Ejemplo n.º 60
0
        public virtual IMarkdownToken TryMatch(IMarkdownParser parser, IMarkdownParsingContext context)
        {
            var match = NpTable.Match(context.CurrentMarkdown);
            if (match.Length == 0)
            {
                return null;
            }
            var header = match.Groups[1].Value.ReplaceRegex(Regexes.Lexers.UselessTableHeader, string.Empty).SplitRegex(Regexes.Lexers.TableSplitter);
            var align = ParseAligns(match.Groups[2].Value.ReplaceRegex(Regexes.Lexers.UselessTableAlign, string.Empty).SplitRegex(Regexes.Lexers.TableSplitter));
            if (header.Length > align.Length)
            {
                return null;
            }
            var sourceInfo = context.Consume(match.Length);

            var rows = (from row in match.Groups[3].Value.ReplaceRegex(Regexes.Lexers.EndWithNewLine, string.Empty).Split('\n')
                        select row.ReplaceRegex(Regexes.Lexers.UselessTableRow, string.Empty)).ToList();
            var cells = new string[rows.Count][];
            for (int i = 0; i < rows.Count; i++)
            {
                var columns = rows[i]
                  .ReplaceRegex(Regexes.Lexers.EmptyGfmTableCell, string.Empty)
                  .SplitRegex(Regexes.Lexers.TableSplitter);
                if (columns.Length == header.Length)
                {
                    cells[i] = columns;
                }
                else if (columns.Length < header.Length)
                {
                    cells[i] = new string[header.Length];
                    for (int j = 0; j < columns.Length; j++)
                    {
                        cells[i][j] = columns[j];
                    }
                    for (int j = columns.Length; j < cells[i].Length; j++)
                    {
                        cells[i][j] = string.Empty;
                    }
                }
                else // columns.Length > header.Length
                {
                    cells[i] = new string[header.Length];
                    for (int j = 0; j < header.Length; j++)
                    {
                        cells[i][j] = columns[j];
                    }
                }
            }

            return new TwoPhaseBlockToken(
                this,
                parser.Context,
                sourceInfo,
                (p, t) =>
                    new MarkdownTableBlockToken(
                        t.Rule,
                        t.Context,
                        (from text in header
                         let si = t.SourceInfo.Copy(text)
                         select new MarkdownTableItemBlockToken(t.Rule, t.Context, p.TokenizeInline(si), si)).ToImmutableArray(),
                        align.ToImmutableArray(),
                        cells.Select(
                            (row, index) =>
                                (from col in row
                                 let si = t.SourceInfo.Copy(col, index + 2)
                                 select new MarkdownTableItemBlockToken(t.Rule, t.Context, p.TokenizeInline(si), si)).ToImmutableArray()
                        ).ToImmutableArray(),
                        t.SourceInfo));
        }