public void FlushBlock()
        {
            if (inBody == 0)
            {
                if (inBody == 0 && Sharpen.Runtime.EqualsIgnoreCase("TITLE", lastStartTag))
                {
                    SetTitle(tokenBuilder.ToString().Trim());
                }
                textBuilder.Length  = 0;
                tokenBuilder.Length = 0;
                return;
            }

            int length = tokenBuilder.Length;

            if (length == 0)
            {
                return;
            }
            else if (length == 1)
            {
                if (sbLastWasWhitespace)
                {
                    textBuilder.Length  = 0;
                    tokenBuilder.Length = 0;
                    return;
                }
            }

            string[] tokens              = UnicodeTokenizer.Tokenize(tokenBuilder);
            int      numWords            = 0;
            int      numLinkedWords      = 0;
            int      numWrappedLines     = 0;
            int      currentLineLength   = -1;      // don't count the first space
            int      maxLineLength       = 80;
            int      numTokens           = 0;
            int      numWordsCurrentLine = 0;

            foreach (string token in tokens)
            {
                if (token == ANCHOR_TEXT_START)
                {
                    inAnchorText = true;
                }
                else
                {
                    if (token == ANCHOR_TEXT_END)
                    {
                        inAnchorText = false;
                    }
                    else
                    {
                        if (IsWord(token))
                        {
                            numTokens++;
                            numWords++;
                            numWordsCurrentLine++;

                            if (inAnchorText)
                            {
                                numLinkedWords++;
                            }
                            int tokenLength = token.Length;
                            currentLineLength += tokenLength + 1;
                            if (currentLineLength > maxLineLength)
                            {
                                numWrappedLines++;
                                currentLineLength   = tokenLength;
                                numWordsCurrentLine = 1;
                            }
                        }
                        else
                        {
                            numTokens++;
                        }
                    }
                }
            }
            if (numTokens == 0)
            {
                return;
            }
            int numWordsInWrappedLines;

            if (numWrappedLines == 0)
            {
                numWordsInWrappedLines = numWords;
                numWrappedLines        = 1;
            }
            else
            {
                numWordsInWrappedLines = numWords - numWordsCurrentLine;
            }
            TextBlock tb = new TextBlock(textBuilder.ToString().Trim(), currentContainedTextElements
                                         , numWords, numLinkedWords, numWordsInWrappedLines, numWrappedLines, offsetBlocks
                                         );

            currentContainedTextElements = new BitSet();
            offsetBlocks++;
            textBuilder.Length  = 0;
            tokenBuilder.Length = 0;
            tb.SetTagLevel(blockTagLevel);
            AddTextBlock(tb);
            blockTagLevel = -1;
        }
示例#2
0
        public void FlushBlock()
        {
            if (InBody == 0)
            {
                if ("TITLE".Equals(_lastStartTag, StringComparison.CurrentCultureIgnoreCase) && InBody == 0)
                {
                    Title = TokenBuffer.ToString().Trim();
                }
                _textBuffer.Length = 0;
                TokenBuffer.Length = 0;
                return;
            }

            int length = TokenBuffer.Length;

            switch (length)
            {
            case 0:
                return;

            case 1:
                if (SbLastWasWhitespace)
                {
                    _textBuffer.Length = 0;
                    TokenBuffer.Length = 0;
                    return;
                }
                break;
            }
            string[] tokens = UnicodeTokenizer.Tokenize(TokenBuffer);

            int       numWords            = 0;
            int       numLinkedWords      = 0;
            int       numWrappedLines     = 0;
            int       currentLineLength   = -1; // don't count the first space
            const int maxLineLength       = 80;
            int       numTokens           = 0;
            int       numWordsCurrentLine = 0;

            foreach (string token in tokens)
            {
                if (ANCHOR_TEXT_START.Equals(token))
                {
                    _inAnchorText = true;
                }
                else if (ANCHOR_TEXT_END.Equals(token))
                {
                    _inAnchorText = false;
                }
                else if (IsWord(token))
                {
                    numTokens++;
                    numWords++;
                    numWordsCurrentLine++;
                    if (_inAnchorText)
                    {
                        numLinkedWords++;
                    }
                    int tokenLength = token.Length;
                    currentLineLength += tokenLength + 1;
                    if (currentLineLength > maxLineLength)
                    {
                        numWrappedLines++;
                        currentLineLength   = tokenLength;
                        numWordsCurrentLine = 1;
                    }
                }
                else
                {
                    numTokens++;
                }
            }
            if (numTokens == 0)
            {
                return;
            }
            int numWordsInWrappedLines;

            if (numWrappedLines == 0)
            {
                numWordsInWrappedLines = numWords;
                numWrappedLines        = 1;
            }
            else
            {
                numWordsInWrappedLines = numWords - numWordsCurrentLine;
            }

            var tb = new TextBlock(
                _textBuffer.ToString().Trim(),
                _currentContainedTextElements,
                numWords,
                numLinkedWords,
                numWordsInWrappedLines,
                numWrappedLines,
                _offsetBlocks);

            _currentContainedTextElements = new BitArray(short.MaxValue);

            _offsetBlocks++;

            _textBuffer.Length = 0;
            TokenBuffer.Length = 0;

            tb.TagLevel = _blockTagLevel;
            AddTextBlock(tb);
            _blockTagLevel = -1;
        }