1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
15 * The Original Code is mozilla.org code.
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998
20 * the Initial Developer. All Rights Reserved.
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
38 #ifndef ____nstxttohtmlconv___h___
39 #define ____nstxttohtmlconv___h___
41 #include "nsITXTToHTMLConv.h"
43 #include "nsVoidArray.h"
44 #include "nsIFactory.h"
47 #define NS_NSTXTTOHTMLCONVERTER_CID \
48 { /* 9ef9fa14-1dd1-11b2-9d65-d72d6d1f025e */ \
52 {0x9d, 0x65, 0xd7, 0x2d, 0x6d, 0x1f, 0x02, 0x5e} \
55 // Internal representation of a "token"
56 typedef struct convToken
{
57 nsString token
; // the actual string (i.e. "http://")
58 nsString modText
; // replacement text or href prepend text.
59 PRBool prepend
; // flag indicating how the modText should be used.
63 * Convert plain text to HTML.
65 * OVERVIEW OF HOW THIS CLASS WORKS:
67 * This class stores an array of tokens that should be replaced by something,
68 * or something that should be prepended.
69 * The "token" member of convToken is the text to search for. This is a
70 * substring of the desired token. Tokens are delimited by TOKEN_DELIMITERS.
71 * That entire token will be replaced by modText (if prepend is false); or it
72 * will be linkified and modText will be prepended to the token if prepend is
75 * Note that all of the text will be in a preformatted block, so there is no
76 * need to emit line-end tags, or set the font face to monospace.
78 * This works as a stream converter, so data will arrive by
79 * OnStartRequest/OnDataAvailable/OnStopRequest calls.
81 * OStopR will possibly process a remaining token.
83 * If the data of one pass contains a part of a token, that part will be stored
84 * in mBuffer. The rest of the data will be sent to the next listener.
86 * XXX this seems suboptimal. this means that this design will only work for
87 * links. and it is impossible to append anything to the token. this means that,
88 * for example, making *foo* bold is not possible.
90 class nsTXTToHTMLConv
: public nsITXTToHTMLConv
{
93 NS_DECL_NSISTREAMCONVERTER
94 NS_DECL_NSITXTTOHTMLCONV
95 NS_DECL_NSIREQUESTOBSERVER
96 NS_DECL_NSISTREAMLISTENER
99 virtual ~nsTXTToHTMLConv();
102 // For factory creation.
104 Create(nsISupports
*aOuter
, REFNSIID aIID
, void **aResult
) {
107 return NS_ERROR_NO_AGGREGATION
;
109 nsTXTToHTMLConv
* _s
= new nsTXTToHTMLConv();
111 return NS_ERROR_OUT_OF_MEMORY
;
118 rv
= _s
->QueryInterface(aIID
, aResult
);
125 // return the token and it's location in the underlying buffer.
126 PRInt32
FindToken(PRInt32 cursor
, convToken
* *_retval
);
128 // return the cursor location after munging HTML into the
129 // underlying buffer, according to mToken
130 PRInt32
CatHTML(PRInt32 front
, PRInt32 back
);
132 nsCOMPtr
<nsIStreamListener
> mListener
; // final listener (consumer)
133 nsString mBuffer
; // any carry over data
134 nsVoidArray mTokens
; // list of tokens to search for
135 convToken
*mToken
; // current token (if any)
136 nsString mPageTitle
; // Page title
137 PRBool mPreFormatHTML
; // Whether to use <pre> tags
140 #endif // ____nstxttohtmlconv___h___