Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simple HTML viewer #735

Merged
merged 8 commits into from
Nov 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions .github/workflows/update-pot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ jobs:
run: |
apt update
apt install -y git meson gcc gettext cmake appstream desktop-file-utils \
libadwaita-1-dev libgtk-4-dev libjson-glib-dev libsoup-3.0-dev libtext-engine-dev \
blueprint-compiler
libadwaita-1-dev libgtk-4-dev libjson-glib-dev libsoup-3.0-dev blueprint-compiler

- name: Setup Repository
run: |
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,17 +107,17 @@ Extension Manager depends on the following libraries:
- libadwaita
- libjson-glib
- libsoup
- libxml2
- [blueprint](https://gitlab.gnome.org/jwestman/blueprint-compiler)
- [text-engine](https://github.com/mjakeman/text-engine/)

On Debian-based distributions, the required dependencies can be installed with the following command:
```shell
sudo apt install blueprint-compiler gettext libadwaita-1-dev libgtk-4-dev libjson-glib-dev libsoup-3.0-dev libtext-engine-dev meson
sudo apt install blueprint-compiler gettext libadwaita-1-dev libgtk-4-dev libjson-glib-dev libsoup-3.0-dev libxml2-dev meson
```

### Building From Source
```shell
meson setup _build
ninja -C _build
ninja install -C _build
meson compile -C _build
meson install -C _build
```
15 changes: 0 additions & 15 deletions build-aux/com.mattjakeman.ExtensionManager.Devel.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,21 +54,6 @@
}
]
},
{
"name" : "text-engine",
"builddir" : true,
"buildsystem" : "meson",
"config-opts" : [
"--libdir=/app/lib"
],
"sources" : [
{
"type" : "git",
"url" : "https://github.com/mjakeman/text-engine.git",
"branch" : "master"
}
]
},
{
"name" : "extension-manager",
"builddir" : true,
Expand Down
15 changes: 0 additions & 15 deletions build-aux/com.mattjakeman.ExtensionManager.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,21 +53,6 @@
}
]
},
{
"name" : "text-engine",
"builddir" : true,
"buildsystem" : "meson",
"config-opts" : [
"--libdir=/app/lib"
],
"sources" : [
{
"type" : "git",
"url" : "https://github.com/mjakeman/text-engine.git",
"branch" : "master"
}
]
},
{
"name" : "extension-manager",
"builddir" : true,
Expand Down
27 changes: 6 additions & 21 deletions src/exm-application.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* exm-application.c
*
* Copyright 2022 Matthew Jakeman
* Copyright 2022-2024 Matthew Jakeman <mjakeman26@outlook.co.nz>
*
* 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
Expand All @@ -14,6 +14,8 @@
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* SPDX-License-Identifier: GPL-3.0-or-later
*/

#include "exm-config.h"
Expand Down Expand Up @@ -42,14 +44,6 @@ exm_application_new (gchar *application_id,
NULL);
}

static void
exm_application_finalize (GObject *object)
{
ExmApplication *self = (ExmApplication *)object;

G_OBJECT_CLASS (exm_application_parent_class)->finalize (object);
}

