]>
git.saurik.com Git - wxWidgets.git/blob - src/html/htmlfilt.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxHtmlFilter - input filter for translating into HTML format
4 // Author: Vaclav Slavik
6 // Copyright: (c) 1999 Vaclav Slavik
7 // Licence: wxWindows Licence
8 /////////////////////////////////////////////////////////////////////////////
12 #pragma implementation "htmlfilt.h"
15 #include "wx/wxprec.h"
18 #if wxUSE_HTML && wxUSE_STREAMS
29 #include "wx/strconv.h"
30 #include "wx/html/htmlfilt.h"
31 #include "wx/html/htmlwin.h"
33 // utility function: read a wxString from a wxInputStream
34 static void ReadString(wxString
& str
, wxInputStream
* s
, wxMBConv
& conv
)
36 size_t streamSize
= s
->GetSize();
38 if (streamSize
== ~(size_t)0)
40 const size_t bufSize
= 4095;
41 char buffer
[bufSize
+1];
46 s
->Read(buffer
, bufSize
);
47 lastRead
= s
->LastRead();
49 str
.Append(wxString(buffer
, conv
));
51 while (lastRead
== bufSize
);
55 char* src
= new char[streamSize
+1];
56 s
->Read(src
, streamSize
);
58 str
= wxString(src
, conv
);
65 There is code for several default filters:
69 IMPLEMENT_ABSTRACT_CLASS(wxHtmlFilter
, wxObject
)
71 //--------------------------------------------------------------------------------
72 // wxHtmlFilterPlainText
73 // filter for text/plain or uknown
74 //--------------------------------------------------------------------------------
76 IMPLEMENT_DYNAMIC_CLASS(wxHtmlFilterPlainText
, wxHtmlFilter
)
78 bool wxHtmlFilterPlainText::CanRead(const wxFSFile
& WXUNUSED(file
)) const
85 wxString
wxHtmlFilterPlainText::ReadFile(const wxFSFile
& file
) const
87 wxInputStream
*s
= file
.GetStream();
90 if (s
== NULL
) return wxEmptyString
;
91 ReadString(doc
, s
, wxConvISO8859_1
);
93 doc
.Replace(wxT("&"), wxT("&"), TRUE
);
94 doc
.Replace(wxT("<"), wxT("<"), TRUE
);
95 doc
.Replace(wxT(">"), wxT(">"), TRUE
);
96 doc2
= wxT("<HTML><BODY><PRE>\n") + doc
+ wxT("\n</PRE></BODY></HTML>");
104 //--------------------------------------------------------------------------------
106 // filter for image/*
107 //--------------------------------------------------------------------------------
109 class wxHtmlFilterImage
: public wxHtmlFilter
111 DECLARE_DYNAMIC_CLASS(wxHtmlFilterImage
)
114 virtual bool CanRead(const wxFSFile
& file
) const;
115 virtual wxString
ReadFile(const wxFSFile
& file
) const;
118 IMPLEMENT_DYNAMIC_CLASS(wxHtmlFilterImage
, wxHtmlFilter
)
122 bool wxHtmlFilterImage::CanRead(const wxFSFile
& file
) const
124 return (file
.GetMimeType().Left(6) == wxT("image/"));
129 wxString
wxHtmlFilterImage::ReadFile(const wxFSFile
& file
) const
131 wxString res
= wxT("<HTML><BODY><IMG SRC=\"") + file
.GetLocation() + wxT("\"></BODY></HTML>");
138 //--------------------------------------------------------------------------------
140 // filter for text/html
141 //--------------------------------------------------------------------------------
144 IMPLEMENT_DYNAMIC_CLASS(wxHtmlFilterHTML
, wxHtmlFilter
)
146 bool wxHtmlFilterHTML::CanRead(const wxFSFile
& file
) const
148 // return (file.GetMimeType() == "text/html");
149 // This is true in most case but some page can return:
150 // "text/html; char-encoding=...."
151 // So we use Find instead
152 return (file
.GetMimeType().Find(wxT("text/html")) == 0);
157 wxString
wxHtmlFilterHTML::ReadFile(const wxFSFile
& file
) const
159 wxInputStream
*s
= file
.GetStream();
164 wxLogError(_("Cannot open HTML document: %s"), file
.GetLocation().c_str());
165 return wxEmptyString
;
168 // NB: We convert input file to wchar_t here in Unicode mode, based on
169 // either Content-Type header or <meta> tags. In ANSI mode, we don't
170 // do it as it is done by wxHtmlParser (for this reason, we add <meta>
171 // tag if we used Content-Type header).
174 if ((charsetPos
= file
.GetMimeType().Find(_T("; charset="))) != wxNOT_FOUND
)
176 wxString charset
= file
.GetMimeType().Mid(charsetPos
+ 10);
177 wxCSConv
conv(charset
);
178 ReadString(doc
, s
, conv
);
183 ReadString(tmpdoc
, s
, wxConvISO8859_1
);
184 wxString charset
= wxHtmlParser::ExtractCharsetInformation(tmpdoc
);
189 wxCSConv
conv(charset
);
190 doc
= wxString(tmpdoc
.mb_str(wxConvISO8859_1
), conv
);
193 #else // !wxUSE_UNICODE
194 ReadString(doc
, s
, wxConvLibc
);
195 // add meta tag if we obtained this through http:
196 if (!file
.GetMimeType().empty())
199 wxString mime
= file
.GetMimeType();
200 hdr
.Printf(_T("<meta http-equiv=\"Content-Type\" content=\"%s\">"), mime
.c_str());
213 class wxHtmlFilterModule
: public wxModule
215 DECLARE_DYNAMIC_CLASS(wxHtmlFilterModule
)
218 virtual bool OnInit()
220 wxHtmlWindow::AddFilter(new wxHtmlFilterHTML
);
221 wxHtmlWindow::AddFilter(new wxHtmlFilterImage
);
224 virtual void OnExit() {}
227 IMPLEMENT_DYNAMIC_CLASS(wxHtmlFilterModule
, wxModule
)