[geany/geany-plugins] b97337: Rework TableConvert-plugin to be a little more generic.

Frank Lanitz git-noreply at geany.org
Thu Jan 31 22:27:42 UTC 2013


Branch:      refs/heads/master
Author:      Frank Lanitz <frank at frank.uvena.de>
Committer:   Frank Lanitz <frank at frank.uvena.de>
Date:        Thu, 31 Jan 2013 22:27:42 UTC
Commit:      b973375af88f3baf08d03deaec176a521f41862f
             https://github.com/geany/geany-plugins/commit/b973375af88f3baf08d03deaec176a521f41862f

Log Message:
-----------
Rework TableConvert-plugin to be a little more generic.


Modified Paths:
--------------
    tableconvert/src/tableconvert.c

Modified: tableconvert/src/tableconvert.c
161 files changed, 121 insertions(+), 40 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,9 +41,126 @@ 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
+};
+
+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\t<tr>\n\t\t<td>", 
+		"</td>\n</tr>\n</tbody>", 
+		"</td>\n\t<td>",
+		"<tr>\n\t<td>",
+		"</td>\n</tr>",
+		"\n",
+		"\n</table>"
+	},
+	/* SQL */
+	{
+		"",   
+		"",   
+		"",   
+		"",   
+		"",   
+		",",  
+		"\t(",
+		")",  
+		",\n",	
+		";"
+	}
+};
 
 static GtkWidget *main_menu_item = NULL;
 
+static GString* convert_to_table_worker(gchar **rows, gboolean header, gint type)
+{
+	guint i;
+	guint j;
+	GString *replacement_str = NULL;
+
+	g_return_val_if_fail(rows != NULL, NULL);
+	
+	/* Adding start of table to replacement */
+	replacement_str = g_string_new(tablerules[type].start);
+
+	/* Adding special header if requested 
+	 * e.g. <thead> */
+	if (header == TRUE)
+	{
+		g_string_append(replacement_str, tablerules[type].header_start);
+	}
+
+	/* 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);
+		
+		if (i == 0 &&
+			header == TRUE)
+		{
+			g_string_append(replacement_str, tablerules[type].header_stop);
+		}
+
+		g_string_append(replacement_str, tablerules[type].linestart);
+		
+		for (j = 0; columns[j] != NULL; j++)
+		{
+			if (j > 0)
+			{
+				g_string_append(replacement_str, tablerules[type].columnsplit);
+			}
+			g_string_append(replacement_str, columns[j]);
+		}
+
+		g_string_append(replacement_str, tablerules[type].lineend);
+		
+		if (rows[i+1] != NULL)
+		{
+			g_string_append(replacement_str, tablerules[type].linesplit);
+		}
+		g_free(columns);
+	}
+	
+	/* Adding the footer of table */
+	
+	g_string_append(replacement_str, tablerules[type].end);
+	return replacement_str;
+}
+
 
 static GString* convert_to_table_html(gchar **rows, gboolean header)
 {
@@ -108,42 +225,6 @@ static GString* convert_to_table_html(gchar **rows, gboolean header)
 	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);
-
-		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, "\\\\\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)
 {
@@ -220,17 +301,17 @@ static void convert_to_table(gboolean header)
 				}
 				case GEANY_FILETYPES_HTML:
 				{
-					replacement_str = convert_to_table_html(rows, header);
+					replacement_str = convert_to_table_worker(rows, header, TC_HTML);
 					break;
 				}
 				case GEANY_FILETYPES_LATEX:
 				{
-					replacement_str = convert_to_table_latex(rows, header);
+					replacement_str = convert_to_table_worker(rows, header, TC_LATEX);
 					break;
 				}
 				case GEANY_FILETYPES_SQL:
 				{
-					replacement_str = convert_to_table_sql(rows);
+					replacement_str = convert_to_table_worker(rows, header, TC_SQL);
 					break;
 				}
 				default:



--------------
This E-Mail was brought to you by github_commit_mail.py (Source: https://github.com/geany/infrastructure).


More information about the Plugins-Commits mailing list