<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">###############################################################################
#
#   Package: NaturalDocs::Parser::Native
#
###############################################################################
#
#   A package that converts comments from Natural Docs' native format into &lt;NaturalDocs::Parser::ParsedTopic&gt; objects.
#   Unlike most second-level packages, these are packages and not object classes.
#
###############################################################################

# This file is part of Natural Docs, which is Copyright (C) 2003-2008 Greg Valure
# Natural Docs is licensed under the GPL


use strict;
use integer;

package NaturalDocs::Parser::Native;


###############################################################################
# Group: Variables


# Return values of TagType().  Not documented here.
use constant POSSIBLE_OPENING_TAG =&gt; 1;
use constant POSSIBLE_CLOSING_TAG =&gt; 2;
use constant NOT_A_TAG =&gt; 3;


#
#   var: package
#
#   A &lt;SymbolString&gt; representing the package normal topics will be a part of at the current point in the file.  This is a package variable
#   because it needs to be reserved between function calls.
#
my $package;

#
#   hash: functionListIgnoredHeadings
#
#   An existence hash of all the headings that prevent the parser from creating function list symbols.  Whenever one of
#   these headings are used in a function list topic, symbols are not created from definition lists until the next heading.  The keys
#   are in all lowercase.
#
my %functionListIgnoredHeadings = ( 'parameters' =&gt; 1,
                                                       'parameter' =&gt; 1,
                                                       'params' =&gt; 1,
                                                       'param' =&gt; 1,
                                                       'arguments' =&gt; 1,
                                                       'argument' =&gt; 1,
                                                       'args' =&gt; 1,
                                                       'arg' =&gt; 1 );


###############################################################################
# Group: Interface Functions


#
#   Function: Start
#
#   This will be called whenever a file is about to be parsed.  It allows the package to reset its internal state.
#
sub Start
    {
    my ($self) = @_;
    $package = undef;
    };


#
#   Function: IsMine
#
#   Examines the comment and returns whether it is *definitely* Natural Docs content, i.e. it is owned by this package.  Note
#   that a comment can fail this function and still be interpreted as a Natural Docs content, for example a JavaDoc-styled comment
#   that doesn't have header lines but no JavaDoc tags either.
#
#   Parameters:
#
#       commentLines - An arrayref of the comment lines.  Must have been run through &lt;NaturalDocs::Parser-&gt;CleanComment()&gt;.
#       isJavaDoc - Whether the comment was JavaDoc-styled.
#
#   Returns:
#
#       Whether the comment is *definitely* Natural Docs content.
#
sub IsMine #(string[] commentLines, bool isJavaDoc)
    {
    my ($self, $commentLines, $isJavaDoc) = @_;

    # Skip to the first line with content.
    my $line = 0;

    while ($line &lt; scalar @$commentLines &amp;&amp; !length $commentLines-&gt;[$line])
        {  $line++;  };

    return $self-&gt;ParseHeaderLine($commentLines-&gt;[$line]);
    };



