1 /* This file is part of Metaproxy.
2 Copyright (C) 2005-2013 Index Data
4 Metaproxy is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
9 Metaproxy is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 #include "html_parser.hpp"
28 #define SPACECHR " \t\r\n\f"
31 namespace metaproxy_1 {
32 class HTMLParser::Rep {
33 friend class HTMLParser;
35 void parse_str(HTMLParserEvent &event, const char *cp);
36 void tagText(HTMLParserEvent &event,
37 const char *text_start, const char *text_end);
38 int tagEnd(HTMLParserEvent &event,
39 const char *tag, int tag_len, const char *cp);
40 int tagStart(HTMLParserEvent &event,
41 int *tag_len, const char *cp, const char which);
42 int tagAttrs(HTMLParserEvent &event,
43 const char *name, int len,
51 namespace mp = metaproxy_1;
53 mp::HTMLParser::Rep::Rep()
58 mp::HTMLParser::Rep::~Rep()
62 mp::HTMLParser::HTMLParser() : m_p(new Rep)
66 mp::HTMLParser::~HTMLParser()
70 void mp::HTMLParser::set_verbose(int v)
76 void mp::HTMLParser::parse(mp::HTMLParserEvent & event, const char *str) const
78 m_p->parse_str(event, str);
81 static int skipSpace(const char *cp)
84 while (cp[i] && strchr(SPACECHR, cp[i]))
89 static int skipName(const char *cp)
92 for (i = 0; cp[i] && !strchr(SPACECHR "/>=", cp[i]); i++)
97 static int skipAttribute(const char *cp, int *attr_len,
98 const char **value, int *val_len)
100 int i = skipName(cp);
104 return skipSpace(cp);
105 i += skipSpace(cp + i);
110 i += skipSpace(cp + i);
111 if (cp[i] == '\"' || cp[i] == '\'')
115 while (cp[i] != tr && cp[i])
124 while (cp[i] && !strchr(SPACECHR ">", cp[i]))
131 i += skipSpace(cp + i);
135 int mp::HTMLParser::Rep::tagAttrs(HTMLParserEvent &event,
136 const char *name, int len,
139 int i = skipSpace(cp);
140 while (cp[i] && cp[i] != '>' && cp[i] != '/')
142 const char *attr_name = cp + i;
146 int nor = skipAttribute(cp+i, &attr_len, &value, &val_len);
151 printf ("------ attr %.*s=%.*s\n", attr_len, attr_name,
153 event.attribute(name, len, attr_name, attr_len, value, val_len);
163 int mp::HTMLParser::Rep::tagStart(HTMLParserEvent &event,
165 const char *cp, const char which)
174 printf("------ tag close %.*s\n", i, cp);
175 event.closeTag(cp, i);
178 for (i = 0; cp[i] && cp[i] != '>'; i++)
181 event.openTagStart(cp, i);
183 printf("------ dtd %.*s\n", i, cp);
186 for (i = 0; cp[i] && cp[i] != '>'; i++)
189 event.openTagStart(cp, i);
191 printf("------ pi %.*s\n", i, cp);
197 printf("------ tag open %.*s\n", i, cp);
198 event.openTagStart(cp, i);
200 i += tagAttrs(event, cp, i, cp + i);
207 int mp::HTMLParser::Rep::tagEnd(HTMLParserEvent &event,
208 const char *tag, int tag_len, const char *cp)
212 while (cp[i] && cp[i] != '>')
220 event.anyTagEnd(tag, tag_len, close_it);
226 void mp::HTMLParser::Rep::tagText(HTMLParserEvent &event,
227 const char *text_start, const char *text_end)
229 if (text_end - text_start) //got text to flush
232 printf("------ text %.*s\n",
233 (int) (text_end - text_start), text_start);
234 event.text(text_start, text_end-text_start);
238 void mp::HTMLParser::Rep::parse_str(HTMLParserEvent &event, const char *cp)
240 const char *text_start = cp;
241 const char *text_end = cp;
244 if (cp[0] == '<' && cp[1]) //tag?
249 if (!strchr(SPACECHR, cp[1])) //valid tag starts
254 tagText(event, text_start, text_end); //flush any text
256 i += tagStart(event, &tag_len, cp, which);
257 i += tagEnd(event, cp, tag_len, cp + i);
268 tagText(event, text_start, text_end); //flush any text
274 * c-file-style: "Stroustrup"
275 * indent-tabs-mode: nil
277 * vim: shiftwidth=4 tabstop=8 expandtab