Branch: refs/heads/master Author: Frank Lanitz frank@frank.uvena.de Committer: Frank Lanitz frank@frank.uvena.de Date: Fri, 01 Feb 2013 06:47:18 UTC Commit: e4e387563ab822bc2a5a9ec98cafe09a894b6ea4 https://github.com/geany/geany-plugins/commit/e4e387563ab822bc2a5a9ec98cafe0...
Log Message: ----------- Merge pull request #82 from frlan/Tableconvert_more_generic_rework
Tableconvert more generic rework
Modified Paths: -------------- tableconvert/src/tableconvert.c tableconvert/testfiles/test.html tableconvert/testfiles/test.sql tableconvert/testfiles/test.tex
Modified: tableconvert/src/tableconvert.c 226 files changed, 108 insertions(+), 118 deletions(-) =================================================================== @@ -1,7 +1,7 @@ /* * tableconvert.c * - * Copyright 2011-2012 Frank Lanitz <frank(at)frank(dot)uvena(dot)de> + * Copyright 2011-2013 Frank Lanitz <frank(at)frank(dot)uvena(dot)de> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -41,11 +41,72 @@ enum COUNT_KB };
+typedef struct { + const gchar *start; + const gchar *header_start; + const gchar *header_stop; + const gchar *body_start; + const gchar *body_end; + const gchar *columnsplit; + const gchar *linestart; + const gchar *lineend; + const gchar *linesplit; + const gchar *end; +} TableConvertRule; + +enum { + TC_LATEX = 0, + TC_HTML, + TC_SQL +};
-static GtkWidget *main_menu_item = NULL; +TableConvertRule tablerules[] = { + /* LaTeX */ + { + "\begin{table}[h]\n\begin{tabular}{}\n", + "", + "", + "", + "", + " & ", + "\t", + "\\", + "\n", + "\end{tabular}\n\end{table}" + }, + /* HTML */ + { + "<table>\n", + "<thead>\n", + "</thead>\n", + "<tbody>\n", + "\n</tbody>", + "</td>\n\t<td>", + "<tr>\n\t<td>", + "</td>\n</tr>", + "\n", + "\n</table>" + }, + /* SQL */ + { + "", + "", + "", + "", + "", + ",", + "\t(", + ")", + ",\n", + ";" + } +};
-static GString* convert_to_table_html(gchar **rows, gboolean header) +static GtkWidget *main_menu_item = NULL; + +static gchar* convert_to_table_worker(gchar **rows, gboolean header, + const TableConvertRule *rule) { guint i; guint j; @@ -53,138 +114,62 @@ static GString* convert_to_table_html(gchar **rows, gboolean header)
g_return_val_if_fail(rows != NULL, NULL);
- /* Adding header to replacement */ - replacement_str = g_string_new("<table>\n"); + /* Adding start of table to replacement */ + replacement_str = g_string_new(rule->start);
- /* Adding <thead> if requested */ + /* Adding special header if requested + * e.g. <thead> */ if (header == TRUE) { - g_string_append(replacement_str, "<thead>\n"); + g_string_append(replacement_str, rule->header_start); }
/* Iteration onto rows and building up lines of table for * replacement */ - for (i = 0; rows[i] != NULL ; i++) + for (i = 0; rows[i] != NULL; i++) { gchar **columns = NULL; columns = g_strsplit_set(rows[i], "\t", -1);
- /* Adding <tbody> after first line if header and body - * is requested */ if (i == 1 && header == TRUE) { - g_string_append(replacement_str, "<tbody>\n"); - } - - g_string_append(replacement_str, "\t<tr>\n"); - for (j = 0; columns[j] != NULL; j++) - { - g_string_append(replacement_str, "\t\t<td>"); - g_string_append(replacement_str, columns[j]); - g_string_append(replacement_str, "</td>\n"); - } - - g_string_append(replacement_str, "\t</tr>\n"); - - /* Adding closing </thead> after first row if header - * is requested */ - if (i == 0 && - header == TRUE) - { - g_string_append(replacement_str, "</thead>\n"); + g_string_append(replacement_str, rule->header_stop); + /* We are assuming, that if someone inserts a head, + * only in this case we will insert some special body. + * Might needs to be discussed further */ + g_string_append(replacement_str, rule->body_start); } - g_free(columns); - } - - /* Adding the footer of table */ - /* Closing </tbody> if requested */ - if (header == TRUE) - { - g_string_append(replacement_str, "</tbody>\n"); - }
- g_string_append(replacement_str, "</table>\n"); - return replacement_str; -} - -static GString* convert_to_table_latex(gchar** rows, gboolean header) -{ - guint i; - guint j; - GString *replacement_str = NULL; - - g_return_val_if_fail(rows != NULL, NULL); - - /* Adding header to replacement */ - replacement_str = g_string_new("\begin{table}[h]\n\begin{tabular}{}\n"); - - /* Iteration onto rows and building up lines of table for - * replacement */ - for (i = 0; rows[i] != NULL ; i++) - { - gchar **columns = NULL; - columns = g_strsplit_set(rows[i], "\t", -1); + g_string_append(replacement_str, rule->linestart);
for (j = 0; columns[j] != NULL; j++) { if (j > 0) { - g_string_append(replacement_str, " & "); + g_string_append(replacement_str, rule->columnsplit); } g_string_append(replacement_str, columns[j]); }
- g_string_append(replacement_str, "\\\n"); - - g_free(columns); - } - /* Adding the footer of table */ - - g_string_append(replacement_str, "\end{tabular}\n\end{table}"); - return replacement_str; -} - -static GString* convert_to_table_sql(gchar** rows) -{ - guint i; - guint j; - GString *replacement_str = NULL; - - g_return_val_if_fail(rows != NULL, NULL); - - /* Adding start */ - replacement_str = g_string_new(""); - - /* Iteration onto rows and building up lines for replacement */ - for (i = 0; rows[i] != NULL ; i++) - { - gchar **columns = NULL; - - g_string_append(replacement_str, "\t('"); - columns = g_strsplit_set(rows[i], "\t", -1); - - for (j = 0; columns[j] != NULL; j++) - { - if (j > 0) - { - g_string_append(replacement_str, "','"); - } - g_string_append(replacement_str, columns[j]); - } + g_string_append(replacement_str, rule->lineend);
if (rows[i+1] != NULL) { - g_string_append(replacement_str, "'),\n"); - } - else - { - g_string_append(replacement_str, "')\n"); + g_string_append(replacement_str, rule->linesplit); } + g_strfreev(columns); + }
- g_free(columns); + if (header == TRUE) + { + g_string_append(replacement_str, rule->body_end); } - return replacement_str; + + /* Adding the footer of table */ + g_string_append(replacement_str, rule->end); + + return g_string_free(replacement_str, FALSE); }
static void convert_to_table(gboolean header) @@ -198,7 +183,6 @@ static void convert_to_table(gboolean header) { gchar *selection = NULL; gchar **rows = NULL; - GString *replacement_str = NULL; gchar *replacement = NULL;
/* Actually grabbing selection and splitting it into single @@ -214,57 +198,63 @@ static void convert_to_table(gboolean header) { case GEANY_FILETYPES_NONE: { - g_free(rows); - g_free(replacement); + g_strfreev(rows); return; } case GEANY_FILETYPES_HTML: { - replacement_str = convert_to_table_html(rows, header); + replacement = convert_to_table_worker(rows, + header, + &tablerules[TC_HTML]); break; } case GEANY_FILETYPES_LATEX: { - replacement_str = convert_to_table_latex(rows, header); + replacement = convert_to_table_worker(rows, + header, + &tablerules[TC_LATEX]); break; } case GEANY_FILETYPES_SQL: { - replacement_str = convert_to_table_sql(rows); + replacement = convert_to_table_worker(rows, + header, + &tablerules[TC_SQL]); break; } default: { - replacement_str = NULL; + /* We just don't do anything */ } } /* filetype switch */ } else { /* OK. Something went not as expected. - * We did have a selection but cannot parse it into rows. - * Aborting */ + * We did have a selection but cannot parse it into rows. + * Aborting */ g_warning(_("Something went wrong on parsing selection. Aborting")); return; }
/* The replacement should have been prepared at this point. Let's go - * on and put it into document and replace selection with it. */ - if (replacement_str != NULL) + * on and put it into document and replace selection with it. */ + if (replacement != NULL) { - replacement = g_string_free(replacement_str, FALSE); sci_replace_sel(doc->editor->sci, replacement); } - g_free(rows); + g_strfreev(rows); g_free(replacement); } /* in case of there was no selection we are just doing nothing */ return; }
+ static void kb_convert_to_table(G_GNUC_UNUSED guint key_id) { g_return_if_fail(document_get_current() != NULL); + convert_to_table(TRUE); }
@@ -292,7 +282,7 @@ void plugin_init(GeanyData *data) gtk_container_add(GTK_CONTAINER(geany->main_widgets->tools_menu), main_menu_item); ui_widget_set_tooltip_text(main_menu_item, _("Converts current marked list to a table.")); - g_signal_connect(G_OBJECT(main_menu_item), "activate", G_CALLBACK(convert_to_table), NULL); + g_signal_connect(G_OBJECT(main_menu_item), "activate", G_CALLBACK(cb_table_convert), NULL); gtk_widget_show_all(main_menu_item); ui_add_document_sensitive(main_menu_item); }
Modified: tableconvert/testfiles/test.html 10 files changed, 10 insertions(+), 0 deletions(-) =================================================================== @@ -0,0 +1,10 @@ +aaaa bbbbb ccccc ddddd fffff +aaaa bbbbb ccccc ddddd fffff +aaaa bbbbb ccccc ddddd fffff +aaaa bbbbb ccccc ddddd fffff +aaaa bbbbb ccccc ddddd fffff +aaaa bbbbb ccccc ddddd fffff +aaaa bbbbb ccccc ddddd fffff +aaaa bbbbb ccccc ddddd fffff +aaaa bbbbb ccccc ddddd fffff +aaaa bbbbb ccccc ddddd fffff
Modified: tableconvert/testfiles/test.sql 10 files changed, 10 insertions(+), 0 deletions(-) =================================================================== @@ -0,0 +1,10 @@ +aaaa bbbbb ccccc ddddd fffff +aaaa bbbbb ccccc ddddd fffff +aaaa bbbbb ccccc ddddd fffff +aaaa bbbbb ccccc ddddd fffff +aaaa bbbbb ccccc ddddd fffff +aaaa bbbbb ccccc ddddd fffff +aaaa bbbbb ccccc ddddd fffff +aaaa bbbbb ccccc ddddd fffff +aaaa bbbbb ccccc ddddd fffff +aaaa bbbbb ccccc ddddd fffff
Modified: tableconvert/testfiles/test.tex 10 files changed, 10 insertions(+), 0 deletions(-) =================================================================== @@ -0,0 +1,10 @@ +aaaa bbbbb ccccc ddddd fffff +aaaa bbbbb ccccc ddddd fffff +aaaa bbbbb ccccc ddddd fffff +aaaa bbbbb ccccc ddddd fffff +aaaa bbbbb ccccc ddddd fffff +aaaa bbbbb ccccc ddddd fffff +aaaa bbbbb ccccc ddddd fffff +aaaa bbbbb ccccc ddddd fffff +aaaa bbbbb ccccc ddddd fffff +aaaa bbbbb ccccc ddddd fffff
-------------- This E-Mail was brought to you by github_commit_mail.py (Source: https://github.com/geany/infrastructure).