#
#   Function: ParseComment
#
#   This will be called whenever a comment capable of containing Natural Docs content is found.
#
#   Parameters:
#
#       commentLines - An arrayref of the comment lines.  Must have been run through &lt;NaturalDocs::Parser-&gt;CleanComment()&gt;.
#                               *The original memory will be changed.*
#       isJavaDoc - Whether the comment is JavaDoc styled.
#       lineNumber - The line number of the first of the comment lines.
#       parsedTopics - A reference to the array where any new &lt;NaturalDocs::Parser::ParsedTopics&gt; should be placed.
#
#   Returns:
#
#       The number of parsed topics added to the array, or zero if none.
#
sub ParseComment #(commentLines, isJavaDoc, lineNumber, parsedTopics)
    {
    my ($self, $commentLines, $isJavaDoc, $lineNumber, $parsedTopics) = @_;

    my $topicCount = 0;
    my $prevLineBlank = 1;
    my $inCodeSection = 0;

    my ($type, $scope, $isPlural, $title, $symbol);
    #my $package;  # package variable.
    my ($newKeyword, $newTitle);

    my $index = 0;

    my $bodyStart = 0;
    my $bodyEnd = 0;  # Not inclusive.

    while ($index &lt; scalar @$commentLines)
        {
        # Everything but leading whitespace was removed beforehand.

        # If we're in a code section...
        if ($inCodeSection)
            {
            if ($commentLines-&gt;[$index] =~ /^ *\( *(?:end|finish|done)(?: +(?:table|code|example|diagram))? *\)$/i)
                {  $inCodeSection = undef;  };

            $prevLineBlank = 0;
            $bodyEnd++;
            }

        # If the line is empty...
        elsif (!length($commentLines-&gt;[$index]))
            {
            $prevLineBlank = 1;

            if ($topicCount)
                {  $bodyEnd++;  };
            }

        # If the line has a recognized header and the previous line is blank...
        elsif ($prevLineBlank &amp;&amp; (($newKeyword, $newTitle) = $self-&gt;ParseHeaderLine($commentLines-&gt;[$index])) )
            {
            # Process the previous one, if any.

            if ($topicCount)
                {
                if ($scope == ::SCOPE_START() || $scope == ::SCOPE_END())
                    {  $package = undef;  };

                my $body = $self-&gt;FormatBody($commentLines, $bodyStart, $bodyEnd, $type, $isPlural);
                my $newTopic = $self-&gt;MakeParsedTopic($type, $title, $package, $body, $lineNumber + $bodyStart - 1, $isPlural);
                push @$parsedTopics, $newTopic;

                $package = $newTopic-&gt;Package();
                };

            $title = $newTitle;

            my $typeInfo;
            ($type, $typeInfo, $isPlural) = NaturalDocs::Topics-&gt;KeywordInfo($newKeyword);
            $scope = $typeInfo-&gt;Scope();

            $bodyStart = $index + 1;
            $bodyEnd = $index + 1;

            $topicCount++;

            $prevLineBlank = 0;
            }

        # If we're on a non-empty, non-header line of a JavaDoc-styled comment and we haven't started a topic yet...
        elsif ($isJavaDoc &amp;&amp; !$topicCount)
            {
            $type = undef;
            $scope = ::SCOPE_NORMAL();  # The scope repair and topic merging processes will handle if this is a class topic.
            $isPlural = undef;
            $title = undef;
            $symbol = undef;

            $bodyStart = $index;
            $bodyEnd = $index + 1;

            $topicCount++;

            $prevLineBlank = undef;
            }

        # If we're on a normal content line within a topic
        elsif ($topicCount)
            {
            $prevLineBlank = 0;
            $bodyEnd++;

            if ($commentLines-&gt;[$index] =~ /^ *\( *(?:(?:start|begin)? +)?(?:table|code|example|diagram) *\)$/i)
                {  $inCodeSection = 1;  };
            };


        $index++;
        };


    # Last one, if any.  This is the only one that gets the prototypes.
    if ($bodyStart)
        {
        if ($scope == ::SCOPE_START() || $scope == ::SCOPE_END())
            {  $package = undef;  };

        my $body = $self-&gt;FormatBody($commentLines, $bodyStart, $bodyEnd, $type, $isPlural);
        my $newTopic = $self-&gt;MakeParsedTopic($type, $title, $package, $body, $lineNumber + $bodyStart - 1, $isPlural);
        push @$parsedTopics, $newTopic;
        $topicCount++;

        $package = $newTopic-&gt;Package();
        };

    return $topicCount;
    };


#
#   Function: ParseHeaderLine
#
#   If the passed line is a topic header, returns the array ( keyword, title ).  Otherwise returns an empty array.
#
sub ParseHeaderLine #(line)
    {
    my ($self, $line) = @_;

    if ($line =~ /^ *([a-z0-9 ]*[a-z0-9]): +(.*)$/i)
        {
        my ($keyword, $title) = ($1, $2);

        # We need to do it this way because if you do "if (ND:T-&gt;KeywordInfo($keyword)" and the last element of the array it
        # returns is false, the statement is false.  That is really retarded, but there it is.
        my ($type, undef, undef) = NaturalDocs::Topics-&gt;KeywordInfo($keyword);

        if ($type)
            {  return ($keyword, $title);  }
        else
            {  return ( );  };
        }
    else
        {  return ( );  };
    };



###############################################################################
# Group: Support Functions


#
#   Function: MakeParsedTopic
#
#   Creates a &lt;NaturalDocs::Parser::ParsedTopic&gt; object for the passed parameters.  Scope is gotten from
#   the package variable &lt;package&gt; instead of from the parameters.  The summary is generated from the body.
#
#   Parameters:
#
#       type         - The &lt;TopicType&gt;.  May be undef for headerless topics.
#       title          - The title of the topic.  May be undef for headerless topics.
#       package    - The package &lt;SymbolString&gt; the topic appears in.
#       body        - The topic's body in &lt;NDMarkup&gt;.
#       lineNumber - The topic's line number.
#       isList         - Whether the topic is a list.
#
#   Returns:
#
#       The &lt;NaturalDocs::Parser::ParsedTopic&gt; object.
#
sub MakeParsedTopic #(type, title, package, body, lineNumber, isList)
    {
    my ($self, $type, $title, $package, $body, $lineNumber, $isList) = @_;

    my $summary;

    if (defined $body)
        {  $summary = NaturalDocs::Parser-&gt;GetSummaryFromBody($body);  };

    return NaturalDocs::Parser::ParsedTopic-&gt;New($type, $title, $package, undef, undef, $summary,
                                                                         $body, $lineNumber, $isList);
    };


