~ubuntu-branches/ubuntu/raring/geany/raring-proposed

« back to all changes in this revision

Viewing changes to plugins/export.c

  • Committer: Bazaar Package Importer
  • Author(s): Damián Viano
  • Date: 2008-05-02 11:37:45 UTC
  • mto: (3.1.1 lenny) (1.3.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 12.
  • Revision ID: james.westby@ubuntu.com-20080502113745-7q62rqhl2ku02ptu
Import upstream version 0.14

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *      export.c - this file is part of Geany, a fast and lightweight IDE
 
3
 *
 
4
 *      Copyright 2007-2008 Enrico Tröger <enrico(dot)troeger(at)uvena(dot)de>
 
5
 *      Copyright 2007-2008 Nick Treleaven <nick(dot)treleaven(at)btinternet(dot)com>
 
6
 *
 
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.
 
11
 *
 
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.
 
16
 *
 
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,
 
20
 *      MA 02110-1301, USA.
 
21
 *
 
22
 * $Id: export.c 2430 2008-03-31 13:09:56Z ntrel $
 
23
 */
 
24
 
 
25
/* Export plugin. */
 
26
 
 
27
#include <ctype.h>
 
28
#include <math.h>
 
29
 
 
30
#include "geany.h"
 
31
#include "support.h"
 
32
#include "plugindata.h"
 
33
#include "editor.h"
 
34
#include "document.h"
 
35
#include "prefs.h"
 
36
#include "utils.h"
 
37
#include "pluginmacros.h"
 
38
 
 
39
 
 
40
PluginFields    *plugin_fields;
 
41
GeanyData               *geany_data;
 
42
 
 
43
PLUGIN_VERSION_CHECK(20)
 
44
PLUGIN_INFO(_("Export"), _("Exports the current file into different formats."), VERSION,
 
45
        _("The Geany developer team"))
 
46
 
 
47
#define ROTATE_RGB(color) \
 
48
        (((color) & 0xFF0000) >> 16) + ((color) & 0x00FF00) + (((color) & 0x0000FF) << 16)
 
49
#define TEMPLATE_HTML "\
 
50
<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"\n\
 
51
  \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n\
 
52
<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\" lang=\"en\">\n\
 
53
\n\
 
54
<head>\n\
 
55
        <title>{export_filename}</title>\n\
 
56
        <meta http-equiv=\"content-type\" content=\"text/html;charset=utf-8\" />\n\
 
57
        <meta name=\"generator\" content=\"Geany " VERSION "\" />\n\
 
58
        <meta name=\"date\" content=\"{export_date}\">\n\
 
59
        <style type=\"text/css\">\n\
 
60
{export_styles}\n\
 
61
        </style>\n\
 
62
</head>\n\
 
63
\n\
 
64
<body>\n\
 
65
<p>\n\
 
66
{export_content}\n\
 
67
</p>\n\
 
68
</body>\n\
 
69
</html>\n"
 
70
 
 
71
#define TEMPLATE_LATEX "\
 
72
% {export_filename} (LaTeX code generated by Geany " VERSION " on {export_date})\n\
 
73
\\documentclass[a4paper]{article}\n\
 
74
\\usepackage[a4paper,margin=2cm]{geometry}\n\
 
75
\\usepackage[utf8x]{inputenc}\n\
 
76
\\usepackage[T1]{fontenc}\n\
 
77
\\usepackage{color}\n\
 
78
\\setlength{\\parindent}{0em}\n\
 
79
\\setlength{\\parskip}{2ex plus1ex minus0.5ex}\n\
 
80
{export_styles}\n\
 
81
\\begin{document}\
 
82
\n\
 
83
\\ttfamily\n\
 
84
\\setlength{\\fboxrule}{0pt}\n\
 
85
\\setlength{\\fboxsep}{0pt}\n\
 
86
{export_content}\
 
87
\\end{document}\n"
 
88
 
 
89
 
 
90
enum
 
91
{
 
92
        FORE = 0,
 
93
        BACK,
 
94
        BOLD,
 
95
        ITALIC,
 
96
        USED,
 
97
        MAX_TYPES
 
98
};
 
99
 
 
100
enum
 
101
{
 
102
        DATE_TYPE_DEFAULT,
 
103
        DATE_TYPE_HTML
 
104
};
 
105
 
 
106
typedef void (*ExportFunc) (gint idx, const gchar *filename, gboolean use_zoom);
 
107
typedef struct
 
108
{
 
109
        gint idx;
 
110
        gboolean have_zoom_level_checkbox;
 
111
        ExportFunc export_func;
 
112
} ExportInfo;
 
113
 
 
114
static void on_file_save_dialog_response(GtkDialog *dialog, gint response, gpointer user_data);
 
115
static void write_html_file(gint idx, const gchar *filename, gboolean use_zoom);
 
116
static void write_latex_file(gint idx, const gchar *filename, gboolean use_zoom);
 
117
 
 
118
 
 
119
/* converts a RGB colour into a LaTeX compatible representation, taken from SciTE */
 
120
static gchar* get_tex_rgb(gint rgb_colour)
 
121
{
 
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);
 
129
 
 
130
        return g_strdup_printf("%d.%d, %d.%d, %d.%d", r / 10, r % 10, g / 10, g % 10, b / 10, b % 10);
 
131
}
 
