implemented (more) correct <pre> handling

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@9870 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Václav Slavík
2001-04-24 22:05:19 +00:00
parent 3da2715fb8
commit 1309ba6c7e
3 changed files with 139 additions and 221 deletions

View File

@@ -36,97 +36,100 @@ class WXDLLEXPORT wxHtmlParser : public wxObject
{ {
DECLARE_ABSTRACT_CLASS(wxHtmlParser) DECLARE_ABSTRACT_CLASS(wxHtmlParser)
public: public:
wxHtmlParser() : wxObject(), m_HandlersHash(wxKEY_STRING) {m_FS = NULL; m_Cache = NULL; m_HandlersStack = NULL;} wxHtmlParser() : wxObject(), m_HandlersHash(wxKEY_STRING)
virtual ~wxHtmlParser(); { m_FS = NULL; m_Cache = NULL; m_HandlersStack = NULL; }
virtual ~wxHtmlParser();
void SetFS(wxFileSystem *fs) {m_FS = fs;} // Sets the class which will be used for opening files
// Sets the class which will be used for opening files void SetFS(wxFileSystem *fs) { m_FS = fs; }
wxFileSystem* GetFS() const {return m_FS;}
wxObject* Parse(const wxString& source); wxFileSystem* GetFS() const { return m_FS; }
// You can simply call this method when you need parsed output.
// This method does these things:
// 1. call InitParser(source);
// 2. call DoParsing();
// 3. call GetProduct(); (it's return value is then returned)
// 4. call DoneParser();
virtual void InitParser(const wxString& source); // You can simply call this method when you need parsed output.
// Sets the source. This must be called before running Parse() method. // This method does these things:
virtual void DoneParser(); // 1. call InitParser(source);
// This must be called after Parse(). // 2. call DoParsing();
// 3. call GetProduct(); (it's return value is then returned)
void DoParsing(int begin_pos, int end_pos); // 4. call DoneParser();
inline void DoParsing() {DoParsing(0, m_Source.Length());}; wxObject* Parse(const wxString& source);
// Parses the m_Source from begin_pos to end_pos-1.
// (in noparams version it parses whole m_Source)
virtual wxObject* GetProduct() = 0; // Sets the source. This must be called before running Parse() method.
// Returns product of parsing virtual void InitParser(const wxString& source);
// Returned value is result of parsing of the part. The type of this result // This must be called after Parse().
// depends on internal representation in derived parser virtual void DoneParser();
// (see wxHtmlWinParser for details).
virtual void AddTagHandler(wxHtmlTagHandler *handler); // Parses the m_Source from begin_pos to end_pos-1.
// adds handler to the list & hash table of handlers. // (in noparams version it parses whole m_Source)
void DoParsing(int begin_pos, int end_pos);
void PushTagHandler(wxHtmlTagHandler *handler, wxString tags); inline void DoParsing() {DoParsing(0, m_Source.Length());};
// Forces the handler to handle additional tags (not returned by GetSupportedTags).
// The handler should already be in use by this parser.
// Example: you want to parse following pseudo-html structure:
// <myitems>
// <it name="one" value="1">
// <it name="two" value="2">
// </myitems>
// <it> This last it has different meaning, we don't want it to be parsed by myitems handler!
// handler can handle only 'myitems' (e.g. it's GetSupportedTags returns "MYITEMS")
// you can call PushTagHandler(handler, "IT") when you find <myitems>
// and call PopTagHandler() when you find </myitems>
void PopTagHandler();
// Restores state before last call to PushTagHandler
wxString* GetSource() {return &m_Source;} // Returns product of parsing
// Returned value is result of parsing of the part. The type of this result
// depends on internal representation in derived parser
// (see wxHtmlWinParser for details).
virtual wxObject* GetProduct() = 0;
protected: // adds handler to the list & hash table of handlers.
virtual void AddTagHandler(wxHtmlTagHandler *handler);
virtual void AddText(const char* txt) = 0; // Forces the handler to handle additional tags (not returned by GetSupportedTags).
// Adds text to the output. // The handler should already be in use by this parser.
// This is called from Parse() and must be overriden in derived classes. // Example: you want to parse following pseudo-html structure:
// txt is not guaranteed to be only one word. It is largest continuous part of text // <myitems>
// (= not broken by tags) // <it name="one" value="1">
// NOTE : using char* because of speed improvements // <it name="two" value="2">
// </myitems>
// <it> This last it has different meaning, we don't want it to be parsed by myitems handler!
// handler can handle only 'myitems' (e.g. it's GetSupportedTags returns "MYITEMS")
// you can call PushTagHandler(handler, "IT") when you find <myitems>
// and call PopTagHandler() when you find </myitems>
void PushTagHandler(wxHtmlTagHandler *handler, wxString tags);
virtual void AddTag(const wxHtmlTag& tag); // Restores state before last call to PushTagHandler
// Adds tag and proceeds it. Parse() may (and usually is) called from this method. void PopTagHandler();
// This is called from Parse() and may be overriden.
// Default behavior is that it looks for proper handler in m_Handlers. The tag is
// ignored if no hander is found.
// Derived class is *responsible* for filling in m_Handlers table.
wxString* GetSource() {return &m_Source;}
void SetSource(const wxString& src);
protected: protected:
wxString m_Source; // Adds text to the output.
// source being parsed // This is called from Parse() and must be overriden in derived classes.
wxHtmlTagsCache *m_Cache; // txt is not guaranteed to be only one word. It is largest continuous part of text
// tags cache, used during parsing. // (= not broken by tags)
wxHashTable m_HandlersHash; // NOTE : using char* because of speed improvements
wxList m_HandlersList; virtual void AddText(const char* txt) = 0;
// handlers that handle particular tags. The table is accessed by
// key = tag's name.
// This attribute MUST be filled by derived class otherwise it would
// be empty and no tags would be recognized
// (see wxHtmlWinParser for details about filling it)
// m_HandlersHash is for random access based on knowledge of tag name (BR, P, etc.)
// it may (and often does) contain more references to one object
// m_HandlersList is list of all handlers and it is guaranteed to contain
// only one reference to each handler instance.
wxFileSystem *m_FS;
// class for opening files (file system)
wxList *m_HandlersStack;
// handlers stack used by PushTagHandler and PopTagHandler
// Adds tag and proceeds it. Parse() may (and usually is) called from this method.
// This is called from Parse() and may be overriden.
// Default behavior is that it looks for proper handler in m_Handlers. The tag is
// ignored if no hander is found.
// Derived class is *responsible* for filling in m_Handlers table.
virtual void AddTag(const wxHtmlTag& tag);
protected:
// source being parsed
wxString m_Source;
// tags cache, used during parsing.
wxHtmlTagsCache *m_Cache;
wxHashTable m_HandlersHash;
// handlers that handle particular tags. The table is accessed by
// key = tag's name.
// This attribute MUST be filled by derived class otherwise it would
// be empty and no tags would be recognized
// (see wxHtmlWinParser for details about filling it)
// m_HandlersHash is for random access based on knowledge of tag name (BR, P, etc.)
// it may (and often does) contain more references to one object
// m_HandlersList is list of all handlers and it is guaranteed to contain
// only one reference to each handler instance.
wxList m_HandlersList;
// class for opening files (file system)
wxFileSystem *m_FS;
// handlers stack used by PushTagHandler and PopTagHandler
wxList *m_HandlersStack;
}; };
@@ -149,34 +152,35 @@ class WXDLLEXPORT wxHtmlTagHandler : public wxObject
{ {
DECLARE_ABSTRACT_CLASS(wxHtmlTagHandler) DECLARE_ABSTRACT_CLASS(wxHtmlTagHandler)
protected: public:
wxHtmlParser *m_Parser; wxHtmlTagHandler() : wxObject () { m_Parser = NULL; }
public: // Sets the parser.
wxHtmlTagHandler() : wxObject () {m_Parser = NULL;}; // NOTE : each _instance_ of handler is guaranteed to be called
// only by one parser. This means you don't have to care about
virtual void SetParser(wxHtmlParser *parser) {m_Parser = parser;} // reentrancy.
// Sets the parser. virtual void SetParser(wxHtmlParser *parser)
// NOTE : each _instance_ of handler is guaranteed to be called { m_Parser = parser; }
// only by one parser. This means you don't have to care about
// reentrancy.
virtual wxString GetSupportedTags() = 0;
// Returns list of supported tags. The list is in uppercase and
// tags are delimited by ','.
// Example : "I,B,FONT,P"
// is capable of handling italic, bold, font and paragraph tags
virtual bool HandleTag(const wxHtmlTag& tag) = 0;
// This is hadling core method. It does all the Steps 1-3.
// To process step 2, you can call ParseInner()
// returned value : TRUE if it called ParseInner(),
// FALSE etherwise
protected: // Returns list of supported tags. The list is in uppercase and
void ParseInner(const wxHtmlTag& tag) {m_Parser->DoParsing(tag.GetBeginPos(), tag.GetEndPos1());} // tags are delimited by ','.
// parses input between beginning and ending tag. // Example : "I,B,FONT,P"
// m_Parser must be set. // is capable of handling italic, bold, font and paragraph tags
virtual wxString GetSupportedTags() = 0;
// This is hadling core method. It does all the Steps 1-3.
// To process step 2, you can call ParseInner()
// returned value : TRUE if it called ParseInner(),
// FALSE etherwise
virtual bool HandleTag(const wxHtmlTag& tag) = 0;
protected:
// parses input between beginning and ending tag.
// m_Parser must be set.
void ParseInner(const wxHtmlTag& tag)
{ m_Parser->DoParsing(tag.GetBeginPos(), tag.GetEndPos1()); }
wxHtmlParser *m_Parser;
}; };

