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