132
 
 
133
 
 
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)
 
136
{
 
137
        static gchar buf[4];
 
138
        int i = 0;
 
139
 
 
140
        do
 
141
        {
 
142
                buf[i] = (style % 26) + 'a';
 
143
                style /= 26;
 
144
                i++;
 
145
        } while (style > 0);
 
146
        buf[i] = '\0';
 
147
 
 
148
        return buf;
 
149
}
 
150
 
 
151
 
 
152
static void create_file_save_as_dialog(const gchar *extension, ExportFunc func,
 
153
                                                                           gboolean show_zoom_level_checkbox)
 
154
{
 
155
        gint idx;
 
156
        GtkWidget *dialog;
 
157
        GtkTooltips *tooltips;
 
158
        ExportInfo *exi;
 
159
 
 
160
        if (extension == NULL)
 
161
                return;
 
162
 
 
163
        idx = p_document->get_cur_idx();
 
164
        tooltips = GTK_TOOLTIPS(p_support->lookup_widget(app->window, "tooltips"));
 
165
 
 
166
        exi = g_new(ExportInfo, 1);
 
167
        exi->idx = idx;
 
168
        exi->export_func = func;
 
169
        exi->have_zoom_level_checkbox = FALSE;
 
170
 
 
171
        dialog = gtk_file_chooser_dialog_new(_("Export File"), GTK_WINDOW(app->window),
 
172
                                GTK_FILE_CHOOSER_ACTION_SAVE, NULL, NULL);
 
173
        gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
 
174
        gtk_window_set_destroy_with_parent(GTK_WINDOW(dialog), TRUE);
 
175
        gtk_window_set_skip_taskbar_hint(GTK_WINDOW(dialog), TRUE);
 
176
        gtk_window_set_type_hint(GTK_WINDOW(dialog), GDK_WINDOW_TYPE_HINT_DIALOG);
 
177
        gtk_widget_set_name(dialog, "GeanyExportDialog");
 
178
 
 
179
        gtk_dialog_add_buttons(GTK_DIALOG(dialog),
 
180
                GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT, NULL);
 
181
        gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT);
 
182
 
 
183
        if (show_zoom_level_checkbox)
 
184
        {
 
185
                GtkWidget *vbox, *check_zoom_level;
 
186
 
 
187
                vbox = gtk_vbox_new(FALSE, 0);
 
188
                check_zoom_level = gtk_check_button_new_with_mnemonic(_("_Use current zoom level"));
 
189
                gtk_tooltips_set_tip(tooltips, check_zoom_level,
 
190
                        _("Renders the font size of the document together with the current zoom level."), NULL);
 
191
                gtk_box_pack_start(GTK_BOX(vbox), check_zoom_level, FALSE, FALSE, 0);
 
192
                gtk_widget_show_all(vbox);
 
193
                gtk_file_chooser_set_extra_widget(GTK_FILE_CHOOSER(dialog), vbox);
 
194
 
 
195
                g_object_set_data_full(G_OBJECT(dialog), "check_zoom_level",
 
196
                                        gtk_widget_ref(check_zoom_level), (GDestroyNotify) gtk_widget_unref);
 
197
 
 
198
                exi->have_zoom_level_checkbox = TRUE;
 
199
        }
 
