[gimp] app: move appstream to pango markup function to gimp-utils.h.
- From: Jehan <jehanp src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gimp] app: move appstream to pango markup function to gimp-utils.h.
- Date: Sun, 20 Feb 2022 17:13:01 +0000 (UTC)
commit 8b2ee06f7172ab5eb48a07e9535d67416aa186cb
Author: Jehan <jehan girinstud io>
Date: Sat Feb 19 22:29:58 2022 +0100
app: move appstream to pango markup function to gimp-utils.h.
I'm going to reuse this code in other parts of the file so make it a
utils function.
While doing so, I'm also improving a tiny bit the formatting of lists.
app/core/gimp-utils.c | 241 +++++++++++++++++++++++++++++++++++++++++++++++
app/core/gimp-utils.h | 2 +
app/core/gimpextension.c | 212 +----------------------------------------
3 files changed, 246 insertions(+), 209 deletions(-)
---
diff --git a/app/core/gimp-utils.c b/app/core/gimp-utils.c
index 14afd88677..671a02ce0c 100644
--- a/app/core/gimp-utils.c
+++ b/app/core/gimp-utils.c
@@ -49,6 +49,8 @@
#include "core-types.h"
+#include "config/gimpxmlparser.h"
+
#include "gimp.h"
#include "gimp-utils.h"
#include "gimpasync.h"
@@ -61,6 +63,39 @@
#define MAX_FUNC 100
+typedef struct
+{
+ GString *text;
+ gint level;
+
+ gboolean numbered_list;
+ gint list_num;
+ gboolean unnumbered_list;
+
+ const gchar *lang;
+ GString *original;
+ gint foreign_level;
+} ParseState;
+
+
+static void appstream_text_start_element (GMarkupParseContext *context,
+ const gchar *element_name,
+ const gchar **attribute_names,
+ const gchar **attribute_values,
+ gpointer user_data,
+ GError **error);
+static void appstream_text_end_element (GMarkupParseContext *context,
+ const gchar *element_name,
+ gpointer user_data,
+ GError **error);
+static void appstream_text_characters (GMarkupParseContext *context,
+ const gchar *text,
+ gsize text_len,
+ gpointer user_data,
+ GError **error);
+static const gchar* gimp_extension_get_tag_lang (const gchar **attribute_names,
+ const gchar **attribute_values);
+
gint
gimp_get_pid (void)
{
@@ -917,6 +952,53 @@ gimp_ascii_strtod (const gchar *nptr,
return TRUE;
}
+gchar *
+gimp_appstream_to_pango_markup (const gchar *as_text)
+{
+ static const GMarkupParser appstream_text_parser =
+ {
+ appstream_text_start_element,
+ appstream_text_end_element,
+ appstream_text_characters,
+ NULL, /* passthrough */
+ NULL /* error */
+ };
+
+ GimpXmlParser *xml_parser;
+ gchar *markup = NULL;
+ GError *error = NULL;
+ ParseState state;
+
+ state.level = 0;
+ state.foreign_level = -1;
+ state.text = g_string_new (NULL);
+ state.numbered_list = FALSE;
+ state.unnumbered_list = FALSE;
+ state.lang = g_getenv ("LANGUAGE");
+ state.original = NULL;
+
+ xml_parser = gimp_xml_parser_new (&appstream_text_parser, &state);
+ if (as_text &&
+ ! gimp_xml_parser_parse_buffer (xml_parser, as_text, -1, &error))
+ {
+ g_printerr ("%s: %s\n", G_STRFUNC, error->message);
+ g_error_free (error);
+ }
+
+ /* Append possibly last original text without proper localization. */
+ if (state.original)
+ {
+ g_string_append (state.text, state.original->str);
+ g_string_free (state.original, TRUE);
+ }
+
+ markup = g_string_free (state.text, FALSE);
+ gimp_xml_parser_free (xml_parser);
+
+ return markup;
+}
+
+
gint
gimp_g_list_compare (GList *list1,
GList *list2)
@@ -1222,3 +1304,162 @@ gimp_create_image_from_buffer (Gimp *gimp,
return image;
}
+
+
+/* Private functions */
+
+static void
+appstream_text_start_element (GMarkupParseContext *context,
+ const gchar *element_name,
+ const gchar **attribute_names,
+ const gchar **attribute_values,
+ gpointer user_data,
+ GError **error)
+{
+ ParseState *state = user_data;
+ GString *output = state->text;
+ const gchar *tag_lang;
+
+ state->level++;
+
+ if (state->foreign_level >= 0)
+ return;
+
+ tag_lang = gimp_extension_get_tag_lang (attribute_names, attribute_values);
+ if ((state->lang == NULL && tag_lang == NULL) ||
+ g_strcmp0 (tag_lang, state->lang) == 0)
+ {
+ /* Current tag is our current language. */
+ if (state->original)
+ g_string_free (state->original, TRUE);
+ state->original = NULL;
+
+ output = state->text;
+ }
+ else if (tag_lang == NULL)
+ {
+ /* Current tag is the original language (and we want a
+ * localization).
+ */
+ if (state->original)
+ {
+ g_string_append (state->text, state->original->str);
+ g_string_free (state->original, TRUE);
+ }
+ state->original = g_string_new (NULL);
+
+ output = state->original;
+ }
+ else
+ {
+ /* Current tag is an unrelated language */
+ state->foreign_level = state->level;
+ return;
+ }
+
+ if ((state->numbered_list || state->unnumbered_list) &&
+ (g_strcmp0 (element_name, "ul") == 0 ||
+ g_strcmp0 (element_name, "ol") == 0))
+ {
+ g_set_error (error, GIMP_ERROR, GIMP_FAILED,
+ _("This parser does not support imbricated lists."));
+ }
+ else if (g_strcmp0 (element_name, "ul") == 0)
+ {
+ state->unnumbered_list = TRUE;
+ }
+ else if (g_strcmp0 (element_name, "ol") == 0)
+ {
+ state->numbered_list = TRUE;
+ state->list_num = 0;
+ }
+ else if (g_strcmp0 (element_name, "li") == 0)
+ {
+ state->list_num++;
+ if (state->numbered_list)
+ g_string_append_printf (output,
+ "\n<span weight='ultrabold' >\xe2\x9d\xa8%d\xe2\x9d\xa9</span> ",
+ state->list_num);
+ else if (state->unnumbered_list)
+ g_string_append (output, "\n<span weight='ultrabold' >\xe2\x80\xa2</span> ");
+ else
+ g_set_error (error, GIMP_ERROR, GIMP_FAILED,
+ _("<li> must be inside <ol> or <ul> tags."));
+ }
+ else if (g_strcmp0 (element_name, "p") != 0)
+ {
+ g_set_error (error, GIMP_ERROR, GIMP_FAILED,
+ _("Unknown tag <%s>."), element_name);
+ }
+}
+
+static void
+appstream_text_end_element (GMarkupParseContext *context,
+ const gchar *element_name,
+ gpointer user_data,
+ GError **error)
+{
+ ParseState *state = user_data;
+
+ state->level--;
+
+ if (g_strcmp0 (element_name, "p") == 0)
+ {
+ if (state->foreign_level < 0)
+ {
+ if (state->original)
+ g_string_append (state->original, "\n\n");
+ else
+ g_string_append (state->text, "\n\n");
+ }
+ }
+ else if (g_strcmp0 (element_name, "ul") == 0 ||
+ g_strcmp0 (element_name, "ol") == 0)
+ {
+ state->numbered_list = FALSE;
+ state->unnumbered_list = FALSE;
+ }
+ else if (g_strcmp0 (element_name, "li") == 0)
+ {
+ g_string_append (state->text, "\n");
+ }
+
+ if (state->foreign_level > state->level)
+ state->foreign_level = -1;
+}
+
+static void
+appstream_text_characters (GMarkupParseContext *context,
+ const gchar *text,
+ gsize text_len,
+ gpointer user_data,
+ GError **error)
+{
+ ParseState *state = user_data;
+
+ if (state->foreign_level < 0)
+ {
+ if (state->original)
+ g_string_append (state->original, text);
+ else
+ g_string_append (state->text, text);
+ }
+}
+
+static const gchar *
+gimp_extension_get_tag_lang (const gchar **attribute_names,
+ const gchar **attribute_values)
+{
+ while (*attribute_names)
+ {
+ if (! strcmp (*attribute_names, "xml:lang"))
+ {
+ return *attribute_values;
+ }
+
+ attribute_names++;
+ attribute_values++;
+ }
+
+ return NULL;
+}
diff --git a/app/core/gimp-utils.h b/app/core/gimp-utils.h
index 371c42b920..4523b9247c 100644
--- a/app/core/gimp-utils.h
+++ b/app/core/gimp-utils.h
@@ -101,6 +101,8 @@ gboolean gimp_ascii_strtod (const gchar *nptr,
gchar **endptr,
gdouble *result);
+gchar * gimp_appstream_to_pango_markup (const gchar *as_text);
+
gint gimp_g_list_compare (GList *list1,
GList *list2);
diff --git a/app/core/gimpextension.c b/app/core/gimpextension.c
index 7a1892f213..b0df6b83d1 100644
--- a/app/core/gimpextension.c
+++ b/app/core/gimpextension.c
@@ -27,8 +27,7 @@
#include "core-types.h"
-#include "config/gimpxmlparser.h"
-
+#include "gimp-utils.h"
#include "gimperror.h"
#include "gimpextension.h"
#include "gimpextension-error.h"
@@ -100,24 +99,6 @@ static GList * gimp_extension_validate_paths (GimpExtension *extension,
gboolean as_directories,
GError **error);
-static void appstream_text_start_element (GMarkupParseContext *context,
- const gchar *element_name,
- const gchar **attribute_names,
- const gchar **attribute_values,
- gpointer user_data,
- GError **error);
-static void appstream_text_end_element (GMarkupParseContext *context,
- const gchar *element_name,
- gpointer user_data,
- GError **error);
-static void appstream_text_characters (GMarkupParseContext *context,
- const gchar *text,
- gsize text_len,
- gpointer user_data,
- GError **error);
-static const gchar* gimp_extension_get_tag_lang (const gchar **attribute_names,
- const gchar **attribute_values);
-
G_DEFINE_TYPE_WITH_PRIVATE (GimpExtension, gimp_extension, GIMP_TYPE_OBJECT)
@@ -337,49 +318,13 @@ gimp_extension_get_path (GimpExtension *extension)
gchar *
gimp_extension_get_markup_description (GimpExtension *extension)
{
- static const GMarkupParser appstream_text_parser =
- {
- appstream_text_start_element,
- appstream_text_end_element,
- appstream_text_characters,
- NULL, /* passthrough */
- NULL /* error */
- };
const gchar *description;
- GimpXmlParser *xml_parser;
- gchar *markup = NULL;
- GError *error = NULL;
- ParseState state;
-
- state.level = 0;
- state.foreign_level = -1;
- state.text = g_string_new (NULL);
- state.numbered_list = FALSE;
- state.unnumbered_list = FALSE;
- state.lang = g_getenv ("LANGUAGE");
- state.original = NULL;
+ g_return_val_if_fail (GIMP_IS_EXTENSION (extension), NULL);
- xml_parser = gimp_xml_parser_new (&appstream_text_parser, &state);
description = gimp_extension_get_description (extension);
- if (description &&
- ! gimp_xml_parser_parse_buffer (xml_parser, description, -1, &error))
- {
- g_printerr ("%s: %s\n", G_STRFUNC, error->message);
- g_error_free (error);
- }
-
- /* Append possibly last original text without proper localization. */
- if (state.original)
- {
- g_string_append (state.text, state.original->str);
- g_string_free (state.original, TRUE);
- }
-
- markup = g_string_free (state.text, FALSE);
- gimp_xml_parser_free (xml_parser);
- return markup;
+ return gimp_appstream_to_pango_markup (description);
}
gboolean
@@ -891,154 +836,3 @@ gimp_extension_validate_paths (GimpExtension *extension,
return list;
}
-
-static void
-appstream_text_start_element (GMarkupParseContext *context,
- const gchar *element_name,
- const gchar **attribute_names,
- const gchar **attribute_values,
- gpointer user_data,
- GError **error)
-{
- ParseState *state = user_data;
- GString *output = state->text;
- const gchar *tag_lang;
-
- state->level++;
-
- if (state->foreign_level >= 0)
- return;
-
- tag_lang = gimp_extension_get_tag_lang (attribute_names, attribute_values);
- if ((state->lang == NULL && tag_lang == NULL) ||
- g_strcmp0 (tag_lang, state->lang) == 0)
- {
- /* Current tag is our current language. */
- if (state->original)
- g_string_free (state->original, TRUE);
- state->original = NULL;
-
- output = state->text;
- }
- else if (tag_lang == NULL)
- {
- /* Current tag is the original language (and we want a
- * localization).
- */
- if (state->original)
- {
- g_string_append (state->text, state->original->str);
- g_string_free (state->original, TRUE);
- }
- state->original = g_string_new (NULL);
-
- output = state->original;
- }
- else
- {
- /* Current tag is an unrelated language */
- state->foreign_level = state->level;
- return;
- }
-
- if ((state->numbered_list || state->unnumbered_list) &&
- (g_strcmp0 (element_name, "ul") == 0 ||
- g_strcmp0 (element_name, "ol") == 0))
- {
- g_set_error (error, GIMP_ERROR, GIMP_FAILED,
- _("This parser does not support imbricated lists."));
- }
- else if (g_strcmp0 (element_name, "ul") == 0)
- {
- state->unnumbered_list = TRUE;
- }
- else if (g_strcmp0 (element_name, "ol") == 0)
- {
- state->numbered_list = TRUE;
- state->list_num = 0;
- }
- else if (g_strcmp0 (element_name, "li") == 0)
- {
- state->list_num++;
- if (state->numbered_list)
- g_string_append_printf (output, "\n %d. ",
- state->list_num);
- else if (state->unnumbered_list)
- g_string_append (output, "\n * ");
- else
- g_set_error (error, GIMP_ERROR, GIMP_FAILED,
- _("<li> must be inside <ol> or <ul> tags."));
- }
- else if (g_strcmp0 (element_name, "p") != 0)
- {
- g_set_error (error, GIMP_ERROR, GIMP_FAILED,
- _("Unknown tag <%s>."), element_name);
- }
-}
-
-static void
-appstream_text_end_element (GMarkupParseContext *context,
- const gchar *element_name,
- gpointer user_data,
- GError **error)
-{
- ParseState *state = user_data;
-
- state->level--;
-
- if (g_strcmp0 (element_name, "p") == 0)
- {
- if (state->foreign_level < 0)
- {
- if (state->original)
- g_string_append (state->original, "\n\n");
- else
- g_string_append (state->text, "\n\n");
- }
- }
- else if (g_strcmp0 (element_name, "ul") == 0 ||
- g_strcmp0 (element_name, "ol") == 0)
- {
- state->numbered_list = FALSE;
- state->unnumbered_list = FALSE;
- }
-
- if (state->foreign_level > state->level)
- state->foreign_level = -1;
-}
-
-static void
-appstream_text_characters (GMarkupParseContext *context,
- const gchar *text,
- gsize text_len,
- gpointer user_data,
- GError **error)
-{
- ParseState *state = user_data;
-
- if (state->foreign_level < 0)
- {
- if (state->original)
- g_string_append (state->original, text);
- else
- g_string_append (state->text, text);
- }
-}
-
-static const gchar *
-gimp_extension_get_tag_lang (const gchar **attribute_names,
- const gchar **attribute_values)
-{
- while (*attribute_names)
- {
- if (! strcmp (*attribute_names, "xml:lang"))
- {
- return *attribute_values;
- }
-
- attribute_names++;
- attribute_values++;
- }
-
- return NULL;
-}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]