summaryrefslogtreecommitdiff
path: root/plugins/MirOTR/src/striphtml.cpp
diff options
context:
space:
mode:
authorRené Schümann <white06tiger@gmail.com>2015-03-14 19:58:15 +0000
committerRené Schümann <white06tiger@gmail.com>2015-03-14 19:58:15 +0000
commit5a97f4b7c941dc704ab4dd2c370d19bb556c1ce6 (patch)
tree7ca8e397891238535958645ae5af8427068bcfd9 /plugins/MirOTR/src/striphtml.cpp
parent02eaf116807ce4b8b49dfaee47910d2dffbfef8f (diff)
MirOTR: part 3, file/folder structure change
git-svn-id: http://svn.miranda-ng.org/main/trunk@12404 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c
Diffstat (limited to 'plugins/MirOTR/src/striphtml.cpp')
-rw-r--r--plugins/MirOTR/src/striphtml.cpp122
1 files changed, 122 insertions, 0 deletions
diff --git a/plugins/MirOTR/src/striphtml.cpp b/plugins/MirOTR/src/striphtml.cpp
new file mode 100644
index 0000000000..4a3c3759ff
--- /dev/null
+++ b/plugins/MirOTR/src/striphtml.cpp
@@ -0,0 +1,122 @@
+#include "stdafx.h"
+
+void starttag_cb (void *cbdata, ekhtml_string_t *tag, ekhtml_attr_t *attrs) {
+ STRIPHTML_DATA *data = (STRIPHTML_DATA *)cbdata;
+ switch (tag->len) {
+ case 1:
+ switch (tag->str[0]) {
+ case 'a':
+ case 'A':
+ for(ekhtml_attr_t *attr=attrs; attr; attr=attr->next) {
+ if (_strnicmp(attr->name.str, "href", attr->name.len)==0) {
+ data->stack.push(strncpy((char*)mir_calloc(attr->val.len+1), attr->val.str, attr->val.len));
+ break;
+ }
+ }
+ break;
+ case 'i':
+ case 'I':
+ data->buffer.append(" *");
+ break;
+ case 'b':
+ case 'B':
+ data->buffer.append(" _");
+ break;
+ }
+ break;
+ case 2:
+ if (toupper(tag->str[0]) == 'B' && toupper(tag->str[1]) == 'R')
+ data->buffer.append("\r\n");
+ break;
+ case 3:
+ if (_strnicmp(tag->str, "img", 3) == 0) {
+ data->buffer.append("IMAGE [ ");
+ for(ekhtml_attr_t *attr=attrs; attr; attr=attr->next) {
+ if (_strnicmp(attr->name.str, "src", attr->name.len)==0) {
+ data->buffer.append(attr->val.str, attr->val.len);
+ break;
+ }
+ }
+ data->buffer.append(" ] ");
+ }
+ break;
+ }
+}
+
+void endtag_cb (void *cbdata, ekhtml_string_t *tag) {
+ STRIPHTML_DATA *data = (STRIPHTML_DATA *)cbdata;
+ switch (tag->len) {
+ case 1:
+ switch (*(tag->str)) {
+ case 'a':
+ case 'A':
+ if (data->stack.empty()) break;
+ data->buffer.append(" [ ");
+ data->buffer.append(data->stack.top());
+ mir_free(data->stack.top());
+ data->stack.pop();
+ data->buffer.append(" ] ");
+ break;
+ case 'i':
+ case 'I':
+ data->buffer.append("* ");
+ break;
+ case 'b':
+ case 'B':
+ data->buffer.append("_ ");
+ break;
+ }
+ }
+
+}
+
+void data_cb (void *cbdata, ekhtml_string_t *text) {
+ STRIPHTML_DATA *data = (STRIPHTML_DATA *)cbdata;
+ if(text->len <= 0)
+ return;
+ char* s = (char*) mir_calloc(text->len+1);
+ decode_html_entities_utf8(s, text->str, text->len);
+
+ if (!data->stack.empty()) {
+ char *top = data->stack.top();
+ if (_stricmp(s, top)==0) {
+ mir_free(top);
+ data->stack.pop();
+ }
+ }
+
+
+ data->buffer.append(s);
+ mir_free(s);
+}
+
+char * striphtml(char *html) {
+ STRIPHTML_DATA data;
+ ekhtml_string_t ekstring;
+
+ ekstring.len = strlen(html);
+ ekstring.str = html;
+
+ data.buffer.clear();
+ data.buffer.reserve(ekstring.len);
+
+ ekhtml_parser_t *parser = ekhtml_parser_new(&data);
+ ekhtml_parser_datacb_set(parser, &data_cb);
+ ekhtml_parser_startcb_add(parser, NULL, &starttag_cb);
+ ekhtml_parser_endcb_add(parser, NULL, &endtag_cb);
+
+ ekhtml_parser_feed(parser, &ekstring);
+ ekhtml_parser_flush(parser, 1);
+
+ while (!data.stack.empty()) {
+ mir_free(data.stack.top());
+ data.stack.pop();
+ }
+
+ ekhtml_parser_destroy(parser);
+
+ char *s = mir_strdup(data.buffer.c_str());
+ data.buffer.erase();
+ return s;
+ //ekhtml_parser_create(
+}