200
 
 
201
        g_signal_connect((gpointer) dialog, "delete_event",
 
202
                G_CALLBACK(gtk_widget_hide_on_delete), NULL);
 
203
        g_signal_connect((gpointer) dialog, "response",
 
204
                G_CALLBACK(on_file_save_dialog_response), exi);
 
205
 
 
206
        gtk_window_set_transient_for(GTK_WINDOW(dialog), GTK_WINDOW(app->window));
 
207
 
 
208
        /* if the current document has a filename we use it as the default. */
 
209
        gtk_file_chooser_unselect_all(GTK_FILE_CHOOSER(dialog));
 
210
        if (doc_list[idx].file_name != NULL)
 
211
        {
 
212
                gchar *base_name = g_path_get_basename(doc_list[idx].file_name);
 
213
                gchar *short_name = p_utils->remove_ext_from_filename(base_name);
 
214
                gchar *file_name;
 
215
                gchar *locale_filename;
 
216
                gchar *locale_dirname;
 
217
                gchar *suffix = "";
 
218
 
 
219
                if (g_str_has_suffix(doc_list[idx].file_name, extension))
 
220
                        suffix = "_export";
 
221
 
 
222
                file_name = g_strconcat(short_name, suffix, extension, NULL);
 
223
                locale_filename = p_utils->get_locale_from_utf8(doc_list[idx].file_name);
 
224
                locale_dirname = g_path_get_dirname(locale_filename);
 
225
                /* set the current name to base_name.html which probably doesn't exist yet so
 
226
                 * gtk_file_chooser_set_filename() can't be used and we need
 
227
                 * gtk_file_chooser_set_current_folder() additionally */
 
228
                gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog), locale_dirname);
 
229
                gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(dialog), file_name);
 
230
                g_free(locale_filename);
 
231
                g_free(short_name);
 
232
                g_free(file_name);
 
233
                g_free(base_name);
 
234
        }
 
235
        else
 
236
        {
 
237
                const gchar *default_open_path = prefs->default_open_path;
 
238
                gchar *fname = g_strconcat(GEANY_STRING_UNTITLED, extension, NULL);
 
239
 
 
240
                gtk_file_chooser_unselect_all(GTK_FILE_CHOOSER(dialog));
 
241
                gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(dialog), fname);
 
242
 
 
243
                /* use default startup directory(if set) if no files are open */
 
244
                if (NZV(default_open_path) && g_path_is_absolute(default_open_path))
 
245
                {
 
246
                        gchar *locale_path = p_utils->get_locale_from_utf8(default_open_path);
 
247
                        gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog), locale_path);
 
248
                        g_free(locale_path);
 
249
                }
 
250
                g_free(fname);
 
251
        }
 
252
        gtk_dialog_run(GTK_DIALOG(dialog));
 
253
}
 
254
 
 
255
 
 
256
static void on_menu_create_latex_activate(GtkMenuItem *menuitem, gpointer user_data)
 
257
{
 
258
        create_file_save_as_dialog(".tex", write_latex_file, FALSE);
 
259
}
 
260
 
 
261
 
 
262
static void on_menu_create_html_activate(GtkMenuItem *menuitem, gpointer user_data)
 
263
{
 
264
        create_file_save_as_dialog(".html", write_html_file, TRUE);
 
265
}
 
266
 
 
267
 
 
268
static void write_data(const gchar *filename, const gchar *data)
 
269
{
 
270
        gint error_nr = p_utils->write_file(filename, data);
 
271
        gchar *utf8_filename = p_utils->get_utf8_from_locale(filename);
 
272
 
 
273
        if (error_nr == 0)
 
274
                p_ui->set_statusbar(TRUE, _("Document successfully exported as '%s'."), utf8_filename);
 
275
        else
 
276
                p_ui->set_statusbar(TRUE, _("File '%s' could not be written (%s)."),
 
277
                        utf8_filename, g_strerror(error_nr));
 
278
 
 
279
        g_free(utf8_filename);
 
280
}
 
