2 * export.c - this file is part of Geany, a fast and lightweight IDE
4 * Copyright 2007-2010 Enrico Tröger <enrico(dot)troeger(at)uvena(dot)de>
5 * Copyright 2007-2010 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,
30 #include "geanyplugin.h"
33 GeanyData
*geany_data
;
34 GeanyFunctions
*geany_functions
;
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\
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\
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\
81 \\setlength{\\fboxrule}{0pt}\n\
82 \\setlength{\\fboxsep}{0pt}\n\
103 typedef void (*ExportFunc
) (GeanyDocument
*doc
, const gchar
*filename
, gboolean use_zoom
);
107 gboolean have_zoom_level_checkbox
;
108 ExportFunc export_func
;
111 static void on_file_save_dialog_response(GtkDialog
*dialog
, gint response
, gpointer user_data
);
112 static void write_html_file(GeanyDocument
*doc
, const gchar
*filename
, gboolean use_zoom
);
113 static void write_latex_file(GeanyDocument
*doc
, const gchar
*filename
, gboolean use_zoom
);
116 /* converts a RGB colour into a LaTeX compatible representation, taken from SciTE */
117 static gchar
* get_tex_rgb(gint rgb_colour
)
119 /* texcolor[rgb]{0,0.5,0}{....} */
120 gdouble rf
= (rgb_colour
% 256) / 256.0;
121 gdouble gf
= ((rgb_colour
& - 16711936) / 256) / 256.0;
122 gdouble bf
= ((rgb_colour
& 0xff0000) / 65536) / 256.0;
123 gint r
= (gint
) (rf
* 10 + 0.5);
124 gint g
= (gint
) (gf
* 10 + 0.5);
125 gint b
= (gint
) (bf
* 10 + 0.5);
127 return g_strdup_printf("%d.%d, %d.%d, %d.%d", r
/ 10, r
% 10, g
/ 10, g
% 10, b
/ 10, b
% 10);
131 /* convert a style number (0..127) into a string representation (aa, ab, .., ba, bb, .., zy, zz) */
132 static gchar
*get_tex_style(gint style
)
139 buf
[i
] = (style
% 26) + 'a';
149 static void create_file_save_as_dialog(const gchar
*extension
, ExportFunc func
,
150 gboolean show_zoom_level_checkbox
)
156 if (extension
== NULL
)
159 doc
= document_get_current();
161 exi
= g_new(ExportInfo
, 1);
163 exi
->export_func
= func
;
164 exi
->have_zoom_level_checkbox
= FALSE
;
166 dialog
= gtk_file_chooser_dialog_new(_("Export File"), GTK_WINDOW(geany
->main_widgets
->window
),
167 GTK_FILE_CHOOSER_ACTION_SAVE
, NULL
, NULL
);
168 gtk_window_set_modal(GTK_WINDOW(dialog
), TRUE
);
169 gtk_window_set_destroy_with_parent(GTK_WINDOW(dialog
), TRUE
);
170 gtk_window_set_skip_taskbar_hint(GTK_WINDOW(dialog
), TRUE
);
171 gtk_window_set_type_hint(GTK_WINDOW(dialog
), GDK_WINDOW_TYPE_HINT_DIALOG
);
172 gtk_widget_set_name(dialog
, "GeanyExportDialog");
174 gtk_dialog_add_buttons(GTK_DIALOG(dialog
),
175 GTK_STOCK_CANCEL
, GTK_RESPONSE_CANCEL
, GTK_STOCK_SAVE
, GTK_RESPONSE_ACCEPT
, NULL
);
176 gtk_dialog_set_default_response(GTK_DIALOG(dialog
), GTK_RESPONSE_ACCEPT
);
178 if (show_zoom_level_checkbox
)
180 GtkWidget
*vbox
, *check_zoom_level
;
182 vbox
= gtk_vbox_new(FALSE
, 0);
183 check_zoom_level
= gtk_check_button_new_with_mnemonic(_("_Use current zoom level"));
184 ui_widget_set_tooltip_text(check_zoom_level
,
185 _("Renders the font size of the document together with the current zoom level"));
186 gtk_box_pack_start(GTK_BOX(vbox
), check_zoom_level
, FALSE
, FALSE
, 0);
187 gtk_widget_show_all(vbox
);
188 gtk_file_chooser_set_extra_widget(GTK_FILE_CHOOSER(dialog
), vbox
);
190 g_object_set_data_full(G_OBJECT(dialog
), "check_zoom_level",
191 g_object_ref(check_zoom_level
), (GDestroyNotify
) g_object_unref
);
193 exi
->have_zoom_level_checkbox
= TRUE
;
196 g_signal_connect(dialog
, "delete-event", G_CALLBACK(gtk_widget_hide_on_delete
), NULL
);
197 g_signal_connect(dialog
, "response", G_CALLBACK(on_file_save_dialog_response
), exi
);
199 gtk_window_set_transient_for(GTK_WINDOW(dialog
), GTK_WINDOW(geany
->main_widgets
->window
));
201 /* if the current document has a filename we use it as the default. */
202 gtk_file_chooser_unselect_all(GTK_FILE_CHOOSER(dialog
));
203 if (doc
->file_name
!= NULL
)
205 gchar
*base_name
= g_path_get_basename(doc
->file_name
);
206 gchar
*short_name
= utils_remove_ext_from_filename(base_name
);
208 gchar
*locale_filename
;
209 gchar
*locale_dirname
;
210 const gchar
*suffix
= "";
212 if (g_str_has_suffix(doc
->file_name
, extension
))
215 file_name
= g_strconcat(short_name
, suffix
, extension
, NULL
);
216 locale_filename
= utils_get_locale_from_utf8(doc
->file_name
);
217 locale_dirname
= g_path_get_dirname(locale_filename
);
218 /* set the current name to base_name.html which probably doesn't exist yet so
219 * gtk_file_chooser_set_filename() can't be used and we need
220 * gtk_file_chooser_set_current_folder() additionally */
221 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog
), locale_dirname
);
222 gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(dialog
), file_name
);
223 g_free(locale_dirname
);
224 g_free(locale_filename
);
231 const gchar
*default_open_path
= geany
->prefs
->default_open_path
;
232 gchar
*fname
= g_strconcat(GEANY_STRING_UNTITLED
, extension
, NULL
);
234 gtk_file_chooser_unselect_all(GTK_FILE_CHOOSER(dialog
));
235 gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(dialog
), fname
);
237 /* use default startup directory(if set) if no files are open */
238 if (NZV(default_open_path
) && g_path_is_absolute(default_open_path
))
240 gchar
*locale_path
= utils_get_locale_from_utf8(default_open_path
);
241 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog
), locale_path
);
246 gtk_dialog_run(GTK_DIALOG(dialog
));
250 static void on_menu_create_latex_activate(GtkMenuItem
*menuitem
, gpointer user_data
)
252 create_file_save_as_dialog(".tex", write_latex_file
, FALSE
);
256 static void on_menu_create_html_activate(GtkMenuItem
*menuitem
, gpointer user_data
)
258 create_file_save_as_dialog(".html", write_html_file
, TRUE
);
262 static void write_data(const gchar
*filename
, const gchar
*data
)
264 gint error_nr
= utils_write_file(filename
, data
);
265 gchar
*utf8_filename
= utils_get_utf8_from_locale(filename
);
268 ui_set_statusbar(TRUE
, _("Document successfully exported as '%s'."), utf8_filename
);
270 ui_set_statusbar(TRUE
, _("File '%s' could not be written (%s)."),
271 utf8_filename
, g_strerror(error_nr
));
273 g_free(utf8_filename
);
277 static gchar
*get_date(gint type
)
281 if (type
== DATE_TYPE_HTML
)
284 format
= "%Y-%m-%dT%H:%M:%S%z";
286 format
= "%Y-%m-%dT%H:%M:%S";
291 return utils_get_date_time(format
, NULL
);
295 static void on_file_save_dialog_response(GtkDialog
*dialog
, gint response
, gpointer user_data
)
297 ExportInfo
*exi
= user_data
;
299 if (response
== GTK_RESPONSE_ACCEPT
&& exi
!= NULL
)
301 gchar
*new_filename
= gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog
));
302 gchar
*utf8_filename
;
303 gboolean use_zoom_level
= FALSE
;
305 if (exi
->have_zoom_level_checkbox
)
307 use_zoom_level
= gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(
308 ui_lookup_widget(GTK_WIDGET(dialog
), "check_zoom_level")));
311 utf8_filename
= utils_get_utf8_from_locale(new_filename
);
313 /* check if file exists and ask whether to overwrite or not */
314 if (g_file_test(new_filename
, G_FILE_TEST_EXISTS
))
316 if (dialogs_show_question(
317 _("The file '%s' already exists. Do you want to overwrite it?"),
318 utf8_filename
) == FALSE
)
322 exi
->export_func(exi
->doc
, new_filename
, use_zoom_level
);
324 g_free(utf8_filename
);
325 g_free(new_filename
);
328 gtk_widget_destroy(GTK_WIDGET(dialog
));
332 static void write_latex_file(GeanyDocument
*doc
, const gchar
*filename
, gboolean use_zoom
)
334 GeanyEditor
*editor
= doc
->editor
;
335 gint i
, doc_len
, style
= -1, old_style
= 0, column
= 0;
336 gchar c
, c_next
, *tmp
, *date
;
337 /* 0 - fore, 1 - back, 2 - bold, 3 - italic, 4 - font size, 5 - used(0/1) */
338 gint styles
[STYLE_MAX
+ 1][MAX_TYPES
];
339 gboolean block_open
= FALSE
;
343 gint style_max
= pow(2, scintilla_send_message(doc
->editor
->sci
, SCI_GETSTYLEBITS
, 0, 0));
345 /* first read all styles from Scintilla */
346 for (i
= 0; i
< style_max
; i
++)
348 styles
[i
][FORE
] = scintilla_send_message(doc
->editor
->sci
, SCI_STYLEGETFORE
, i
, 0);
349 styles
[i
][BACK
] = scintilla_send_message(doc
->editor
->sci
, SCI_STYLEGETBACK
, i
, 0);
350 styles
[i
][BOLD
] = scintilla_send_message(doc
->editor
->sci
, SCI_STYLEGETBOLD
, i
, 0);
351 styles
[i
][ITALIC
] = scintilla_send_message(doc
->editor
->sci
, SCI_STYLEGETITALIC
, i
, 0);
355 /* read the document and write the LaTeX code */
356 body
= g_string_new("");
357 doc_len
= sci_get_length(doc
->editor
->sci
);
358 for (i
= 0; i
< doc_len
; i
++)
360 style
= sci_get_style_at(doc
->editor
->sci
, i
);
361 c
= sci_get_char_at(doc
->editor
->sci
, i
);
362 c_next
= sci_get_char_at(doc
->editor
->sci
, i
+ 1);
364 if (style
!= old_style
|| ! block_open
)
367 styles
[style
][USED
] = 1;
370 g_string_append(body
, "}\n");
373 g_string_append_printf(body
, "\\style%s{", get_tex_style(style
));
377 /* escape the current character if necessary else just add it */
383 if (c
== '\r' && c_next
== '\n')
384 continue; /* when using CR/LF skip CR and add the line break with LF */
388 g_string_append(body
, "}");
391 g_string_append(body
, " \\\\\n");
397 gint tab_width
= sci_get_tab_width(editor
->sci
);
398 gint tab_stop
= tab_width
- (column
% tab_width
);
400 column
+= tab_stop
- 1; /* -1 because we add 1 at the end of the loop */
401 g_string_append_printf(body
, "\\hspace*{%dem}", tab_stop
);
408 g_string_append(body
, "{\\hspace*{1em}}");
409 i
++; /* skip the next character */
412 g_string_append_c(body
, ' ');
423 g_string_append_printf(body
, "\\%c", c
);
428 g_string_append(body
, "\\symbol{92}");
433 g_string_append(body
, "\\symbol{126}");
438 g_string_append(body
, "\\symbol{94}");
441 /** TODO still don't work for "---" or "----" */
442 case '-': /* mask "--" */
446 g_string_append(body
, "-\\/-");
447 i
++; /* skip the next character */
450 g_string_append_c(body
, '-');
454 case '<': /* mask "<<" */
458 g_string_append(body
, "<\\/<");
459 i
++; /* skip the next character */
462 g_string_append_c(body
, '<');
466 case '>': /* mask ">>" */
470 g_string_append(body
, ">\\/>");
471 i
++; /* skip the next character */
474 g_string_append_c(body
, '>');
478 default: g_string_append_c(body
, c
);
484 g_string_append(body
, "}\n");
488 /* force writing of style 0 (used at least for line breaks) */
491 /* write used styles in the header */
492 cmds
= g_string_new("");
493 for (i
= 0; i
<= STYLE_MAX
; i
++)
497 g_string_append_printf(cmds
,
498 "\\newcommand{\\style%s}[1]{\\noindent{", get_tex_style(i
));
500 g_string_append(cmds
, "\\textbf{");
501 if (styles
[i
][ITALIC
])
502 g_string_append(cmds
, "\\textit{");
504 tmp
= get_tex_rgb(styles
[i
][FORE
]);
505 g_string_append_printf(cmds
, "\\textcolor[rgb]{%s}{", tmp
);
507 tmp
= get_tex_rgb(styles
[i
][BACK
]);
508 g_string_append_printf(cmds
, "\\fcolorbox[rgb]{0, 0, 0}{%s}{", tmp
);
509 g_string_append(cmds
, "#1}}");
513 g_string_append_c(cmds
, '}');
514 if (styles
[i
][ITALIC
])
515 g_string_append_c(cmds
, '}');
516 g_string_append(cmds
, "}}\n");
520 date
= get_date(DATE_TYPE_DEFAULT
);
522 latex
= g_string_new(TEMPLATE_LATEX
);
523 utils_string_replace_all(latex
, "{export_content}", body
->str
);
524 utils_string_replace_all(latex
, "{export_styles}", cmds
->str
);
525 utils_string_replace_all(latex
, "{export_date}", date
);
526 if (doc
->file_name
== NULL
)
527 utils_string_replace_all(latex
, "{export_filename}", GEANY_STRING_UNTITLED
);
529 utils_string_replace_all(latex
, "{export_filename}", doc
->file_name
);
531 write_data(filename
, latex
->str
);
533 g_string_free(body
, TRUE
);
534 g_string_free(cmds
, TRUE
);
535 g_string_free(latex
, TRUE
);
540 static void write_html_file(GeanyDocument
*doc
, const gchar
*filename
, gboolean use_zoom
)
542 GeanyEditor
*editor
= doc
->editor
;
543 gint i
, doc_len
, style
= -1, old_style
= 0, column
= 0;
544 gchar c
, c_next
, *date
;
545 /* 0 - fore, 1 - back, 2 - bold, 3 - italic, 4 - font size, 5 - used(0/1) */
546 gint styles
[STYLE_MAX
+ 1][MAX_TYPES
];
547 gboolean span_open
= FALSE
;
548 const gchar
*font_name
;
550 PangoFontDescription
*font_desc
;
554 gint style_max
= pow(2, scintilla_send_message(doc
->editor
->sci
, SCI_GETSTYLEBITS
, 0, 0));
556 /* first read all styles from Scintilla */
557 for (i
= 0; i
< style_max
; i
++)
559 styles
[i
][FORE
] = ROTATE_RGB(scintilla_send_message(doc
->editor
->sci
, SCI_STYLEGETFORE
, i
, 0));
560 styles
[i
][BACK
] = ROTATE_RGB(scintilla_send_message(doc
->editor
->sci
, SCI_STYLEGETBACK
, i
, 0));
561 styles
[i
][BOLD
] = scintilla_send_message(doc
->editor
->sci
, SCI_STYLEGETBOLD
, i
, 0);
562 styles
[i
][ITALIC
] = scintilla_send_message(doc
->editor
->sci
, SCI_STYLEGETITALIC
, i
, 0);
566 /* read Geany's font and font size */
567 font_desc
= pango_font_description_from_string(geany
->interface_prefs
->editor_font
);
568 font_name
= pango_font_description_get_family(font_desc
);
569 /*font_size = pango_font_description_get_size(font_desc) / PANGO_SCALE;*/
570 /* take the zoom level also into account */
571 font_size
= scintilla_send_message(doc
->editor
->sci
, SCI_STYLEGETSIZE
, 0, 0);
573 font_size
+= scintilla_send_message(doc
->editor
->sci
, SCI_GETZOOM
, 0, 0);
575 /* read the document and write the HTML body */
576 body
= g_string_new("");
577 doc_len
= sci_get_length(doc
->editor
->sci
);
578 for (i
= 0; i
< doc_len
; i
++)
580 style
= sci_get_style_at(doc
->editor
->sci
, i
);
581 c
= sci_get_char_at(doc
->editor
->sci
, i
);
582 /* sci_get_char_at() takes care of index boundaries and return 0 if i is too high */
583 c_next
= sci_get_char_at(doc
->editor
->sci
, i
+ 1);
585 if ((style
!= old_style
|| ! span_open
) && ! isspace(c
))
588 styles
[style
][USED
] = 1;
591 g_string_append(body
, "</span>");
593 g_string_append_printf(body
, "<span class=\"style_%d\">", style
);
597 /* escape the current character if necessary else just add it */
603 if (c
== '\r' && c_next
== '\n')
604 continue; /* when using CR/LF skip CR and add the line break with LF */
608 g_string_append(body
, "</span>");
611 g_string_append(body
, "<br />\n");
618 gint tab_width
= sci_get_tab_width(editor
->sci
);
619 gint tab_stop
= tab_width
- (column
% tab_width
);
621 column
+= tab_stop
- 1; /* -1 because we add 1 at the end of the loop */
622 for (j
= 0; j
< tab_stop
; j
++)
624 g_string_append(body
, " ");
630 g_string_append(body
, " ");
635 g_string_append(body
, "<");
640 g_string_append(body
, ">");
645 g_string_append(body
, "&");
648 default: g_string_append_c(body
, c
);
654 g_string_append(body
, "</span>");
658 /* write used styles in the header */
659 css
= g_string_new("");
660 g_string_append_printf(css
,
661 "\tbody\n\t{\n\t\tfont-family: %s, monospace;\n\t\tfont-size: %dpt;\n\t}\n",
662 font_name
, font_size
);
664 for (i
= 0; i
<= STYLE_MAX
; i
++)
668 g_string_append_printf(css
,
669 "\t.style_%d\n\t{\n\t\tcolor: #%06x;\n\t\tbackground-color: #%06x;\n%s%s\t}\n",
670 i
, styles
[i
][FORE
], styles
[i
][BACK
],
671 (styles
[i
][BOLD
]) ? "\t\tfont-weight: bold;\n" : "",
672 (styles
[i
][ITALIC
]) ? "\t\tfont-style: italic;\n" : "");
676 date
= get_date(DATE_TYPE_HTML
);
678 html
= g_string_new(TEMPLATE_HTML
);
679 utils_string_replace_all(html
, "{export_date}", date
);
680 utils_string_replace_all(html
, "{export_content}", body
->str
);
681 utils_string_replace_all(html
, "{export_styles}", css
->str
);
682 if (doc
->file_name
== NULL
)
683 utils_string_replace_all(html
, "{export_filename}", GEANY_STRING_UNTITLED
);
685 utils_string_replace_all(html
, "{export_filename}", doc
->file_name
);
687 write_data(filename
, html
->str
);
689 pango_font_description_free(font_desc
);
690 g_string_free(body
, TRUE
);
691 g_string_free(css
, TRUE
);
692 g_string_free(html
, TRUE
);
697 void plugin_init(GeanyData
*data
)
699 GtkWidget
*menu_export
;
700 GtkWidget
*menu_export_menu
;
701 GtkWidget
*menu_create_html
;
702 GtkWidget
*menu_create_latex
;
704 menu_export
= gtk_image_menu_item_new_with_mnemonic(_("_Export"));
705 gtk_container_add(GTK_CONTAINER(geany
->main_widgets
->tools_menu
), menu_export
);
707 menu_export_menu
= gtk_menu_new ();
708 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menu_export
), menu_export_menu
);
711 menu_create_html
= gtk_menu_item_new_with_mnemonic(_("As _HTML"));
712 gtk_container_add(GTK_CONTAINER (menu_export_menu
), menu_create_html
);
714 g_signal_connect(menu_create_html
, "activate", G_CALLBACK(on_menu_create_html_activate
), NULL
);
717 menu_create_latex
= gtk_menu_item_new_with_mnemonic(_("As _LaTeX"));
718 gtk_container_add(GTK_CONTAINER (menu_export_menu
), menu_create_latex
);
720 g_signal_connect(menu_create_latex
, "activate",
721 G_CALLBACK(on_menu_create_latex_activate
), NULL
);
723 /* disable menu_item when there are no documents open */
724 ui_add_document_sensitive(menu_export
);
725 main_menu_item
= menu_export
;
727 gtk_widget_show_all(menu_export
);
731 void plugin_cleanup(void)
733 gtk_widget_destroy(main_menu_item
);