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
4 : *
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/
9 : *
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
13 : * License.
14 : *
15 : * The Original Code is mozilla.org code.
16 : *
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.
21 : *
22 : * Contributor(s):
23 : *
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.
35 : *
36 : * ***** END LICENSE BLOCK ***** */
37 :
38 : #ifndef ____nstxttohtmlconv___h___
39 : #define ____nstxttohtmlconv___h___
40 :
41 : #include "nsITXTToHTMLConv.h"
42 : #include "nsCOMPtr.h"
43 : #include "nsTArray.h"
44 : #include "nsAutoPtr.h"
45 : #include "nsIFactory.h"
46 : #include "nsString.h"
47 :
48 : #define NS_NSTXTTOHTMLCONVERTER_CID \
49 : { /* 9ef9fa14-1dd1-11b2-9d65-d72d6d1f025e */ \
50 : 0x9ef9fa14, \
51 : 0x1dd1, \
52 : 0x11b2, \
53 : {0x9d, 0x65, 0xd7, 0x2d, 0x6d, 0x1f, 0x02, 0x5e} \
54 : }
55 :
56 : // Internal representation of a "token"
57 0 : typedef struct convToken {
58 : nsString token; // the actual string (i.e. "http://")
59 : nsString modText; // replacement text or href prepend text.
60 : bool prepend; // flag indicating how the modText should be used.
61 : } convToken;
62 :
63 : /**
64 : * Convert plain text to HTML.
65 : *
66 : * OVERVIEW OF HOW THIS CLASS WORKS:
67 : *
68 : * This class stores an array of tokens that should be replaced by something,
69 : * or something that should be prepended.
70 : * The "token" member of convToken is the text to search for. This is a
71 : * substring of the desired token. Tokens are delimited by TOKEN_DELIMITERS.
72 : * That entire token will be replaced by modText (if prepend is false); or it
73 : * will be linkified and modText will be prepended to the token if prepend is
74 : * true.
75 : *
76 : * Note that all of the text will be in a preformatted block, so there is no
77 : * need to emit line-end tags, or set the font face to monospace.
78 : *
79 : * This works as a stream converter, so data will arrive by
80 : * OnStartRequest/OnDataAvailable/OnStopRequest calls.
81 : *
82 : * OStopR will possibly process a remaining token.
83 : *
84 : * If the data of one pass contains a part of a token, that part will be stored
85 : * in mBuffer. The rest of the data will be sent to the next listener.
86 : *
87 : * XXX this seems suboptimal. this means that this design will only work for
88 : * links. and it is impossible to append anything to the token. this means that,
89 : * for example, making *foo* bold is not possible.
90 : */
91 : class nsTXTToHTMLConv : public nsITXTToHTMLConv {
92 : public:
93 : NS_DECL_ISUPPORTS
94 : NS_DECL_NSISTREAMCONVERTER
95 : NS_DECL_NSITXTTOHTMLCONV
96 : NS_DECL_NSIREQUESTOBSERVER
97 : NS_DECL_NSISTREAMLISTENER
98 :
99 : nsTXTToHTMLConv();
100 : virtual ~nsTXTToHTMLConv();
101 : nsresult Init();
102 :
103 : // For factory creation.
104 : static NS_METHOD
105 : Create(nsISupports *aOuter, REFNSIID aIID, void **aResult) {
106 : nsresult rv;
107 : if (aOuter)
108 : return NS_ERROR_NO_AGGREGATION;
109 :
110 : nsTXTToHTMLConv* _s = new nsTXTToHTMLConv();
111 : if (_s == nsnull)
112 : return NS_ERROR_OUT_OF_MEMORY;
113 : NS_ADDREF(_s);
114 : rv = _s->Init();
115 : if (NS_FAILED(rv)) {
116 : delete _s;
117 : return rv;
118 : }
119 : rv = _s->QueryInterface(aIID, aResult);
120 : NS_RELEASE(_s);
121 : return rv;
122 : }
123 :
124 :
125 : protected:
126 : // return the token and it's location in the underlying buffer.
127 : PRInt32 FindToken(PRInt32 cursor, convToken* *_retval);
128 :
129 : // return the cursor location after munging HTML into the
130 : // underlying buffer, according to mToken
131 : PRInt32 CatHTML(PRInt32 front, PRInt32 back);
132 :
133 : nsCOMPtr<nsIStreamListener> mListener; // final listener (consumer)
134 : nsString mBuffer; // any carry over data
135 : nsTArray<nsAutoPtr<convToken> > mTokens; // list of tokens to search for
136 : convToken *mToken; // current token (if any)
137 : nsString mPageTitle; // Page title
138 : bool mPreFormatHTML; // Whether to use <pre> tags
139 : };
140 :
141 : #endif // ____nstxttohtmlconv___h___
142 :
|