281
 
 
282
 
 
283
static const gchar *get_date(gint type)
 
284
{
 
285
        static gchar str[128];
 
286
        gchar *format;
 
287
        time_t t;
 
288
        struct tm *tmp;
 
289
 
 
290
        t = time(NULL);
 
291
        tmp = localtime(&t);
 
292
        if (tmp == NULL)
 
293
                return "";
 
294
 
 
295
        if (type == DATE_TYPE_HTML)
 
296
/** needs testing */
 
297
#ifdef _GNU_SOURCE
 
298
                format = "%Y-%m-%dT%H:%M:%S%z";
 
299
#else
 
300
                format = "%Y-%m-%dT%H:%M:%S";
 
301
#endif
 
302
        else
 
303
                format = "%c";
 
304
 
 
305
        strftime(str, sizeof str, format, tmp);
 
306
 
 
307
        return str;
 
308
}
 
309
 
 
310
 
 
311
static void on_file_save_dialog_response(GtkDialog *dialog, gint response, gpointer user_data)
 
312
{
 
313
        ExportInfo *exi = user_data;
 
314
 
 
315
        if (response == GTK_RESPONSE_ACCEPT && exi != NULL)
 
316
        {
 
317
                gchar *new_filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
 
318
                gchar *utf8_filename;
 
319
                gboolean use_zoom_level = FALSE;
 
320
 
 
321
                if (exi->have_zoom_level_checkbox)
 
322
                {
 
323
                        use_zoom_level = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(
 
324
                                p_support->lookup_widget(GTK_WIDGET(dialog), "check_zoom_level")));
 
325
                }
 
326
 
 
327
                utf8_filename = p_utils->get_utf8_from_locale(new_filename);
 
328
 
 
329
                /* check if file exists and ask whether to overwrite or not */
 
330
                if (g_file_test(new_filename, G_FILE_TEST_EXISTS))
 
331
                {
 
332
                        if (p_dialogs->show_question(
 
333
                                _("The file '%s' already exists. Do you want to overwrite it?"),
 
334
                                utf8_filename) == FALSE)
 
335
                                return;
 
336
                }
 
337
 
 
338
                exi->export_func(exi->idx, new_filename, use_zoom_level);
 
339
 
 
340
                g_free(utf8_filename);
 
341
                g_free(new_filename);
 
342
        }
 
343
        g_free(exi);
 
344
        gtk_widget_destroy(GTK_WIDGET(dialog));
 
345
}
 
346
 
 
347
 
 
348
static void write_latex_file(gint idx, const gchar *filename, gboolean use_zoom)
 
