Exemplo n.º 1
0
        public static Header Parse(string text, out int length)
        {
            try {
                var attrs = MatchAttribute.GetMatchAttributes <Header>()
                            .Select(x => new {
                    match = x.GetRegex().IsMatch(text),
                    attr  = x
                });

                Match match = attrs.Where(x => x.match)
                              .FirstOrDefault().attr
                              .GetRegex().Match(text);

                Regex regex      = new Regex(@"#+");
                var   headerText = regex.Match(match.Value).Value;

                length = match.Index + match.Length;
                return(new Header()
                {
                    Level = headerText.Length,
                    Children = MarkdownRaw.Parse(match.Value.Replace(headerText, "")
                                                 .Trim()).Children
                });
            }catch (Exception e) {
                throw new FormatException();
            }
        }
Exemplo n.º 2
0
        public static SetextHeader Parse(string text, out int length)
        {
            try {
                var attrs = MatchAttribute.GetMatchAttributes <SetextHeader>()
                            .Select(x => new {
                    match = x.GetRegex().IsMatch(text),
                    attr  = x
                });

                Match match = attrs.Where(x => x.match)
                              .FirstOrDefault().attr
                              .GetRegex().Match(text);

                Regex regex      = new Regex(@"(\s*.+)\r?\n");
                Match title      = regex.Match(match.Value);
                var   headerText = title.Value.Trim();
                var   sy         = match.Value.Substring(title.Length)[0];
                length = match.Index + match.Length;
                return(new SetextHeader()
                {
                    Level = sy == '=' ? 1 : 0,
                    Children = MarkdownRaw.Parse(headerText).Children
                });
            }catch (Exception e) {
                throw new FormatException();
            }
        }
Exemplo n.º 3
0
        public static Italic Parse(string text, out int length)
        {
            var attrs = MatchAttribute.GetMatchAttributes <Italic>()
                        .Select(x => new {
                match = x.GetRegex().IsMatch(text),
                attr  = x
            });

            if (!attrs.Any(x => x.match))
            {
                throw new FormatException();
            }
            var temp  = attrs.Where(x => x.match).FirstOrDefault();
            var match = temp.attr.GetRegex()
                        .Match(text);

            if (temp.attr.Regex == @"^\*[^\*\r\n]+\*")
            {
                text = match.Value.Substring(1, match.Value.Length - 2);
            }
            else
            {
                text = match.Value.Substring(2, match.Value.Length - 4);
            }
            length = match.Index + match.Length;

            return(new Italic()
            {
                Children = MarkdownRaw.Parse(text).Children
            });
        }
Exemplo n.º 4
0
        public static Bold Parse(string text, out int length)
        {
            var attrs = MatchAttribute.GetMatchAttributes <Bold>()
                        .Select(x => new {
                match = x.GetRegex().IsMatch(text),
                attr  = x
            });

            if (!attrs.Any(x => x.match))
            {
                throw new FormatException();
            }
            var match = attrs.Where(x => x.match).FirstOrDefault().attr.GetRegex()
                        .Match(text);

            length = match.Index + match.Length;
            text   = match.Value.Substring(2, match.Value.Length - 4);
            return(new Bold()
            {
                Children = MarkdownRaw.Parse(text).Children
            });
        }