Logo Search packages:      
Sourcecode: mailody version File versions  Download package

QString LinkLocatorNS::LinkLocator::convertToHtml ( const QString &  plainText,
int  flags = 0,
int  maxUrlLen = 4096,
int  maxAddressLen = 255 
) [static]

Converts plaintext into html. The following characters are converted to HTML entities: & " < >. Newlines are also preserved.

Parameters:
plainText The text to be converted into HTML.
flags The flags to consider when processing plainText. Currently supported flags are:
  • PreserveSpaces, preserves the appearance of sequences of space and tab characters in the resulting HTML.
  • IgnoreUrls, doesn't parse any URLs.
  • HighlightText, interprets text highlighting markup like *bold*, _underlined_ and /italic/.
  • UseParagraph, don't use
    's, but
's to break which can by handy for a syntaxhighlighter
Parameters:
maxUrlLen The maximum length of permitted URLs. (See maxUrlLen().)
maxAddressLen The maximum length of permitted email addresses. (See maxAddressLen().)
Returns:
An HTML version of the text supplied in the 'plainText' parameter, suitable for inclusion in the BODY of an HTML document.

Definition at line 207 of file linklocator.cpp.

References getEmailAddress(), getUrl(), highlightedText(), mPos, mText, setMaxAddressLen(), and setMaxUrlLen().

{
  LinkLocator locator(plainText);
  locator.setMaxUrlLen(maxUrlLen);
  locator.setMaxAddressLen(maxAddressLen);

  QString str;
  QString result((QChar*)0, (int)locator.mText.length() * 2);
  QChar ch;
  int x;
  bool startOfLine = true;

  for (locator.mPos = 0, x = 0; locator.mPos < (int)locator.mText.length(); locator.mPos++, x++)
  {
    ch = locator.mText[locator.mPos];
    if ( flags & PreserveSpaces )
    {
      if (ch==' ')
      {
        if (startOfLine) {
          result += "&nbsp;";
          locator.mPos++, x++;
          startOfLine = false;
        }
        while (locator.mText[locator.mPos] == ' ')
        {
          result += ' ';
          locator.mPos++, x++;
          if (locator.mText[locator.mPos] == ' ') {
            result += "&nbsp;";
            locator.mPos++, x++;
          }
        }
        locator.mPos--, x--;
        continue;
      }
      else if (ch=='\t')
      {
        do
        {
          result += "&nbsp;";
          x++;
        }
        while((x&7) != 0);
        x--;
        startOfLine = false;
        continue;
      }
    }
    if (ch=='\n')
    {
      if ( flags & UseParagraph )
        result += "</p>\n<p>\n";
      else
        result += "<br />\n";
      startOfLine = true;
      x = -1;
      continue;
    }

    startOfLine = false;
    if (ch=='&')
      result += "&amp;";
    else if (ch=='"')
      result += "&quot;";
    else if (ch=='<')
      result += "&lt;";
    else if (ch=='>')
      result += "&gt;";
    else
    {
      const int start = locator.mPos;
      if ( !(flags & IgnoreUrls) ) {
        str = locator.getUrl();
        if (!str.isEmpty())
        {
          QString hyperlink;
          if(str.left(4) == "www.")
            hyperlink = "http://" + str;
          else if(str.left(4) == "ftp.")
            hyperlink = "ftp://" + str;
          else
            hyperlink = str;

          str = str.replace('&', "&amp;");
          result += "<a href=\"" + hyperlink + "\">" + str + "</a>";
          x += locator.mPos - start;
          continue;
        }
        str = locator.getEmailAddress();
        if(!str.isEmpty())
        {
          // len is the length of the local part
          int len = str.find('@');
          QString localPart = str.left(len);

          // remove the local part from the result (as '&'s have been expanded to
          // &amp; we have to take care of the 4 additional characters per '&')
          result.truncate(result.length() - len - (localPart.contains('&')*4));
          x -= len;

          result += "<a href=\"mailto:" + str + "\">" + str + "</a>";
          x += str.length() - 1;
          continue;
        }
      }
      if ( flags & HighlightText ) {
        str = locator.highlightedText();
        if ( !str.isEmpty() ) {
          result += str;
          x += locator.mPos - start;
          continue;
        }
      }
      result += ch;
    }
  }

  return flags & UseParagraph ? result.replace("<p>\n</p>","<p>&nbsp;</p>")
    : result;
}


Generated by  Doxygen 1.6.0   Back to index