]>
git.saurik.com Git - wxWidgets.git/blob - src/html/htmlfilt.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/html/htmlfilt.cpp
3 // Purpose: wxHtmlFilter - input filter for translating into HTML format
4 // Author: Vaclav Slavik
5 // Copyright: (c) 1999 Vaclav Slavik
6 // Licence: wxWindows licence
7 /////////////////////////////////////////////////////////////////////////////
15 #if wxUSE_HTML && wxUSE_STREAMS
22 #include "wx/strconv.h"
23 #include "wx/sstream.h"
24 #include "wx/html/htmlfilt.h"
25 #include "wx/html/htmlwin.h"
27 // utility function: read entire contents of an wxInputStream into a wxString
29 // TODO: error handling?
30 static void ReadString(wxString
& str
, wxInputStream
* s
, wxMBConv
& conv
)
32 wxStringOutputStream
out(&str
, conv
);
38 There is code for several default filters:
42 IMPLEMENT_ABSTRACT_CLASS(wxHtmlFilter
, wxObject
)
44 //--------------------------------------------------------------------------------
45 // wxHtmlFilterPlainText
46 // filter for text/plain or uknown
47 //--------------------------------------------------------------------------------
49 IMPLEMENT_DYNAMIC_CLASS(wxHtmlFilterPlainText
, wxHtmlFilter
)
51 bool wxHtmlFilterPlainText::CanRead(const wxFSFile
& WXUNUSED(file
)) const
58 wxString
wxHtmlFilterPlainText::ReadFile(const wxFSFile
& file
) const
60 wxInputStream
*s
= file
.GetStream();
63 if (s
== NULL
) return wxEmptyString
;
64 ReadString(doc
, s
, wxConvISO8859_1
);
66 doc
.Replace(wxT("&"), wxT("&"), true);
67 doc
.Replace(wxT("<"), wxT("<"), true);
68 doc
.Replace(wxT(">"), wxT(">"), true);
69 doc2
= wxT("<HTML><BODY><PRE>\n") + doc
+ wxT("\n</PRE></BODY></HTML>");
77 //--------------------------------------------------------------------------------
80 //--------------------------------------------------------------------------------
82 class wxHtmlFilterImage
: public wxHtmlFilter
84 DECLARE_DYNAMIC_CLASS(wxHtmlFilterImage
)
87 virtual bool CanRead(const wxFSFile
& file
) const;
88 virtual wxString
ReadFile(const wxFSFile
& file
) const;
91 IMPLEMENT_DYNAMIC_CLASS(wxHtmlFilterImage
, wxHtmlFilter
)
95 bool wxHtmlFilterImage::CanRead(const wxFSFile
& file
) const
97 return (file
.GetMimeType().Left(6) == wxT("image/"));
102 wxString
wxHtmlFilterImage::ReadFile(const wxFSFile
& file
) const
104 wxString res
= wxT("<HTML><BODY><IMG SRC=\"") + file
.GetLocation() + wxT("\"></BODY></HTML>");
111 //--------------------------------------------------------------------------------
113 // filter for text/html
114 //--------------------------------------------------------------------------------
117 IMPLEMENT_DYNAMIC_CLASS(wxHtmlFilterHTML
, wxHtmlFilter
)
119 bool wxHtmlFilterHTML::CanRead(const wxFSFile
& file
) const
121 // return (file.GetMimeType() == "text/html");
122 // This is true in most case but some page can return:
123 // "text/html; char-encoding=...."
124 // So we use Find instead
125 return (file
.GetMimeType().Find(wxT("text/html")) == 0);
130 wxString
wxHtmlFilterHTML::ReadFile(const wxFSFile
& file
) const
132 wxInputStream
*s
= file
.GetStream();
137 wxLogError(_("Cannot open HTML document: %s"), file
.GetLocation().c_str());
138 return wxEmptyString
;
141 // NB: We convert input file to wchar_t here in Unicode mode, based on
142 // either Content-Type header or <meta> tags. In ANSI mode, we don't
143 // do it as it is done by wxHtmlParser (for this reason, we add <meta>
144 // tag if we used Content-Type header).
147 if ((charsetPos
= file
.GetMimeType().Find(wxT("; charset="))) != wxNOT_FOUND
)
149 wxString charset
= file
.GetMimeType().Mid(charsetPos
+ 10);
150 wxCSConv
conv(charset
);
151 ReadString(doc
, s
, conv
);
155 size_t size
= s
->GetSize();
156 wxCharBuffer
buf( size
);
157 s
->Read( buf
.data(), size
);
158 wxString
tmpdoc( buf
, wxConvISO8859_1
);
160 wxString charset
= wxHtmlParser::ExtractCharsetInformation(tmpdoc
);
165 wxCSConv
conv(charset
);
166 doc
= wxString( buf
, conv
);
169 #else // !wxUSE_UNICODE
170 ReadString(doc
, s
, wxConvLibc
);
171 // add meta tag if we obtained this through http:
172 if (!file
.GetMimeType().empty())
175 wxString mime
= file
.GetMimeType();
176 hdr
.Printf(wxT("<meta http-equiv=\"Content-Type\" content=\"%s\">"), mime
.c_str());
189 class wxHtmlFilterModule
: public wxModule
191 DECLARE_DYNAMIC_CLASS(wxHtmlFilterModule
)
194 virtual bool OnInit()
196 wxHtmlWindow::AddFilter(new wxHtmlFilterHTML
);
197 wxHtmlWindow::AddFilter(new wxHtmlFilterImage
);
200 virtual void OnExit() {}
203 IMPLEMENT_DYNAMIC_CLASS(wxHtmlFilterModule
, wxModule
)