#
#    Function: FormatBody
#
#    Converts the section body to &lt;NDMarkup&gt;.
#
#    Parameters:
#
#       commentLines - The arrayref of comment lines.
#       startingIndex  - The starting index of the body to format.
#       endingIndex   - The ending index of the body to format, *not* inclusive.
#       type               - The type of the section.  May be undef for headerless comments.
#       isList              - Whether it's a list topic.
#
#    Returns:
#
#        The body formatted in &lt;NDMarkup&gt;.
#
sub FormatBody #(commentLines, startingIndex, endingIndex, type, isList)
    {
    my ($self, $commentLines, $startingIndex, $endingIndex, $type, $isList) = @_;

    use constant TAG_NONE =&gt; 1;
    use constant TAG_PARAGRAPH =&gt; 2;
    use constant TAG_BULLETLIST =&gt; 3;
    use constant TAG_DESCRIPTIONLIST =&gt; 4;
    use constant TAG_HEADING =&gt; 5;
    use constant TAG_PREFIXCODE =&gt; 6;
    use constant TAG_TAGCODE =&gt; 7;

    my %tagEnders = ( TAG_NONE() =&gt; '',
                                 TAG_PARAGRAPH() =&gt; '&lt;/p&gt;',
                                 TAG_BULLETLIST() =&gt; '&lt;/li&gt;&lt;/ul&gt;',
                                 TAG_DESCRIPTIONLIST() =&gt; '&lt;/dd&gt;&lt;/dl&gt;',
                                 TAG_HEADING() =&gt; '&lt;/h&gt;',
                                 TAG_PREFIXCODE() =&gt; '&lt;/code&gt;',
                                 TAG_TAGCODE() =&gt; '&lt;/code&gt;' );

    my $topLevelTag = TAG_NONE;

    my $output;
    my $textBlock;
    my $prevLineBlank = 1;

    my $codeBlock;
    my $removedCodeSpaces;

    my $ignoreListSymbols;

    my $index = $startingIndex;

    while ($index &lt; $endingIndex)
        {
        # If we're in a tagged code section...
        if ($topLevelTag == TAG_TAGCODE)
            {
            if ($commentLines-&gt;[$index] =~ /^ *\( *(?:end|finish|done)(?: +(?:table|code|example|diagram))? *\)$/i)
                {
                $codeBlock =~ s/\n+$//;
                $output .= NaturalDocs::NDMarkup-&gt;ConvertAmpChars($codeBlock) . '&lt;/code&gt;';
                $codeBlock = undef;
                $topLevelTag = TAG_NONE;
                $prevLineBlank = undef;
                }
            else
                {
                $self-&gt;AddToCodeBlock($commentLines-&gt;[$index], \$codeBlock, \$removedCodeSpaces);
                };
            }

        # If the line starts with a code designator...
        elsif ($commentLines-&gt;[$index] =~ /^ *[&gt;:|](.*)$/)
            {
            my $code = $1;

            if ($topLevelTag == TAG_PREFIXCODE)
                {
                $self-&gt;AddToCodeBlock($code, \$codeBlock, \$removedCodeSpaces);
                }
            else # $topLevelTag != TAG_PREFIXCODE
                {
                if (defined $textBlock)
                    {
                    $output .= $self-&gt;RichFormatTextBlock($textBlock) . $tagEnders{$topLevelTag};
                    $textBlock = undef;
                    };

                $topLevelTag = TAG_PREFIXCODE;
                $output .= '&lt;code&gt;';
                $self-&gt;AddToCodeBlock($code, \$codeBlock, \$removedCodeSpaces);
                };
            }

        # If we're not in either code style...
        else
            {
            # Strip any leading whitespace.
            $commentLines-&gt;[$index] =~ s/^ +//;

            # If we were in a prefixed code section...
            if ($topLevelTag == TAG_PREFIXCODE)
                {
                $codeBlock =~ s/\n+$//;
                $output .= NaturalDocs::NDMarkup-&gt;ConvertAmpChars($codeBlock) . '&lt;/code&gt;';
                $codeBlock = undef;
                $topLevelTag = TAG_NONE;
                $prevLineBlank = undef;
                };


            # If the line is blank...
            if (!length($commentLines-&gt;[$index]))
                {
                # End a paragraph.  Everything else ignores it for now.
                if ($topLevelTag == TAG_PARAGRAPH)
                    {
                    $output .= $self-&gt;RichFormatTextBlock($textBlock) . '&lt;/p&gt;';
                    $textBlock = undef;
                    $topLevelTag = TAG_NONE;
                    };

                $prevLineBlank = 1;
                }

            # If the line starts with a bullet...
            elsif ($commentLines-&gt;[$index] =~ /^[-\*o+] +([^ ].*)$/ &amp;&amp;
                    substr($1, 0, 2) ne '- ')  # Make sure "o - Something" is a definition, not a bullet.
                {
                my $bulletedText = $1;

                if (defined $textBlock)
                    {  $output .= $self-&gt;RichFormatTextBlock($textBlock);  };

                if ($topLevelTag == TAG_BULLETLIST)
                    {
                    $output .= '&lt;/li&gt;&lt;li&gt;';
                    }
                else #($topLevelTag != TAG_BULLETLIST)
                    {
                    $output .= $tagEnders{$topLevelTag} . '&lt;ul&gt;&lt;li&gt;';
                    $topLevelTag = TAG_BULLETLIST;
                    };

                $textBlock = $bulletedText;

                $prevLineBlank = undef;
                }

            # If the line looks like a description list entry...
            elsif ($commentLines-&gt;[$index] =~ /^(.+?) +- +([^ ].*)$/ &amp;&amp; $topLevelTag != TAG_PARAGRAPH)
                {
                my $entry = $1;
                my $description = $2;

                if (defined $textBlock)
                    {  $output .= $self-&gt;RichFormatTextBlock($textBlock);  };

                if ($topLevelTag == TAG_DESCRIPTIONLIST)
                    {
                    $output .= '&lt;/dd&gt;';
                    }
                else #($topLevelTag != TAG_DESCRIPTIONLIST)
                    {
                    $output .= $tagEnders{$topLevelTag} . '&lt;dl&gt;';
                    $topLevelTag = TAG_DESCRIPTIONLIST;
                    };

                if (($isList &amp;&amp; !$ignoreListSymbols) || $type eq ::TOPIC_ENUMERATION())
                    {
                    $output .= '&lt;ds&gt;' . NaturalDocs::NDMarkup-&gt;ConvertAmpChars($entry) . '&lt;/ds&gt;&lt;dd&gt;';
                    }
                else
                    {
                    $output .= '&lt;de&gt;' . NaturalDocs::NDMarkup-&gt;ConvertAmpChars($entry) . '&lt;/de&gt;&lt;dd&gt;';
                    };

                $textBlock = $description;

                $prevLineBlank = undef;
                }

            # If the line could be a header...
            elsif ($prevLineBlank &amp;&amp; $commentLines-&gt;[$index] =~ /^(.*)([^ ]):$/)
                {
                my $headerText = $1 . $2;

                if (defined $textBlock)
                    {
                    $output .= $self-&gt;RichFormatTextBlock($textBlock);
                    $textBlock = undef;
                    }

                $output .= $tagEnders{$topLevelTag};
                $topLevelTag = TAG_NONE;

                $output .= '&lt;h&gt;' . $self-&gt;RichFormatTextBlock($headerText) . '&lt;/h&gt;';

                if ($type eq ::TOPIC_FUNCTION() &amp;&amp; $isList)
                    {
                    $ignoreListSymbols = exists $functionListIgnoredHeadings{lc($headerText)};
                    };

                $prevLineBlank = undef;
                }

            # If the line looks like a code tag...
            elsif ($commentLines-&gt;[$index] =~ /^\( *(?:(?:start|begin)? +)?(?:table|code|example|diagram) *\)$/i)
                {
                if (defined $textBlock)
                    {
                    $output .= $self-&gt;RichFormatTextBlock($textBlock);
                    $textBlock = undef;
                    };

                $output .= $tagEnders{$topLevelTag} . '&lt;code&gt;';
                $topLevelTag = TAG_TAGCODE;
                }

            # If the line looks like an inline image...
            elsif ($commentLines-&gt;[$index] =~ /^(\( *see +)([^\)]+?)( *\))$/i)
                {
                if (defined $textBlock)
                    {
                    $output .= $self-&gt;RichFormatTextBlock($textBlock);
                    $textBlock = undef;
                    };

                $output .= $tagEnders{$topLevelTag};
                $topLevelTag = TAG_NONE;

                $output .= '&lt;img mode="inline" target="' . NaturalDocs::NDMarkup-&gt;ConvertAmpChars($2) . '" '
                                . 'original="' . NaturalDocs::NDMarkup-&gt;ConvertAmpChars($1 . $2 . $3) . '"&gt;';

                $prevLineBlank = undef;
                }

            # If the line isn't any of those, we consider it normal text.
            else
                {
                # A blank line followed by normal text ends lists.  We don't handle this when we detect if the line's blank because
                # we don't want blank lines between list items to break the list.
                if ($prevLineBlank &amp;&amp; ($topLevelTag == TAG_BULLETLIST || $topLevelTag == TAG_DESCRIPTIONLIST))
                    {
                    $output .= $self-&gt;RichFormatTextBlock($textBlock) . $tagEnders{$topLevelTag} . '&lt;p&gt;';

                    $topLevelTag = TAG_PARAGRAPH;
                    $textBlock = undef;
                    }

                elsif ($topLevelTag == TAG_NONE)
                    {
                    $output .= '&lt;p&gt;';
                    $topLevelTag = TAG_PARAGRAPH;
                    # textBlock will already be undef.
                    };

                if (defined $textBlock)
                    {  $textBlock .= ' ';  };

                $textBlock .= $commentLines-&gt;[$index];

                $prevLineBlank = undef;
                };
            };

        $index++;
        };

    # Clean up anything left dangling.
    if (defined $textBlock)
        {
        $output .= $self-&gt;RichFormatTextBlock($textBlock) . $tagEnders{$topLevelTag};
        }
    elsif (defined $codeBlock)
        {
        $codeBlock =~ s/\n+$//;
        $output .= NaturalDocs::NDMarkup-&gt;ConvertAmpChars($codeBlock) . '&lt;/code&gt;';
        };

    return $output;
    };


