michael@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef ____nstxttohtmlconv___h___ michael@0: #define ____nstxttohtmlconv___h___ michael@0: michael@0: #include "nsITXTToHTMLConv.h" michael@0: #include "nsCOMPtr.h" michael@0: #include "nsTArray.h" michael@0: #include "nsString.h" michael@0: michael@0: #define NS_NSTXTTOHTMLCONVERTER_CID \ michael@0: { /* 9ef9fa14-1dd1-11b2-9d65-d72d6d1f025e */ \ michael@0: 0x9ef9fa14, \ michael@0: 0x1dd1, \ michael@0: 0x11b2, \ michael@0: {0x9d, 0x65, 0xd7, 0x2d, 0x6d, 0x1f, 0x02, 0x5e} \ michael@0: } michael@0: michael@0: // Internal representation of a "token" michael@0: typedef struct convToken { michael@0: nsString token; // the actual string (i.e. "http://") michael@0: nsString modText; // replacement text or href prepend text. michael@0: bool prepend; // flag indicating how the modText should be used. michael@0: } convToken; michael@0: michael@0: template class nsAutoPtr; michael@0: michael@0: /** michael@0: * Convert plain text to HTML. michael@0: * michael@0: * OVERVIEW OF HOW THIS CLASS WORKS: michael@0: * michael@0: * This class stores an array of tokens that should be replaced by something, michael@0: * or something that should be prepended. michael@0: * The "token" member of convToken is the text to search for. This is a michael@0: * substring of the desired token. Tokens are delimited by TOKEN_DELIMITERS. michael@0: * That entire token will be replaced by modText (if prepend is false); or it michael@0: * will be linkified and modText will be prepended to the token if prepend is michael@0: * true. michael@0: * michael@0: * Note that all of the text will be in a preformatted block, so there is no michael@0: * need to emit line-end tags, or set the font face to monospace. michael@0: * michael@0: * This works as a stream converter, so data will arrive by michael@0: * OnStartRequest/OnDataAvailable/OnStopRequest calls. michael@0: * michael@0: * OStopR will possibly process a remaining token. michael@0: * michael@0: * If the data of one pass contains a part of a token, that part will be stored michael@0: * in mBuffer. The rest of the data will be sent to the next listener. michael@0: * michael@0: * XXX this seems suboptimal. this means that this design will only work for michael@0: * links. and it is impossible to append anything to the token. this means that, michael@0: * for example, making *foo* bold is not possible. michael@0: */ michael@0: class nsTXTToHTMLConv : public nsITXTToHTMLConv { michael@0: public: michael@0: NS_DECL_ISUPPORTS michael@0: NS_DECL_NSISTREAMCONVERTER michael@0: NS_DECL_NSITXTTOHTMLCONV michael@0: NS_DECL_NSIREQUESTOBSERVER michael@0: NS_DECL_NSISTREAMLISTENER michael@0: michael@0: nsTXTToHTMLConv(); michael@0: virtual ~nsTXTToHTMLConv(); michael@0: nsresult Init(); michael@0: michael@0: // For factory creation. michael@0: static NS_METHOD michael@0: Create(nsISupports *aOuter, REFNSIID aIID, void **aResult) { michael@0: nsresult rv; michael@0: if (aOuter) michael@0: return NS_ERROR_NO_AGGREGATION; michael@0: michael@0: nsTXTToHTMLConv* _s = new nsTXTToHTMLConv(); michael@0: if (_s == nullptr) michael@0: return NS_ERROR_OUT_OF_MEMORY; michael@0: NS_ADDREF(_s); michael@0: rv = _s->Init(); michael@0: if (NS_FAILED(rv)) { michael@0: delete _s; michael@0: return rv; michael@0: } michael@0: rv = _s->QueryInterface(aIID, aResult); michael@0: NS_RELEASE(_s); michael@0: return rv; michael@0: } michael@0: michael@0: michael@0: protected: michael@0: // return the token and it's location in the underlying buffer. michael@0: int32_t FindToken(int32_t cursor, convToken* *_retval); michael@0: michael@0: // return the cursor location after munging HTML into the michael@0: // underlying buffer, according to mToken michael@0: int32_t CatHTML(int32_t front, int32_t back); michael@0: michael@0: nsCOMPtr mListener; // final listener (consumer) michael@0: nsString mBuffer; // any carry over data michael@0: nsTArray > mTokens; // list of tokens to search for michael@0: convToken *mToken; // current token (if any) michael@0: nsString mPageTitle; // Page title michael@0: bool mPreFormatHTML; // Whether to use
 tags
michael@0: };
michael@0: 
michael@0: #endif // ____nstxttohtmlconv___h___
michael@0: