2
* export.c - this file is part of Geany, a fast and lightweight IDE
4
* Copyright 2007-2008 Enrico Tröger <enrico(dot)troeger(at)uvena(dot)de>
5
* Copyright 2007-2008 Nick Treleaven <nick(dot)treleaven(at)btinternet(dot)com>
7
* This program is free software; you can redistribute it and/or modify
8
* it under the terms of the GNU General Public License as published by
9
* the Free Software Foundation; either version 2 of the License, or
10
* (at your option) any later version.
12
* This program is distributed in the hope that it will be useful,
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
* GNU General Public License for more details.
17
* You should have received a copy of the GNU General Public License
18
* along with this program; if not, write to the Free Software
19
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
22
* $Id: export.c 2430 2008-03-31 13:09:56Z ntrel $
32
#include "plugindata.h"
37
#include "pluginmacros.h"
40
PluginFields *plugin_fields;
41
GeanyData *geany_data;
43
PLUGIN_VERSION_CHECK(20)
44
PLUGIN_INFO(_("Export"), _("Exports the current file into different formats."), VERSION,
45
_("The Geany developer team"))
47
#define ROTATE_RGB(color) \
48
(((color) & 0xFF0000) >> 16) + ((color) & 0x00FF00) + (((color) & 0x0000FF) << 16)
49
#define TEMPLATE_HTML "\
50
<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"\n\
51
\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n\
52
<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\" lang=\"en\">\n\
55
<title>{export_filename}</title>\n\
56
<meta http-equiv=\"content-type\" content=\"text/html;charset=utf-8\" />\n\
57
<meta name=\"generator\" content=\"Geany " VERSION "\" />\n\
58
<meta name=\"date\" content=\"{export_date}\">\n\
59
<style type=\"text/css\">\n\
71
#define TEMPLATE_LATEX "\
72
% {export_filename} (LaTeX code generated by Geany " VERSION " on {export_date})\n\
73
\\documentclass[a4paper]{article}\n\
74
\\usepackage[a4paper,margin=2cm]{geometry}\n\
75
\\usepackage[utf8x]{inputenc}\n\
76
\\usepackage[T1]{fontenc}\n\
77
\\usepackage{color}\n\
78
\\setlength{\\parindent}{0em}\n\
79
\\setlength{\\parskip}{2ex plus1ex minus0.5ex}\n\
84
\\setlength{\\fboxrule}{0pt}\n\
85
\\setlength{\\fboxsep}{0pt}\n\
106
typedef void (*ExportFunc) (gint idx, const gchar *filename, gboolean use_zoom);
110
gboolean have_zoom_level_checkbox;
111
ExportFunc export_func;
114
static void on_file_save_dialog_response(GtkDialog *dialog, gint response, gpointer user_data);
115
static void write_html_file(gint idx, const gchar *filename, gboolean use_zoom);
116
static void write_latex_file(gint idx, const gchar *filename, gboolean use_zoom);
119
/* converts a RGB colour into a LaTeX compatible representation, taken from SciTE */
120
static gchar* get_tex_rgb(gint rgb_colour)
122
/* texcolor[rgb]{0,0.5,0}{....} */
123
gdouble rf = (rgb_colour % 256) / 256.0;
124
gdouble gf = ((rgb_colour & - 16711936) / 256) / 256.0;
125
gdouble bf = ((rgb_colour & 0xff0000) / 65536) / 256.0;
126
gint r = (gint) (rf * 10 + 0.5);
127
gint g = (gint) (gf * 10 + 0.5);
128
gint b = (gint) (bf * 10 + 0.5);
130
return g_strdup_printf("%d.%d, %d.%d, %d.%d", r / 10, r % 10, g / 10, g % 10, b / 10, b % 10);
134
/* convert a style number (0..127) into a string representation (aa, ab, .., ba, bb, .., zy, zz) */
135
static gchar *get_tex_style(gint style)
142
buf[i] = (style % 26) + 'a';
152
static void create_file_save_as_dialog(const gchar *extension, ExportFunc func,
153
gboolean show_zoom_level_checkbox)
157
GtkTooltips *tooltips;
160
if (extension == NULL)
163
idx = p_document->get_cur_idx();
164
tooltips = GTK_TOOLTIPS(p_support->lookup_widget(app->window, "tooltips"));
166
exi = g_new(ExportInfo, 1);
168
exi->export_func = func;
169
exi->have_zoom_level_checkbox = FALSE;
171
dialog = gtk_file_chooser_dialog_new(_("Export File"), GTK_WINDOW(app->window),
172
GTK_FILE_CHOOSER_ACTION_SAVE, NULL, NULL);
173
gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
174
gtk_window_set_destroy_with_parent(GTK_WINDOW(dialog), TRUE);
175
gtk_window_set_skip_taskbar_hint(GTK_WINDOW(dialog), TRUE);
176
gtk_window_set_type_hint(GTK_WINDOW(dialog), GDK_WINDOW_TYPE_HINT_DIALOG);
177
gtk_widget_set_name(dialog, "GeanyExportDialog");
179
gtk_dialog_add_buttons(GTK_DIALOG(dialog),
180
GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT, NULL);
181
gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT);
183
if (show_zoom_level_checkbox)
185
GtkWidget *vbox, *check_zoom_level;
187
vbox = gtk_vbox_new(FALSE, 0);
188
check_zoom_level = gtk_check_button_new_with_mnemonic(_("_Use current zoom level"));
189
gtk_tooltips_set_tip(tooltips, check_zoom_level,
190
_("Renders the font size of the document together with the current zoom level."), NULL);
191
gtk_box_pack_start(GTK_BOX(vbox), check_zoom_level, FALSE, FALSE, 0);
192
gtk_widget_show_all(vbox);
193
gtk_file_chooser_set_extra_widget(GTK_FILE_CHOOSER(dialog), vbox);
195
g_object_set_data_full(G_OBJECT(dialog), "check_zoom_level",
196
gtk_widget_ref(check_zoom_level), (GDestroyNotify) gtk_widget_unref);
198
exi->have_zoom_level_checkbox = TRUE;
201
g_signal_connect((gpointer) dialog, "delete_event",
202
G_CALLBACK(gtk_widget_hide_on_delete), NULL);
203
g_signal_connect((gpointer) dialog, "response",
204
G_CALLBACK(on_file_save_dialog_response), exi);
206
gtk_window_set_transient_for(GTK_WINDOW(dialog), GTK_WINDOW(app->window));
208
/* if the current document has a filename we use it as the default. */
209
gtk_file_chooser_unselect_all(GTK_FILE_CHOOSER(dialog));
210
if (doc_list[idx].file_name != NULL)
212
gchar *base_name = g_path_get_basename(doc_list[idx].file_name);
213
gchar *short_name = p_utils->remove_ext_from_filename(base_name);
215
gchar *locale_filename;
216
gchar *locale_dirname;
219
if (g_str_has_suffix(doc_list[idx].file_name, extension))
222
file_name = g_strconcat(short_name, suffix, extension, NULL);
223
locale_filename = p_utils->get_locale_from_utf8(doc_list[idx].file_name);
224
locale_dirname = g_path_get_dirname(locale_filename);
225
/* set the current name to base_name.html which probably doesn't exist yet so
226
* gtk_file_chooser_set_filename() can't be used and we need
227
* gtk_file_chooser_set_current_folder() additionally */
228
gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog), locale_dirname);
229
gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(dialog), file_name);
230
g_free(locale_filename);
237
const gchar *default_open_path = prefs->default_open_path;
238
gchar *fname = g_strconcat(GEANY_STRING_UNTITLED, extension, NULL);
240
gtk_file_chooser_unselect_all(GTK_FILE_CHOOSER(dialog));
241
gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(dialog), fname);
243
/* use default startup directory(if set) if no files are open */
244
if (NZV(default_open_path) && g_path_is_absolute(default_open_path))
246
gchar *locale_path = p_utils->get_locale_from_utf8(default_open_path);
247
gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog), locale_path);
252
gtk_dialog_run(GTK_DIALOG(dialog));
256
static void on_menu_create_latex_activate(GtkMenuItem *menuitem, gpointer user_data)
258
create_file_save_as_dialog(".tex", write_latex_file, FALSE);
262
static void on_menu_create_html_activate(GtkMenuItem *menuitem, gpointer user_data)
264
create_file_save_as_dialog(".html", write_html_file, TRUE);
268
static void write_data(const gchar *filename, const gchar *data)
270
gint error_nr = p_utils->write_file(filename, data);
271
gchar *utf8_filename = p_utils->get_utf8_from_locale(filename);
274
p_ui->set_statusbar(TRUE, _("Document successfully exported as '%s'."), utf8_filename);
276
p_ui->set_statusbar(TRUE, _("File '%s' could not be written (%s)."),
277
utf8_filename, g_strerror(error_nr));
279
g_free(utf8_filename);
283
static const gchar *get_date(gint type)
285
static gchar str[128];
295
if (type == DATE_TYPE_HTML)
298
format = "%Y-%m-%dT%H:%M:%S%z";
300
format = "%Y-%m-%dT%H:%M:%S";
305
strftime(str, sizeof str, format, tmp);
311
static void on_file_save_dialog_response(GtkDialog *dialog, gint response, gpointer user_data)
313
ExportInfo *exi = user_data;
315
if (response == GTK_RESPONSE_ACCEPT && exi != NULL)
317
gchar *new_filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
318
gchar *utf8_filename;
319
gboolean use_zoom_level = FALSE;
321
if (exi->have_zoom_level_checkbox)
323
use_zoom_level = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(
324
p_support->lookup_widget(GTK_WIDGET(dialog), "check_zoom_level")));
327
utf8_filename = p_utils->get_utf8_from_locale(new_filename);
329
/* check if file exists and ask whether to overwrite or not */
330
if (g_file_test(new_filename, G_FILE_TEST_EXISTS))
332
if (p_dialogs->show_question(
333
_("The file '%s' already exists. Do you want to overwrite it?"),
334
utf8_filename) == FALSE)
338
exi->export_func(exi->idx, new_filename, use_zoom_level);
340
g_free(utf8_filename);
341
g_free(new_filename);
344
gtk_widget_destroy(GTK_WIDGET(dialog));
348
static void write_latex_file(gint idx, const gchar *filename, gboolean use_zoom)
350
gint i, style = -1, old_style = 0, column = 0;
351
gchar c, c_next, *tmp;
352
/* 0 - fore, 1 - back, 2 - bold, 3 - italic, 4 - font size, 5 - used(0/1) */
353
gint styles[STYLE_MAX + 1][MAX_TYPES];
354
gboolean block_open = FALSE;
358
gint style_max = pow(2, p_sci->send_message(doc_list[idx].sci, SCI_GETSTYLEBITS, 0, 0));
360
/* first read all styles from Scintilla */
361
for (i = 0; i < style_max; i++)
363
styles[i][FORE] = p_sci->send_message(doc_list[idx].sci, SCI_STYLEGETFORE, i, 0);
364
styles[i][BACK] = p_sci->send_message(doc_list[idx].sci, SCI_STYLEGETBACK, i, 0);
365
styles[i][BOLD] = p_sci->send_message(doc_list[idx].sci, SCI_STYLEGETBOLD, i, 0);
366
styles[i][ITALIC] = p_sci->send_message(doc_list[idx].sci, SCI_STYLEGETITALIC, i, 0);
370
/* read the document and write the LaTeX code */
371
body = g_string_new("");
372
for (i = 0; i < p_sci->get_length(doc_list[idx].sci); i++)
374
style = p_sci->get_style_at(doc_list[idx].sci, i);
375
c = p_sci->get_char_at(doc_list[idx].sci, i);
376
c_next = p_sci->get_char_at(doc_list[idx].sci, i + 1);
378
if (style != old_style || ! block_open)
381
styles[style][USED] = 1;
384
g_string_append(body, "}\n");
387
g_string_append_printf(body, "\\style%s{", get_tex_style(style));
391
/* escape the current character if necessary else just add it */
397
if (c == '\r' && c_next == '\n')
398
continue; /* when using CR/LF skip CR and add the line break with LF */
402
g_string_append(body, "}");
405
g_string_append(body, " \\\\\n");
411
gint tab_stop = geany_data->editor_prefs->tab_width -
412
(column % geany_data->editor_prefs->tab_width);
414
column += tab_stop - 1; /* -1 because we add 1 at the end of the loop */
415
g_string_append_printf(body, "\\hspace*{%dem}", tab_stop);
422
g_string_append(body, "{\\hspace*{1em}}");
423
i++; /* skip the next character */
426
g_string_append_c(body, ' ');
437
g_string_append_printf(body, "\\%c", c);
442
g_string_append(body, "\\symbol{92}");
447
g_string_append(body, "\\symbol{126}");
452
g_string_append(body, "\\symbol{94}");
455
/** TODO still don't work for "---" or "----" */
456
case '-': /* mask "--" */
460
g_string_append(body, "-\\/-");
461
i++; /* skip the next character */
464
g_string_append_c(body, '-');
468
case '<': /* mask "<<" */
472
g_string_append(body, "<\\/<");
473
i++; /* skip the next character */
476
g_string_append_c(body, '<');
480
case '>': /* mask ">>" */
484
g_string_append(body, ">\\/>");
485
i++; /* skip the next character */
488
g_string_append_c(body, '>');
492
default: g_string_append_c(body, c);
498
g_string_append(body, "}\n");
502
/* force writing of style 0 (used at least for line breaks) */
505
/* write used styles in the header */
506
cmds = g_string_new("");
507
for (i = 0; i <= STYLE_MAX; i++)
511
g_string_append_printf(cmds,
512
"\\newcommand{\\style%s}[1]{\\noindent{", get_tex_style(i));
514
g_string_append(cmds, "\\textbf{");
515
if (styles[i][ITALIC])
516
g_string_append(cmds, "\\textit{");
518
tmp = get_tex_rgb(styles[i][FORE]);
519
g_string_append_printf(cmds, "\\textcolor[rgb]{%s}{", tmp);
521
tmp = get_tex_rgb(styles[i][BACK]);
522
g_string_append_printf(cmds, "\\fcolorbox[rgb]{0, 0, 0}{%s}{", tmp);
523
g_string_append(cmds, "#1}}");
527
g_string_append_c(cmds, '}');
528
if (styles[i][ITALIC])
529
g_string_append_c(cmds, '}');
530
g_string_append(cmds, "}}\n");
535
latex = g_string_new(TEMPLATE_LATEX);
536
p_utils->string_replace_all(latex, "{export_content}", body->str);
537
p_utils->string_replace_all(latex, "{export_styles}", cmds->str);
538
p_utils->string_replace_all(latex, "{export_date}", get_date(DATE_TYPE_DEFAULT));
539
if (doc_list[idx].file_name == NULL)
540
p_utils->string_replace_all(latex, "{export_filename}", GEANY_STRING_UNTITLED);
542
p_utils->string_replace_all(latex, "{export_filename}", doc_list[idx].file_name);
544
write_data(filename, latex->str);
546
g_string_free(body, TRUE);
547
g_string_free(cmds, TRUE);
548
g_string_free(latex, TRUE);
552
static void write_html_file(gint idx, const gchar *filename, gboolean use_zoom)
554
gint i, style = -1, old_style = 0, column = 0;
556
/* 0 - fore, 1 - back, 2 - bold, 3 - italic, 4 - font size, 5 - used(0/1) */
557
gint styles[STYLE_MAX + 1][MAX_TYPES];
558
gboolean span_open = FALSE;
559
const gchar *font_name;
561
PangoFontDescription *font_desc;
565
gint style_max = pow(2, p_sci->send_message(doc_list[idx].sci, SCI_GETSTYLEBITS, 0, 0));
567
/* first read all styles from Scintilla */
568
for (i = 0; i < style_max; i++)
570
styles[i][FORE] = ROTATE_RGB(p_sci->send_message(doc_list[idx].sci, SCI_STYLEGETFORE, i, 0));
571
styles[i][BACK] = ROTATE_RGB(p_sci->send_message(doc_list[idx].sci, SCI_STYLEGETBACK, i, 0));
572
styles[i][BOLD] = p_sci->send_message(doc_list[idx].sci, SCI_STYLEGETBOLD, i, 0);
573
styles[i][ITALIC] = p_sci->send_message(doc_list[idx].sci, SCI_STYLEGETITALIC, i, 0);
577
/* read Geany's font and font size */
578
font_desc = pango_font_description_from_string(prefs->editor_font);
579
font_name = pango_font_description_get_family(font_desc);
580
/*font_size = pango_font_description_get_size(font_desc) / PANGO_SCALE;*/
581
/* take the zoom level also into account */
582
font_size = p_sci->send_message(doc_list[idx].sci, SCI_STYLEGETSIZE, 0, 0);
584
font_size += p_sci->send_message(doc_list[idx].sci, SCI_GETZOOM, 0, 0);
586
/* read the document and write the HTML body */
587
body = g_string_new("");
588
for (i = 0; i < p_sci->get_length(doc_list[idx].sci); i++)
590
style = p_sci->get_style_at(doc_list[idx].sci, i);
591
c = p_sci->get_char_at(doc_list[idx].sci, i);
592
/* p_sci->get_char_at() takes care of index boundaries and return 0 if i is too high */
593
c_next = p_sci->get_char_at(doc_list[idx].sci, i + 1);
595
if ((style != old_style || ! span_open) && ! isspace(c))
598
styles[style][USED] = 1;
601
g_string_append(body, "</span>");
603
g_string_append_printf(body, "<span class=\"style_%d\">", style);
607
/* escape the current character if necessary else just add it */
613
if (c == '\r' && c_next == '\n')
614
continue; /* when using CR/LF skip CR and add the line break with LF */
618
g_string_append(body, "</span>");
621
g_string_append(body, "<br />\n");
628
gint tab_stop = geany_data->editor_prefs->tab_width -
629
(column % geany_data->editor_prefs->tab_width);
631
column += tab_stop - 1; /* -1 because we add 1 at the end of the loop */
632
for (j = 0; j < tab_stop; j++)
634
g_string_append(body, " ");
640
g_string_append(body, " ");
645
g_string_append(body, "<");
650
g_string_append(body, ">");
655
g_string_append(body, "&");
658
default: g_string_append_c(body, c);
664
g_string_append(body, "</span>");
668
/* write used styles in the header */
669
css = g_string_new("");
670
g_string_append_printf(css,
671
"\tbody\n\t{\n\t\tfont-family: %s, monospace;\n\t\tfont-size: %dpt;\n\t}\n",
672
font_name, font_size);
674
for (i = 0; i <= STYLE_MAX; i++)
678
g_string_append_printf(css,
679
"\t.style_%d\n\t{\n\t\tcolor: #%06x;\n\t\tbackground-color: #%06x;\n%s%s\t}\n",
680
i, styles[i][FORE], styles[i][BACK],
681
(styles[i][BOLD]) ? "\t\tfont-weight: bold;\n" : "",
682
(styles[i][ITALIC]) ? "\t\tfont-style: italic;\n" : "");
687
html = g_string_new(TEMPLATE_HTML);
688
p_utils->string_replace_all(html, "{export_date}", get_date(DATE_TYPE_HTML));
689
p_utils->string_replace_all(html, "{export_content}", body->str);
690
p_utils->string_replace_all(html, "{export_styles}", css->str);
691
if (doc_list[idx].file_name == NULL)
692
p_utils->string_replace_all(html, "{export_filename}", GEANY_STRING_UNTITLED);
694
p_utils->string_replace_all(html, "{export_filename}", doc_list[idx].file_name);
696
write_data(filename, html->str);
698
pango_font_description_free(font_desc);
699
g_string_free(body, TRUE);
700
g_string_free(css, TRUE);
701
g_string_free(html, TRUE);
705
void init(GeanyData *data)
707
GtkWidget *menu_export;
708
GtkWidget *menu_export_menu;
709
GtkWidget *menu_create_html;
710
GtkWidget *menu_create_latex;
712
menu_export = gtk_image_menu_item_new_with_mnemonic(_("_Export"));
713
gtk_container_add(GTK_CONTAINER(data->tools_menu), menu_export);
715
menu_export_menu = gtk_menu_new ();
716
gtk_menu_item_set_submenu(GTK_MENU_ITEM(menu_export), menu_export_menu);
719
menu_create_html = gtk_menu_item_new_with_mnemonic(_("As _HTML"));
720
gtk_container_add(GTK_CONTAINER (menu_export_menu), menu_create_html);
722
g_signal_connect((gpointer) menu_create_html, "activate",
723
G_CALLBACK(on_menu_create_html_activate), NULL);
726
menu_create_latex = gtk_menu_item_new_with_mnemonic(_("As _LaTeX"));
727
gtk_container_add(GTK_CONTAINER (menu_export_menu), menu_create_latex);
729
g_signal_connect((gpointer) menu_create_latex, "activate",
730
G_CALLBACK(on_menu_create_latex_activate), NULL);
732
/* disable menu_item when there are no documents open */
733
plugin_fields->menu_item = menu_export;
734
plugin_fields->flags = PLUGIN_IS_DOCUMENT_SENSITIVE;
736
gtk_widget_show_all(menu_export);
742
gtk_widget_destroy(plugin_fields->menu_item);