]>
Commit | Line | Data |
---|---|---|
5526e819 | 1 | ///////////////////////////////////////////////////////////////////////////// |
93763ad5 | 2 | // Name: src/html/htmlfilt.cpp |
5526e819 VS |
3 | // Purpose: wxHtmlFilter - input filter for translating into HTML format |
4 | // Author: Vaclav Slavik | |
69941f05 | 5 | // RCS-ID: $Id$ |
5526e819 | 6 | // Copyright: (c) 1999 Vaclav Slavik |
65571936 | 7 | // Licence: wxWindows licence |
5526e819 VS |
8 | ///////////////////////////////////////////////////////////////////////////// |
9 | ||
4dcaf11a | 10 | #include "wx/wxprec.h" |
5526e819 | 11 | |
2b5f62a0 | 12 | #ifdef __BORLANDC__ |
93763ad5 | 13 | #pragma hdrstop |
5526e819 VS |
14 | #endif |
15 | ||
93763ad5 WS |
16 | #if wxUSE_HTML && wxUSE_STREAMS |
17 | ||
5526e819 | 18 | #ifndef WXPRECOMP |
04dbb646 VZ |
19 | #include "wx/log.h" |
20 | #include "wx/intl.h" | |
5526e819 VS |
21 | #endif |
22 | ||
2b5f62a0 | 23 | #include "wx/strconv.h" |
69941f05 | 24 | #include "wx/html/htmlfilt.h" |
4dcaf11a | 25 | #include "wx/html/htmlwin.h" |
5526e819 | 26 | |
eb37e1d2 | 27 | // utility function: read a wxString from a wxInputStream |
2b5f62a0 | 28 | static void ReadString(wxString& str, wxInputStream* s, wxMBConv& conv) |
eb37e1d2 MB |
29 | { |
30 | size_t streamSize = s->GetSize(); | |
31 | ||
2b5f62a0 | 32 | if (streamSize == ~(size_t)0) |
eb37e1d2 MB |
33 | { |
34 | const size_t bufSize = 4095; | |
35 | char buffer[bufSize+1]; | |
36 | size_t lastRead; | |
37 | ||
38 | do | |
39 | { | |
40 | s->Read(buffer, bufSize); | |
41 | lastRead = s->LastRead(); | |
42 | buffer[lastRead] = 0; | |
2b5f62a0 | 43 | str.Append(wxString(buffer, conv)); |
eb37e1d2 | 44 | } |
2b5f62a0 | 45 | while (lastRead == bufSize); |
eb37e1d2 MB |
46 | } |
47 | else | |
48 | { | |
49 | char* src = new char[streamSize+1]; | |
50 | s->Read(src, streamSize); | |
51 | src[streamSize] = 0; | |
2b5f62a0 VZ |
52 | str = wxString(src, conv); |
53 | delete[] src; | |
eb37e1d2 MB |
54 | } |
55 | } | |
5526e819 VS |
56 | |
57 | /* | |
58 | ||
59 | There is code for several default filters: | |
60 | ||
61 | */ | |
62 | ||
63 | IMPLEMENT_ABSTRACT_CLASS(wxHtmlFilter, wxObject) | |
64 | ||
65 | //-------------------------------------------------------------------------------- | |
66 | // wxHtmlFilterPlainText | |
67 | // filter for text/plain or uknown | |
68 | //-------------------------------------------------------------------------------- | |
69 | ||
70 | IMPLEMENT_DYNAMIC_CLASS(wxHtmlFilterPlainText, wxHtmlFilter) | |
71 | ||
a4c97004 | 72 | bool wxHtmlFilterPlainText::CanRead(const wxFSFile& WXUNUSED(file)) const |
5526e819 | 73 | { |
d1da8872 | 74 | return true; |
5526e819 VS |
75 | } |
76 | ||
77 | ||
78 | ||
420ec58a | 79 | wxString wxHtmlFilterPlainText::ReadFile(const wxFSFile& file) const |
5526e819 VS |
80 | { |
81 | wxInputStream *s = file.GetStream(); | |
5526e819 VS |
82 | wxString doc, doc2; |
83 | ||
84 | if (s == NULL) return wxEmptyString; | |
2b5f62a0 | 85 | ReadString(doc, s, wxConvISO8859_1); |
5526e819 | 86 | |
d1da8872 WS |
87 | doc.Replace(wxT("&"), wxT("&"), true); |
88 | doc.Replace(wxT("<"), wxT("<"), true); | |
89 | doc.Replace(wxT(">"), wxT(">"), true); | |
2b5f62a0 | 90 | doc2 = wxT("<HTML><BODY><PRE>\n") + doc + wxT("\n</PRE></BODY></HTML>"); |
5526e819 VS |
91 | return doc2; |
92 | } | |
93 | ||
94 | ||
95 | ||
96 | ||
97 | ||
98 | //-------------------------------------------------------------------------------- | |
99 | // wxHtmlFilterImage | |
100 | // filter for image/* | |
101 | //-------------------------------------------------------------------------------- | |
102 | ||
103 | class wxHtmlFilterImage : public wxHtmlFilter | |
104 | { | |
105 | DECLARE_DYNAMIC_CLASS(wxHtmlFilterImage) | |
106 | ||
107 | public: | |
420ec58a VS |
108 | virtual bool CanRead(const wxFSFile& file) const; |
109 | virtual wxString ReadFile(const wxFSFile& file) const; | |
5526e819 VS |
110 | }; |
111 | ||
112 | IMPLEMENT_DYNAMIC_CLASS(wxHtmlFilterImage, wxHtmlFilter) | |
113 | ||
114 | ||
115 | ||
420ec58a | 116 | bool wxHtmlFilterImage::CanRead(const wxFSFile& file) const |
5526e819 | 117 | { |
0413cec5 | 118 | return (file.GetMimeType().Left(6) == wxT("image/")); |
5526e819 VS |
119 | } |
120 | ||
121 | ||
122 | ||
420ec58a | 123 | wxString wxHtmlFilterImage::ReadFile(const wxFSFile& file) const |
5526e819 | 124 | { |
2b5f62a0 VZ |
125 | wxString res = wxT("<HTML><BODY><IMG SRC=\"") + file.GetLocation() + wxT("\"></BODY></HTML>"); |
126 | return res; | |
5526e819 VS |
127 | } |
128 | ||
129 | ||
130 | ||
131 | ||
132 | //-------------------------------------------------------------------------------- | |
2b5f62a0 VZ |
133 | // wxHtmlFilterHTML |
134 | // filter for text/html | |
5526e819 VS |
135 | //-------------------------------------------------------------------------------- |
136 | ||
5526e819 VS |
137 | |
138 | IMPLEMENT_DYNAMIC_CLASS(wxHtmlFilterHTML, wxHtmlFilter) | |
139 | ||
420ec58a | 140 | bool wxHtmlFilterHTML::CanRead(const wxFSFile& file) const |
5526e819 | 141 | { |
f61815af GL |
142 | // return (file.GetMimeType() == "text/html"); |
143 | // This is true in most case but some page can return: | |
144 | // "text/html; char-encoding=...." | |
145 | // So we use Find instead | |
77611ad4 | 146 | return (file.GetMimeType().Find(wxT("text/html")) == 0); |
5526e819 VS |
147 | } |
148 | ||
149 | ||
150 | ||
420ec58a | 151 | wxString wxHtmlFilterHTML::ReadFile(const wxFSFile& file) const |
5526e819 VS |
152 | { |
153 | wxInputStream *s = file.GetStream(); | |
5526e819 VS |
154 | wxString doc; |
155 | ||
04dbb646 | 156 | if (s == NULL) |
f3c82859 | 157 | { |
f6bcfd97 | 158 | wxLogError(_("Cannot open HTML document: %s"), file.GetLocation().c_str()); |
f3c82859 VS |
159 | return wxEmptyString; |
160 | } | |
5526e819 | 161 | |
d1da8872 | 162 | // NB: We convert input file to wchar_t here in Unicode mode, based on |
2b5f62a0 VZ |
163 | // either Content-Type header or <meta> tags. In ANSI mode, we don't |
164 | // do it as it is done by wxHtmlParser (for this reason, we add <meta> | |
165 | // tag if we used Content-Type header). | |
166 | #if wxUSE_UNICODE | |
d1da8872 | 167 | int charsetPos; |
2b5f62a0 VZ |
168 | if ((charsetPos = file.GetMimeType().Find(_T("; charset="))) != wxNOT_FOUND) |
169 | { | |
170 | wxString charset = file.GetMimeType().Mid(charsetPos + 10); | |
171 | wxCSConv conv(charset); | |
172 | ReadString(doc, s, conv); | |
173 | } | |
174 | else | |
175 | { | |
176 | wxString tmpdoc; | |
177 | ReadString(tmpdoc, s, wxConvISO8859_1); | |
178 | wxString charset = wxHtmlParser::ExtractCharsetInformation(tmpdoc); | |
179 | if (charset.empty()) | |
180 | doc = tmpdoc; | |
181 | else | |
182 | { | |
183 | wxCSConv conv(charset); | |
184 | doc = wxString(tmpdoc.mb_str(wxConvISO8859_1), conv); | |
185 | } | |
186 | } | |
187 | #else // !wxUSE_UNICODE | |
188 | ReadString(doc, s, wxConvLibc); | |
04dbb646 | 189 | // add meta tag if we obtained this through http: |
2b5f62a0 | 190 | if (!file.GetMimeType().empty()) |
981e62aa | 191 | { |
2b5f62a0 VZ |
192 | wxString hdr; |
193 | wxString mime = file.GetMimeType(); | |
194 | hdr.Printf(_T("<meta http-equiv=\"Content-Type\" content=\"%s\">"), mime.c_str()); | |
195 | return hdr+doc; | |
981e62aa | 196 | } |
2b5f62a0 | 197 | #endif |
981e62aa | 198 | |
3ca6a5f0 | 199 | return doc; |
5526e819 VS |
200 | } |
201 | ||
202 | ||
203 | ||
204 | ||
205 | ///// Module: | |
206 | ||
207 | class wxHtmlFilterModule : public wxModule | |
208 | { | |
209 | DECLARE_DYNAMIC_CLASS(wxHtmlFilterModule) | |
210 | ||
211 | public: | |
212 | virtual bool OnInit() | |
213 | { | |
214 | wxHtmlWindow::AddFilter(new wxHtmlFilterHTML); | |
215 | wxHtmlWindow::AddFilter(new wxHtmlFilterImage); | |
d1da8872 | 216 | return true; |
5526e819 VS |
217 | } |
218 | virtual void OnExit() {} | |
219 | }; | |
220 | ||
221 | IMPLEMENT_DYNAMIC_CLASS(wxHtmlFilterModule, wxModule) | |
222 | ||
8dd71e2b | 223 | #endif |