Beispiel #1
0
        public static string Markup(
            string src,
            string path = null,
            IReadOnlyDictionary <string, AzureFileInfo> azureMarkdownFileInfoMapping = null,
            IReadOnlyDictionary <string, AzureVideoInfo> azureVideoInfoMapping       = null,
            IReadOnlyDictionary <string, AzureFileInfo> azureResourceFileInfoMapping = null)
        {
            var engine  = (MarkdownEngine)_builder.CreateEngine(_renderer);
            var context = engine.Context;

            if (path != null)
            {
                context = engine.Context.CreateContext(engine.Context.Variables.SetItem("path", path));
            }

            if (azureMarkdownFileInfoMapping != null && azureMarkdownFileInfoMapping.Count != 0)
            {
                context = context.CreateContext(context.Variables.SetItem("azureMarkdownFileInfoMapping", azureMarkdownFileInfoMapping));
            }

            if (azureVideoInfoMapping != null && azureVideoInfoMapping.Count != 0)
            {
                context = context.CreateContext(context.Variables.SetItem("azureVideoInfoMapping", azureVideoInfoMapping));
            }

            if (azureResourceFileInfoMapping != null && azureResourceFileInfoMapping.Count != 0)
            {
                context = context.CreateContext(context.Variables.SetItem("azureResourceFileInfoMapping", azureResourceFileInfoMapping));
            }

            return(engine.Mark(SourceInfo.Create(MarkdownEngine.Normalize(src), path), context));
        }
Beispiel #2
0
 public virtual StringBuffer Render(MarkdownEngine engine, DfmXrefInlineToken token, MarkdownInlineContext context)
 {
     var href = token.Href == null ? string.Empty : $" href=\"{StringHelper.HtmlEncode(token.Href)}\"";
     var name = token.Name == null ? null : StringHelper.HtmlEncode(token.Name);
     var title = token.Title == null ? null : $" title=\"{StringHelper.HtmlEncode(token.Title)}\"";
     return $"<xref{href}{title}>{name}</xref>";
 }
Beispiel #3
0
        public IMarkdownToken TryMatch(MarkdownEngine engine, ref string source)
        {
            var match = YamlHeader.Match(source);
            if (match.Length == 0)
            {
                return null;
            }

            // ---
            // a: b
            // ---
            var value = match.Groups[1].Value;
            try
            {
                using (StringReader reader = new StringReader(value))
                    YamlUtility.Deserialize<Dictionary<string, object>>(reader);
            }
            catch (Exception)
            {
                return null;
            }

            source = source.Substring(match.Length);
            return new DfmYamlHeaderBlockToken(this, value);
        }
Beispiel #4
0
        public IMarkdownToken TryMatch(MarkdownEngine engine, ref string source)
        {
            var match = YamlHeader.Match(source);

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

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

            try
            {
                using (StringReader reader = new StringReader(value))
                    YamlUtility.Deserialize <Dictionary <string, object> >(reader);
            }
            catch (Exception)
            {
                return(null);
            }

            source = source.Substring(match.Length);
            return(new DfmYamlHeaderBlockToken(this, value));
        }
Beispiel #5
0
        public void Transform_givenImages_generatesExpectedResult()
        {
            var text     = LoadText("Images.md");
            var markdown = new MarkdownEngine();
            var result   = markdown.Transform(text);

            Approvals.Verify(AsXaml(result));
        }
Beispiel #6
0
        public virtual StringBuffer Render(MarkdownEngine engine, DfmXrefInlineToken token, MarkdownInlineContext context)
        {
            var href  = token.Href == null ? string.Empty : $" href=\"{StringHelper.HtmlEncode(token.Href)}\"";
            var name  = token.Name == null ? null : StringHelper.HtmlEncode(token.Name);
            var title = token.Title == null ? null : $" title=\"{StringHelper.HtmlEncode(token.Title)}\"";

            return($"<xref{href}{title}>{name}</xref>");
        }
Beispiel #7
0
        public void Transform_givenLinksInline_generatesExpectedResult()
        {
            var text     = LoadText("Links_inline_style.md");
            var markdown = new MarkdownEngine();
            var result   = markdown.Transform(text);

            Approvals.Verify(AsXaml(result));
        }
Beispiel #8
0
        private string GenerateYamlHeaderContent(IReadOnlyDictionary <string, string> properties, IReadOnlyDictionary <string, string> tags)
        {
            var propertiesSw = new StringWriter();

            YamlUtility.Serialize(propertiesSw, properties);
            var tagsSw = new StringWriter();

            YamlUtility.Serialize(tagsSw, tags);
            return(MarkdownEngine.Normalize(propertiesSw.ToString() + "\n" + tagsSw.ToString()));
        }
 public virtual IMarkdownToken TryMatch(MarkdownEngine engine, ref string source)
 {
     var match = _sectionEnd.Match(source);
     if (match.Length == 0)
     {
         return null;
     }
     source = source.Substring(match.Length);
     
     return new DfmSectionEndBlockToken(this);
 }
        public virtual IMarkdownToken TryMatch(MarkdownEngine engine, ref string source)
        {
            var match = _sectionBegin.Match(source);
            if (match.Length == 0)
            {
                return null;
            }
            source = source.Substring(match.Length);

            var attributes = ExtractAttibutes(match.Groups["attributes"].Value);
            return new DfmSectionBeginBlockToken(this, attributes);
        }
        public virtual IMarkdownToken TryMatch(MarkdownEngine engine, ref string source)
        {
            var match = _sectionEnd.Match(source);

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

            return(new DfmSectionEndBlockToken(this));
        }
