示例#1
0
文件: Md.cs 项目: mattgroy/clean-code
        private static string ParseLine(string markdown)
        {
            var isInsideEmTags  = false;
            var strongTagsPairs = new Stack <(Marker, Marker)>();
            var line            = markdown;

            var tagStack = new Stack <Marker>();

            for (var i = 0; i < line.Length; i++)
            {
                if (line[i] == '\\')
                {
                    line = line.Remove(i, 1);
                    i++;
                }

                var tag = Marker.CreateTag(line, i);

                if (tag == null)
                {
                    continue;
                }
                i += tag.Length - 1;

                if (Marker.IsClosingTag(line, tag.Pos) &&
                    tagStack.Any(openTag => openTag.Type == tag.Type))
                {
                    var tags = Marker.GetTagsPair(line, tag, tagStack);
                    if (tags.openingTag.Type == MarkerType.Em)
                    {
                        isInsideEmTags = false;
                        strongTagsPairs.Clear();
                    }
                    else if (tags.openingTag.Type == MarkerType.Strong && isInsideEmTags)
                    {
                        strongTagsPairs.Push(tags);
                        continue;
                    }

                    line = Marker.ConvertToHtmlTag(line, tags.openingTag, tags.closingTag);
                }

                else if (Marker.IsOpeningTag(line, tag.Pos))
                {
                    if (tag.Type == MarkerType.Em)
                    {
                        isInsideEmTags = true;
                        line           = ConvertStrongTagFromStack(strongTagsPairs, line);
                    }

                    tagStack.Push(tag);
                }
            }

            line = ConvertStrongTagFromStack(strongTagsPairs, line);
            return(line);
        }
示例#2
0
        public static string Parse(string markdown)
        {
            var line = markdown;

            var stackTag = new Stack <Marker>();

            for (var i = 0; i < line.Length; i++)
            {
                if (line[i] == '\\')
                {
                    line = line.Remove(i, 1);
                    continue;
                }
                if (!Marker.TryCreateTag(line, i,
                                         stackTag.Count > 0 ? stackTag.Peek() : null,
                                         out var tag))
                {
                    continue;
                }

                i += tag.Length - 1;

                if (Marker.IsClosingTag(line, tag.Pos) && stackTag.Any(openTag => openTag.Type == tag.Type) && !IsRepeatTag(tag, stackTag.Peek()))
                {
                    var tags = Marker.GetTagsPair(tag, stackTag);
                    line = Marker.ConvertToHtmlTag(line, tags.openingTag, tags.closingTag);
                }

                else if (Marker.IsOpeningTag(line, tag.Pos))
                {
                    stackTag.Push(tag);
                    if (tag.Type == MarkerType.Code)
                    {
                        i = SkipCode(line, i);
                    }
                }
            }
            return(line);
        }