#
#   Function: AddToCodeBlock
#
#   Adds a line of text to a code block, handling all the indentation processing required.
#
#   Parameters:
#
#       line - The line of text to add.
#       codeBlockRef - A reference to the code block to add it to.
#       removedSpacesRef - A reference to a variable to hold the number of spaces removed.  It needs to be stored between calls.
#                                      It will reset itself automatically when the code block codeBlockRef points to is undef.
#
sub AddToCodeBlock #(line, codeBlockRef, removedSpacesRef)
    {
    my ($self, $line, $codeBlockRef, $removedSpacesRef) = @_;

    $line =~ /^( *)(.*)$/;
    my ($spaces, $code) = ($1, $2);

    if (!defined $$codeBlockRef)
        {
        if (length($code))
            {
            $$codeBlockRef = $code . "\n";
            $$removedSpacesRef = length($spaces);
            };
        # else ignore leading line breaks.
        }

    elsif (length $code)
        {
        # Make sure we have the minimum amount of spaces to the left possible.
        if (length($spaces) != $$removedSpacesRef)
            {
            my $spaceDifference = abs( length($spaces) - $$removedSpacesRef );
            my $spacesToAdd = ' ' x $spaceDifference;

            if (length($spaces) &gt; $$removedSpacesRef)
                {
                $$codeBlockRef .= $spacesToAdd;
                }
            else
                {
                $$codeBlockRef =~ s/^(.)/$spacesToAdd . $1/gme;
                $$removedSpacesRef = length($spaces);
                };
            };

        $$codeBlockRef .= $code . "\n";
        }

    else # (!length $code)
        {
        $$codeBlockRef .= "\n";
        };
    };


