From 7d0b1443916345947ee72ff07ebb56493d09598e Mon Sep 17 00:00:00 2001 From: John Mark Bell Date: Wed, 24 Mar 2004 23:51:08 +0000 Subject: [project @ 2004-03-24 23:51:08 by jmb] Text Export support. svn path=/import/netsurf/; revision=665 --- !NetSurf/Resources/en/Messages | 2 + !NetSurf/Resources/fr/Messages | 2 + makefile | 2 +- riscos/gui.h | 2 +- riscos/menus.c | 14 ++++- riscos/save.c | 7 +++ riscos/save_text.c | 130 +++++++++++++++++++++++++++++++++++++++++ riscos/save_text.h | 15 +++++ riscos/window.c | 6 ++ utils/config.h | 1 + 10 files changed, 177 insertions(+), 4 deletions(-) create mode 100644 riscos/save_text.c create mode 100644 riscos/save_text.h diff --git a/!NetSurf/Resources/en/Messages b/!NetSurf/Resources/en/Messages index 4cb9aee78..6fa0198ce 100644 --- a/!NetSurf/Resources/en/Messages +++ b/!NetSurf/Resources/en/Messages @@ -13,6 +13,7 @@ Save:Save Export:Export ExportAs:Export as Draw:Draw +Text:Text Print:Print... Selection:Selection Copy:Copy to clipboard @@ -77,3 +78,4 @@ SelectMenu:Select SaveSource:Source SaveDraw:Webpage +SaveText:Webpage diff --git a/!NetSurf/Resources/fr/Messages b/!NetSurf/Resources/fr/Messages index 275a4af23..e3f32f4d9 100644 --- a/!NetSurf/Resources/fr/Messages +++ b/!NetSurf/Resources/fr/Messages @@ -13,6 +13,7 @@ Save:Sauver Export:Exporter ExportAs:Exporter sous Draw:Draw +Text:Text Print:Imprimer... Selection:Sélection Copy:Copier vers le presse papier @@ -77,3 +78,4 @@ SelectMenu:S SaveSource:Source SaveDraw:PageWeb +SaveText:PageWeb diff --git a/makefile b/makefile index 25c6b4cff..f5e319e3a 100644 --- a/makefile +++ b/makefile @@ -19,7 +19,7 @@ OBJECTS = $(OBJECTS_COMMON) \ draw.o gif.o jpeg.o plugin.o png.o sprite.o \ about.o filetype.o font.o uri.o url_protocol.o history.o \ version.o thumbnail.o \ - save.o save_complete.o save_draw.o schedule.o + save.o save_complete.o save_draw.o save_text.o schedule.o OBJECTS_DEBUG = $(OBJECTS_COMMON) \ netsurfd.o \ options.o filetyped.o fontd.o diff --git a/riscos/gui.h b/riscos/gui.h index a0f7c6a19..24f96c21c 100644 --- a/riscos/gui.h +++ b/riscos/gui.h @@ -31,7 +31,7 @@ extern bool gui_redraw_debug; extern gui_window *current_gui; typedef enum { GUI_BROWSER_WINDOW, GUI_DOWNLOAD_WINDOW } gui_window_type; -typedef enum { GUI_SAVE_SOURCE, GUI_SAVE_DRAW } gui_save_type; +typedef enum { GUI_SAVE_SOURCE, GUI_SAVE_DRAW, GUI_SAVE_TEXT } gui_save_type; extern gui_save_type gui_current_save_type; typedef enum { GUI_DRAG_SELECTION, GUI_DRAG_DOWNLOAD_SAVE, GUI_DRAG_SAVE } gui_drag_type; diff --git a/riscos/menus.c b/riscos/menus.c index dda598b89..0cb8cb288 100644 --- a/riscos/menus.c +++ b/riscos/menus.c @@ -48,10 +48,11 @@ wimp_menu *iconbar_menu = (wimp_menu *) & (wimp_MENU(4)) { int iconbar_menu_height = 4 * 44; /* browser window menu structure - based on Style Guide */ -static wimp_MENU(1) export_menu = { +static wimp_MENU(2) export_menu = { { "ExportAs" }, 7,2,7,0, 200, 44, 0, { - { wimp_MENU_LAST | wimp_MENU_GIVE_WARNING, wimp_NO_SUB_MENU, DEFAULT_FLAGS, { "Draw" } } + { wimp_MENU_GIVE_WARNING, wimp_NO_SUB_MENU, DEFAULT_FLAGS, { "Draw" } }, + { wimp_MENU_LAST | wimp_MENU_GIVE_WARNING, wimp_NO_SUB_MENU, DEFAULT_FLAGS, { "Text" } } } }; static wimp_menu *browser_export_menu = (wimp_menu *) &export_menu; @@ -125,6 +126,7 @@ void ro_gui_menus_init(void) iconbar_menu->entries[0].sub_menu = (wimp_menu *) dialog_info; browser_page_menu->entries[1].sub_menu = (wimp_menu *) dialog_saveas; browser_export_menu->entries[0].sub_menu = (wimp_menu *) dialog_saveas; + browser_export_menu->entries[1].sub_menu = (wimp_menu *) dialog_saveas; browser_view_menu->entries[0].sub_menu = (wimp_menu *) dialog_zoom; } @@ -302,6 +304,14 @@ void ro_gui_menu_warning(wimp_message_menu_warning *warning) ICON_SAVE_PATH, messages_get("SaveDraw")); break; + case 1: /* Export as -> Text */ + gui_current_save_type = GUI_SAVE_TEXT; + ro_gui_set_icon_string(dialog_saveas, + ICON_SAVE_ICON, "file_fff"); + ro_gui_set_icon_string(dialog_saveas, + ICON_SAVE_PATH, + messages_get("SaveText")); + break; case -1: default: /* Save */ diff --git a/riscos/save.c b/riscos/save.c index 33a5351ba..c3773bc71 100644 --- a/riscos/save.c +++ b/riscos/save.c @@ -15,6 +15,7 @@ #include "oslib/wimp.h" #include "netsurf/riscos/gui.h" #include "netsurf/riscos/save_draw.h" +#include "netsurf/riscos/save_text.h" #include "netsurf/utils/log.h" #include "netsurf/utils/messages.h" #include "netsurf/utils/utils.h" @@ -124,6 +125,12 @@ void ro_gui_save_datasave_ack(wimp_message *message) return; save_as_draw(c, path); break; + + case GUI_SAVE_TEXT: + if (!c) + return; + save_as_text(c, path); + break; } wimp_create_menu(wimp_CLOSE_MENU, 0, 0); diff --git a/riscos/save_text.c b/riscos/save_text.c new file mode 100644 index 000000000..c9a3ed2b8 --- /dev/null +++ b/riscos/save_text.c @@ -0,0 +1,130 @@ +/* + * This file is part of NetSurf, http://netsurf.sourceforge.net/ + * Licensed under the GNU General Public License, + * http://www.opensource.org/licenses/gpl-license + * Copyright 2004 John M Bell + */ + +#include +#include + +#include "libxml/HTMLtree.h" + +#include "oslib/osfile.h" + +#include "netsurf/utils/config.h" +#include "netsurf/content/content.h" +#include "netsurf/riscos/save_text.h" +#include "netsurf/utils/log.h" +#include "netsurf/utils/utils.h" + +#ifdef WITH_TEXT_EXPORT + +static void extract_text(xmlDoc *doc); +static void extract_text_from_tree(xmlNode *n); + +static char *buffer = 0; +static int output_size = 0; + +void save_as_text(struct content *c, char *path) { + + htmlParserCtxtPtr toSave; + + if (c->type != CONTENT_HTML) { + return; + } + + /* allocate a buffer the same size as the source + * the output is guaranteed to be less than this + */ + buffer = xcalloc(c->source_size, sizeof(char)); + + toSave = htmlCreateMemoryParserCtxt(c->source_data, c->source_size); + htmlParseDocument(toSave); + + extract_text(toSave->myDoc); + + if (output_size > 0) { + xosfile_save_stamped(path, 0xfff, (byte*)buffer, + (byte*)buffer+output_size); + } + + xmlFreeDoc(toSave->myDoc); + htmlFreeParserCtxt(toSave); + xfree(buffer); +} + +void extract_text(xmlDoc *doc) +{ + xmlNode *html; + + /* find the html element */ + for (html = doc->children; + html!=0 && html->type != XML_ELEMENT_NODE; + html = html->next) + ; + if (html == 0 || strcmp((const char*)html->name, "html") != 0) { + return; + } + + extract_text_from_tree(html); +} + +void extract_text_from_tree(xmlNode *n) +{ + xmlNode *this; + char *text; + int len = 0; + int need_nl = 0; + + if (n->type == XML_ELEMENT_NODE) { + if (strcmp(n->name, "dl") == 0 || + strcmp(n->name, "h1") == 0 || + strcmp(n->name, "h2") == 0 || + strcmp(n->name, "h3") == 0 || + strcmp(n->name, "ol") == 0 || + strcmp(n->name, "title") == 0 || + strcmp(n->name, "ul") == 0) { + need_nl = 2; + } + else if (strcmp(n->name, "applet") == 0 || + strcmp(n->name, "br") == 0 || + strcmp(n->name, "div") == 0 || + strcmp(n->name, "dt") == 0 || + strcmp(n->name, "h4") == 0 || + strcmp(n->name, "h5") == 0 || + strcmp(n->name, "h6") == 0 || + strcmp(n->name, "li") == 0 || + strcmp(n->name, "object") == 0 || + strcmp(n->name, "p") == 0 || + strcmp(n->name, "tr") == 0) { + need_nl = 1; + } + /* do nothing, we just recurse through these nodes */ + } + else if (n->type == XML_TEXT_NODE) { + text = squash_tolat1(n->content); + len = strlen(text); + strcat(buffer, text); + output_size += len; + xfree(text); + return; + } + else { + return; + } + + /* now recurse */ + for (this = n->children; this != 0; this = this->next) { + extract_text_from_tree(this); + } + + if (need_nl) { + for (len = 0; len != need_nl; len++) { + strcat(buffer, "\n"); + output_size += 1; + } + } +} + +#endif diff --git a/riscos/save_text.h b/riscos/save_text.h new file mode 100644 index 000000000..2ed39d97f --- /dev/null +++ b/riscos/save_text.h @@ -0,0 +1,15 @@ +/* + * This file is part of NetSurf, http://netsurf.sourceforge.net/ + * Licensed under the GNU General Public License, + * http://www.opensource.org/licenses/gpl-license + * Copyright 2004 John M Bell + */ + +#ifndef _NETSURF_RISCOS_SAVE_TEXT_H_ +#define _NETSURF_RISCOS_SAVE_TEXT_H_ + +struct content; + +void save_as_text(struct content *c, char *path); + +#endif diff --git a/riscos/window.c b/riscos/window.c index a10114d6c..270fcd52b 100644 --- a/riscos/window.c +++ b/riscos/window.c @@ -24,6 +24,7 @@ #include "netsurf/riscos/options.h" #include "netsurf/riscos/save_complete.h" #include "netsurf/riscos/save_draw.h" +#include "netsurf/riscos/save_text.h" #include "netsurf/riscos/theme.h" #include "netsurf/riscos/thumbnail.h" #include "netsurf/utils/log.h" @@ -785,6 +786,11 @@ bool ro_gui_window_keypress(gui_window *g, int key, bool toolbar) #endif ); return true; +#ifdef WITH_TEXT_EXPORT + case wimp_KEY_CONTROL + wimp_KEY_F3: +/* save_as_text(g->data.browser.bw->current_content);*/ + return true; +#endif #ifdef WITH_SAVE_COMPLETE case wimp_KEY_SHIFT + wimp_KEY_F3: save_complete(g->data.browser.bw->current_content); diff --git a/utils/config.h b/utils/config.h index 3520df52b..1211b606f 100644 --- a/utils/config.h +++ b/utils/config.h @@ -48,6 +48,7 @@ /* Export modules */ #define WITH_SAVE_COMPLETE #define WITH_DRAW_EXPORT +#define WITH_TEXT_EXPORT #endif -- cgit v1.2.3