Beispiel #12
0
        public override StringBuffer Render(MarkdownEngine engine, MarkdownBlockquoteBlockToken token, MarkdownBlockContext context)
        {
            if (token.Tokens.Length > 0)
            {
                var ft = token.Tokens[0] as DfmNoteBlockToken;
                if (ft != null)
                {
                    return($"<blockquote class=\"{ft.NoteType}\">" + RenderTokens(engine, token.Tokens.RemoveAt(0), context, true, token.Rule) + "</blockquote>");
                }
            }

            return(base.Render(engine, token, context));
        }
Beispiel #13
0
        public override StringBuffer Render(MarkdownEngine engine, MarkdownBlockquoteBlockToken token, MarkdownBlockContext context)
        {
            if (token.Tokens.Length > 0)
            {
                var ft = token.Tokens[0] as DfmNoteBlockToken;
                if (ft != null)
                {
                    return $"<blockquote class=\"{ft.NoteType}\">" + RenderTokens(engine, token.Tokens.RemoveAt(0), context, true, token.Rule) + "</blockquote>";
                }
            }

            return base.Render(engine, token, context);
        }
 public override IMarkdownToken TryMatch(MarkdownEngine engine, ref string source)
 {
     var match = Blockquote.Match(source);
     if (match.Length == 0)
     {
         return null;
     }
     source = source.Substring(match.Length);
     var capStr = LeadingBlockquote.Replace(match.Value, string.Empty);
     var c = engine.SwitchContext(MarkdownBlockContext.IsBlockQuote, true);
     var tokens = engine.Tokenize(capStr);
     engine.SwitchContext(c);
     return new DfmBlockquoteBlockToken(this, tokens);
 }
Beispiel #15
0
        public IMarkdownToken TryMatch(MarkdownEngine engine, ref string source)
        {
            var match = Xref.Match(source);
            if (match.Length == 0)
            {
                return null;
            }
            source = source.Substring(match.Length);

            // @String=>cap[3]=String, @'string'=>cap[2]=string
            // For cross-reference, add ~/ prefix
            var content = string.IsNullOrEmpty(match.Groups[2].Value) ? match.Groups[3].Value : match.Groups[2].Value;
            return new DfmXrefInlineToken(this, content, null, null);
        }
        public virtual IMarkdownToken TryMatch(MarkdownEngine engine, ref string source)
        {
            var match = _sectionBegin.Match(source);

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

            var attributes = ExtractAttibutes(match.Groups["attributes"].Value);

            return(new DfmSectionBeginBlockToken(this, attributes));
        }
Beispiel #17
0
 public virtual IMarkdownToken TryMatch(MarkdownEngine engine, ref string source)
 {
     if (!engine.Context.Variables.ContainsKey(MarkdownBlockContext.IsBlockQuote) || !(bool)engine.Context.Variables[MarkdownBlockContext.IsBlockQuote])
     {
         return null;
     }
     var match = _dfmNoteRegex.Match(source);
     if (match.Length == 0)
     {
         return null;
     }
     source = source.Substring(match.Length);
     return new DfmNoteBlockToken(this, match.Groups["notetype"].Value, match.Groups["rawmarkdown"].Value);
 }
Beispiel #18
0
        public virtual IMarkdownToken TryMatch(MarkdownEngine engine, ref string source)
        {
            if (!engine.Context.Variables.ContainsKey(MarkdownBlockContext.IsBlockQuote) || !(bool)engine.Context.Variables[MarkdownBlockContext.IsBlockQuote])
            {
                return(null);
            }
            var match = _dfmNoteRegex.Match(source);

            if (match.Length == 0)
            {
                return(null);
            }
            source = source.Substring(match.Length);
            return(new DfmNoteBlockToken(this, match.Groups["notetype"].Value, match.Groups["rawmarkdown"].Value));
        }
        public override IMarkdownToken TryMatch(MarkdownEngine engine, ref string source)
        {
            var match = Blockquote.Match(source);

            if (match.Length == 0)
            {
                return(null);
            }
            source = source.Substring(match.Length);
            var capStr = LeadingBlockquote.Replace(match.Value, string.Empty);
            var c      = engine.SwitchContext(MarkdownBlockContext.IsBlockQuote, true);
            var tokens = engine.Tokenize(capStr);

            engine.SwitchContext(c);
            return(new DfmBlockquoteBlockToken(this, tokens));
        }