#
#   Function: RichFormatTextBlock
#
#   Applies rich &lt;NDMarkup&gt; formatting to a chunk of text.  This includes both amp chars, formatting tags, and link tags.
#
#   Parameters:
#
#       text - The block of text to format.
#
#   Returns:
#
#       The formatted text block.
#
sub RichFormatTextBlock #(text)
    {
    my ($self, $text) = @_;
    my $output;


    # First find bare urls, e-mail addresses, and images.  We have to do this before the split because they may contain underscores
    # or asterisks.  We have to mark the tags with \x1E and \x1F so they don't get confused with angle brackets from the comment.
    # We can't convert the amp chars beforehand because we need lookbehinds in the regexps below and they need to be
    # constant length.  Sucks, huh?

    $text =~ s{
                       # The previous character can't be an alphanumeric or an opening angle bracket.
                       (?&lt;!  [a-z0-9&lt;]  )

                       # Optional mailto:.  Ignored in output.
                       (?:mailto\:)?

                       # Begin capture
                       (

                       # The user portion.  Alphanumeric and - _.  Dots can appear between, but not at the edges or more than
                       # one in a row.
                       (?:  [a-z0-9\-_]+  \.  )*   [a-z0-9\-_]+

                       @

                       # The domain.  Alphanumeric and -.  Dots same as above, however, there must be at least two sections
                       # and the last one must be two to four alphanumeric characters (.com, .uk, .info, .203 for IP addresses)
                       (?:  [a-z0-9\-]+  \.  )+  [a-z]{2,4}

                       # End capture.
                       )

                       # The next character can't be an alphanumeric, which should prevent .abcde from matching the two to
                       # four character requirement, or a closing angle bracket.
                       (?!  [a-z0-9&gt;]  )

                       }

                       {"\x1E" . 'email target="' . NaturalDocs::NDMarkup-&gt;ConvertAmpChars($1) . '" '
                       . 'name="' . NaturalDocs::NDMarkup-&gt;ConvertAmpChars($1) . '"' . "\x1F"}igxe;

    $text =~ s{
                       # The previous character can't be an alphanumeric or an opening angle bracket.
                       (?&lt;!  [a-z0-9&lt;]  )

                       # Begin capture.
                       (

                       # URL must start with one of the acceptable protocols.
                       (?:http|https|ftp|news|file)\:

                       # The acceptable URL characters as far as I know.
                       [a-z0-9\-\=\~\@\#\%\&amp;\_\+\/\;\:\?\*\.\,]*

                       # The URL characters minus period and comma.  If it ends on them, they're probably intended as
                       # punctuation.
                       [a-z0-9\-\=\~\@\#\%\&amp;\_\+\/\;\:\?\*]

                       # End capture.
                       )

                       # The next character must not be an acceptable character or a closing angle bracket.  This will prevent the URL
                       # from ending early just to get a match.
                       (?!  [a-z0-9\-\=\~\@\#\%\&amp;\_\+\/\;\:\?\*\&gt;]  )

                       }

                       {"\x1E" . 'url target="' . NaturalDocs::NDMarkup-&gt;ConvertAmpChars($1) . '" '
                       . 'name="' . NaturalDocs::NDMarkup-&gt;ConvertAmpChars($1) . '"' . "\x1F"}igxe;


    # Find image links.  Inline images should already be pulled out by now.

    $text =~ s{(\( *see +)([^\)]+?)( *\))}
                      {"\x1E" . 'img mode="link" target="' . NaturalDocs::NDMarkup-&gt;ConvertAmpChars($2) . '" '
                        . 'original="' . NaturalDocs::NDMarkup-&gt;ConvertAmpChars($1 . $2 . $3) . '"' . "\x1F"}gie;



    # Split the text from the potential tags.

    my @tempTextBlocks = split(/([\*_&lt;&gt;\x1E\x1F])/, $text);

    # Since the symbols are considered dividers, empty strings could appear between two in a row or at the beginning/end of the
    # array.  This could seriously screw up TagType(), so we need to get rid of them.
    my @textBlocks;

    while (scalar @tempTextBlocks)
        {
        my $tempTextBlock = shift @tempTextBlocks;

        if (length $tempTextBlock)
            {  push @textBlocks, $tempTextBlock;  };
        };


    my $bold;
    my $underline;
    my $underlineHasWhitespace;

    my $index = 0;

    while ($index &lt; scalar @textBlocks)
        {
        if ($textBlocks[$index] eq "\x1E")
            {
            $output .= '&lt;';
            $index++;

            while ($textBlocks[$index] ne "\x1F")
                {
                $output .= $textBlocks[$index];
                $index++;
                };

            $output .= '&gt;';
            }

        elsif ($textBlocks[$index] eq '&lt;' &amp;&amp; $self-&gt;TagType(\@textBlocks, $index) == POSSIBLE_OPENING_TAG)
            {
            my $endingIndex = $self-&gt;ClosingTag(\@textBlocks, $index, undef);

            if ($endingIndex != -1)
                {
                my $linkText;
                $index++;

                while ($index &lt; $endingIndex)
                    {
                    $linkText .= $textBlocks[$index];
                    $index++;
                    };
                # Index will be incremented again at the end of the loop.

                $linkText = NaturalDocs::NDMarkup-&gt;ConvertAmpChars($linkText);

                if ($linkText =~ /^(?:mailto\:)?((?:[a-z0-9\-_]+\.)*[a-z0-9\-_]+@(?:[a-z0-9\-]+\.)+[a-z]{2,4})$/i)
                    {  $output .= '&lt;email target="' . $1 . '" name="' . $1 . '"&gt;';  }
                elsif ($linkText =~ /^(?:http|https|ftp|news|file)\:/i)
                    {  $output .= '&lt;url target="' . $linkText . '" name="' . $linkText . '"&gt;';  }
                else
                    {  $output .= '&lt;link target="' . $linkText . '" name="' . $linkText . '" original="&amp;lt;' . $linkText . '&amp;gt;"&gt;';  };
                }

            else # it's not a link.
                {
                $output .= '&amp;lt;';
                };
            }

        elsif ($textBlocks[$index] eq '*')
            {
            my $tagType = $self-&gt;TagType(\@textBlocks, $index);

            if ($tagType == POSSIBLE_OPENING_TAG &amp;&amp; $self-&gt;ClosingTag(\@textBlocks, $index, undef) != -1)
                {
                # ClosingTag() makes sure tags aren't opened multiple times in a row.
                $bold = 1;
                $output .= '&lt;b&gt;';
                }
            elsif ($bold &amp;&amp; $tagType == POSSIBLE_CLOSING_TAG)
                {
                $bold = undef;
                $output .= '&lt;/b&gt;';
                }
            else
                {
                $output .= '*';
                };
            }

        elsif ($textBlocks[$index] eq '_')
            {
            my $tagType = $self-&gt;TagType(\@textBlocks, $index);

             if ($tagType == POSSIBLE_OPENING_TAG &amp;&amp; $self-&gt;ClosingTag(\@textBlocks, $index, \$underlineHasWhitespace) != -1)
                {
                # ClosingTag() makes sure tags aren't opened multiple times in a row.
                $underline = 1;
                #underlineHasWhitespace is set by ClosingTag().
                $output .= '&lt;u&gt;';
                }
            elsif ($underline &amp;&amp; $tagType == POSSIBLE_CLOSING_TAG)
                {
                $underline = undef;
                #underlineHasWhitespace will be reset by the next opening underline.
                $output .= '&lt;/u&gt;';
                }
            elsif ($underline &amp;&amp; !$underlineHasWhitespace)
                {
                # If there's no whitespace between underline tags, all underscores are replaced by spaces so
                # _some_underlined_text_ becomes &lt;u&gt;some underlined text&lt;/u&gt;.  The standard _some underlined text_
                # will work too.
                $output .= ' ';
                }
            else
                {
                $output .= '_';
                };
            }

        else # plain text or a &gt; that isn't part of a link
            {
            $output .= NaturalDocs::NDMarkup-&gt;ConvertAmpChars($textBlocks[$index]);
           };

        $index++;
        };

    return $output;
    };


#
#   Function: TagType
#
#   Returns whether the tag is a possible opening or closing tag, or neither.  "Possible" because it doesn't check if an opening tag is
#   closed or a closing tag is opened, just whether the surrounding characters allow it to be a candidate for a tag.  For example, in
#   "A _B" the underscore is a possible opening underline tag, but in "A_B" it is not.  Support function for &lt;RichFormatTextBlock()&gt;.
#
#   Parameters:
#
#       textBlocks  - A reference to an array of text blocks.
#       index         - The index of the tag.
#
#   Returns:
#
#       POSSIBLE_OPENING_TAG, POSSIBLE_CLOSING_TAG, or NOT_A_TAG.
#
sub TagType #(textBlocks, index)
    {
    my ($self, $textBlocks, $index) = @_;


    # Possible opening tags

    if ( ( $textBlocks-&gt;[$index] =~ /^[\*_&lt;]$/ ) &amp;&amp;

        # Before it must be whitespace, the beginning of the text, or ({["'-/*_.
        ( $index == 0 || $textBlocks-&gt;[$index-1] =~ /[\ \t\n\(\{\[\"\'\-\/\*\_]$/ ) &amp;&amp;

        # Notes for 2.0: Include Spanish upside down ! and ? as well as opening quotes (66) and apostrophes (6).  Look into
        # Unicode character classes as well.

        # After it must be non-whitespace.
        ( $index + 1 &lt; scalar @$textBlocks &amp;&amp; $textBlocks-&gt;[$index+1] !~ /^[\ \t\n]/) &amp;&amp;

        # Make sure we don't accept &lt;&lt;, &lt;=, &lt;-, or *= as opening tags.
        ( $textBlocks-&gt;[$index] ne '&lt;' || $textBlocks-&gt;[$index+1] !~ /^[&lt;=-]/ ) &amp;&amp;
        ( $textBlocks-&gt;[$index] ne '*' || $textBlocks-&gt;[$index+1] !~ /^[\=\*]/ ) &amp;&amp;

        # Make sure we don't accept * or _ before it unless it's &lt;.
        ( $textBlocks-&gt;[$index] eq '&lt;' || $index == 0 || $textBlocks-&gt;[$index-1] !~ /[\*\_]$/) )
        {
        return POSSIBLE_OPENING_TAG;
        }


    # Possible closing tags

    elsif ( ( $textBlocks-&gt;[$index] =~ /^[\*_&gt;]$/) &amp;&amp;

            # After it must be whitespace, the end of the text, or )}].,!?"';:-/*_.
            ( $index + 1 == scalar @$textBlocks || $textBlocks-&gt;[$index+1] =~ /^[ \t\n\)\]\}\.\,\!\?\"\'\;\:\-\/\*\_]/ ||
              # Links also get plurals, like &lt;link&gt;s, &lt;linx&gt;es, &lt;link&gt;'s, and &lt;links&gt;'.
              ( $textBlocks-&gt;[$index] eq '&gt;' &amp;&amp; $textBlocks-&gt;[$index+1] =~ /^(?:es|s|\')/ ) ) &amp;&amp;

            # Notes for 2.0: Include closing quotes (99) and apostrophes (9).  Look into Unicode character classes as well.

            # Before it must be non-whitespace.
            ( $index != 0 &amp;&amp; $textBlocks-&gt;[$index-1] !~ /[ \t\n]$/ ) &amp;&amp;

            # Make sure we don't accept &gt;&gt;, -&gt;, or =&gt; as closing tags.  &gt;= is already taken care of.
            ( $textBlocks-&gt;[$index] ne '&gt;' || $textBlocks-&gt;[$index-1] !~ /[&gt;=-]$/ ) &amp;&amp;

            # Make sure we don't accept * or _ after it unless it's &gt;.
            ( $textBlocks-&gt;[$index] eq '&gt;' || $textBlocks-&gt;[$index+1] !~ /[\*\_]$/) )
        {
        return POSSIBLE_CLOSING_TAG;
        }

    else
        {
        return NOT_A_TAG;
        };

    };


#
#   Function: ClosingTag
#
#   Returns whether a tag is closed or not, where it's closed if it is, and optionally whether there is any whitespace between the
#   tags.  Support function for &lt;RichFormatTextBlock()&gt;.
#
#   The results of this function are in full context, meaning that if it says a tag is closed, it can be interpreted as that tag in the
#   final output.  It takes into account any spoiling factors, like there being two opening tags in a row.
#
#   Parameters:
#
#       textBlocks             - A reference to an array of text blocks.
#       index                    - The index of the opening tag.
#       hasWhitespaceRef  - A reference to the variable that will hold whether there is whitespace between the tags or not.  If
#                                     undef, the function will not check.  If the tag is not closed, the variable will not be changed.
#
#   Returns:
#
#       If the tag is closed, it returns the index of the closing tag and puts whether there was whitespace between the tags in
#       hasWhitespaceRef if it was specified.  If the tag is not closed, it returns -1 and doesn't touch the variable pointed to by
#       hasWhitespaceRef.
#
sub ClosingTag #(textBlocks, index, hasWhitespace)
    {
    my ($self, $textBlocks, $index, $hasWhitespaceRef) = @_;

    my $hasWhitespace;
    my $closingTag;

    if ($textBlocks-&gt;[$index] eq '*' || $textBlocks-&gt;[$index] eq '_')
        {  $closingTag = $textBlocks-&gt;[$index];  }
    elsif ($textBlocks-&gt;[$index] eq '&lt;')
        {  $closingTag = '&gt;';  }
    else
        {  return -1;  };

    my $beginningIndex = $index;
    $index++;

    while ($index &lt; scalar @$textBlocks)
        {
        if ($textBlocks-&gt;[$index] eq '&lt;' &amp;&amp; $self-&gt;TagType($textBlocks, $index) == POSSIBLE_OPENING_TAG)
            {
            # If we hit a &lt; and we're checking whether a link is closed, it's not.  The first &lt; becomes literal and the second one
            # becomes the new link opening.
            if ($closingTag eq '&gt;')
                {
                return -1;
                }

            # If we're not searching for the end of a link, we have to skip the link because formatting tags cannot appear within
            # them.  That's of course provided it's closed.
            else
                {
                my $linkHasWhitespace;

                my $endIndex = $self-&gt;ClosingTag($textBlocks, $index,
                                                                    ($hasWhitespaceRef &amp;&amp; !$hasWhitespace ? \$linkHasWhitespace : undef) );

                if ($endIndex != -1)
                    {
                    if ($linkHasWhitespace)
                        {  $hasWhitespace = 1;  };

                    # index will be incremented again at the end of the loop, which will bring us past the link's &gt;.
                    $index = $endIndex;
                    };
                };
            }

        elsif ($textBlocks-&gt;[$index] eq $closingTag)
            {
            my $tagType = $self-&gt;TagType($textBlocks, $index);

            if ($tagType == POSSIBLE_CLOSING_TAG)
                {
                # There needs to be something between the tags for them to count.
                if ($index == $beginningIndex + 1)
                    {  return -1;  }
                else
                    {
                    # Success!

                    if ($hasWhitespaceRef)
                        {  $$hasWhitespaceRef = $hasWhitespace;  };

                    return $index;
                    };
                }

            # If there are two opening tags of the same type, the first becomes literal and the next becomes part of a tag.
            elsif ($tagType == POSSIBLE_OPENING_TAG)
                {  return -1;  }
            }

        elsif ($hasWhitespaceRef &amp;&amp; !$hasWhitespace)
            {
            if ($textBlocks-&gt;[$index] =~ /[ \t\n]/)
                {  $hasWhitespace = 1;  };
            };

        $index++;
        };

    # Hit the end of the text blocks if we're here.
    return -1;
    };


1;
</pre></body></html>