1 |
786 |
skrzyp |
// ####ECOSHOSTGPLCOPYRIGHTBEGIN####
|
2 |
|
|
// -------------------------------------------
|
3 |
|
|
// This file is part of the eCos host tools.
|
4 |
|
|
// Copyright (C) 1998, 1999, 2000 Free Software Foundation, Inc.
|
5 |
|
|
//
|
6 |
|
|
// This program is free software; you can redistribute it and/or modify
|
7 |
|
|
// it under the terms of the GNU General Public License as published by
|
8 |
|
|
// the Free Software Foundation; either version 2 or (at your option) any
|
9 |
|
|
// later version.
|
10 |
|
|
//
|
11 |
|
|
// This program is distributed in the hope that it will be useful, but
|
12 |
|
|
// WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
14 |
|
|
// General Public License for more details.
|
15 |
|
|
//
|
16 |
|
|
// You should have received a copy of the GNU General Public License
|
17 |
|
|
// along with this program; if not, write to the
|
18 |
|
|
// Free Software Foundation, Inc., 51 Franklin Street,
|
19 |
|
|
// Fifth Floor, Boston, MA 02110-1301, USA.
|
20 |
|
|
// -------------------------------------------
|
21 |
|
|
// ####ECOSHOSTGPLCOPYRIGHTEND####
|
22 |
|
|
//===========================================================================
|
23 |
|
|
//#####DESCRIPTIONBEGIN####
|
24 |
|
|
//
|
25 |
|
|
// Author(s): julians
|
26 |
|
|
// Contact(s): julians
|
27 |
|
|
// Date: 2001/04/04
|
28 |
|
|
// Version: 0.01
|
29 |
|
|
// Purpose:
|
30 |
|
|
// Description: HTML parser/HTML Help file generator
|
31 |
|
|
// Requires:
|
32 |
|
|
// Provides:
|
33 |
|
|
// See also:
|
34 |
|
|
// Known bugs:
|
35 |
|
|
// Usage:
|
36 |
|
|
//
|
37 |
|
|
//####DESCRIPTIONEND####
|
38 |
|
|
//
|
39 |
|
|
//===========================================================================
|
40 |
|
|
|
41 |
|
|
#ifndef _EC_HTMLPARSER_H_
|
42 |
|
|
#define _EC_HTMLPARSER_H_
|
43 |
|
|
|
44 |
|
|
#ifdef __GNUG__
|
45 |
|
|
#pragma interface "htmlparser.cpp"
|
46 |
|
|
#endif
|
47 |
|
|
|
48 |
|
|
#include "wx/module.h"
|
49 |
|
|
#include "wx/stream.h"
|
50 |
|
|
|
51 |
|
|
/*
|
52 |
|
|
|
53 |
|
|
So how are going to represent it: compare with my Latex parser.
|
54 |
|
|
This generates a hierarchy because it respects the hierarchical nature of the Latex
|
55 |
|
|
commands. However, we don't _have_ to do that, we can make it linear, e.g.
|
56 |
|
|
|
57 |
|
|
tag-with-attributes text-chunk end-tag-with-attributes tag-with-attributes text-chunk
|
58 |
|
|
|
59 |
|
|
Otherwise, we need knowledge about HTML tags to parse hierarchically. This wouldn't be hard.
|
60 |
|
|
Would need to specify which tags have open/close parts, which don't, and for which it's optional
|
61 |
|
|
(such as <P>).
|
62 |
|
|
|
63 |
|
|
|
64 |
|
|
*/
|
65 |
|
|
|
66 |
|
|
/*
|
67 |
|
|
* wxSimpleHtmlAttribute
|
68 |
|
|
* Representation of an attribute
|
69 |
|
|
*/
|
70 |
|
|
|
71 |
|
|
class wxSimpleHtmlAttribute
|
72 |
|
|
{
|
73 |
|
|
friend class wxSimpleHtmlTag;
|
74 |
|
|
public:
|
75 |
|
|
wxSimpleHtmlAttribute(const wxString& name, const wxString& value)
|
76 |
|
|
{
|
77 |
|
|
m_name = name; m_value = value; m_next = NULL;
|
78 |
|
|
}
|
79 |
|
|
//// Operations
|
80 |
|
|
|
81 |
|
|
// Write this attribute
|
82 |
|
|
void Write(wxOutputStream& stream);
|
83 |
|
|
|
84 |
|
|
//// Accessors
|
85 |
|
|
const wxString& GetName() const { return m_name; }
|
86 |
|
|
const wxString& GetValue() const { return m_value; }
|
87 |
|
|
|
88 |
|
|
wxSimpleHtmlAttribute* GetNextAttribute() { return m_next; }
|
89 |
|
|
void SetNextAttribute(wxSimpleHtmlAttribute* attr) { m_next = attr; }
|
90 |
|
|
|
91 |
|
|
bool HasName(const wxString& name) const { return (0 == m_name.CmpNoCase(name)); }
|
92 |
|
|
bool HasValue(const wxString& val) const { return (0 == m_value.CmpNoCase(val)); }
|
93 |
|
|
|
94 |
|
|
private:
|
95 |
|
|
wxString m_name;
|
96 |
|
|
wxString m_value;
|
97 |
|
|
wxSimpleHtmlAttribute* m_next;
|
98 |
|
|
};
|
99 |
|
|
|
100 |
|
|
|
101 |
|
|
/*
|
102 |
|
|
* wxSimpleHtmlTag
|
103 |
|
|
* Representation of a tag or chunk of text
|
104 |
|
|
*/
|
105 |
|
|
|
106 |
|
|
enum { wxSimpleHtmlTag_Text, wxSimpleHtmlTag_TopLevel, wxSimpleHtmlTag_Open, wxSimpleHtmlTag_Close, wxSimpleHtmlTag_Directive };
|
107 |
|
|
|
108 |
|
|
class wxSimpleHtmlTag
|
109 |
|
|
{
|
110 |
|
|
public:
|
111 |
|
|
wxSimpleHtmlTag(const wxString& tagName, int tagType);
|
112 |
|
|
~wxSimpleHtmlTag();
|
113 |
|
|
|
114 |
|
|
//// Operations
|
115 |
|
|
void ClearAttributes();
|
116 |
|
|
wxSimpleHtmlAttribute* FindAttribute(const wxString& name) const ;
|
117 |
|
|
void AppendAttribute(const wxString& name, const wxString& value);
|
118 |
|
|
void ClearChildren();
|
119 |
|
|
void AppendTag(wxSimpleHtmlTag* tag);
|
120 |
|
|
// Write this tag
|
121 |
|
|
void Write(wxOutputStream& stream);
|
122 |
|
|
|
123 |
|
|
// Gets the text from this tag and its descendants
|
124 |
|
|
wxString GetTagText();
|
125 |
|
|
|
126 |
|
|
//// Accessors
|
127 |
|
|
const wxString& GetName() const { return m_name; }
|
128 |
|
|
void SetName(const wxString& name) { m_name = name; }
|
129 |
|
|
|
130 |
|
|
int GetType() const { return m_type; }
|
131 |
|
|
void SetType(int t) { m_type = t; }
|
132 |
|
|
|
133 |
|
|
// If type is wxSimpleHtmlTag_Text, m_text will contain some text.
|
134 |
|
|
const wxString& GetText() const { return m_text; }
|
135 |
|
|
void SetText(const wxString& text) { m_text = text; }
|
136 |
|
|
|
137 |
|
|
wxSimpleHtmlAttribute* GetFirstAttribute() { return m_attributes; }
|
138 |
|
|
void SetFirstAttribute(wxSimpleHtmlAttribute* attr) { m_attributes = attr; }
|
139 |
|
|
|
140 |
|
|
int GetAttributeCount() const ;
|
141 |
|
|
wxSimpleHtmlAttribute* GetAttribute(int i) const ;
|
142 |
|
|
|
143 |
|
|
wxSimpleHtmlTag* GetChildren() const { return m_children; }
|
144 |
|
|
void SetChildren(wxSimpleHtmlTag* children) { m_children = children; }
|
145 |
|
|
|
146 |
|
|
wxSimpleHtmlTag* GetParent() const { return m_parent; }
|
147 |
|
|
void SetParent(wxSimpleHtmlTag* parent) { m_parent = parent; }
|
148 |
|
|
int GetChildCount() const;
|
149 |
|
|
wxSimpleHtmlTag* GetChild(int i) const;
|
150 |
|
|
wxSimpleHtmlTag* GetNext() const { return m_next; }
|
151 |
|
|
|
152 |
|
|
//// Convenience accessors & search functions
|
153 |
|
|
bool NameIs(const wxString& name) { return (m_name.CmpNoCase(name) == 0); }
|
154 |
|
|
bool HasAttribute(const wxString& name, const wxString& value) const;
|
155 |
|
|
bool HasAttribute(const wxString& name) const;
|
156 |
|
|
bool GetAttributeValue(wxString& value, const wxString& attrName);
|
157 |
|
|
|
158 |
|
|
// Search forward from this tag until we find a tag with this name & attribute
|
159 |
|
|
wxSimpleHtmlTag* FindTag(const wxString& tagName, const wxString& attrName);
|
160 |
|
|
|
161 |
|
|
// Gather the text until we hit the given close tag
|
162 |
|
|
bool FindTextUntilTagClose(wxString& text, const wxString& tagName);
|
163 |
|
|
|
164 |
|
|
private:
|
165 |
|
|
wxString m_name;
|
166 |
|
|
int m_type;
|
167 |
|
|
wxString m_text;
|
168 |
|
|
wxSimpleHtmlAttribute* m_attributes;
|
169 |
|
|
|
170 |
|
|
// List of children
|
171 |
|
|
wxSimpleHtmlTag* m_children;
|
172 |
|
|
wxSimpleHtmlTag* m_next; // Next sibling
|
173 |
|
|
wxSimpleHtmlTag* m_parent;
|
174 |
|
|
};
|
175 |
|
|
|
176 |
|
|
/*
|
177 |
|
|
* wxSimpleHtmlParser
|
178 |
|
|
* Simple HTML parser, for such tasks as scanning HTML for keywords, contents, etc.
|
179 |
|
|
*/
|
180 |
|
|
|
181 |
|
|
class wxSimpleHtmlParser : public wxObject
|
182 |
|
|
{
|
183 |
|
|
|
184 |
|
|
public:
|
185 |
|
|
wxSimpleHtmlParser();
|
186 |
|
|
~wxSimpleHtmlParser();
|
187 |
|
|
|
188 |
|
|
//// Operations
|
189 |
|
|
bool ParseFile(const wxString& filename);
|
190 |
|
|
bool ParseString(const wxString& str);
|
191 |
|
|
void Clear();
|
192 |
|
|
// Write this file
|
193 |
|
|
void Write(wxOutputStream& stream);
|
194 |
|
|
bool WriteFile(wxString& filename);
|
195 |
|
|
|
196 |
|
|
//// Helpers
|
197 |
|
|
|
198 |
|
|
// Main recursive parsing function
|
199 |
|
|
bool ParseHtml(wxSimpleHtmlTag* parent);
|
200 |
|
|
|
201 |
|
|
wxSimpleHtmlTag* ParseTagHeader();
|
202 |
|
|
wxSimpleHtmlTag* ParseTagClose();
|
203 |
|
|
bool ParseAttributes(wxSimpleHtmlTag* tag);
|
204 |
|
|
wxSimpleHtmlTag* ParseDirective(); // e.g. <!DOCTYPE ....>
|
205 |
|
|
bool ParseComment(); // Throw away comments
|
206 |
|
|
// Plain text, up until an angled bracket
|
207 |
|
|
bool ParseText(wxString& text);
|
208 |
|
|
|
209 |
|
|
bool EatWhitespace(); // Throw away whitespace
|
210 |
|
|
bool EatWhitespace(int& pos); // Throw away whitespace: using 'pos'
|
211 |
|
|
bool ReadString(wxString& str, bool eatIt = FALSE);
|
212 |
|
|
bool ReadWord(wxString& str, bool eatIt = FALSE);
|
213 |
|
|
bool ReadNumber(wxString& str, bool eatIt = FALSE);
|
214 |
|
|
// Could be number, string, whatever, but read up until whitespace.
|
215 |
|
|
bool ReadLiteral(wxString& str, bool eatIt = FALSE);
|
216 |
|
|
|
217 |
|
|
bool IsDirective();
|
218 |
|
|
bool IsComment();
|
219 |
|
|
bool IsString();
|
220 |
|
|
bool IsWord();
|
221 |
|
|
bool IsTagClose();
|
222 |
|
|
bool IsTagStartBracket(int ch);
|
223 |
|
|
bool IsTagEndBracket(int ch);
|
224 |
|
|
bool IsWhitespace(int ch);
|
225 |
|
|
bool IsAlpha(int ch);
|
226 |
|
|
bool IsWordChar(int ch);
|
227 |
|
|
bool IsNumeric(int ch);
|
228 |
|
|
|
229 |
|
|
// Matches this string (case insensitive)
|
230 |
|
|
bool Matches(const wxString& tok, bool eatIt = FALSE) ;
|
231 |
|
|
bool Eof() const { return (m_pos >= m_length); }
|
232 |
|
|
bool Eof(int pos) const { return (pos >= m_length); }
|
233 |
|
|
|
234 |
|
|
void SetPosition(int pos) { m_pos = pos; }
|
235 |
|
|
|
236 |
|
|
|
237 |
|
|
//// Accessors
|
238 |
|
|
wxSimpleHtmlTag* GetTopLevelTag() const { return m_topLevel; }
|
239 |
|
|
|
240 |
|
|
// Safe way of getting a character
|
241 |
|
|
int GetChar(size_t i) const;
|
242 |
|
|
|
243 |
|
|
private:
|
244 |
|
|
|
245 |
|
|
wxSimpleHtmlTag* m_topLevel;
|
246 |
|
|
int m_pos; // Position in string
|
247 |
|
|
int m_length; // Length of string
|
248 |
|
|
wxString m_text; // The actual text
|
249 |
|
|
|
250 |
|
|
};
|
251 |
|
|
|
252 |
|
|
/*
|
253 |
|
|
* wxSimpleHtmlTagSpec
|
254 |
|
|
* Describes a tag, and what type it is.
|
255 |
|
|
* wxSimpleHtmlModule will initialise/cleanup a list of these, one per tag type
|
256 |
|
|
*/
|
257 |
|
|
|
258 |
|
|
#if 0
|
259 |
|
|
class wxSimpleHtmlTagSpec : public wxObject
|
260 |
|
|
{
|
261 |
|
|
|
262 |
|
|
public:
|
263 |
|
|
wxSimpleHtmlTagSpec(const wxString& name, int type);
|
264 |
|
|
|
265 |
|
|
//// Operations
|
266 |
|
|
static void AddTagSpec(wxSimpleHtmlTagSpec* spec);
|
267 |
|
|
static void Clear();
|
268 |
|
|
|
269 |
|
|
//// Accessors
|
270 |
|
|
const wxString& GetName() const { return m_name; }
|
271 |
|
|
int GetType() const { return m_type; }
|
272 |
|
|
|
273 |
|
|
private:
|
274 |
|
|
|
275 |
|
|
wxString m_name;
|
276 |
|
|
int m_type;
|
277 |
|
|
|
278 |
|
|
static wxList* sm_tagSpecs;
|
279 |
|
|
};
|
280 |
|
|
|
281 |
|
|
/*
|
282 |
|
|
* wxSimpleHtmlModule
|
283 |
|
|
* Responsible for init/cleanup of appropriate data structures
|
284 |
|
|
*/
|
285 |
|
|
|
286 |
|
|
class wxSimpleHtmlModule : public wxModule
|
287 |
|
|
{
|
288 |
|
|
DECLARE_DYNAMIC_CLASS(wxSimpleHtmlModule)
|
289 |
|
|
|
290 |
|
|
public:
|
291 |
|
|
wxSimpleHtmlModule() {};
|
292 |
|
|
|
293 |
|
|
bool OnInit() ;
|
294 |
|
|
void OnExit() ;
|
295 |
|
|
};
|
296 |
|
|
#endif
|
297 |
|
|
|
298 |
|
|
#endif
|