Commit | Line | Data |
---|---|---|
5526e819 | 1 | ///////////////////////////////////////////////////////////////////////////// |
69941f05 | 2 | // Name: htmlfilt.cpp |
5526e819 VS |
3 | // Purpose: wxHtmlFilter - input filter for translating into HTML format |
4 | // Author: Vaclav Slavik | |
69941f05 | 5 | // RCS-ID: $Id$ |
5526e819 VS |
6 | // Copyright: (c) 1999 Vaclav Slavik |
7 | // Licence: wxWindows Licence | |
8 | ///////////////////////////////////////////////////////////////////////////// | |
9 | ||
10 | ||
11 | #ifdef __GNUG__ | |
69941f05 | 12 | #pragma implementation |
5526e819 VS |
13 | #endif |
14 | ||
4dcaf11a | 15 | #include "wx/wxprec.h" |
5526e819 | 16 | |
314260fb | 17 | #include "wx/defs.h" |
f6bcfd97 | 18 | #if wxUSE_HTML && wxUSE_STREAMS |
5526e819 VS |
19 | |
20 | #ifdef __BORDLANDC__ | |
21 | #pragma hdrstop | |
22 | #endif | |
23 | ||
24 | #ifndef WXPRECOMP | |
04dbb646 VZ |
25 | #include "wx/log.h" |
26 | #include "wx/intl.h" | |
5526e819 VS |
27 | #endif |
28 | ||
69941f05 | 29 | #include "wx/html/htmlfilt.h" |
4dcaf11a | 30 | #include "wx/html/htmlwin.h" |
5526e819 VS |
31 | |
32 | ||
33 | /* | |
34 | ||
35 | There is code for several default filters: | |
36 | ||
37 | */ | |
38 | ||
39 | IMPLEMENT_ABSTRACT_CLASS(wxHtmlFilter, wxObject) | |
40 | ||
41 | //-------------------------------------------------------------------------------- | |
42 | // wxHtmlFilterPlainText | |
43 | // filter for text/plain or uknown | |
44 | //-------------------------------------------------------------------------------- | |
45 | ||
46 | IMPLEMENT_DYNAMIC_CLASS(wxHtmlFilterPlainText, wxHtmlFilter) | |
47 | ||
a4c97004 | 48 | bool wxHtmlFilterPlainText::CanRead(const wxFSFile& WXUNUSED(file)) const |
5526e819 VS |
49 | { |
50 | return TRUE; | |
51 | } | |
52 | ||
53 | ||
54 | ||
420ec58a | 55 | wxString wxHtmlFilterPlainText::ReadFile(const wxFSFile& file) const |
5526e819 VS |
56 | { |
57 | wxInputStream *s = file.GetStream(); | |
58 | char *src; | |
59 | wxString doc, doc2; | |
60 | ||
61 | if (s == NULL) return wxEmptyString; | |
4f9297b0 VS |
62 | src = new char[s->GetSize()+1]; |
63 | src[s->GetSize()] = 0; | |
64 | s->Read(src, s->GetSize()); | |
5526e819 | 65 | doc = src; |
8dd71e2b | 66 | delete [] src; |
5526e819 | 67 | |
66a77a74 OK |
68 | doc.Replace(wxT("<"), wxT("<"), TRUE); |
69 | doc.Replace(wxT(">"), wxT(">"), TRUE); | |
5526e819 VS |
70 | doc2 = "<HTML><BODY><PRE>\n" + doc + "\n</PRE></BODY></HTML>"; |
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 VS |
104 | { |
105 | return ("<HTML><BODY><IMG SRC=\"" + file.GetLocation() + "\"></BODY></HTML>"); | |
106 | } | |
107 | ||
108 | ||
109 | ||
110 | ||
111 | //-------------------------------------------------------------------------------- | |
112 | // wxHtmlFilterPlainText | |
113 | // filter for text/plain or uknown | |
114 | //-------------------------------------------------------------------------------- | |
115 | ||
116 | class wxHtmlFilterHTML : public wxHtmlFilter | |
117 | { | |
118 | DECLARE_DYNAMIC_CLASS(wxHtmlFilterHTML) | |
119 | ||
120 | public: | |
420ec58a VS |
121 | virtual bool CanRead(const wxFSFile& file) const; |
122 | virtual wxString ReadFile(const wxFSFile& file) const; | |
5526e819 VS |
123 | }; |
124 | ||
125 | ||
126 | IMPLEMENT_DYNAMIC_CLASS(wxHtmlFilterHTML, wxHtmlFilter) | |
127 | ||
420ec58a | 128 | bool wxHtmlFilterHTML::CanRead(const wxFSFile& file) const |
5526e819 | 129 | { |
f61815af GL |
130 | // return (file.GetMimeType() == "text/html"); |
131 | // This is true in most case but some page can return: | |
132 | // "text/html; char-encoding=...." | |
133 | // So we use Find instead | |
77611ad4 | 134 | return (file.GetMimeType().Find(wxT("text/html")) == 0); |
5526e819 VS |
135 | } |
136 | ||
137 | ||
138 | ||
420ec58a | 139 | wxString wxHtmlFilterHTML::ReadFile(const wxFSFile& file) const |
5526e819 VS |
140 | { |
141 | wxInputStream *s = file.GetStream(); | |
142 | char *src; | |
143 | wxString doc; | |
144 | ||
04dbb646 | 145 | if (s == NULL) |
f3c82859 | 146 | { |
f6bcfd97 | 147 | wxLogError(_("Cannot open HTML document: %s"), file.GetLocation().c_str()); |
f3c82859 VS |
148 | return wxEmptyString; |
149 | } | |
4f9297b0 VS |
150 | src = new char[s->GetSize() + 1]; |
151 | src[s->GetSize()] = 0; | |
152 | s->Read(src, s->GetSize()); | |
5526e819 | 153 | doc = src; |
2776d7c3 | 154 | delete[] src; |
5526e819 | 155 | |
04dbb646 | 156 | // add meta tag if we obtained this through http: |
981e62aa VS |
157 | if (file.GetMimeType().Find(_T("; charset=")) == 0) |
158 | { | |
159 | wxString s(_T("<meta http-equiv=\"Content-Type\" content=\"")); | |
160 | s << file.GetMimeType() << _T("\">"); | |
7ee77720 | 161 | return s+doc; |
981e62aa VS |
162 | } |
163 | ||
3ca6a5f0 | 164 | return doc; |
5526e819 VS |
165 | } |
166 | ||
167 | ||
168 | ||
169 | ||
170 | ///// Module: | |
171 | ||
172 | class wxHtmlFilterModule : public wxModule | |
173 | { | |
174 | DECLARE_DYNAMIC_CLASS(wxHtmlFilterModule) | |
175 | ||
176 | public: | |
177 | virtual bool OnInit() | |
178 | { | |
179 | wxHtmlWindow::AddFilter(new wxHtmlFilterHTML); | |
180 | wxHtmlWindow::AddFilter(new wxHtmlFilterImage); | |
181 | return TRUE; | |
182 | } | |
183 | virtual void OnExit() {} | |
184 | }; | |
185 | ||
186 | IMPLEMENT_DYNAMIC_CLASS(wxHtmlFilterModule, wxModule) | |
187 | ||
8dd71e2b | 188 | #endif |