349
{
 
350
        gint i, style = -1, old_style = 0, column = 0;
 
351
        gchar c, c_next, *tmp;
 
352
        /* 0 - fore, 1 - back, 2 - bold, 3 - italic, 4 - font size, 5 - used(0/1) */
 
353
        gint styles[STYLE_MAX + 1][MAX_TYPES];
 
354
        gboolean block_open = FALSE;
 
355
        GString *body;
 
356
        GString *cmds;
 
357
        GString *latex;
 
358
        gint style_max = pow(2, p_sci->send_message(doc_list[idx].sci, SCI_GETSTYLEBITS, 0, 0));
 
359
 
 
360
        /* first read all styles from Scintilla */
 
361
        for (i = 0; i < style_max; i++)
 
362
        {
 
363
                styles[i][FORE] = p_sci->send_message(doc_list[idx].sci, SCI_STYLEGETFORE, i, 0);
 
364
                styles[i][BACK] = p_sci->send_message(doc_list[idx].sci, SCI_STYLEGETBACK, i, 0);
 
365
                styles[i][BOLD] = p_sci->send_message(doc_list[idx].sci, SCI_STYLEGETBOLD, i, 0);
 
366
                styles[i][ITALIC] = p_sci->send_message(doc_list[idx].sci, SCI_STYLEGETITALIC, i, 0);
 
367
                styles[i][USED] = 0;
 
368
        }
 
369
 
 
370
        /* read the document and write the LaTeX code */
 
371
        body = g_string_new("");
 
372
        for (i = 0; i < p_sci->get_length(doc_list[idx].sci); i++)
 
373
        {
 
374
                style = p_sci->get_style_at(doc_list[idx].sci, i);
 
375
                c = p_sci->get_char_at(doc_list[idx].sci, i);
 
376
                c_next = p_sci->get_char_at(doc_list[idx].sci, i + 1);
 
377
 
 
378
                if (style != old_style || ! block_open)
 
379
                {
 
380
                        old_style = style;
 
381
                        styles[style][USED] = 1;
 
382
                        if (block_open)
 
383
                        {
 
384
                                g_string_append(body, "}\n");
 
385
                                block_open = FALSE;
 
386
                        }
 
387
                        g_string_append_printf(body, "\\style%s{", get_tex_style(style));
 
388
 
 
389
                        block_open = TRUE;
 
390
                }
 
391
                /* escape the current character if necessary else just add it */
 
392
                switch (c)
 
393
                {
 
394
                        case '\r':
 
395
                        case '\n':
 
396
                        {
 
397
                                if (c == '\r' && c_next == '\n')
 
398
                                        continue; /* when using CR/LF skip CR and add the line break with LF */
 
399
 
 
400
                                if (block_open)
 
401
                                {
 
402
                                        g_string_append(body, "}");
 
403
                                        block_open = FALSE;
 
404
                                }
 
405
                                g_string_append(body, " \\\\\n");
 
406
                                column = -1;
 
407
                                break;
 
408
                        }
 
409
                        case '\t':
 
410
                        {
 
411
                                gint tab_stop = geany_data->editor_prefs->tab_width -
 
412
                                        (column % geany_data->editor_prefs->tab_width);
 
413
 
 
414
                                column += tab_stop - 1; /* -1 because we add 1 at the end of the loop */
 
415
                                g_string_append_printf(body, "\\hspace*{%dem}", tab_stop);
 
416
                                break;
 
417
                        }
 
418
                        case ' ':
 
419
                        {
 
420
                                if (c_next == ' ')
 
421
                                {
 
422
                                        g_string_append(body, "{\\hspace*{1em}}");
 
423
                                        i++; /* skip the next character */
 
424
                                }
 
425
                                else
 
426
                                        g_string_append_c(body, ' ');
 
427
                                break;
 
428
                        }
 
429
                        case '{':
 
430
                        case '}':
 
431
                        case '_':
 
432
                        case '&':
 
433
                        case '$':
 
434
                        case '#':
 
435
                        case '%':
 
436
                        {
 
437
                                g_string_append_printf(body, "\\%c", c);
 
438
                                break;
 
439
                        }
 
440
                        case '\\':
 
441
                        {
 
442
                                g_string_append(body, "\\symbol{92}");
 
443
                                break;
 
444
                        }
 
445
                        case '~':
 
446
                        {
 
447
                                g_string_append(body, "\\symbol{126}");
 
448
                                break;
 
449
                        }
 
450
                        case '^':
 
451
                        {
 
452
                                g_string_append(body, "\\symbol{94}");
 
453
                                break;
 
454
                        }
 
455
                        /** TODO still don't work for "---" or "----" */
 
456
                        case '-':  /* mask "--" */
 
457
                        {
 
458
                                if (c_next == '-')
 
459
                                {
 
460
                                        g_string_append(body, "-\\/-");
 
461
                                        i++; /* skip the next character */
 
462
                                }
 
463
                                else
 
464
                                        g_string_append_c(body, '-');
 
465
 
 
466
                                break;
 
467
                        }
 
468
                        case '<':  /* mask "<<" */
 
469
                        {
 
470
                                if (c_next == '<')
 
471
                                {
 
472
                                        g_string_append(body, "<\\/<");
 
473
                                        i++; /* skip the next character */
 
474
                                }
 
475
                                else
 
476
                                        g_string_append_c(body, '<');
 
477
 
 
478
                                break;
 
479
                        }
 
480
                        case '>':  /* mask ">>" */
 
481
                        {
 
482
                                if (c_next == '>')
 
483
                                {
 
484
                                        g_string_append(body, ">\\/>");
 
485
                                        i++; /* skip the next character */
 
486
                                }
 
487
                                else
 
488
                                        g_string_append_c(body, '>');
 
489
 
 
490
                                break;
 
491
                        }
 
492
                        default: g_string_append_c(body, c);
 
493
                }
 
494
                column++;
 
495
        }
 
496
        if (block_open)
 
497
        {
 
498
                g_string_append(body, "}\n");
 
499
                block_open = FALSE;
 
500
        }
 
501
 
 
502
        /* force writing of style 0 (used at least for line breaks) */
 
503
        styles[0][USED] = 1;
 
504
 
 
505
        /* write used styles in the header */
 
506
        cmds = g_string_new("");
 
507
        for (i = 0; i <= STYLE_MAX; i++)
 
508
        {
 
509
                if (styles[i][USED])
 
510
                {
 
511
                        g_string_append_printf(cmds,
 
512
                                "\\newcommand{\\style%s}[1]{\\noindent{", get_tex_style(i));
 
513
                        if (styles[i][BOLD])
 
514
                                g_string_append(cmds, "\\textbf{");
 
515
                        if (styles[i][ITALIC])
 
516
                                g_string_append(cmds, "\\textit{");
 
517
 
 
518
                        tmp = get_tex_rgb(styles[i][FORE]);
 
519
                        g_string_append_printf(cmds, "\\textcolor[rgb]{%s}{", tmp);
 
520
                        g_free(tmp);
 
521
                        tmp = get_tex_rgb(styles[i][BACK]);
 
522
                        g_string_append_printf(cmds, "\\fcolorbox[rgb]{0, 0, 0}{%s}{", tmp);
 
523
                        g_string_append(cmds, "#1}}");
 
524
                        g_free(tmp);
 
525
 
 
526
                        if (styles[i][BOLD])
 
527
                                g_string_append_c(cmds, '}');
 
528
                        if (styles[i][ITALIC])
 
529
                                g_string_append_c(cmds, '}');
 
530
                        g_string_append(cmds, "}}\n");
 
531
                }
 
532
        }
 
533
 
 
534
        /* write all */
 
535
        latex = g_string_new(TEMPLATE_LATEX);
 
536
        p_utils->string_replace_all(latex, "{export_content}", body->str);
 
537
        p_utils->string_replace_all(latex, "{export_styles}", cmds->str);
 
538
        p_utils->string_replace_all(latex, "{export_date}", get_date(DATE_TYPE_DEFAULT));
 
539
        if (doc_list[idx].file_name == NULL)
 
540
                p_utils->string_replace_all(latex, "{export_filename}", GEANY_STRING_UNTITLED);
 
541
        else
 
542
                p_utils->string_replace_all(latex, "{export_filename}", doc_list[idx].file_name);
 
543
 
 
544
        write_data(filename, latex->str);
 
545
 
 
546
        g_string_free(body, TRUE);
 
547
        g_string_free(cmds, TRUE);
 
548
        g_string_free(latex, TRUE);
 
549
}
 