View File

@@ -51,24 +51,23 @@ wxObject* wxHtmlParser::Parse(const wxString& source)
return result; return result;
} }
void wxHtmlParser::InitParser(const wxString& source) void wxHtmlParser::InitParser(const wxString& source)
{ {
m_Source = source; SetSource(source);
m_Cache = new wxHtmlTagsCache(m_Source);
} }
void wxHtmlParser::DoneParser() void wxHtmlParser::DoneParser()
{ {
delete m_Cache; delete m_Cache;
m_Cache = NULL; m_Cache = NULL;
} }
void wxHtmlParser::SetSource(const wxString& src)
{
m_Source = src;
delete m_Cache;
m_Cache = new wxHtmlTagsCache(m_Source);
}
void wxHtmlParser::DoParsing(int begin_pos, int end_pos) void wxHtmlParser::DoParsing(int begin_pos, int end_pos)
{ {
@@ -117,8 +116,6 @@ void wxHtmlParser::DoParsing(int begin_pos, int end_pos)
delete[] temp; delete[] temp;
} }
void wxHtmlParser::AddTag(const wxHtmlTag& tag) void wxHtmlParser::AddTag(const wxHtmlTag& tag)
{ {
wxHtmlTagHandler *h; wxHtmlTagHandler *h;
@@ -134,8 +131,6 @@ void wxHtmlParser::AddTag(const wxHtmlTag& tag)
} }
} }
void wxHtmlParser::AddTagHandler(wxHtmlTagHandler *handler) void wxHtmlParser::AddTagHandler(wxHtmlTagHandler *handler)
{ {
wxString s(handler->GetSupportedTags()); wxString s(handler->GetSupportedTags());
@@ -150,8 +145,6 @@ void wxHtmlParser::AddTagHandler(wxHtmlTagHandler *handler)
handler->SetParser(this); handler->SetParser(this);
} }
void wxHtmlParser::PushTagHandler(wxHtmlTagHandler *handler, wxString tags) void wxHtmlParser::PushTagHandler(wxHtmlTagHandler *handler, wxString tags)
{ {
wxStringTokenizer tokenizer(tags, ", "); wxStringTokenizer tokenizer(tags, ", ");
@@ -173,8 +166,6 @@ void wxHtmlParser::PushTagHandler(wxHtmlTagHandler *handler, wxString tags)
} }
} }
void wxHtmlParser::PopTagHandler() void wxHtmlParser::PopTagHandler()
{ {
wxNode *first; wxNode *first;
@@ -189,8 +180,6 @@ void wxHtmlParser::PopTagHandler()
m_HandlersStack->DeleteNode(first); m_HandlersStack->DeleteNode(first);
} }
wxHtmlParser::~wxHtmlParser() wxHtmlParser::~wxHtmlParser()
{ {
if (m_HandlersStack) delete m_HandlersStack; if (m_HandlersStack) delete m_HandlersStack;
@@ -199,8 +188,6 @@ wxHtmlParser::~wxHtmlParser()
m_HandlersList.Clear(); m_HandlersList.Clear();
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// wxHtmlTagHandler // wxHtmlTagHandler
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------

View File

@@ -33,82 +33,6 @@
FORCE_LINK_ME(m_pre) FORCE_LINK_ME(m_pre)
//-----------------------------------------------------------------------------
// wxHtmlCodeCell
//-----------------------------------------------------------------------------
class wxHtmlPRECell : public wxHtmlCell
{
private:
wxString** m_Text;
// list of wxString objects.
int m_LinesCnt;
// number of lines
int m_LineHeight;
// height of single line of text
public:
wxHtmlPRECell(const wxString& s, wxDC& dc);
~wxHtmlPRECell();
void Draw(wxDC& dc, int x, int y, int view_y1, int view_y2);
};
wxHtmlPRECell::wxHtmlPRECell(const wxString& s, wxDC& dc) : wxHtmlCell()
{
wxStringTokenizer tokenizer(s, "\n");
wxString tmp;
long int x, z;
int i;
m_LineHeight = dc.GetCharHeight();
m_LinesCnt = 0;
m_Text = NULL;
m_Width = m_Height = 0;
i = 0;
while (tokenizer.HasMoreTokens())
{
if (i % 10 == 0) m_Text = (wxString**) realloc(m_Text, sizeof(wxString*) * (i + 10));
tmp = tokenizer.NextToken();
tmp.Replace(wxT("&copy;"), wxT("(c)"), TRUE);
tmp.Replace(wxT("&nbsp;"), wxT(" "), TRUE);
tmp.Replace(wxT("&quot;"), wxT("\""), TRUE);
tmp.Replace(wxT("&lt;"), wxT("<"), TRUE);
tmp.Replace(wxT("&gt;"), wxT(">"), TRUE);
tmp.Replace(wxT("&amp;"), wxT("&"), TRUE);
tmp.Replace(wxT("\t"), wxT(" "), TRUE);
tmp.Replace(wxT("\r"), wxT(""), TRUE);
m_Text[i++] = new wxString(tmp);
dc.GetTextExtent(tmp, &x, &z, &z);
if (x > m_Width) m_Width = x;
m_Height += m_LineHeight;
m_LinesCnt++;
}
}
wxHtmlPRECell::~wxHtmlPRECell()
{
for (int i = 0; i < m_LinesCnt; i++) delete m_Text[i];
free(m_Text);
}
void wxHtmlPRECell::Draw(wxDC& dc, int x, int y, int view_y1, int view_y2)
{
for (int i = 0; i < m_LinesCnt; i++)
dc.DrawText(*(m_Text[i]), x + m_PosX, y + m_PosY + m_LineHeight * i);
wxHtmlCell::Draw(dc, x, y, view_y1, view_y2);
}
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// The list handler: // The list handler:
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
@@ -126,11 +50,7 @@ TAG_HANDLER_BEGIN(PRE, "PRE")
bold = m_WParser->GetFontBold(), bold = m_WParser->GetFontBold(),
fsize = m_WParser->GetFontSize(); fsize = m_WParser->GetFontSize();
m_WParser->CloseContainer(); c = m_WParser->GetContainer();
c = m_WParser->OpenContainer();
c->SetAlignHor(wxHTML_ALIGN_LEFT);
c->SetIndent(m_WParser->GetCharHeight(), wxHTML_INDENT_VERTICAL);
m_WParser->SetFontUnderlined(FALSE); m_WParser->SetFontUnderlined(FALSE);
m_WParser->SetFontBold(FALSE); m_WParser->SetFontBold(FALSE);
m_WParser->SetFontItalic(FALSE); m_WParser->SetFontItalic(FALSE);
@@ -138,18 +58,27 @@ TAG_HANDLER_BEGIN(PRE, "PRE")
m_WParser->SetFontSize(3); m_WParser->SetFontSize(3);
c->InsertCell(new wxHtmlFontCell(m_WParser->CreateCurrentFont())); c->InsertCell(new wxHtmlFontCell(m_WParser->CreateCurrentFont()));
{ m_WParser->CloseContainer();
wxString cit; c = m_WParser->OpenContainer();
wxEncodingConverter *encconv = m_WParser->GetEncodingConverter(); c->SetAlignHor(wxHTML_ALIGN_LEFT);
cit = m_WParser->GetSource()->Mid(tag.GetBeginPos(),
tag.GetEndPos1() - tag.GetBeginPos()); wxString src, srcMid;
if (encconv)
c->InsertCell(new wxHtmlPRECell(encconv->Convert(cit), src = *m_WParser->GetSource();
*(m_WParser->GetDC()))); srcMid = src.Mid(tag.GetBeginPos(),
else tag.GetEndPos1() - tag.GetBeginPos());
c->InsertCell(new wxHtmlPRECell(cit, srcMid.Replace(wxT("\t"), wxT(" "));
*(m_WParser->GetDC()))); srcMid.Replace(wxT(" "), wxT("&nbsp;"));
} srcMid.Replace(wxT("\n"), wxT("<br>"));
// It is safe to temporarily change the source being parsed,
// provided we restore the state back after parsing
m_Parser->SetSource(srcMid);
m_Parser->DoParsing();
m_Parser->SetSource(src);
m_WParser->CloseContainer();
c = m_WParser->OpenContainer();
m_WParser->SetFontUnderlined(underlined); m_WParser->SetFontUnderlined(underlined);
m_WParser->SetFontBold(bold); m_WParser->SetFontBold(bold);
@@ -158,8 +87,6 @@ TAG_HANDLER_BEGIN(PRE, "PRE")
m_WParser->SetFontSize(fsize); m_WParser->SetFontSize(fsize);
c->InsertCell(new wxHtmlFontCell(m_WParser->CreateCurrentFont())); c->InsertCell(new wxHtmlFontCell(m_WParser->CreateCurrentFont()));
m_WParser->CloseContainer();
m_WParser->OpenContainer();
return TRUE; return TRUE;
} }