]>
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 /////////////////////////////////////////////////////////////////////////////
10 #include "wx/wxprec.h"
13 #if wxUSE_HTML && wxUSE_STREAMS
24 #include "wx/strconv.h"
25 #include "wx/html/htmlfilt.h"
26 #include "wx/html/htmlwin.h"
28 // utility function: read a wxString from a wxInputStream
29 static void ReadString(wxString
& str
, wxInputStream
* s
, wxMBConv
& conv
)
31 size_t streamSize
= s
->GetSize();
33 if (streamSize
== ~(size_t)0)
35 const size_t bufSize
= 4095;
36 char buffer
[bufSize
+1];
41 s
->Read(buffer
, bufSize
);
42 lastRead
= s
->LastRead();
44 str
.Append(wxString(buffer
, conv
));
46 while (lastRead
== bufSize
);
50 char* src
= new char[streamSize
+1];
51 s
->Read(src
, streamSize
);
53 str
= wxString(src
, conv
);
60 There is code for several default filters:
64 IMPLEMENT_ABSTRACT_CLASS(wxHtmlFilter
, wxObject
)
66 //--------------------------------------------------------------------------------
67 // wxHtmlFilterPlainText
68 // filter for text/plain or uknown
69 //--------------------------------------------------------------------------------
71 IMPLEMENT_DYNAMIC_CLASS(wxHtmlFilterPlainText
, wxHtmlFilter
)
73 bool wxHtmlFilterPlainText::CanRead(const wxFSFile
& WXUNUSED(file
)) const
80 wxString
wxHtmlFilterPlainText::ReadFile(const wxFSFile
& file
) const
82 wxInputStream
*s
= file
.GetStream();
85 if (s
== NULL
) return wxEmptyString
;
86 ReadString(doc
, s
, wxConvISO8859_1
);
88 doc
.Replace(wxT("&"), wxT("&"), true);
89 doc
.Replace(wxT("<"), wxT("<"), true);
90 doc
.Replace(wxT(">"), wxT(">"), true);
91 doc2
= wxT("<HTML><BODY><PRE>\n") + doc
+ wxT("\n</PRE></BODY></HTML>");
99 //--------------------------------------------------------------------------------
101 // filter for image/*
102 //--------------------------------------------------------------------------------
104 class wxHtmlFilterImage
: public wxHtmlFilter
106 DECLARE_DYNAMIC_CLASS(wxHtmlFilterImage
)
109 virtual bool CanRead(const wxFSFile
& file
) const;
110 virtual wxString
ReadFile(const wxFSFile
& file
) const;
113 IMPLEMENT_DYNAMIC_CLASS(wxHtmlFilterImage
, wxHtmlFilter
)
117 bool wxHtmlFilterImage::CanRead(const wxFSFile
& file
) const
119 return (file
.GetMimeType().Left(6) == wxT("image/"));
124 wxString
wxHtmlFilterImage::ReadFile(const wxFSFile
& file
) const
126 wxString res
= wxT("<HTML><BODY><IMG SRC=\"") + file
.GetLocation() + wxT("\"></BODY></HTML>");
133 //--------------------------------------------------------------------------------
135 // filter for text/html
136 //--------------------------------------------------------------------------------
139 IMPLEMENT_DYNAMIC_CLASS(wxHtmlFilterHTML
, wxHtmlFilter
)
141 bool wxHtmlFilterHTML::CanRead(const wxFSFile
& file
) const
143 // return (file.GetMimeType() == "text/html");
144 // This is true in most case but some page can return:
145 // "text/html; char-encoding=...."
146 // So we use Find instead
147 return (file
.GetMimeType().Find(wxT("text/html")) == 0);
152 wxString
wxHtmlFilterHTML::ReadFile(const wxFSFile
& file
) const
154 wxInputStream
*s
= file
.GetStream();
159 wxLogError(_("Cannot open HTML document: %s"), file
.GetLocation().c_str());
160 return wxEmptyString
;
163 // NB: We convert input file to wchar_t here in Unicode mode, based on
164 // either Content-Type header or <meta> tags. In ANSI mode, we don't
165 // do it as it is done by wxHtmlParser (for this reason, we add <meta>
166 // tag if we used Content-Type header).
169 if ((charsetPos
= file
.GetMimeType().Find(_T("; charset="))) != wxNOT_FOUND
)
171 wxString charset
= file
.GetMimeType().Mid(charsetPos
+ 10);
172 wxCSConv
conv(charset
);
173 ReadString(doc
, s
, conv
);
178 ReadString(tmpdoc
, s
, wxConvISO8859_1
);
179 wxString charset
= wxHtmlParser::ExtractCharsetInformation(tmpdoc
);
184 wxCSConv
conv(charset
);
185 doc
= wxString(tmpdoc
.mb_str(wxConvISO8859_1
), conv
);
188 #else // !wxUSE_UNICODE
189 ReadString(doc
, s
, wxConvLibc
);
190 // add meta tag if we obtained this through http:
191 if (!file
.GetMimeType().empty())
194 wxString mime
= file
.GetMimeType();
195 hdr
.Printf(_T("<meta http-equiv=\"Content-Type\" content=\"%s\">"), mime
.c_str());
208 class wxHtmlFilterModule
: public wxModule
210 DECLARE_DYNAMIC_CLASS(wxHtmlFilterModule
)
213 virtual bool OnInit()
215 wxHtmlWindow::AddFilter(new wxHtmlFilterHTML
);
216 wxHtmlWindow::AddFilter(new wxHtmlFilterImage
);
219 virtual void OnExit() {}
222 IMPLEMENT_DYNAMIC_CLASS(wxHtmlFilterModule
, wxModule
)