550
 
 
551
 
 
552
static void write_html_file(gint idx, const gchar *filename, gboolean use_zoom)
 
553
{
 
554
        gint i, style = -1, old_style = 0, column = 0;
 
555
        gchar c, c_next;
 
556
        /* 0 - fore, 1 - back, 2 - bold, 3 - italic, 4 - font size, 5 - used(0/1) */
 
557
        gint styles[STYLE_MAX + 1][MAX_TYPES];
 
558
        gboolean span_open = FALSE;
 
559
        const gchar *font_name;
 
560
        gint font_size;
 
561
        PangoFontDescription *font_desc;
 
562
        GString *body;
 
563
        GString *css;
 
564
        GString *html;
 
565
        gint style_max = pow(2, p_sci->send_message(doc_list[idx].sci, SCI_GETSTYLEBITS, 0, 0));
 
566
 
 
567
        /* first read all styles from Scintilla */
 
568
        for (i = 0; i < style_max; i++)
 
569
        {
 
570
                styles[i][FORE] = ROTATE_RGB(p_sci->send_message(doc_list[idx].sci, SCI_STYLEGETFORE, i, 0));
 
571
                styles[i][BACK] = ROTATE_RGB(p_sci->send_message(doc_list[idx].sci, SCI_STYLEGETBACK, i, 0));
 
572
                styles[i][BOLD] = p_sci->send_message(doc_list[idx].sci, SCI_STYLEGETBOLD, i, 0);
 
573
                styles[i][ITALIC] = p_sci->send_message(doc_list[idx].sci, SCI_STYLEGETITALIC, i, 0);
 
574
                styles[i][USED] = 0;
 
575
        }
 
576
 
 
577
        /* read Geany's font and font size */
 
578
        font_desc = pango_font_description_from_string(prefs->editor_font);
 
579
        font_name = pango_font_description_get_family(font_desc);
 
580
        /*font_size = pango_font_description_get_size(font_desc) / PANGO_SCALE;*/
 
581
        /* take the zoom level also into account */
 
582
        font_size = p_sci->send_message(doc_list[idx].sci, SCI_STYLEGETSIZE, 0, 0);
 
583
        if (use_zoom)
 
584
                font_size += p_sci->send_message(doc_list[idx].sci, SCI_GETZOOM, 0, 0);
 
585
 
 
586
        /* read the document and write the HTML body */
 
587
        body = g_string_new("");
 
588
        for (i = 0; i < p_sci->get_length(doc_list[idx].sci); i++)
 
589
        {
 
590
                style = p_sci->get_style_at(doc_list[idx].sci, i);
 
591
                c = p_sci->get_char_at(doc_list[idx].sci, i);
 
592
                /* p_sci->get_char_at() takes care of index boundaries and return 0 if i is too high */
 
593
                c_next = p_sci->get_char_at(doc_list[idx].sci, i + 1);
 
594
 
 
595
                if ((style != old_style || ! span_open) && ! isspace(c))
 
596
                {
 
597
                        old_style = style;
 
598
                        styles[style][USED] = 1;
 
599
                        if (span_open)
 
600
                        {
 
601
                                g_string_append(body, "</span>");
 
602
                        }
 
603
                        g_string_append_printf(body, "<span class=\"style_%d\">", style);
 
604
 
 
605
                        span_open = TRUE;
 
606
                }
 
607
                /* escape the current character if necessary else just add it */
 
608
                switch (c)
 
609
                {
 
610
                        case '\r':
 
611
                        case '\n':
 
612
                        {
 
613
                                if (c == '\r' && c_next == '\n')
 
614
                                        continue; /* when using CR/LF skip CR and add the line break with LF */
 
615
 
 
616
                                if (span_open)
 
617
                                {
 
618
                                        g_string_append(body, "</span>");
 
619
                                        span_open = FALSE;
 
620
                                }
 
621
                                g_string_append(body, "<br />\n");
 
622
                                column = -1;
 
623
                                break;
 
624
                        }
 
625
                        case '\t':
 
626
                        {
 
627
                                gint j;
 
628
                                gint tab_stop = geany_data->editor_prefs->tab_width -
 
629
                                        (column % geany_data->editor_prefs->tab_width);
 
630
 
 
631
                                column += tab_stop - 1; /* -1 because we add 1 at the end of the loop */
 
632
                                for (j = 0; j < tab_stop; j++)
 
633
                                {
 
634
                                        g_string_append(body, "&nbsp;");
 
635
                                }
 
636
                                break;
 
637
                        }
 
638
                        case ' ':
 
639
                        {
 
640
                                g_string_append(body, "&nbsp;");
 
641
                                break;
 
642
                        }
 
643
                        case '<':
 
644
                        {
 
645
                                g_string_append(body, "&lt;");
 
646
                                break;
 
647
                        }
 
648
                        case '>':
 
649
                        {
 
650
                                g_string_append(body, "&gt;");
 
651
                                break;
 
652
                        }
 
653
                        case '&':
 
654
                        {
 
655
                                g_string_append(body, "&amp;");
 
656
                                break;
 
657
                        }
 
658
                        default: g_string_append_c(body, c);
 
659
                }
 
660
                column++;
 
661
        }
 
662
        if (span_open)
 
663
        {
 
664
                g_string_append(body, "</span>");
 
665
                span_open = FALSE;
 
666
        }
 
667
 
 
668
        /* write used styles in the header */
 
669
        css = g_string_new("");
 
670
        g_string_append_printf(css,
 
671
        "\tbody\n\t{\n\t\tfont-family: %s, monospace;\n\t\tfont-size: %dpt;\n\t}\n",
 
672
                                font_name, font_size);
 
673
 
 
674
        for (i = 0; i <= STYLE_MAX; i++)
 
675
        {
 
676
                if (styles[i][USED])
 
677
                {
 
678
                        g_string_append_printf(css,
 
679
        "\t.style_%d\n\t{\n\t\tcolor: #%06x;\n\t\tbackground-color: #%06x;\n%s%s\t}\n",
 
680
                                i, styles[i][FORE], styles[i][BACK],
 
681
                                (styles[i][BOLD]) ? "\t\tfont-weight: bold;\n" : "",
 
682
                                (styles[i][ITALIC]) ? "\t\tfont-style: italic;\n" : "");
 
683
                }
 
684
        }
 
685
 
 
686
        /* write all */
 
687
        html = g_string_new(TEMPLATE_HTML);
 
688
        p_utils->string_replace_all(html, "{export_date}", get_date(DATE_TYPE_HTML));
 
689
        p_utils->string_replace_all(html, "{export_content}", body->str);
 
690
        p_utils->string_replace_all(html, "{export_styles}", css->str);
 
691
        if (doc_list[idx].file_name == NULL)
 
692
                p_utils->string_replace_all(html, "{export_filename}", GEANY_STRING_UNTITLED);
 
693
        else
 
694
                p_utils->string_replace_all(html, "{export_filename}", doc_list[idx].file_name);
 
695
 
 
696
        write_data(filename, html->str);
 
697
 
 
698
        pango_font_description_free(font_desc);
 
699
        g_string_free(body, TRUE);
 
700
        g_string_free(css, TRUE);
 
701
        g_string_free(html, TRUE);
 
702
}
 
