Merge pull request #1251 from techee/bool
[geany-mirror.git] / plugins / export.c
blobe5165f6cf4e344f3be1bdc406cfedb232ab0591b
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;
36 PLUGIN_VERSION_CHECK(GEANY_API_VERSION)
37 PLUGIN_SET_INFO(_("Export"), _("Exports the current file into different formats."), VERSION,
38 _("The Geany developer team"))
41 static GtkWidget *main_menu_item = NULL;
44 #define ROTATE_RGB(color) \
45 (((color) & 0xFF0000) >> 16) + ((color) & 0x00FF00) + (((color) & 0x0000FF) << 16)
46 #define TEMPLATE_HTML "\
47 <!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"\n\
48 \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n\
49 <html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\" lang=\"en\">\n\
50 \n\
51 <head>\n\
52 <title>{export_filename}</title>\n\
53 <meta http-equiv=\"content-type\" content=\"text/html;charset=utf-8\" />\n\
54 <meta name=\"generator\" content=\"Geany " VERSION "\" />\n\
55 <meta name=\"date\" content=\"{export_date}\" />\n\
56 <style type=\"text/css\">\n\
57 {export_styles}\n\
58 </style>\n\
59 </head>\n\
60 \n\
61 <body>\n\
62 <p>\n\
63 {export_content}\n\
64 </p>\n\
65 </body>\n\
66 </html>\n"
68 #define TEMPLATE_LATEX "\
69 % {export_filename} (LaTeX code generated by Geany " VERSION " on {export_date})\n\
70 \\documentclass[a4paper]{article}\n\
71 \\usepackage[a4paper,margin=2cm]{geometry}\n\
72 \\usepackage[utf8]{inputenc}\n\
73 \\usepackage[T1]{fontenc}\n\
74 \\usepackage{color}\n\
75 \\setlength{\\parindent}{0em}\n\
76 \\setlength{\\parskip}{2ex plus1ex minus0.5ex}\n\
77 {export_styles}\n\
78 \\begin{document}\
79 \n\
80 \\ttfamily\n\
81 \\setlength{\\fboxrule}{0pt}\n\
82 \\setlength{\\fboxsep}{0pt}\n\
83 {export_content}\
84 \\end{document}\n"
87 enum
89 FORE = 0,
90 BACK,
91 BOLD,
92 ITALIC,
93 USED,
94 MAX_TYPES
97 enum
99 DATE_TYPE_DEFAULT,
100 DATE_TYPE_HTML
103 typedef void (*ExportFunc) (GeanyDocument *doc, const gchar *filename,
104 gboolean use_zoom, gboolean insert_line_numbers);
105 typedef struct
107 GeanyDocument *doc;
108 gboolean have_zoom_level_checkbox;
109 ExportFunc export_func;
110 } ExportInfo;
112 static void on_file_save_dialog_response(GtkDialog *dialog, gint response, gpointer user_data);
113 static void write_html_file(GeanyDocument *doc, const gchar *filename,
114 gboolean use_zoom, gboolean insert_line_numbers);
115 static void write_latex_file(GeanyDocument *doc, const gchar *filename,
116 gboolean use_zoom, gboolean insert_line_numbers);
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)
137 static gchar buf[4];
138 int i = 0;
142 buf[i] = (style % 26) + 'a';
143 style /= 26;
144 i++;
145 } while (style > 0);
146 buf[i] = '\0';
148 return buf;
152 static void create_file_save_as_dialog(const gchar *extension, ExportFunc func,
153 gboolean show_zoom_level_checkbox)
155 GtkWidget *dialog, *vbox;
156 GeanyDocument *doc;
157 ExportInfo *exi;
159 g_return_if_fail(extension != NULL);
161 doc = document_get_current();
162 g_return_if_fail(doc != NULL);
164 exi = g_new(ExportInfo, 1);
165 exi->doc = doc;
166 exi->export_func = func;
167 exi->have_zoom_level_checkbox = FALSE;
169 dialog = gtk_file_chooser_dialog_new(_("Export File"), GTK_WINDOW(geany->main_widgets->window),
170 GTK_FILE_CHOOSER_ACTION_SAVE, NULL, NULL);
171 gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
172 gtk_window_set_destroy_with_parent(GTK_WINDOW(dialog), TRUE);
173 gtk_window_set_skip_taskbar_hint(GTK_WINDOW(dialog), TRUE);
174 gtk_window_set_type_hint(GTK_WINDOW(dialog), GDK_WINDOW_TYPE_HINT_DIALOG);
175 gtk_widget_set_name(dialog, "GeanyExportDialog");
177 gtk_dialog_add_buttons(GTK_DIALOG(dialog),
178 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT, NULL);
179 gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT);
181 /* file chooser extra widget */
182 vbox = gtk_vbox_new(FALSE, 0);
183 gtk_file_chooser_set_extra_widget(GTK_FILE_CHOOSER(dialog), vbox);
185 GtkWidget *check_line_numbers;
187 check_line_numbers = gtk_check_button_new_with_mnemonic(_("_Insert line numbers"));
188 gtk_widget_set_tooltip_text(check_line_numbers,
189 _("Insert line numbers before each line in the exported document"));
190 gtk_box_pack_start(GTK_BOX(vbox), check_line_numbers, FALSE, FALSE, 0);
191 gtk_widget_show_all(vbox);
193 ui_hookup_widget(dialog, check_line_numbers, "check_line_numbers");
195 if (show_zoom_level_checkbox)
197 GtkWidget *check_zoom_level;
199 check_zoom_level = gtk_check_button_new_with_mnemonic(_("_Use current zoom level"));
200 gtk_widget_set_tooltip_text(check_zoom_level,
201 _("Renders the font size of the document together with the current zoom level"));
202 gtk_box_pack_start(GTK_BOX(vbox), check_zoom_level, FALSE, FALSE, 0);
203 gtk_widget_show_all(vbox);
205 ui_hookup_widget(dialog, check_zoom_level, "check_zoom_level");
206 exi->have_zoom_level_checkbox = TRUE;
209 g_signal_connect(dialog, "delete-event", G_CALLBACK(gtk_widget_hide_on_delete), NULL);
210 g_signal_connect(dialog, "response", G_CALLBACK(on_file_save_dialog_response), exi);
212 gtk_window_set_transient_for(GTK_WINDOW(dialog), GTK_WINDOW(geany->main_widgets->window));
214 /* if the current document has a filename we use it as the default. */
215 gtk_file_chooser_unselect_all(GTK_FILE_CHOOSER(dialog));
216 if (doc->file_name != NULL)
218 gchar *base_name = g_path_get_basename(doc->file_name);
219 gchar *file_name;
220 gchar *locale_filename;
221 gchar *locale_dirname;
222 const gchar *suffix = "";
224 if (g_str_has_suffix(doc->file_name, extension))
225 suffix = "_export";
227 file_name = g_strconcat(base_name, suffix, extension, NULL);
228 locale_filename = utils_get_locale_from_utf8(doc->file_name);
229 locale_dirname = g_path_get_dirname(locale_filename);
230 /* set the current name to base_name.html which probably doesn't exist yet so
231 * gtk_file_chooser_set_filename() can't be used and we need
232 * gtk_file_chooser_set_current_folder() additionally */
233 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog), locale_dirname);
234 gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(dialog), file_name);
235 g_free(locale_dirname);
236 g_free(locale_filename);
237 g_free(file_name);
238 g_free(base_name);
240 else
242 const gchar *default_open_path = geany->prefs->default_open_path;
243 gchar *fname = g_strconcat(GEANY_STRING_UNTITLED, extension, NULL);
245 gtk_file_chooser_unselect_all(GTK_FILE_CHOOSER(dialog));
246 gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(dialog), fname);
248 /* use default startup directory(if set) if no files are open */
249 if (!EMPTY(default_open_path) && g_path_is_absolute(default_open_path))
251 gchar *locale_path = utils_get_locale_from_utf8(default_open_path);
252 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog), locale_path);
253 g_free(locale_path);
255 g_free(fname);
257 gtk_dialog_run(GTK_DIALOG(dialog));
261 static void on_menu_create_latex_activate(GtkMenuItem *menuitem, gpointer user_data)
263 create_file_save_as_dialog(".tex", write_latex_file, FALSE);
267 static void on_menu_create_html_activate(GtkMenuItem *menuitem, gpointer user_data)
269 create_file_save_as_dialog(".html", write_html_file, TRUE);
273 static void write_data(const gchar *filename, const gchar *data)
275 gint error_nr = utils_write_file(filename, data);
276 gchar *utf8_filename = utils_get_utf8_from_locale(filename);
278 if (error_nr == 0)
279 ui_set_statusbar(TRUE, _("Document successfully exported as '%s'."), utf8_filename);
280 else
281 ui_set_statusbar(TRUE, _("File '%s' could not be written (%s)."),
282 utf8_filename, g_strerror(error_nr));
284 g_free(utf8_filename);
288 static gchar *get_date(gint type)
290 const gchar *format;
292 if (type == DATE_TYPE_HTML)
293 /* needs testing */
294 #ifdef _GNU_SOURCE
295 format = "%Y-%m-%dT%H:%M:%S%z";
296 #else
297 format = "%Y-%m-%dT%H:%M:%S";
298 #endif
299 else
300 format = "%c";
302 return utils_get_date_time(format, NULL);
306 static void on_file_save_dialog_response(GtkDialog *dialog, gint response, gpointer user_data)
308 ExportInfo *exi = user_data;
310 if (response == GTK_RESPONSE_ACCEPT && exi != NULL)
312 gchar *new_filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
313 gchar *utf8_filename;
314 gboolean insert_line_numbers;
315 gboolean use_zoom_level = FALSE;
317 if (exi->have_zoom_level_checkbox)
319 use_zoom_level = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(
320 ui_lookup_widget(GTK_WIDGET(dialog), "check_zoom_level")));
322 insert_line_numbers = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(
323 ui_lookup_widget(GTK_WIDGET(dialog), "check_line_numbers")));
325 utf8_filename = utils_get_utf8_from_locale(new_filename);
327 /* check if file exists and ask whether to overwrite or not */
328 if (g_file_test(new_filename, G_FILE_TEST_EXISTS))
330 if (dialogs_show_question(
331 _("The file '%s' already exists. Do you want to overwrite it?"),
332 utf8_filename) == FALSE)
333 return;
336 exi->export_func(exi->doc, new_filename, use_zoom_level, insert_line_numbers);
338 g_free(utf8_filename);
339 g_free(new_filename);
341 g_free(exi);
342 gtk_widget_destroy(GTK_WIDGET(dialog));
346 /* returns the "width" (count of needed characters) for the given number */
347 static gint get_line_numbers_arity(gint line_number)
349 gint a = 0;
350 while ((line_number /= 10) != 0)
351 a++;
352 return a;
356 static gint get_line_number_width(GeanyDocument *doc)
358 gint line_count = sci_get_line_count(doc->editor->sci);
359 return get_line_numbers_arity(line_count);
363 static void write_latex_file(GeanyDocument *doc, const gchar *filename,
364 gboolean use_zoom, gboolean insert_line_numbers)
366 GeanyEditor *editor = doc->editor;
367 ScintillaObject *sci = doc->editor->sci;
368 gint i, doc_len, style = -1, old_style = 0, column = 0;
369 gint k, line_number, line_number_width, line_number_max_width = 0, pad;
370 gchar c, c_next, *tmp, *date;
371 /* 0 - fore, 1 - back, 2 - bold, 3 - italic, 4 - font size, 5 - used(0/1) */
372 gint styles[STYLE_MAX + 1][MAX_TYPES];
373 gboolean block_open = FALSE;
374 GString *body;
375 GString *cmds;
376 GString *latex;
377 gint style_max = pow(2, scintilla_send_message(sci, SCI_GETSTYLEBITS, 0, 0));
379 /* first read all styles from Scintilla */
380 for (i = 0; i < style_max; i++)
382 styles[i][FORE] = scintilla_send_message(sci, SCI_STYLEGETFORE, i, 0);
383 styles[i][BACK] = scintilla_send_message(sci, SCI_STYLEGETBACK, i, 0);
384 styles[i][BOLD] = scintilla_send_message(sci, SCI_STYLEGETBOLD, i, 0);
385 styles[i][ITALIC] = scintilla_send_message(sci, SCI_STYLEGETITALIC, i, 0);
386 styles[i][USED] = 0;
389 if (insert_line_numbers)
390 line_number_max_width = get_line_number_width(doc);
392 /* read the document and write the LaTeX code */
393 body = g_string_new("");
394 doc_len = sci_get_length(sci);
395 for (i = 0; i < doc_len; i++)
397 style = sci_get_style_at(sci, i);
398 c = sci_get_char_at(sci, i);
399 c_next = sci_get_char_at(sci, i + 1);
401 /* line numbers */
402 if (insert_line_numbers && column == 0)
404 line_number = sci_get_line_from_position(sci, i) + 1;
405 line_number_width = get_line_numbers_arity(line_number);
406 /* padding */
407 pad = line_number_max_width - line_number_width;
408 for (k = 0; k < pad; k++)
410 g_string_append(body, " ");
412 g_string_append_printf(body, "%d ", line_number);
415 if (style != old_style || ! block_open)
417 old_style = style;
418 styles[style][USED] = 1;
419 if (block_open)
421 g_string_append(body, "}\n");
422 block_open = FALSE;
424 if (i < doc_len)
426 g_string_append_printf(body, "\\style%s{", get_tex_style(style));
427 block_open = TRUE;
430 /* escape the current character if necessary else just add it */
431 switch (c)
433 case '\r':
434 case '\n':
436 if (c == '\r' && c_next == '\n')
437 continue; /* when using CR/LF skip CR and add the line break with LF */
439 if (block_open)
441 g_string_append(body, "}");
442 block_open = FALSE;
444 g_string_append(body, " \\\\\n");
445 column = -1;
446 break;
448 case '\t':
450 gint tab_width = sci_get_tab_width(editor->sci);
451 gint tab_stop = tab_width - (column % tab_width);
453 column += tab_stop - 1; /* -1 because we add 1 at the end of the loop */
454 g_string_append_printf(body, "\\hspace*{%dem}", tab_stop);
455 break;
457 case ' ':
459 if (c_next == ' ')
461 g_string_append(body, "{\\hspace*{1em}}");
462 i++; /* skip the next character */
464 else
465 g_string_append_c(body, ' ');
466 break;
468 case '{':
469 case '}':
470 case '_':
471 case '&':
472 case '$':
473 case '#':
474 case '%':
476 g_string_append_printf(body, "\\%c", c);
477 break;
479 case '\\':
481 g_string_append(body, "\\symbol{92}");
482 break;
484 case '~':
486 g_string_append(body, "\\symbol{126}");
487 break;
489 case '^':
491 g_string_append(body, "\\symbol{94}");
492 break;
494 /* mask "--", "<<" and ">>" */
495 case '-':
496 case '<':
497 case '>':
499 g_string_append_c(body, c);
500 if (c_next == c)
501 g_string_append(body, "\\/");
503 break;
505 default: g_string_append_c(body, c);
507 column++;
509 if (block_open)
511 g_string_append(body, "}\n");
512 block_open = FALSE;
515 /* force writing of style 0 (used at least for line breaks) */
516 styles[0][USED] = 1;
518 /* write used styles in the header */
519 cmds = g_string_new("");
520 for (i = 0; i < style_max; i++)
522 if (styles[i][USED])
524 g_string_append_printf(cmds,
525 "\\newcommand{\\style%s}[1]{\\noindent{", get_tex_style(i));
526 if (styles[i][BOLD])
527 g_string_append(cmds, "\\textbf{");
528 if (styles[i][ITALIC])
529 g_string_append(cmds, "\\textit{");
531 tmp = get_tex_rgb(styles[i][FORE]);
532 g_string_append_printf(cmds, "\\textcolor[rgb]{%s}{", tmp);
533 g_free(tmp);
534 tmp = get_tex_rgb(styles[i][BACK]);
535 g_string_append_printf(cmds, "\\fcolorbox[rgb]{0, 0, 0}{%s}{", tmp);
536 g_string_append(cmds, "#1}}");
537 g_free(tmp);
539 if (styles[i][BOLD])
540 g_string_append_c(cmds, '}');
541 if (styles[i][ITALIC])
542 g_string_append_c(cmds, '}');
543 g_string_append(cmds, "}}\n");
547 date = get_date(DATE_TYPE_DEFAULT);
548 /* write all */
549 latex = g_string_new(TEMPLATE_LATEX);
550 utils_string_replace_all(latex, "{export_content}", body->str);
551 utils_string_replace_all(latex, "{export_styles}", cmds->str);
552 utils_string_replace_all(latex, "{export_date}", date);
553 utils_string_replace_all(latex, "{export_filename}", DOC_FILENAME(doc));
555 write_data(filename, latex->str);
557 g_string_free(body, TRUE);
558 g_string_free(cmds, TRUE);
559 g_string_free(latex, TRUE);
560 g_free(date);
564 static void write_html_file(GeanyDocument *doc, const gchar *filename,
565 gboolean use_zoom, gboolean insert_line_numbers)
567 GeanyEditor *editor = doc->editor;
568 ScintillaObject *sci = doc->editor->sci;
569 gint i, doc_len, style = -1, old_style = 0, column = 0;
570 gint k, line_number, line_number_width, line_number_max_width = 0, pad;
571 gchar c, c_next, *date, *doc_filename;
572 /* 0 - fore, 1 - back, 2 - bold, 3 - italic, 4 - font size, 5 - used(0/1) */
573 gint styles[STYLE_MAX + 1][MAX_TYPES];
574 gboolean span_open = FALSE;
575 const gchar *font_name;
576 gint font_size;
577 PangoFontDescription *font_desc;
578 GString *body;
579 GString *css;
580 GString *html;
581 gint style_max = pow(2, scintilla_send_message(sci, SCI_GETSTYLEBITS, 0, 0));
583 /* first read all styles from Scintilla */
584 for (i = 0; i < style_max; i++)
586 styles[i][FORE] = ROTATE_RGB(scintilla_send_message(sci, SCI_STYLEGETFORE, i, 0));
587 styles[i][BACK] = ROTATE_RGB(scintilla_send_message(sci, SCI_STYLEGETBACK, i, 0));
588 styles[i][BOLD] = scintilla_send_message(sci, SCI_STYLEGETBOLD, i, 0);
589 styles[i][ITALIC] = scintilla_send_message(sci, SCI_STYLEGETITALIC, i, 0);
590 styles[i][USED] = 0;
593 /* read Geany's font and font size */
594 font_desc = pango_font_description_from_string(geany->interface_prefs->editor_font);
595 font_name = pango_font_description_get_family(font_desc);
596 /*font_size = pango_font_description_get_size(font_desc) / PANGO_SCALE;*/
597 /* take the zoom level also into account */
598 font_size = scintilla_send_message(sci, SCI_STYLEGETSIZE, 0, 0);
599 if (use_zoom)
600 font_size += scintilla_send_message(sci, SCI_GETZOOM, 0, 0);
602 if (insert_line_numbers)
603 line_number_max_width = get_line_number_width(doc);
605 /* read the document and write the HTML body */
606 body = g_string_new("");
607 doc_len = sci_get_length(sci);
608 for (i = 0; i < doc_len; i++)
610 style = sci_get_style_at(sci, i);
611 c = sci_get_char_at(sci, i);
612 /* sci_get_char_at() takes care of index boundaries and return 0 if i is too high */
613 c_next = sci_get_char_at(sci, i + 1);
615 /* line numbers */
616 if (insert_line_numbers && column == 0)
618 line_number = sci_get_line_from_position(sci, i) + 1;
619 line_number_width = get_line_numbers_arity(line_number);
620 /* padding */
621 pad = line_number_max_width - line_number_width;
622 for (k = 0; k < pad; k++)
624 g_string_append(body, "&nbsp;");
626 g_string_append_printf(body, "%d&nbsp;", line_number);
629 if ((style != old_style || ! span_open) && ! isspace(c))
631 old_style = style;
632 styles[style][USED] = 1;
633 if (span_open)
635 g_string_append(body, "</span>");
637 if (i < doc_len)
639 g_string_append_printf(body, "<span class=\"style_%d\">", style);
640 span_open = TRUE;
643 /* escape the current character if necessary else just add it */
644 switch (c)
646 case '\r':
647 case '\n':
649 if (c == '\r' && c_next == '\n')
650 continue; /* when using CR/LF skip CR and add the line break with LF */
652 if (span_open)
654 g_string_append(body, "</span>");
655 span_open = FALSE;
657 g_string_append(body, "<br />\n");
658 column = -1;
659 break;
661 case '\t':
663 gint j;
664 gint tab_width = sci_get_tab_width(editor->sci);
665 gint tab_stop = tab_width - (column % tab_width);
667 column += tab_stop - 1; /* -1 because we add 1 at the end of the loop */
668 for (j = 0; j < tab_stop; j++)
670 g_string_append(body, "&nbsp;");
672 break;
674 case ' ':
676 g_string_append(body, "&nbsp;");
677 break;
679 case '<':
681 g_string_append(body, "&lt;");
682 break;
684 case '>':
686 g_string_append(body, "&gt;");
687 break;
689 case '&':
691 g_string_append(body, "&amp;");
692 break;
694 default: g_string_append_c(body, c);
696 column++;
698 if (span_open)
700 g_string_append(body, "</span>");
701 span_open = FALSE;
704 /* write used styles in the header */
705 css = g_string_new("");
706 g_string_append_printf(css,
707 "\tbody\n\t{\n\t\tfont-family: %s, monospace;\n\t\tfont-size: %dpt;\n\t}\n",
708 font_name, font_size);
710 for (i = 0; i < style_max; i++)
712 if (styles[i][USED])
714 g_string_append_printf(css,
715 "\t.style_%d\n\t{\n\t\tcolor: #%06x;\n\t\tbackground-color: #%06x;\n%s%s\t}\n",
716 i, styles[i][FORE], styles[i][BACK],
717 (styles[i][BOLD]) ? "\t\tfont-weight: bold;\n" : "",
718 (styles[i][ITALIC]) ? "\t\tfont-style: italic;\n" : "");
722 date = get_date(DATE_TYPE_HTML);
723 doc_filename = g_markup_escape_text(DOC_FILENAME(doc), -1);
724 /* write all */
725 html = g_string_new(TEMPLATE_HTML);
726 utils_string_replace_all(html, "{export_date}", date);
727 utils_string_replace_all(html, "{export_content}", body->str);
728 utils_string_replace_all(html, "{export_styles}", css->str);
729 utils_string_replace_all(html, "{export_filename}", doc_filename);
731 write_data(filename, html->str);
733 pango_font_description_free(font_desc);
734 g_string_free(body, TRUE);
735 g_string_free(css, TRUE);
736 g_string_free(html, TRUE);
737 g_free(doc_filename);
738 g_free(date);
742 void plugin_init(GeanyData *data)
744 GtkWidget *menu_export;
745 GtkWidget *menu_export_menu;
746 GtkWidget *menu_create_html;
747 GtkWidget *menu_create_latex;
749 menu_export = gtk_image_menu_item_new_with_mnemonic(_("_Export"));
750 gtk_container_add(GTK_CONTAINER(geany->main_widgets->tools_menu), menu_export);
752 menu_export_menu = gtk_menu_new ();
753 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menu_export), menu_export_menu);
755 /* HTML */
756 menu_create_html = gtk_menu_item_new_with_mnemonic(_("As _HTML..."));
757 gtk_container_add(GTK_CONTAINER (menu_export_menu), menu_create_html);
759 g_signal_connect(menu_create_html, "activate", G_CALLBACK(on_menu_create_html_activate), NULL);
761 /* LaTeX */
762 menu_create_latex = gtk_menu_item_new_with_mnemonic(_("As _LaTeX..."));
763 gtk_container_add(GTK_CONTAINER (menu_export_menu), menu_create_latex);
765 g_signal_connect(menu_create_latex, "activate",
766 G_CALLBACK(on_menu_create_latex_activate), NULL);
768 /* disable menu_item when there are no documents open */
769 ui_add_document_sensitive(menu_export);
770 main_menu_item = menu_export;
772 gtk_widget_show_all(menu_export);
776 void plugin_cleanup(void)
778 gtk_widget_destroy(main_menu_item);