Beispiel #20
0
        public IMarkdownToken TryMatch(MarkdownEngine engine, ref string source)
        {
            var match = Xref.Match(source);

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

            // @String=>cap[3]=String, @'string'=>cap[2]=string
            // For cross-reference, add ~/ prefix
            var content = string.IsNullOrEmpty(match.Groups[2].Value) ? match.Groups[3].Value : match.Groups[2].Value;

            return(new DfmXrefInlineToken(this, content, null, null));
        }
Beispiel #21
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);
            }));
        }
        public IMarkdownToken TryMatch(IMarkdownParser engine, ref string source)
        {
            var match = AzureIncludeRegex.Match(source);

            if (match.Length == 0)
            {
                return(null);
            }
            source = source.Substring(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, engine.Context, match.Value, match.Value));
            }

            object currentFilePath;

            if (!engine.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, engine.Context, match.Value, match.Value));
            }

            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, engine.Context, match.Value, match.Value));
            }

            return(new TwoPhaseBlockToken(this, engine.Context, match.Value, (p, t) =>
            {
                var blockTokens = p.Tokenize(MarkdownEngine.Normalize(File.ReadAllText(includeFilePath)));
                blockTokens = TokenHelper.ParseInlineToken(p, t.Rule, blockTokens, true);
                return new AzureIncludeBlockToken(t.Rule, t.Context, path, value, title, blockTokens, match.Groups[0].Value, match.Value);
            }));
        }
Beispiel #23
0
        public virtual IMarkdownToken TryMatch(MarkdownEngine engine, ref string source)
        {
            var match = _dfmFencesRegex.Match(source);
            if (match.Length == 0)
            {
                return null;
            }
            source = source.Substring(match.Length);

            // [!code-REST-i[title](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;

            return new DfmFencesBlockToken(this, name, path, lang, title);
        }
            public IMarkdownToken Validate(MarkdownEngine engine, MarkdownTagInlineToken token)
            {
                var  m            = OpeningTag.Match(token.Content);
                bool isOpeningTag = true;

                if (m.Length == 0)
                {
                    m = ClosingTag.Match(token.Content);
                    if (m.Length == 0)
                    {
                        return(null);
                    }
                    isOpeningTag = false;
                }

                return(ValidateCore(token, m, isOpeningTag));
            }
Beispiel #25
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));
        }
Beispiel #26
0
        public virtual IMarkdownToken TryMatch(MarkdownEngine engine, ref string source)
        {
            var match = _dfmFencesRegex.Match(source);

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

            // [!code-REST-i[title](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;

            return(new DfmFencesBlockToken(this, name, path, lang, title));
        }
Beispiel #27
0
        public IMarkdownToken TryMatch(MarkdownEngine engine, ref string source)
        {
            var match = Include.Match(source);
            if (match.Length == 0)
            {
                return null;
            }
            source = source.Substring(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, path, value, title, match.Groups[0].Value);
        }
Beispiel #28
0
        public IMarkdownToken TryMatch(MarkdownEngine engine, ref string source)
        {
            var match = Include.Match(source);

            if (match.Length == 0)
            {
                return(null);
            }
            source = source.Substring(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, path, value, title, match.Groups[0].Value));
        }
Beispiel #29
0
 public IMarkdownToken Rewrite(MarkdownEngine engine, IMarkdownToken token)
 {
     return(null);
 }
 public abstract IMarkdownToken TryMatch(MarkdownEngine engine, ref string source);
 public abstract IMarkdownToken TryMatch(MarkdownEngine engine, ref string source);
Beispiel #32
0
 public virtual StringBuffer Render(MarkdownEngine engine, DfmYamlHeaderBlockToken token, MarkdownBlockContext context)
 {
     var content = token.Content == null ? string.Empty : StringHelper.HtmlEncode(token.Content);
     return $"<yamlheader>{content}</yamlheader>";
 }
Beispiel #33
0
 public virtual StringBuffer Render(MarkdownEngine engine, DfmSectionBeginBlockToken token, MarkdownBlockContext context)
 {
     return $"<div{token.Attributes}>";
 }
Beispiel #34
0
 public virtual StringBuffer Render(MarkdownEngine engine, DfmSectionEndBlockToken token, MarkdownBlockContext context)
 {
     return $"</div>";
 }
Beispiel #35
0
        public virtual StringBuffer Render(MarkdownEngine engine, DfmYamlHeaderBlockToken token, MarkdownBlockContext context)
        {
            var content = token.Content == null ? string.Empty : StringHelper.HtmlEncode(token.Content);

            return($"<yamlheader>{content}</yamlheader>");
        }
Beispiel #36
0
 public virtual StringBuffer Render(MarkdownEngine engine, DfmSectionBeginBlockToken token, MarkdownBlockContext context)
 {
     return($"<div{token.Attributes}>");
 }
Beispiel #37
0
 public virtual StringBuffer Render(MarkdownEngine engine, DfmSectionEndBlockToken token, MarkdownBlockContext context)
 {
     return($"</div>");
 }
Beispiel #38
0
 public IMarkdownToken Rewrite(MarkdownEngine engine, IMarkdownToken token)
 {
     return null;
 }