static ExmWindow *
get_current_window (GApplication *app)
{
Expand Down Expand Up @@ -156,8 +150,6 @@ exm_application_class_init (ExmApplicationClass *klass)
GObjectClass *object_class = G_OBJECT_CLASS (klass);
GApplicationClass *app_class = G_APPLICATION_CLASS (klass);

object_class->finalize = exm_application_finalize;

/*
* We connect to the activate callback to create a window when the application
* has been launched. Additionally, this callback notifies us when the user
Expand Down Expand Up @@ -190,26 +182,19 @@ exm_application_show_about (GSimpleAction *action,
adw_about_dialog_set_comments (ADW_ABOUT_DIALOG (about_dialog), _("Browse, install, and manage GNOME Shell Extensions."));
adw_about_dialog_set_developers (ADW_ABOUT_DIALOG (about_dialog), authors);
adw_about_dialog_set_translator_credits (ADW_ABOUT_DIALOG (about_dialog), _("translator-credits"));
adw_about_dialog_set_copyright (ADW_ABOUT_DIALOG (about_dialog), "© 2022 Matthew Jakeman");

// Dependency Attribution
adw_about_dialog_add_legal_section (ADW_ABOUT_DIALOG (about_dialog),
"text-engine",
"Copyright (C) 2022 Matthew Jakeman",
GTK_LICENSE_MPL_2_0,
NULL);
adw_about_dialog_set_copyright (ADW_ABOUT_DIALOG (about_dialog), "© 2022-2024 Matthew Jakeman");

#if WITH_BACKTRACE
adw_about_dialog_add_legal_section (ADW_ABOUT_DIALOG (about_dialog),
"libbacktrace",
"Copyright (C) 2012-2016 Free Software Foundation, Inc.",
"© 2012-2016 Free Software Foundation, Inc.",
GTK_LICENSE_BSD_3,
NULL);
#endif

adw_about_dialog_add_legal_section (ADW_ABOUT_DIALOG (about_dialog),
"blueprint",
"Copyright (C) 2021 James Westman",
"© 2021 James Westman",
GTK_LICENSE_LGPL_3_0,
NULL);

Expand Down
13 changes: 12 additions & 1 deletion src/exm-comment-tile.blp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,18 @@ template $ExmCommentTile: Gtk.Widget {
}
}

$TextDisplay display {}
Gtk.Label {
styles [
"multiline"
]

label: bind template.comment as <$ExmComment>.comment;
margin-top: 6;
use-markup: true;
wrap: true;
wrap-mode: word_char;
xalign: 0;
}

Gtk.Label date {
styles [
Expand Down
19 changes: 3 additions & 16 deletions src/exm-comment-tile.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@

#include "exm-comment-tile.h"

#include <text-engine/format/import.h>
#include <text-engine/ui/display.h>

#include "exm-rating.h"

struct _ExmCommentTile
Expand All @@ -33,9 +30,8 @@ struct _ExmCommentTile

GtkLabel *author;
GtkLabel *author_badge;
GtkLabel *date;
ExmRating *rating;
TextDisplay *display;
GtkLabel *date;
};

G_DEFINE_FINAL_TYPE (ExmCommentTile, exm_comment_tile, GTK_TYPE_WIDGET)
Expand Down Expand Up @@ -111,24 +107,16 @@ exm_comment_tile_constructed (GObject *object)

g_return_if_fail (EXM_IS_COMMENT (self->comment));

TextDocument *document;
GDateTime *datetime;

gchar *text, *date;
gchar *date;
g_object_get (self->comment,
"comment", &text,
"date", &date,
NULL);

document = text_document_new ();
document->frame = format_parse_html (text);
g_free (text);

datetime = g_date_time_new_from_iso8601 (date, g_time_zone_new_utc ());
g_free (date);

g_object_set (self->display, "document", document, NULL);

if (datetime != NULL)
{
gtk_label_set_label (self->date, g_date_time_format (datetime, "%x"));
Expand Down Expand Up @@ -161,11 +149,10 @@ exm_comment_tile_class_init (ExmCommentTileClass *klass)

gtk_widget_class_set_template_from_resource (widget_class, "/com/mattjakeman/ExtensionManager/exm-comment-tile.ui");

gtk_widget_class_bind_template_child (widget_class, ExmCommentTile, display);
gtk_widget_class_bind_template_child (widget_class, ExmCommentTile, author);
gtk_widget_class_bind_template_child (widget_class, ExmCommentTile, author_badge);
gtk_widget_class_bind_template_child (widget_class, ExmCommentTile, date);
gtk_widget_class_bind_template_child (widget_class, ExmCommentTile, rating);
gtk_widget_class_bind_template_child (widget_class, ExmCommentTile, date);

gtk_widget_class_set_layout_manager_type (widget_class, GTK_TYPE_BIN_LAYOUT);
}
Expand Down
2 changes: 2 additions & 0 deletions src/exm-detail-view.blp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ template $ExmDetailView: Adw.NavigationPage {
header_suffix.orientation: vertical;
header_suffix.spacing: 12;
ext_install.halign: start;
comment_box.min-children-per-line: 1;
}

apply => $breakpoint_apply_cb() swapped;
Expand Down Expand Up @@ -304,6 +305,7 @@ template $ExmDetailView: Adw.NavigationPage {
orientation: vertical;

Gtk.FlowBox comment_box {
min-children-per-line: 2;
max-children-per-line: 2;
homogeneous: true;
selection-mode: none;
Expand Down
84 changes: 83 additions & 1 deletion src/exm-utils.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* exm-utils.c
*
* Copyright 2022 Matthew Jakeman <mjakeman26@outlook.co.nz>
* Copyright 2022-2024 Matthew Jakeman <mjakeman26@outlook.co.nz>
*
* 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
Expand All @@ -20,6 +20,9 @@

#include "exm-utils.h"

#include <libxml/HTMLparser.h>
#include <libxml/HTMLtree.h>

char *
exm_utils_read_resource (const char *resource, gsize *length)
{
Expand Down Expand Up @@ -55,3 +58,82 @@ exm_utils_read_resource (const char *resource, gsize *length)
g_clear_object (&file);
return NULL;
}

static GString *
build_text_recursive (xmlNode *node, GString *string)
{
xmlNode *cur_node = NULL;

for (cur_node = node; cur_node != NULL; cur_node = cur_node->next)
{
// ENTER NODE
if (cur_node->type == XML_ELEMENT_NODE)
{
if (g_str_equal (cur_node->name, "b"))
g_string_append (string, "<b>");
else if (g_str_equal (cur_node->name, "i"))
g_string_append (string, "<i>");
else if (g_str_equal (cur_node->name, "u"))
g_string_append (string, "<u>");
else if (g_str_equal (cur_node->name, "br"))
g_string_append (string, "\n");
else if (g_str_equal (cur_node->name, "p"))
g_string_append (string, "");
else
g_info ("Ignored element: %s\n", cur_node->name);
}
else if (cur_node->type == XML_TEXT_NODE)
{
gchar *escaped_text = g_markup_escape_text ((const gchar *)cur_node->content, -1);
g_string_append (string, escaped_text);
g_free (escaped_text);
}

// PROCESS CHILDREN
build_text_recursive (cur_node->children, string);

// EXIT NODE
if (cur_node->type == XML_ELEMENT_NODE)
{
if (g_str_equal (cur_node->name, "b"))
g_string_append (string, "</b>");
else if (g_str_equal (cur_node->name, "i"))
g_string_append (string, "</i>");
else if (g_str_equal (cur_node->name, "u"))
g_string_append (string, "</u>");
}
}

return string;
}

gchar *
exm_utils_convert_html (const gchar *html)
{
htmlDocPtr doc;
xmlNode *root;
GString *string = g_string_new ("");

doc = htmlParseDoc ((const xmlChar *)html, "UTF-8");
if (doc == NULL)
{
g_critical ("Failed to parse HTML document.");
return NULL;
}

root = xmlDocGetRootElement (doc);
if (root == NULL)
{
g_warning ("Empty HTML document.");
xmlFreeDoc (doc);
xmlCleanupParser ();
return NULL;
}

build_text_recursive (root, string);

xmlFreeDoc (doc);
xmlCleanupParser ();

return g_string_free (string, FALSE);
}
5 changes: 4 additions & 1 deletion src/exm-utils.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* exm-utils.h
*
* Copyright 2022 Matthew Jakeman <mjakeman26@outlook.co.nz>
* Copyright 2022-2024 Matthew Jakeman <mjakeman26@outlook.co.nz>
*
* 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
Expand All @@ -24,3 +24,6 @@

char *
exm_utils_read_resource (const char *resource, gsize *length);

gchar *
exm_utils_convert_html (const gchar *html);
2 changes: 1 addition & 1 deletion src/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ exm_deps = [
dependency('gio-unix-2.0'),
dependency('json-glib-1.0'),
dependency('libsoup-3.0'),
dependency('text-engine-0.1')
dependency('libxml-2.0'),
]

if libbacktrace_dep.found()
Expand Down
Loading