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 ui_hookup_widget(dialog
, check_zoom_level
, "check_zoom_level");
192 exi
->have_zoom_level_checkbox
= TRUE
;
195 g_signal_connect(dialog
, "delete-event", G_CALLBACK(gtk_widget_hide_on_delete
), NULL
);
196 g_signal_connect(dialog
, "response", G_CALLBACK(on_file_save_dialog_response
), exi
);
198 gtk_window_set_transient_for(GTK_WINDOW(dialog
), GTK_WINDOW(geany
->main_widgets
->window
));
200 /* if the current document has a filename we use it as the default. */
201 gtk_file_chooser_unselect_all(GTK_FILE_CHOOSER(dialog
));
202 if (doc
->file_name
!= NULL
)
204 gchar
*base_name
= g_path_get_basename(doc
->file_name
);
205 gchar
*short_name
= utils_remove_ext_from_filename(base_name
);
207 gchar
*locale_filename
;
208 gchar
*locale_dirname
;
209 const gchar
*suffix
= "";
211 if (g_str_has_suffix(doc
->file_name
, extension
))
214 file_name
= g_strconcat(short_name
, suffix
, extension
, NULL
);
215 locale_filename
= utils_get_locale_from_utf8(doc
->file_name
);
216 locale_dirname
= g_path_get_dirname(locale_filename
);
217 /* set the current name to base_name.html which probably doesn't exist yet so
218 * gtk_file_chooser_set_filename() can't be used and we need
219 * gtk_file_chooser_set_current_folder() additionally */
220 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog
), locale_dirname
);
221 gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(dialog
), file_name
);
222 g_free(locale_dirname
);
223 g_free(locale_filename
);
230 const gchar
*default_open_path
= geany
->prefs
->default_open_path
;
231 gchar
*fname
= g_strconcat(GEANY_STRING_UNTITLED
, extension
, NULL
);
233 gtk_file_chooser_unselect_all(GTK_FILE_CHOOSER(dialog
));
234 gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(dialog
), fname
);
236 /* use default startup directory(if set) if no files are open */
237 if (NZV(default_open_path
) && g_path_is_absolute(default_open_path
))
239 gchar
*locale_path
= utils_get_locale_from_utf8(default_open_path
);
240 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog
), locale_path
);
245 gtk_dialog_run(GTK_DIALOG(dialog
));
249 static void on_menu_create_latex_activate(GtkMenuItem
*menuitem
, gpointer user_data
)
251 create_file_save_as_dialog(".tex", write_latex_file
, FALSE
);
255 static void on_menu_create_html_activate(GtkMenuItem
*menuitem
, gpointer user_data
)
257 create_file_save_as_dialog(".html", write_html_file
, TRUE
);
261 static void write_data(const gchar
*filename
, const gchar
*data
)
263 gint error_nr
= utils_write_file(filename
, data
);
264 gchar
*utf8_filename
= utils_get_utf8_from_locale(filename
);
267 ui_set_statusbar(TRUE
, _("Document successfully exported as '%s'."), utf8_filename
);
269 ui_set_statusbar(TRUE
, _("File '%s' could not be written (%s)."),
270 utf8_filename
, g_strerror(error_nr
));
272 g_free(utf8_filename
);
276 static gchar
*get_date(gint type
)
280 if (type
== DATE_TYPE_HTML
)
283 format
= "%Y-%m-%dT%H:%M:%S%z";
285 format
= "%Y-%m-%dT%H:%M:%S";
290 return utils_get_date_time(format
, NULL
);
294 static void on_file_save_dialog_response(GtkDialog
*dialog
, gint response
, gpointer user_data
)
296 ExportInfo
*exi
= user_data
;
298 if (response
== GTK_RESPONSE_ACCEPT
&& exi
!= NULL
)
300 gchar
*new_filename
= gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog
));
301 gchar
*utf8_filename
;
302 gboolean use_zoom_level
= FALSE
;
304 if (exi
->have_zoom_level_checkbox
)
306 use_zoom_level
= gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(
307 ui_lookup_widget(GTK_WIDGET(dialog
), "check_zoom_level")));
310 utf8_filename
= utils_get_utf8_from_locale(new_filename
);
312 /* check if file exists and ask whether to overwrite or not */
313 if (g_file_test(new_filename
, G_FILE_TEST_EXISTS
))
315 if (dialogs_show_question(
316 _("The file '%s' already exists. Do you want to overwrite it?"),
317 utf8_filename
) == FALSE
)
321 exi
->export_func(exi
->doc
, new_filename
, use_zoom_level
);
323 g_free(utf8_filename
);
324 g_free(new_filename
);
327 gtk_widget_destroy(GTK_WIDGET(dialog
));
331 static void write_latex_file(GeanyDocument
*doc
, const gchar
*filename
, gboolean use_zoom
)
333 GeanyEditor
*editor
= doc
->editor
;
334 gint i
, doc_len
, style
= -1, old_style
= 0, column
= 0;
335 gchar c
, c_next
, *tmp
, *date
;
336 /* 0 - fore, 1 - back, 2 - bold, 3 - italic, 4 - font size, 5 - used(0/1) */
337 gint styles
[STYLE_MAX
+ 1][MAX_TYPES
];
338 gboolean block_open
= FALSE
;
342 gint style_max
= pow(2, scintilla_send_message(doc
->editor
->sci
, SCI_GETSTYLEBITS
, 0, 0));
344 /* first read all styles from Scintilla */
345 for (i
= 0; i
< style_max
; i
++)
347 styles
[i
][FORE
] = scintilla_send_message(doc
->editor
->sci
, SCI_STYLEGETFORE
, i
, 0);
348 styles
[i
][BACK
] = scintilla_send_message(doc
->editor
->sci
, SCI_STYLEGETBACK
, i
, 0);
349 styles
[i
][BOLD
] = scintilla_send_message(doc
->editor
->sci
, SCI_STYLEGETBOLD
, i
, 0);
350 styles
[i
][ITALIC
] = scintilla_send_message(doc
->editor
->sci
, SCI_STYLEGETITALIC
, i
, 0);
354 /* read the document and write the LaTeX code */
355 body
= g_string_new("");
356 doc_len
= sci_get_length(doc
->editor
->sci
);
357 for (i
= 0; i
< doc_len
; i
++)
359 style
= sci_get_style_at(doc
->editor
->sci
, i
);
360 c
= sci_get_char_at(doc
->editor
->sci
, i
);
361 c_next
= sci_get_char_at(doc
->editor
->sci
, i
+ 1);
363 if (style
!= old_style
|| ! block_open
)
366 styles
[style
][USED
] = 1;
369 g_string_append(body
, "}\n");
372 g_string_append_printf(body
, "\\style%s{", get_tex_style(style
));
376 /* escape the current character if necessary else just add it */
382 if (c
== '\r' && c_next
== '\n')
383 continue; /* when using CR/LF skip CR and add the line break with LF */
387 g_string_append(body
, "}");
390 g_string_append(body
, " \\\\\n");
396 gint tab_width
= sci_get_tab_width(editor
->sci
);
397 gint tab_stop
= tab_width
- (column
% tab_width
);
399 column
+= tab_stop
- 1; /* -1 because we add 1 at the end of the loop */
400 g_string_append_printf(body
, "\\hspace*{%dem}", tab_stop
);
407 g_string_append(body
, "{\\hspace*{1em}}");
408 i
++; /* skip the next character */
411 g_string_append_c(body
, ' ');
422 g_string_append_printf(body
, "\\%c", c
);
427 g_string_append(body
, "\\symbol{92}");
432 g_string_append(body
, "\\symbol{126}");
437 g_string_append(body
, "\\symbol{94}");
440 /** TODO still don't work for "---" or "----" */
441 case '-': /* mask "--" */
445 g_string_append(body
, "-\\/-");
446 i
++; /* skip the next character */
449 g_string_append_c(body
, '-');
453 case '<': /* mask "<<" */
457 g_string_append(body
, "<\\/<");
458 i
++; /* skip the next character */
461 g_string_append_c(body
, '<');
465 case '>': /* mask ">>" */
469 g_string_append(body
, ">\\/>");
470 i
++; /* skip the next character */
473 g_string_append_c(body
, '>');
477 default: g_string_append_c(body
, c
);
483 g_string_append(body
, "}\n");
487 /* force writing of style 0 (used at least for line breaks) */
490 /* write used styles in the header */
491 cmds
= g_string_new("");
492 for (i
= 0; i
<= STYLE_MAX
; i
++)
496 g_string_append_printf(cmds
,
497 "\\newcommand{\\style%s}[1]{\\noindent{", get_tex_style(i
));
499 g_string_append(cmds
, "\\textbf{");
500 if (styles
[i
][ITALIC
])
501 g_string_append(cmds
, "\\textit{");
503 tmp
= get_tex_rgb(styles
[i
][FORE
]);
504 g_string_append_printf(cmds
, "\\textcolor[rgb]{%s}{", tmp
);
506 tmp
= get_tex_rgb(styles
[i
][BACK
]);
507 g_string_append_printf(cmds
, "\\fcolorbox[rgb]{0, 0, 0}{%s}{", tmp
);
508 g_string_append(cmds
, "#1}}");
512 g_string_append_c(cmds
, '}');
513 if (styles
[i
][ITALIC
])
514 g_string_append_c(cmds
, '}');
515 g_string_append(cmds
, "}}\n");
519 date
= get_date(DATE_TYPE_DEFAULT
);
521 latex
= g_string_new(TEMPLATE_LATEX
);
522 utils_string_replace_all(latex
, "{export_content}", body
->str
);
523 utils_string_replace_all(latex
, "{export_styles}", cmds
->str
);
524 utils_string_replace_all(latex
, "{export_date}", date
);
525 if (doc
->file_name
== NULL
)
526 utils_string_replace_all(latex
, "{export_filename}", GEANY_STRING_UNTITLED
);
528 utils_string_replace_all(latex
, "{export_filename}", doc
->file_name
);
530 write_data(filename
, latex
->str
);
532 g_string_free(body
, TRUE
);
533 g_string_free(cmds
, TRUE
);
534 g_string_free(latex
, TRUE
);
539 static void write_html_file(GeanyDocument
*doc
, const gchar
*filename
, gboolean use_zoom
)
541 GeanyEditor
*editor
= doc
->editor
;
542 gint i
, doc_len
, style
= -1, old_style
= 0, column
= 0;
543 gchar c
, c_next
, *date
;
544 /* 0 - fore, 1 - back, 2 - bold, 3 - italic, 4 - font size, 5 - used(0/1) */
545 gint styles
[STYLE_MAX
+ 1][MAX_TYPES
];
546 gboolean span_open
= FALSE
;
547 const gchar
*font_name
;
549 PangoFontDescription
*font_desc
;
553 gint style_max
= pow(2, scintilla_send_message(doc
->editor
->sci
, SCI_GETSTYLEBITS
, 0, 0));
555 /* first read all styles from Scintilla */
556 for (i
= 0; i
< style_max
; i
++)
558 styles
[i
][FORE
] = ROTATE_RGB(scintilla_send_message(doc
->editor
->sci
, SCI_STYLEGETFORE
, i
, 0));
559 styles
[i
][BACK
] = ROTATE_RGB(scintilla_send_message(doc
->editor
->sci
, SCI_STYLEGETBACK
, i
, 0));
560 styles
[i
][BOLD
] = scintilla_send_message(doc
->editor
->sci
, SCI_STYLEGETBOLD
, i
, 0);
561 styles
[i
][ITALIC
] = scintilla_send_message(doc
->editor
->sci
, SCI_STYLEGETITALIC
, i
, 0);
565 /* read Geany's font and font size */
566 font_desc
= pango_font_description_from_string(geany
->interface_prefs
->editor_font
);
567 font_name
= pango_font_description_get_family(font_desc
);
568 /*font_size = pango_font_description_get_size(font_desc) / PANGO_SCALE;*/
569 /* take the zoom level also into account */
570 font_size
= scintilla_send_message(doc
->editor
->sci
, SCI_STYLEGETSIZE
, 0, 0);
572 font_size
+= scintilla_send_message(doc
->editor
->sci
, SCI_GETZOOM
, 0, 0);
574 /* read the document and write the HTML body */
575 body
= g_string_new("");
576 doc_len
= sci_get_length(doc
->editor
->sci
);
577 for (i
= 0; i
< doc_len
; i
++)
579 style
= sci_get_style_at(doc
->editor
->sci
, i
);
580 c
= sci_get_char_at(doc
->editor
->sci
, i
);
581 /* sci_get_char_at() takes care of index boundaries and return 0 if i is too high */
582 c_next
= sci_get_char_at(doc
->editor
->sci
, i
+ 1);
584 if ((style
!= old_style
|| ! span_open
) && ! isspace(c
))
587 styles
[style
][USED
] = 1;
590 g_string_append(body
, "</span>");
592 g_string_append_printf(body
, "<span class=\"style_%d\">", style
);
596 /* escape the current character if necessary else just add it */
602 if (c
== '\r' && c_next
== '\n')
603 continue; /* when using CR/LF skip CR and add the line break with LF */
607 g_string_append(body
, "</span>");
610 g_string_append(body
, "<br />\n");
617 gint tab_width
= sci_get_tab_width(editor
->sci
);
618 gint tab_stop
= tab_width
- (column
% tab_width
);
620 column
+= tab_stop
- 1; /* -1 because we add 1 at the end of the loop */
621 for (j
= 0; j
< tab_stop
; j
++)
623 g_string_append(body
, " ");
629 g_string_append(body
, " ");
634 g_string_append(body
, "<");
639 g_string_append(body
, ">");
644 g_string_append(body
, "&");
647 default: g_string_append_c(body
, c
);
653 g_string_append(body
, "</span>");
657 /* write used styles in the header */
658 css
= g_string_new("");
659 g_string_append_printf(css
,
660 "\tbody\n\t{\n\t\tfont-family: %s, monospace;\n\t\tfont-size: %dpt;\n\t}\n",
661 font_name
, font_size
);
663 for (i
= 0; i
<= STYLE_MAX
; i
++)
667 g_string_append_printf(css
,
668 "\t.style_%d\n\t{\n\t\tcolor: #%06x;\n\t\tbackground-color: #%06x;\n%s%s\t}\n",
669 i
, styles
[i
][FORE
], styles
[i
][BACK
],
670 (styles
[i
][BOLD
]) ? "\t\tfont-weight: bold;\n" : "",
671 (styles
[i
][ITALIC
]) ? "\t\tfont-style: italic;\n" : "");
675 date
= get_date(DATE_TYPE_HTML
);
677 html
= g_string_new(TEMPLATE_HTML
);
678 utils_string_replace_all(html
, "{export_date}", date
);
679 utils_string_replace_all(html
, "{export_content}", body
->str
);
680 utils_string_replace_all(html
, "{export_styles}", css
->str
);
681 if (doc
->file_name
== NULL
)
682 utils_string_replace_all(html
, "{export_filename}", GEANY_STRING_UNTITLED
);
684 utils_string_replace_all(html
, "{export_filename}", doc
->file_name
);
686 write_data(filename
, html
->str
);
688 pango_font_description_free(font_desc
);
689 g_string_free(body
, TRUE
);
690 g_string_free(css
, TRUE
);
691 g_string_free(html
, TRUE
);
696 void plugin_init(GeanyData
*data
)
698 GtkWidget
*menu_export
;
699 GtkWidget
*menu_export_menu
;
700 GtkWidget
*menu_create_html
;
701 GtkWidget
*menu_create_latex
;
703 menu_export
= gtk_image_menu_item_new_with_mnemonic(_("_Export"));
704 gtk_container_add(GTK_CONTAINER(geany
->main_widgets
->tools_menu
), menu_export
);
706 menu_export_menu
= gtk_menu_new ();
707 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menu_export
), menu_export_menu
);
710 menu_create_html
= gtk_menu_item_new_with_mnemonic(_("As _HTML"));
711 gtk_container_add(GTK_CONTAINER (menu_export_menu
), menu_create_html
);
713 g_signal_connect(menu_create_html
, "activate", G_CALLBACK(on_menu_create_html_activate
), NULL
);
716 menu_create_latex
= gtk_menu_item_new_with_mnemonic(_("As _LaTeX"));
717 gtk_container_add(GTK_CONTAINER (menu_export_menu
), menu_create_latex
);
719 g_signal_connect(menu_create_latex
, "activate",
720 G_CALLBACK(on_menu_create_latex_activate
), NULL
);
722 /* disable menu_item when there are no documents open */
723 ui_add_document_sensitive(menu_export
);
724 main_menu_item
= menu_export
;
726 gtk_widget_show_all(menu_export
);
730 void plugin_cleanup(void)
732 gtk_widget_destroy(main_menu_item
);