/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- * * Copyright (C) 2014-2016 Richard Hughes * * Licensed under the GNU Lesser General Public License Version 2.1 * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ /** * SECTION:as-markup * @short_description: Functions for managing AppStream description markup * @include: appstream-glib.h * @stability: Stable * * These functions are used internally to libappstream-glib, and some may be * useful to user-applications. */ #include "config.h" #include #include "as-markup.h" #include "as-node.h" #include "as-utils.h" typedef enum { AS_MARKUP_TAG_UNKNOWN, AS_MARKUP_TAG_PARA, AS_MARKUP_TAG_OL, AS_MARKUP_TAG_UL, AS_MARKUP_TAG_LI, AS_MARKUP_TAG_LAST } AsMarkupTag; typedef struct { AsMarkupTag action; GString *output; GString *temp; } AsMarkupImportHelper; static void as_markup_import_html_flush (AsMarkupImportHelper *helper) { gchar *tmp; guint i; g_auto(GStrv) split = NULL; /* trivial case */ if (helper->action == AS_MARKUP_TAG_UNKNOWN) return; if (helper->temp->len == 0) return; /* split into lines and strip */ split = g_strsplit (helper->temp->str, "\n", -1); for (i = 0; split[i] != NULL; i++) { tmp = g_strstrip (split[i]); if (tmp[0] == '\0') continue; switch (helper->action) { case AS_MARKUP_TAG_PARA: g_string_append_printf (helper->output, "

%s

", tmp); break; case AS_MARKUP_TAG_LI: g_string_append_printf (helper->output, "
  • %s
  • ", tmp); break; default: break; } } g_string_truncate (helper->temp, 0); } static void as_markup_import_html_set_tag (AsMarkupImportHelper *helper, AsMarkupTag action_new) { if (helper->action == AS_MARKUP_TAG_UL && action_new == AS_MARKUP_TAG_LI) { g_string_append (helper->output, ""); helper->action = action_new; } else { helper->action = action_new; } } static void as_markup_import_html_start_cb (GMarkupParseContext *context, const gchar *element_name, const gchar **attribute_names, const gchar **attribute_values, gpointer user_data, GError **error) { AsMarkupImportHelper *helper = (AsMarkupImportHelper *) user_data; /* don't assume the document starts with

    */ if (g_strcmp0 (element_name, "document") == 0 || g_strcmp0 (element_name, "p") == 0) { as_markup_import_html_set_tag (helper, AS_MARKUP_TAG_PARA); return; } if (g_strcmp0 (element_name, "li") == 0) { as_markup_import_html_set_tag (helper, AS_MARKUP_TAG_LI); return; } if (g_strcmp0 (element_name, "ul") == 0) { as_markup_import_html_flush (helper); as_markup_import_html_set_tag (helper, AS_MARKUP_TAG_UL); return; } /* do not include the contents of these tags */ if (g_strcmp0 (element_name, "h1") == 0 || g_strcmp0 (element_name, "h2") == 0) { as_markup_import_html_flush (helper); as_markup_import_html_set_tag (helper, AS_MARKUP_TAG_UNKNOWN); return; } } static void as_markup_import_html_end_cb (GMarkupParseContext *context, const gchar *element_name, gpointer user_data, GError **error) { AsMarkupImportHelper *helper = (AsMarkupImportHelper *) user_data; if (g_strcmp0 (element_name, "document") == 0 || g_strcmp0 (element_name, "p") == 0) { as_markup_import_html_flush (helper); as_markup_import_html_set_tag (helper, AS_MARKUP_TAG_UNKNOWN); return; } /* don't assume the next section starts with

    */ if (g_strcmp0 (element_name, "h1") == 0 || g_strcmp0 (element_name, "h2") == 0) { as_markup_import_html_flush (helper); as_markup_import_html_set_tag (helper, AS_MARKUP_TAG_PARA); return; } if (g_strcmp0 (element_name, "li") == 0) { as_markup_import_html_flush (helper); /* not UL, else we do a new