Remove redundant GEANY_EXPORT_SYMBOL usage in callbacks.h
[geany-mirror.git] / plugins / export.c
blob2d01d882b79e8a1fd74e74c48d915b0907ca36f3
1 /*
2 * export.c - this file is part of Geany, a fast and lightweight IDE
4 * Copyright 2007-2012 Enrico Tröger <enrico(dot)troeger(at)uvena(dot)de>
5 * Copyright 2007-2012 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 along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 /* Export plugin. */
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
28 #include <ctype.h>
29 #include <math.h>
31 #include "geanyplugin.h"
34 GeanyData *geany_data;
35 GeanyFunctions *geany_functions;
37 PLUGIN_VERSION_CHECK(GEANY_API_VERSION)
38 PLUGIN_SET_INFO(_("Export"), _("Exports the current file into different formats."), VERSION,
39 _("The Geany developer team"))
42 static GtkWidget *main_menu_item = NULL;
45 #define ROTATE_RGB(color) \
46 (((color) & 0xFF0000) >> 16) + ((color) & 0x00FF00) + (((color) & 0x0000FF) << 16)
47 #define TEMPLATE_HTML "\
48 <!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"\n\
49 \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n\
50 <html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\" lang=\"en\">\n\
51 \n\
52 <head>\n\
53 <title>{export_filename}</title>\n\
54 <meta http-equiv=\"content-type\" content=\"text/html;charset=utf-8\" />\n\
55 <meta name=\"generator\" content=\"Geany " VERSION "\" />\n\
56 <meta name=\"date\" content=\"{export_date}\" />\n\
57 <style type=\"text/css\">\n\
58 {export_styles}\n\
59 </style>\n\
60 </head>\n\
61 \n\
62 <body>\n\
63 <p>\n\
64 {export_content}\n\
65 </p>\n\
66 </body>\n\
67 </html>\n"
69 #define TEMPLATE_LATEX "\
70 % {export_filename} (LaTeX code generated by Geany " VERSION " on {export_date})\n\
71 \\documentclass[a4paper]{article}\n\
72 \\usepackage[a4paper,margin=2cm]{geometry}\n\
73 \\usepackage[utf8]{inputenc}\n\
74 \\usepackage[T1]{fontenc}\n\
75 \\usepackage{color}\n\
76 \\setlength{\\parindent}{0em}\n\
77 \\setlength{\\parskip}{2ex plus1ex minus0.5ex}\n\
78 {export_styles}\n\
79 \\begin{document}\
80 \n\
81 \\ttfamily\n\
82 \\setlength{\\fboxrule}{0pt}\n\
83 \\setlength{\\fboxsep}{0pt}\n\
84 {export_content}\
85 \\end{document}\n"
88 enum
90 FORE = 0,
91 BACK,
92 BOLD,
93 ITALIC,
94 USED,
95 MAX_TYPES
98 enum
100 DATE_TYPE_DEFAULT,
101 DATE_TYPE_HTML
104 typedef void (*ExportFunc) (GeanyDocument *doc, const gchar *filename,
105 gboolean use_zoom, gboolean insert_line_numbers);
106 typedef struct
108 GeanyDocument *doc;
109 gboolean have_zoom_level_checkbox;
110 ExportFunc export_func;
111 } ExportInfo;
113 static void on_file_save_dialog_response(GtkDialog *dialog, gint response, gpointer user_data);
114 static void write_html_file(GeanyDocument *doc, const gchar *filename,
115 gboolean use_zoom, gboolean insert_line_numbers);
116 static void write_latex_file(GeanyDocument *doc, const gchar *filename,
117 gboolean use_zoom, gboolean insert_line_numbers);
120 /* converts a RGB colour into a LaTeX compatible representation, taken from SciTE */
121 static gchar* get_tex_rgb(gint rgb_colour)
123 /* texcolor[rgb]{0,0.5,0}{....} */
124 gdouble rf = (rgb_colour % 256) / 256.0;
125 gdouble gf = ((rgb_colour & - 16711936) / 256) / 256.0;
126 gdouble bf = ((rgb_colour & 0xff0000) / 65536) / 256.0;
127 gint r = (gint) (rf * 10 + 0.5);
128 gint g = (gint) (gf * 10 + 0.5);
129 gint b = (gint) (bf * 10 + 0.5);
131 return g_strdup_printf("%d.%d, %d.%d, %d.%d", r / 10, r % 10, g / 10, g % 10, b / 10, b % 10);
135 /* convert a style number (0..127) into a string representation (aa, ab, .., ba, bb, .., zy, zz) */
136 static gchar *get_tex_style(gint style)
138 static gchar buf[4];
139 int i = 0;
143 buf[i] = (style % 26) + 'a';
144 style /= 26;
145 i++;
146 } while (style > 0);
147 buf[i] = '\0';
149 return buf;
153 static void create_file_save_as_dialog(const gchar *extension, ExportFunc func,
154 gboolean show_zoom_level_checkbox)
156 GtkWidget *dialog, *vbox;
157 GeanyDocument *doc;
158 ExportInfo *exi;
160 g_return_if_fail(extension != NULL);
162 doc = document_get_current();
163 g_return_if_fail(doc != NULL);
165 exi = g_new(ExportInfo, 1);
166 exi->doc = doc;
167 exi->export_func = func;
168 exi->have_zoom_level_checkbox = FALSE;
170 dialog = gtk_file_chooser_dialog_new(_("Export File"), GTK_WINDOW(geany->main_widgets->window),
171 GTK_FILE_CHOOSER_ACTION_SAVE, NULL, NULL);
172 gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
173 gtk_window_set_destroy_with_parent(GTK_WINDOW(dialog), TRUE);
174 gtk_window_set_skip_taskbar_hint(GTK_WINDOW(dialog), TRUE);
175 gtk_window_set_type_hint(GTK_WINDOW(dialog), GDK_WINDOW_TYPE_HINT_DIALOG);
176 gtk_widget_set_name(dialog, "GeanyExportDialog");
178 gtk_dialog_add_buttons(GTK_DIALOG(dialog),
179 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT, NULL);
180 gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT);
182 /* file chooser extra widget */
183 vbox = gtk_vbox_new(FALSE, 0);
184 gtk_file_chooser_set_extra_widget(GTK_FILE_CHOOSER(dialog), vbox);
186 GtkWidget *check_line_numbers;
188 check_line_numbers = gtk_check_button_new_with_mnemonic(_("_Insert line numbers"));
189 gtk_widget_set_tooltip_text(check_line_numbers,
190 _("Insert line numbers before each line in the exported document"));
191 gtk_box_pack_start(GTK_BOX(vbox), check_line_numbers, FALSE, FALSE, 0);
192 gtk_widget_show_all(vbox);
194 ui_hookup_widget(dialog, check_line_numbers, "check_line_numbers");
196 if (show_zoom_level_checkbox)
198 GtkWidget *check_zoom_level;
200 check_zoom_level = gtk_check_button_new_with_mnemonic(_("_Use current zoom level"));
201 gtk_widget_set_tooltip_text(check_zoom_level,
202 _("Renders the font size of the document together with the current zoom level"));
203 gtk_box_pack_start(GTK_BOX(vbox), check_zoom_level, FALSE, FALSE, 0);
204 gtk_widget_show_all(vbox);
206 ui_hookup_widget(dialog, check_zoom_level, "check_zoom_level");
207 exi->have_zoom_level_checkbox = TRUE;
210 g_signal_connect(dialog, "delete-event", G_CALLBACK(gtk_widget_hide_on_delete), NULL);
211 g_signal_connect(dialog, "response", G_CALLBACK(on_file_save_dialog_response), exi);
213 gtk_window_set_transient_for(GTK_WINDOW(dialog), GTK_WINDOW(geany->main_widgets->window));
215 /* if the current document has a filename we use it as the default. */
216 gtk_file_chooser_unselect_all(GTK_FILE_CHOOSER(dialog));
217 if (doc->file_name != NULL)
219 gchar *base_name = g_path_get_basename(doc->file_name);
220 gchar *file_name;
221 gchar *locale_filename;
222 gchar *locale_dirname;
223 const gchar *suffix = "";
225 if (g_str_has_suffix(doc->file_name, extension))
226 suffix = "_export";
228 file_name = g_strconcat(base_name, suffix, extension, NULL);
229 locale_filename = utils_get_locale_from_utf8(doc->file_name);
230 locale_dirname = g_path_get_dirname(locale_filename);
231 /* set the current name to base_name.html which probably doesn't exist yet so
232 * gtk_file_chooser_set_filename() can't be used and we need
233 * gtk_file_chooser_set_current_folder() additionally */
234 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog), locale_dirname);
235 gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(dialog), file_name);
236 g_free(locale_dirname);
237 g_free(locale_filename);
238 g_free(file_name);
239 g_free(base_name);
241 else
243 const gchar *default_open_path = geany->prefs->default_open_path;
244 gchar *fname = g_strconcat(GEANY_STRING_UNTITLED, extension, NULL);
246 gtk_file_chooser_unselect_all(GTK_FILE_CHOOSER(dialog));
247 gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(dialog), fname);
249 /* use default startup directory(if set) if no files are open */
250 if (!EMPTY(default_open_path) && g_path_is_absolute(default_open_path))
252 gchar *locale_path = utils_get_locale_from_utf8(default_open_path);
253 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog), locale_path);
254 g_free(locale_path);
256 g_free(fname);
258 gtk_dialog_run(GTK_DIALOG(dialog));
262 static void on_menu_create_latex_activate(GtkMenuItem *menuitem, gpointer user_data)
264 create_file_save_as_dialog(".tex", write_latex_file, FALSE);
268 static void on_menu_create_html_activate(GtkMenuItem *menuitem, gpointer user_data)
270 create_file_save_as_dialog(".html", write_html_file, TRUE);
274 static void write_data(const gchar *filename, const gchar *data)
276 gint error_nr = utils_write_file(filename, data);
277 gchar *utf8_filename = utils_get_utf8_from_locale(filename);
279 if (error_nr == 0)
280 ui_set_statusbar(TRUE, _("Document successfully exported as '%s'."), utf8_filename);
281 else
282 ui_set_statusbar(TRUE, _("File '%s' could not be written (%s)."),
283 utf8_filename, g_strerror(error_nr));
285 g_free(utf8_filename);
289 static gchar *get_date(gint type)
291 const gchar *format;
293 if (type == DATE_TYPE_HTML)
294 /* needs testing */
295 #ifdef _GNU_SOURCE
296 format = "%Y-%m-%dT%H:%M:%S%z";
297 #else
298 format = "%Y-%m-%dT%H:%M:%S";
299 #endif
300 else
301 format = "%c";
303 return utils_get_date_time(format, NULL);
307 static void on_file_save_dialog_response(GtkDialog *dialog, gint response, gpointer user_data)
309 ExportInfo *exi = user_data;
311 if (response == GTK_RESPONSE_ACCEPT && exi != NULL)
313 gchar *new_filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
314 gchar *utf8_filename;
315 gboolean insert_line_numbers;
316 gboolean use_zoom_level = FALSE;
318 if (exi->have_zoom_level_checkbox)
320 use_zoom_level = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(
321 ui_lookup_widget(GTK_WIDGET(dialog), "check_zoom_level")));
323 insert_line_numbers = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(
324 ui_lookup_widget(GTK_WIDGET(dialog), "check_line_numbers")));
326 utf8_filename = utils_get_utf8_from_locale(new_filename);
328 /* check if file exists and ask whether to overwrite or not */
329 if (g_file_test(new_filename, G_FILE_TEST_EXISTS))
331 if (dialogs_show_question(
332 _("The file '%s' already exists. Do you want to overwrite it?"),
333 utf8_filename) == FALSE)
334 return;
337 exi->export_func(exi->doc, new_filename, use_zoom_level, insert_line_numbers);
339 g_free(utf8_filename);
340 g_free(new_filename);
342 g_free(exi);
343 gtk_widget_destroy(GTK_WIDGET(dialog));
347 /* returns the "width" (count of needed characters) for the given number */
348 static gint get_line_numbers_arity(gint line_number)
350 gint a = 0;
351 while ((line_number /= 10) != 0)
352 a++;
353 return a;
357 static gint get_line_number_width(GeanyDocument *doc)
359 gint line_count = sci_get_line_count(doc->editor->sci);
360 return get_line_numbers_arity(line_count);
364 static void write_latex_file(GeanyDocument *doc, const gchar *filename,
365 gboolean use_zoom, gboolean insert_line_numbers)
367 GeanyEditor *editor = doc->editor;
368 ScintillaObject *sci = doc->editor->sci;
369 gint i, doc_len, style = -1, old_style = 0, column = 0;
370 gint k, line_number, line_number_width, line_number_max_width = 0, pad;
371 gchar c, c_next, *tmp, *date;
372 /* 0 - fore, 1 - back, 2 - bold, 3 - italic, 4 - font size, 5 - used(0/1) */
373 gint styles[STYLE_MAX + 1][MAX_TYPES];
374 gboolean block_open = FALSE;
375 GString *body;
376 GString *cmds;
377 GString *latex;
378 gint style_max = pow(2, scintilla_send_message(sci, SCI_GETSTYLEBITS, 0, 0));
380 /* first read all styles from Scintilla */
381 for (i = 0; i < style_max; i++)
383 styles[i][FORE] = scintilla_send_message(sci, SCI_STYLEGETFORE, i, 0);
384 styles[i][BACK] = scintilla_send_message(sci, SCI_STYLEGETBACK, i, 0);
385 styles[i][BOLD] = scintilla_send_message(sci, SCI_STYLEGETBOLD, i, 0);
386 styles[i][ITALIC] = scintilla_send_message(sci, SCI_STYLEGETITALIC, i, 0);
387 styles[i][USED] = 0;
390 if (insert_line_numbers)
391 line_number_max_width = get_line_number_width(doc);
393 /* read the document and write the LaTeX code */
394 body = g_string_new("");
395 doc_len = sci_get_length(sci);
396 for (i = 0; i < doc_len; i++)
398 style = sci_get_style_at(sci, i);
399 c = sci_get_char_at(sci, i);
400 c_next = sci_get_char_at(sci, i + 1);
402 /* line numbers */
403 if (insert_line_numbers && column == 0)
405 line_number = sci_get_line_from_position(sci, i) + 1;
406 line_number_width = get_line_numbers_arity(line_number);
407 /* padding */
408 pad = line_number_max_width - line_number_width;
409 for (k = 0; k < pad; k++)
411 g_string_append(body, " ");
413 g_string_append_printf(body, "%d ", line_number);
416 if (style != old_style || ! block_open)
418 old_style = style;
419 styles[style][USED] = 1;
420 if (block_open)
422 g_string_append(body, "}\n");
423 block_open = FALSE;
425 if (i < doc_len)
427 g_string_append_printf(body, "\\style%s{", get_tex_style(style));
428 block_open = TRUE;
431 /* escape the current character if necessary else just add it */
432 switch (c)
434 case '\r':
435 case '\n':
437 if (c == '\r' && c_next == '\n')
438 continue; /* when using CR/LF skip CR and add the line break with LF */
440 if (block_open)
442 g_string_append(body, "}");
443 block_open = FALSE;
445 g_string_append(body, " \\\\\n");
446 column = -1;
447 break;
449 case '\t':
451 gint tab_width = sci_get_tab_width(editor->sci);
452 gint tab_stop = tab_width - (column % tab_width);
454 column += tab_stop - 1; /* -1 because we add 1 at the end of the loop */
455 g_string_append_printf(body, "\\hspace*{%dem}", tab_stop);
456 break;
458 case ' ':
460 if (c_next == ' ')
462 g_string_append(body, "{\\hspace*{1em}}");
463 i++; /* skip the next character */
465 else
466 g_string_append_c(body, ' ');
467 break;
469 case '{':
470 case '}':
471 case '_':
472 case '&':
473 case '$':
474 case '#':
475 case '%':
477 g_string_append_printf(body, "\\%c", c);
478 break;
480 case '\\':
482 g_string_append(body, "\\symbol{92}");
483 break;
485 case '~':
487 g_string_append(body, "\\symbol{126}");
488 break;
490 case '^':
492 g_string_append(body, "\\symbol{94}");
493 break;
495 /* mask "--", "<<" and ">>" */
496 case '-':
497 case '<':
498 case '>':
500 g_string_append_c(body, c);
501 if (c_next == c)
502 g_string_append(body, "\\/");
504 break;
506 default: g_string_append_c(body, c);
508 column++;
510 if (block_open)
512 g_string_append(body, "}\n");
513 block_open = FALSE;
516 /* force writing of style 0 (used at least for line breaks) */
517 styles[0][USED] = 1;
519 /* write used styles in the header */
520 cmds = g_string_new("");
521 for (i = 0; i < style_max; i++)
523 if (styles[i][USED])
525 g_string_append_printf(cmds,
526 "\\newcommand{\\style%s}[1]{\\noindent{", get_tex_style(i));
527 if (styles[i][BOLD])
528 g_string_append(cmds, "\\textbf{");
529 if (styles[i][ITALIC])
530 g_string_append(cmds, "\\textit{");
532 tmp = get_tex_rgb(styles[i][FORE]);
533 g_string_append_printf(cmds, "\\textcolor[rgb]{%s}{", tmp);
534 g_free(tmp);
535 tmp = get_tex_rgb(styles[i][BACK]);
536 g_string_append_printf(cmds, "\\fcolorbox[rgb]{0, 0, 0}{%s}{", tmp);
537 g_string_append(cmds, "#1}}");
538 g_free(tmp);
540 if (styles[i][BOLD])
541 g_string_append_c(cmds, '}');
542 if (styles[i][ITALIC])
543 g_string_append_c(cmds, '}');
544 g_string_append(cmds, "}}\n");
548 date = get_date(DATE_TYPE_DEFAULT);
549 /* write all */
550 latex = g_string_new(TEMPLATE_LATEX);
551 utils_string_replace_all(latex, "{export_content}", body->str);
552 utils_string_replace_all(latex, "{export_styles}", cmds->str);
553 utils_string_replace_all(latex, "{export_date}", date);
554 utils_string_replace_all(latex, "{export_filename}", DOC_FILENAME(doc));
556 write_data(filename, latex->str);
558 g_string_free(body, TRUE);
559 g_string_free(cmds, TRUE);
560 g_string_free(latex, TRUE);
561 g_free(date);
565 static void write_html_file(GeanyDocument *doc, const gchar *filename,
566 gboolean use_zoom, gboolean insert_line_numbers)
568 GeanyEditor *editor = doc->editor;
569 ScintillaObject *sci = doc->editor->sci;
570 gint i, doc_len, style = -1, old_style = 0, column = 0;
571 gint k, line_number, line_number_width, line_number_max_width = 0, pad;
572 gchar c, c_next, *date, *doc_filename;
573 /* 0 - fore, 1 - back, 2 - bold, 3 - italic, 4 - font size, 5 - used(0/1) */
574 gint styles[STYLE_MAX + 1][MAX_TYPES];
575 gboolean span_open = FALSE;
576 const gchar *font_name;
577 gint font_size;
578 PangoFontDescription *font_desc;
579 GString *body;
580 GString *css;
581 GString *html;
582 gint style_max = pow(2, scintilla_send_message(sci, SCI_GETSTYLEBITS, 0, 0));
584 /* first read all styles from Scintilla */
585 for (i = 0; i < style_max; i++)
587 styles[i][FORE] = ROTATE_RGB(scintilla_send_message(sci, SCI_STYLEGETFORE, i, 0));
588 styles[i][BACK] = ROTATE_RGB(scintilla_send_message(sci, SCI_STYLEGETBACK, i, 0));
589 styles[i][BOLD] = scintilla_send_message(sci, SCI_STYLEGETBOLD, i, 0);
590 styles[i][ITALIC] = scintilla_send_message(sci, SCI_STYLEGETITALIC, i, 0);
591 styles[i][USED] = 0;
594 /* read Geany's font and font size */
595 font_desc = pango_font_description_from_string(geany->interface_prefs->editor_font);
596 font_name = pango_font_description_get_family(font_desc);
597 /*font_size = pango_font_description_get_size(font_desc) / PANGO_SCALE;*/
598 /* take the zoom level also into account */
599 font_size = scintilla_send_message(sci, SCI_STYLEGETSIZE, 0, 0);
600 if (use_zoom)
601 font_size += scintilla_send_message(sci, SCI_GETZOOM, 0, 0);
603 if (insert_line_numbers)
604 line_number_max_width = get_line_number_width(doc);
606 /* read the document and write the HTML body */
607 body = g_string_new("");
608 doc_len = sci_get_length(sci);
609 for (i = 0; i < doc_len; i++)
611 style = sci_get_style_at(sci, i);
612 c = sci_get_char_at(sci, i);
613 /* sci_get_char_at() takes care of index boundaries and return 0 if i is too high */
614 c_next = sci_get_char_at(sci, i + 1);
616 /* line numbers */
617 if (insert_line_numbers && column == 0)
619 line_number = sci_get_line_from_position(sci, i) + 1;
620 line_number_width = get_line_numbers_arity(line_number);
621 /* padding */
622 pad = line_number_max_width - line_number_width;
623 for (k = 0; k < pad; k++)
625 g_string_append(body, "&nbsp;");
627 g_string_append_printf(body, "%d&nbsp;", line_number);
630 if ((style != old_style || ! span_open) && ! isspace(c))
632 old_style = style;
633 styles[style][USED] = 1;
634 if (span_open)
636 g_string_append(body, "</span>");
638 if (i < doc_len)
640 g_string_append_printf(body, "<span class=\"style_%d\">", style);
641 span_open = TRUE;
644 /* escape the current character if necessary else just add it */
645 switch (c)
647 case '\r':
648 case '\n':
650 if (c == '\r' && c_next == '\n')
651 continue; /* when using CR/LF skip CR and add the line break with LF */
653 if (span_open)
655 g_string_append(body, "</span>");
656 span_open = FALSE;
658 g_string_append(body, "<br />\n");
659 column = -1;
660 break;
662 case '\t':
664 gint j;
665 gint tab_width = sci_get_tab_width(editor->sci);
666 gint tab_stop = tab_width - (column % tab_width);
668 column += tab_stop - 1; /* -1 because we add 1 at the end of the loop */
669 for (j = 0; j < tab_stop; j++)
671 g_string_append(body, "&nbsp;");
673 break;
675 case ' ':
677 g_string_append(body, "&nbsp;");
678 break;
680 case '<':
682 g_string_append(body, "&lt;");
683 break;
685 case '>':
687 g_string_append(body, "&gt;");
688 break;
690 case '&':
692 g_string_append(body, "&amp;");
693 break;
695 default: g_string_append_c(body, c);
697 column++;
699 if (span_open)
701 g_string_append(body, "</span>");
702 span_open = FALSE;
705 /* write used styles in the header */
706 css = g_string_new("");
707 g_string_append_printf(css,
708 "\tbody\n\t{\n\t\tfont-family: %s, monospace;\n\t\tfont-size: %dpt;\n\t}\n",
709 font_name, font_size);
711 for (i = 0; i < style_max; i++)
713 if (styles[i][USED])
715 g_string_append_printf(css,
716 "\t.style_%d\n\t{\n\t\tcolor: #%06x;\n\t\tbackground-color: #%06x;\n%s%s\t}\n",
717 i, styles[i][FORE], styles[i][BACK],
718 (styles[i][BOLD]) ? "\t\tfont-weight: bold;\n" : "",
719 (styles[i][ITALIC]) ? "\t\tfont-style: italic;\n" : "");
723 date = get_date(DATE_TYPE_HTML);
724 doc_filename = g_markup_escape_text(DOC_FILENAME(doc), -1);
725 /* write all */
726 html = g_string_new(TEMPLATE_HTML);
727 utils_string_replace_all(html, "{export_date}", date);
728 utils_string_replace_all(html, "{export_content}", body->str);
729 utils_string_replace_all(html, "{export_styles}", css->str);
730 utils_string_replace_all(html, "{export_filename}", doc_filename);
732 write_data(filename, html->str);
734 pango_font_description_free(font_desc);
735 g_string_free(body, TRUE);
736 g_string_free(css, TRUE);
737 g_string_free(html, TRUE);
738 g_free(doc_filename);
739 g_free(date);
743 void plugin_init(GeanyData *data)
745 GtkWidget *menu_export;
746 GtkWidget *menu_export_menu;
747 GtkWidget *menu_create_html;
748 GtkWidget *menu_create_latex;
750 menu_export = gtk_image_menu_item_new_with_mnemonic(_("_Export"));
751 gtk_container_add(GTK_CONTAINER(geany->main_widgets->tools_menu), menu_export);
753 menu_export_menu = gtk_menu_new ();
754 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menu_export), menu_export_menu);
756 /* HTML */
757 menu_create_html = gtk_menu_item_new_with_mnemonic(_("As _HTML..."));
758 gtk_container_add(GTK_CONTAINER (menu_export_menu), menu_create_html);
760 g_signal_connect(menu_create_html, "activate", G_CALLBACK(on_menu_create_html_activate), NULL);
762 /* LaTeX */
763 menu_create_latex = gtk_menu_item_new_with_mnemonic(_("As _LaTeX..."));
764 gtk_container_add(GTK_CONTAINER (menu_export_menu), menu_create_latex);
766 g_signal_connect(menu_create_latex, "activate",
767 G_CALLBACK(on_menu_create_latex_activate), NULL);
769 /* disable menu_item when there are no documents open */
770 ui_add_document_sensitive(menu_export);
771 main_menu_item = menu_export;
773 gtk_widget_show_all(menu_export);
777 void plugin_cleanup(void)
779 gtk_widget_destroy(main_menu_item);