703
 
 
704
 
 
705
void init(GeanyData *data)
 
706
{
 
707
        GtkWidget *menu_export;
 
708
        GtkWidget *menu_export_menu;
 
709
        GtkWidget *menu_create_html;
 
710
        GtkWidget *menu_create_latex;
 
711
 
 
712
        menu_export = gtk_image_menu_item_new_with_mnemonic(_("_Export"));
 
713
        gtk_container_add(GTK_CONTAINER(data->tools_menu), menu_export);
 
714
 
 
715
        menu_export_menu = gtk_menu_new ();
 
716
        gtk_menu_item_set_submenu(GTK_MENU_ITEM(menu_export), menu_export_menu);
 
717
 
 
718
        /* HTML */
 
719
        menu_create_html = gtk_menu_item_new_with_mnemonic(_("As _HTML"));
 
720
        gtk_container_add(GTK_CONTAINER (menu_export_menu), menu_create_html);
 
721
 
 
722
        g_signal_connect((gpointer) menu_create_html, "activate",
 
723
                G_CALLBACK(on_menu_create_html_activate), NULL);
 
724
 
 
725
        /* LaTeX */
 
726
        menu_create_latex = gtk_menu_item_new_with_mnemonic(_("As _LaTeX"));
 
727
        gtk_container_add(GTK_CONTAINER (menu_export_menu), menu_create_latex);
 
728
 
 
729
        g_signal_connect((gpointer) menu_create_latex, "activate",
 
730
                G_CALLBACK(on_menu_create_latex_activate), NULL);
 
731
 
 
732
        /* disable menu_item when there are no documents open */
 
733
        plugin_fields->menu_item = menu_export;
 
734
        plugin_fields->flags = PLUGIN_IS_DOCUMENT_SENSITIVE;
 
735
 
 
736
        gtk_widget_show_all(menu_export);
 
737
}
 
738
 
 
739
 
 
740
void cleanup(void)
 
741
{
 
742
        gtk_widget_destroy(plugin_fields->menu_item);
 
743
}