Make code more readable by renaming poorly named macros NZV and NVL
[geany-mirror.git] / plugins / export.c
blob946275d4d4a5f16eaed60a3772cc567f3536810f
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 /** TODO still don't work for "---" or "----" */
496 case '-': /* mask "--" */
498 if (c_next == '-')
500 g_string_append(body, "-\\/-");
501 i++; /* skip the next character */
503 else
504 g_string_append_c(body, '-');
506 break;
508 case '<': /* mask "<<" */
510 if (c_next == '<')
512 g_string_append(body, "<\\/<");
513 i++; /* skip the next character */
515 else
516 g_string_append_c(body, '<');
518 break;
520 case '>': /* mask ">>" */
522 if (c_next == '>')
524 g_string_append(body, ">\\/>");
525 i++; /* skip the next character */
527 else
528 g_string_append_c(body, '>');
530 break;
532 default: g_string_append_c(body, c);
534 column++;
536 if (block_open)
538 g_string_append(body, "}\n");
539 block_open = FALSE;
542 /* force writing of style 0 (used at least for line breaks) */
543 styles[0][USED] = 1;
545 /* write used styles in the header */
546 cmds = g_string_new("");
547 for (i = 0; i <= STYLE_MAX; i++)
549 if (styles[i][USED])
551 g_string_append_printf(cmds,
552 "\\newcommand{\\style%s}[1]{\\noindent{", get_tex_style(i));
553 if (styles[i][BOLD])
554 g_string_append(cmds, "\\textbf{");
555 if (styles[i][ITALIC])
556 g_string_append(cmds, "\\textit{");
558 tmp = get_tex_rgb(styles[i][FORE]);
559 g_string_append_printf(cmds, "\\textcolor[rgb]{%s}{", tmp);
560 g_free(tmp);
561 tmp = get_tex_rgb(styles[i][BACK]);
562 g_string_append_printf(cmds, "\\fcolorbox[rgb]{0, 0, 0}{%s}{", tmp);
563 g_string_append(cmds, "#1}}");
564 g_free(tmp);
566 if (styles[i][BOLD])
567 g_string_append_c(cmds, '}');
568 if (styles[i][ITALIC])
569 g_string_append_c(cmds, '}');
570 g_string_append(cmds, "}}\n");
574 date = get_date(DATE_TYPE_DEFAULT);
575 /* write all */
576 latex = g_string_new(TEMPLATE_LATEX);
577 utils_string_replace_all(latex, "{export_content}", body->str);
578 utils_string_replace_all(latex, "{export_styles}", cmds->str);
579 utils_string_replace_all(latex, "{export_date}", date);
580 if (doc->file_name == NULL)
581 utils_string_replace_all(latex, "{export_filename}", GEANY_STRING_UNTITLED);
582 else
583 utils_string_replace_all(latex, "{export_filename}", doc->file_name);
585 write_data(filename, latex->str);
587 g_string_free(body, TRUE);
588 g_string_free(cmds, TRUE);
589 g_string_free(latex, TRUE);
590 g_free(date);
594 static void write_html_file(GeanyDocument *doc, const gchar *filename,
595 gboolean use_zoom, gboolean insert_line_numbers)
597 GeanyEditor *editor = doc->editor;
598 ScintillaObject *sci = doc->editor->sci;
599 gint i, doc_len, style = -1, old_style = 0, column = 0;
600 gint k, line_number, line_number_width, line_number_max_width = 0, pad;
601 gchar c, c_next, *date;
602 /* 0 - fore, 1 - back, 2 - bold, 3 - italic, 4 - font size, 5 - used(0/1) */
603 gint styles[STYLE_MAX + 1][MAX_TYPES];
604 gboolean span_open = FALSE;
605 const gchar *font_name;
606 gint font_size;
607 PangoFontDescription *font_desc;
608 GString *body;
609 GString *css;
610 GString *html;
611 gint style_max = pow(2, scintilla_send_message(sci, SCI_GETSTYLEBITS, 0, 0));
613 /* first read all styles from Scintilla */
614 for (i = 0; i < style_max; i++)
616 styles[i][FORE] = ROTATE_RGB(scintilla_send_message(sci, SCI_STYLEGETFORE, i, 0));
617 styles[i][BACK] = ROTATE_RGB(scintilla_send_message(sci, SCI_STYLEGETBACK, i, 0));
618 styles[i][BOLD] = scintilla_send_message(sci, SCI_STYLEGETBOLD, i, 0);
619 styles[i][ITALIC] = scintilla_send_message(sci, SCI_STYLEGETITALIC, i, 0);
620 styles[i][USED] = 0;
623 /* read Geany's font and font size */
624 font_desc = pango_font_description_from_string(geany->interface_prefs->editor_font);
625 font_name = pango_font_description_get_family(font_desc);
626 /*font_size = pango_font_description_get_size(font_desc) / PANGO_SCALE;*/
627 /* take the zoom level also into account */
628 font_size = scintilla_send_message(sci, SCI_STYLEGETSIZE, 0, 0);
629 if (use_zoom)
630 font_size += scintilla_send_message(sci, SCI_GETZOOM, 0, 0);
632 if (insert_line_numbers)
633 line_number_max_width = get_line_number_width(doc);
635 /* read the document and write the HTML body */
636 body = g_string_new("");
637 doc_len = sci_get_length(sci);
638 for (i = 0; i <= doc_len; i++)
640 style = sci_get_style_at(sci, i);
641 c = sci_get_char_at(sci, i);
642 /* sci_get_char_at() takes care of index boundaries and return 0 if i is too high */
643 c_next = sci_get_char_at(sci, i + 1);
645 /* line numbers */
646 if (insert_line_numbers && column == 0)
648 line_number = sci_get_line_from_position(sci, i) + 1;
649 line_number_width = get_line_numbers_arity(line_number);
650 /* padding */
651 pad = line_number_max_width - line_number_width;
652 for (k = 0; k < pad; k++)
654 g_string_append(body, "&nbsp;");
656 g_string_append_printf(body, "%d&nbsp;", line_number);
659 if ((style != old_style || ! span_open) && ! isspace(c))
661 old_style = style;
662 styles[style][USED] = 1;
663 if (span_open)
665 g_string_append(body, "</span>");
667 if (i < doc_len)
669 g_string_append_printf(body, "<span class=\"style_%d\">", style);
670 span_open = TRUE;
673 /* escape the current character if necessary else just add it */
674 switch (c)
676 case '\r':
677 case '\n':
679 if (c == '\r' && c_next == '\n')
680 continue; /* when using CR/LF skip CR and add the line break with LF */
682 if (span_open)
684 g_string_append(body, "</span>");
685 span_open = FALSE;
687 g_string_append(body, "<br />\n");
688 column = -1;
689 break;
691 case '\t':
693 gint j;
694 gint tab_width = sci_get_tab_width(editor->sci);
695 gint tab_stop = tab_width - (column % tab_width);
697 column += tab_stop - 1; /* -1 because we add 1 at the end of the loop */
698 for (j = 0; j < tab_stop; j++)
700 g_string_append(body, "&nbsp;");
702 break;
704 case ' ':
706 g_string_append(body, "&nbsp;");
707 break;
709 case '<':
711 g_string_append(body, "&lt;");
712 break;
714 case '>':
716 g_string_append(body, "&gt;");
717 break;
719 case '&':
721 g_string_append(body, "&amp;");
722 break;
724 default: g_string_append_c(body, c);
726 column++;
728 if (span_open)
730 g_string_append(body, "</span>");
731 span_open = FALSE;
734 /* write used styles in the header */
735 css = g_string_new("");
736 g_string_append_printf(css,
737 "\tbody\n\t{\n\t\tfont-family: %s, monospace;\n\t\tfont-size: %dpt;\n\t}\n",
738 font_name, font_size);
740 for (i = 0; i <= STYLE_MAX; i++)
742 if (styles[i][USED])
744 g_string_append_printf(css,
745 "\t.style_%d\n\t{\n\t\tcolor: #%06x;\n\t\tbackground-color: #%06x;\n%s%s\t}\n",
746 i, styles[i][FORE], styles[i][BACK],
747 (styles[i][BOLD]) ? "\t\tfont-weight: bold;\n" : "",
748 (styles[i][ITALIC]) ? "\t\tfont-style: italic;\n" : "");
752 date = get_date(DATE_TYPE_HTML);
753 /* write all */
754 html = g_string_new(TEMPLATE_HTML);
755 utils_string_replace_all(html, "{export_date}", date);
756 utils_string_replace_all(html, "{export_content}", body->str);
757 utils_string_replace_all(html, "{export_styles}", css->str);
758 if (doc->file_name == NULL)
759 utils_string_replace_all(html, "{export_filename}", GEANY_STRING_UNTITLED);
760 else
761 utils_string_replace_all(html, "{export_filename}", doc->file_name);
763 write_data(filename, html->str);
765 pango_font_description_free(font_desc);
766 g_string_free(body, TRUE);
767 g_string_free(css, TRUE);
768 g_string_free(html, TRUE);
769 g_free(date);
773 void plugin_init(GeanyData *data)
775 GtkWidget *menu_export;
776 GtkWidget *menu_export_menu;
777 GtkWidget *menu_create_html;
778 GtkWidget *menu_create_latex;
780 menu_export = gtk_image_menu_item_new_with_mnemonic(_("_Export"));
781 gtk_container_add(GTK_CONTAINER(geany->main_widgets->tools_menu), menu_export);
783 menu_export_menu = gtk_menu_new ();
784 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menu_export), menu_export_menu);
786 /* HTML */
787 menu_create_html = gtk_menu_item_new_with_mnemonic(_("As _HTML..."));
788 gtk_container_add(GTK_CONTAINER (menu_export_menu), menu_create_html);
790 g_signal_connect(menu_create_html, "activate", G_CALLBACK(on_menu_create_html_activate), NULL);
792 /* LaTeX */
793 menu_create_latex = gtk_menu_item_new_with_mnemonic(_("As _LaTeX..."));
794 gtk_container_add(GTK_CONTAINER (menu_export_menu), menu_create_latex);
796 g_signal_connect(menu_create_latex, "activate",
797 G_CALLBACK(on_menu_create_latex_activate), NULL);
799 /* disable menu_item when there are no documents open */
800 ui_add_document_sensitive(menu_export);
801 main_menu_item = menu_export;
803 gtk_widget_show_all(menu_export);
807 void plugin_cleanup(void)
809 gtk_widget_destroy(main_menu_item);