qulogic/talkatu

Parents 73a756a72f63
Children e613440f1a44
Clean up the HTML serializer and make it smarter so it only handle html tags it cares about
--- a/talkatu/talkatumarkup.c Tue Sep 11 22:47:30 2018 -0500
+++ b/talkatu/talkatumarkup.c Wed Sep 12 00:30:05 2018 -0500
@@ -31,6 +31,18 @@
*/
/******************************************************************************
+ * Helpers
+ *****************************************************************************/
+static gchar *
+talkatu_text_tag_get_name(GtkTextTag *tag) {
+ gchar *name = NULL;
+
+ g_object_get(G_OBJECT(tag), "name", &name, NULL);
+
+ return name;
+}
+
+/******************************************************************************
* HTML
*****************************************************************************/
static gint
@@ -259,11 +271,13 @@
GtkTextTag *tag = GTK_TEXT_TAG(l->data);
if(!gtk_text_iter_starts_tag(pos, tag)) {
- gchar *name = NULL;
+ gchar *name = talkatu_text_tag_get_name(tag);
+ const gchar *html_name = talkatu_tag_name_to_html(name);
+ g_free(name);
- g_object_get(G_OBJECT(tag), "name", &name, NULL);
- g_string_append_printf(str, "<%s>", name);
- g_free(name);
+ if(html_name != NULL) {
+ g_string_append_printf(str, "<%s>", html_name);
+ }
}
}
@@ -279,13 +293,14 @@
GtkTextTag *tag = GTK_TEXT_TAG(l->data);
if(gtk_text_iter_starts_tag(pos, tag)) {
- gchar *name = NULL;
-
- g_object_get(G_OBJECT(tag), "name", &name, NULL);
- g_string_append_printf(str, "<%s>", name);
+ gchar *name = talkatu_text_tag_get_name(tag);
+ const gchar *html_name = talkatu_tag_name_to_html(name);
g_free(name);
- stack = g_slist_prepend(stack, tag);
+ if(html_name != NULL) {
+ g_string_append_printf(str, "<%s>", html_name);
+ stack = g_slist_prepend(stack, tag);
+ }
}
}
@@ -296,13 +311,14 @@
GtkTextTag *tag = GTK_TEXT_TAG(l->data);
if(!gtk_text_iter_has_tag(next, tag)) {
- gchar *name = NULL;
-
- g_object_get(G_OBJECT(tag), "name", &name, NULL);
- g_string_append_printf(str, "</%s>", name);
+ gchar *name = talkatu_text_tag_get_name(tag);
+ const gchar *html_name = talkatu_tag_name_to_html(name);
g_free(name);
- stack = g_slist_remove(stack, tag);
+ if(html_name != NULL) {
+ g_string_append_printf(str, "</%s>", html_name);
+ stack = g_slist_remove(stack, tag);
+ }
}
}
@@ -315,11 +331,13 @@
GtkTextTag *tag = GTK_TEXT_TAG(l->data);
if(!gtk_text_iter_ends_tag(real_end, tag)) {
- gchar *name = NULL;
+ gchar *name = talkatu_text_tag_get_name(tag);
+ const gchar *html_name = talkatu_tag_name_to_html(name);
+ g_free(name);
- g_object_get(G_OBJECT(tag), "name", &name, NULL);
- g_string_append_printf(str, "</%s>", name);
- g_free(name);
+ if(html_name != NULL) {
+ g_string_append_printf(str, "</%s>", html_name);
+ }
}
}
g_slist_free(stack);
--- a/talkatu/talkatutag.c Tue Sep 11 22:47:30 2018 -0500
+++ b/talkatu/talkatutag.c Wed Sep 12 00:30:05 2018 -0500
@@ -151,3 +151,50 @@
return NULL;
}
+
+const gchar *
+talkatu_tag_name_to_html(const gchar *tag_name) {
+ const gchar *talkatu_tag_name = NULL;
+
+ if(tag_name == NULL) {
+ return NULL;
+ }
+
+ if(g_ascii_strncasecmp(TALKATU_TAG_PREFIX, tag_name, TALKATU_TAG_PREFIX_LEN) != 0) {
+ return NULL;
+ }
+
+ talkatu_tag_name = tag_name + TALKATU_TAG_PREFIX_LEN;
+
+ if(g_ascii_strcasecmp(talkatu_tag_name, "bold") == 0) {
+ return "b";
+ } else if(g_ascii_strcasecmp(talkatu_tag_name, "italic") == 0) {
+ return "i";
+ } else if(g_ascii_strcasecmp(talkatu_tag_name, "underline") == 0) {
+ return "u";
+ } else if(g_ascii_strcasecmp(talkatu_tag_name, "strikethrough") == 0) {
+ return "s";
+ } else if(g_ascii_strcasecmp(talkatu_tag_name, "subscript") == 0) {
+ return "sub";
+ } else if(g_ascii_strcasecmp(talkatu_tag_name, "superscript") == 0) {
+ return "sup";
+ } else if(g_ascii_strcasecmp(talkatu_tag_name, "preformatted") == 0) {
+ return "pre";
+ } else if(g_ascii_strcasecmp(talkatu_tag_name, "header1") == 0) {
+ return "h1";
+ } else if(g_ascii_strcasecmp(talkatu_tag_name, "header2") == 0) {
+ return "h2";
+ } else if(g_ascii_strcasecmp(talkatu_tag_name, "header3") == 0) {
+ return "h3";
+ } else if(g_ascii_strcasecmp(talkatu_tag_name, "header4") == 0) {
+ return "h4";
+ } else if(g_ascii_strcasecmp(talkatu_tag_name, "header5") == 0) {
+ return "h5";
+ } else if(g_ascii_strcasecmp(talkatu_tag_name, "header6") == 0) {
+ return "h6";
+ } else if(g_ascii_strcasecmp(talkatu_tag_name, "anchor") == 0) {
+ return "a";
+ }
+
+ return NULL;
+}
--- a/talkatu/talkatutag.h Tue Sep 11 22:47:30 2018 -0500
+++ b/talkatu/talkatutag.h Wed Sep 12 00:30:05 2018 -0500
@@ -30,26 +30,30 @@
#include <gtk/gtk.h>
-#define TALKATU_TAG_BOLD ("b")
-#define TALKATU_TAG_ITALIC ("i")
-#define TALKATU_TAG_UNDERLINE ("u")
-#define TALKATU_TAG_STRIKETHROUGH ("s")
-#define TALKATU_TAG_SUBSCRIPT ("sub")
-#define TALKATU_TAG_SUPERSCRIPT ("sup")
-#define TALKATU_TAG_PRE ("pre")
-#define TALKATU_TAG_CODE ("code")
-#define TALKATU_TAG_SEARCH ("search")
-#define TALKATU_TAG_H1 ("h1")
-#define TALKATU_TAG_H2 ("h2")
-#define TALKATU_TAG_H3 ("h3")
-#define TALKATU_TAG_H4 ("h4")
-#define TALKATU_TAG_H5 ("h5")
-#define TALKATU_TAG_H6 ("h6")
-#define TALKATU_TAG_ANCHOR ("a")
+#define TALKATU_TAG_PREFIX "talkatu:"
+#define TALKATU_TAG_PREFIX_LEN (sizeof(TALKATU_TAG_PREFIX)-1)
+
+#define TALKATU_TAG_BOLD TALKATU_TAG_PREFIX "bold"
+#define TALKATU_TAG_ITALIC TALKATU_TAG_PREFIX "italic"
+#define TALKATU_TAG_UNDERLINE TALKATU_TAG_PREFIX "underline"
+#define TALKATU_TAG_STRIKETHROUGH TALKATU_TAG_PREFIX "strikethrough"
+#define TALKATU_TAG_SUBSCRIPT TALKATU_TAG_PREFIX "subscript"
+#define TALKATU_TAG_SUPERSCRIPT TALKATU_TAG_PREFIX "superscript"
+#define TALKATU_TAG_PRE TALKATU_TAG_PREFIX "preformatted"
+#define TALKATU_TAG_CODE TALKATU_TAG_PREFIX "code"
+#define TALKATU_TAG_SEARCH TALKATU_TAG_PREFIX "search"
+#define TALKATU_TAG_H1 TALKATU_TAG_PREFIX "header1"
+#define TALKATU_TAG_H2 TALKATU_TAG_PREFIX "header2"
+#define TALKATU_TAG_H3 TALKATU_TAG_PREFIX "header3"
+#define TALKATU_TAG_H4 TALKATU_TAG_PREFIX "header4"
+#define TALKATU_TAG_H5 TALKATU_TAG_PREFIX "header5"
+#define TALKATU_TAG_H6 TALKATU_TAG_PREFIX "header6"
+#define TALKATU_TAG_ANCHOR TALKATU_TAG_PREFIX "anchor"
G_BEGIN_DECLS
const gchar *talkatu_tag_name_for_action_name(const gchar *action_name);
+const gchar *talkatu_tag_name_to_html(const gchar *tag_name);
G_END_DECLS
--- a/talkatu/tests/talkatutesthtmlserialization.c Tue Sep 11 22:47:30 2018 -0500
+++ b/talkatu/tests/talkatutesthtmlserialization.c Wed Sep 12 00:30:05 2018 -0500
@@ -103,6 +103,12 @@
talkatu_test_html_serialization
);
+ g_test_add_data_func(
+ "/html/serialization/not-a-tag",
+ "html ",
+ talkatu_test_html_serialization
+ );
+
/* range tests */
d = talkatu_test_html_serialization_data_new(
"<b>bold</b>",