]> git.saurik.com Git - wxWidgets.git/blob - src/html/htmlfilt.cpp
Cleaned up makedfile to handle setup.h better. Added command line parsing
[wxWidgets.git] / src / html / htmlfilt.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: htmlfilt.cpp
3 // Purpose: wxHtmlFilter - input filter for translating into HTML format
4 // Author: Vaclav Slavik
5 // RCS-ID: $Id$
6 // Copyright: (c) 1999 Vaclav Slavik
7 // Licence: wxWindows Licence
8 /////////////////////////////////////////////////////////////////////////////
9
10
11 #ifdef __GNUG__
12 #pragma implementation
13 #endif
14
15 #include "wx/wxprec.h"
16
17 #include "wx/defs.h"
18 #if wxUSE_HTML && wxUSE_STREAMS
19
20 #ifdef __BORDLANDC__
21 #pragma hdrstop
22 #endif
23
24 #ifndef WXPRECOMP
25 #include "wx/log.h"
26 #include "wx/intl.h"
27 #endif
28
29 #include "wx/html/htmlfilt.h"
30 #include "wx/html/htmlwin.h"
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
48 bool wxHtmlFilterPlainText::CanRead(const wxFSFile& WXUNUSED(file)) const
49 {
50 return TRUE;
51 }
52
53
54
55 wxString wxHtmlFilterPlainText::ReadFile(const wxFSFile& file) const
56 {
57 wxInputStream *s = file.GetStream();
58 char *src;
59 wxString doc, doc2;
60
61 if (s == NULL) return wxEmptyString;
62 src = new char[s->GetSize()+1];
63 src[s->GetSize()] = 0;
64 s->Read(src, s->GetSize());
65 doc = src;
66 delete [] src;
67
68 doc.Replace(wxT("<"), wxT("&lt;"), TRUE);
69 doc.Replace(wxT(">"), wxT("&gt;"), TRUE);
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:
88 virtual bool CanRead(const wxFSFile& file) const;
89 virtual wxString ReadFile(const wxFSFile& file) const;
90 };
91
92 IMPLEMENT_DYNAMIC_CLASS(wxHtmlFilterImage, wxHtmlFilter)
93
94
95
96 bool wxHtmlFilterImage::CanRead(const wxFSFile& file) const
97 {
98 return (file.GetMimeType().Left(6) == wxT("image/"));
99 }
100
101
102
103 wxString wxHtmlFilterImage::ReadFile(const wxFSFile& file) const
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:
121 virtual bool CanRead(const wxFSFile& file) const;
122 virtual wxString ReadFile(const wxFSFile& file) const;
123 };
124
125
126 IMPLEMENT_DYNAMIC_CLASS(wxHtmlFilterHTML, wxHtmlFilter)
127
128 bool wxHtmlFilterHTML::CanRead(const wxFSFile& file) const
129 {
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
134 return (file.GetMimeType().Find(wxT("text/html")) == 0);
135 }
136
137
138
139 wxString wxHtmlFilterHTML::ReadFile(const wxFSFile& file) const
140 {
141 wxInputStream *s = file.GetStream();
142 char *src;
143 wxString doc;
144
145 if (s == NULL)
146 {
147 wxLogError(_("Cannot open HTML document: %s"), file.GetLocation().c_str());
148 return wxEmptyString;
149 }
150 src = new char[s->GetSize() + 1];
151 src[s->GetSize()] = 0;
152 s->Read(src, s->GetSize());
153 doc = src;
154 delete[] src;
155
156 // add meta tag if we obtained this through http:
157 if (file.GetMimeType().Find(_T("; charset=")) == 0)
158 {
159 wxString s(_T("<meta http-equiv=\"Content-Type\" content=\""));
160 s << file.GetMimeType() << _T("\">");
161 return s;
162 }
163
164 return doc;
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
188 #endif