Ejemplo n.º 1
0
        public void Parse(
            TextReader reader,
            Func <IDocumentElement, bool> onBeginProcessElement,
            Func <IDocumentElement, bool> onEndProcessElement)
        {
            _onBeginProcessElement = onBeginProcessElement;
            _onEndProcessElement   = onEndProcessElement;

            _characterStream = new HtmlCharacterStream(reader)
            {
                State = HtmlStates.Data
            };

            _stringParser = new TextParser(_stringBuilderFactory, _characterStream);

            _document = new DocumentElement
            {
                MimeType         = "text/html",
                ConformanceLevel = 1.0f
            };

            if (!Begin(_document))
            {
                return;
            }

            Element currentElement = _document;

            while (!_characterStream.Eof)
            {
                currentElement = ProcessCurrentState(currentElement);
            }

            if (_document.Children == null || _document.Children.Count == 0)
            {
                _document.ConformanceLevel = 0;
            }
            else
            {
                var visibleChildren = _document.Children.Where(e => !e.SuppressOutput).ToList();
                if (visibleChildren.Count == 0 || visibleChildren.All(e => e.ElementType == ElementTypes.RawText))
                {
                    _document.ConformanceLevel = 0;
                }
            }

            End(_document);
        }
        public void Parse(
            TextReader reader,
            Func <IDocumentElement, bool> onBeginProcessElement,
            Func <IDocumentElement, bool> onEndProcessElement)
        {
            _onBeginProcessElement = onBeginProcessElement;
            _onEndProcessElement   = onEndProcessElement;

            _characterStream = new MarkdownCharacterStream(reader)
            {
                State = MarkdownStates.ParagraphBreak
            };
            _stringParser   = new TextParser(_stringBuilderFactory, _characterStream);
            _elementStack   = new Core.Collections.LinkedList <StackedElement>();
            _references     = new Dictionary <string, string>(StringComparer.OrdinalIgnoreCase);
            _anchorsToFixup = new List <AnchorElement>();

            _document = new DocumentElement
            {
                MimeType         = "application/x-markdown",
                ConformanceLevel = 1.0f
            };

            PushElement(_document);

            var line = _stringBuilderFactory.Create();

            while (!_characterStream.Eof)
            {
                line.Clear();
                _stringParser.TakeUntil(line, 1024, '\n', false);
                AddLine(line.ToString());
            }

            while (!(_elementStack.Last().Element is DocumentElement))
            {
                PopElement();
            }

            FixUpReferences();

            End(_document);
        }