[gnome-builder] plugins/html-preview: port to C
- From: Christian Hergert <chergert src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-builder] plugins/html-preview: port to C
- Date: Tue, 12 Jul 2022 06:39:17 +0000 (UTC)
commit 228c13413f7decae300b603d725d11b90b088757
Author: Christian Hergert <chergert redhat com>
Date: Mon Jul 11 22:55:30 2022 -0700
plugins/html-preview: port to C
- Port to C
- Port to GTK 4
- Remove markdown/rst/shpinx support from this plugin (they will be
part of separate plugins)
- Use IdeWebkitPage for display
src/plugins/html-preview/css/markdown.css | 961 --------
.../gbp-html-preview-workspace-addin.c | 245 ++
.../gbp-html-preview-workspace-addin.h | 31 +
src/plugins/html-preview/gtk/menus.ui | 11 +-
src/plugins/html-preview/html-preview-plugin.c | 37 +
.../html-preview/html-preview.gresource.xml | 8 +-
src/plugins/html-preview/html-preview.plugin | 16 +-
src/plugins/html-preview/html_preview.py | 574 -----
src/plugins/html-preview/js/markdown-view.js | 14 -
src/plugins/html-preview/js/marked.js | 2344 --------------------
src/plugins/html-preview/meson.build | 27 +-
11 files changed, 342 insertions(+), 3926 deletions(-)
---
diff --git a/src/plugins/html-preview/gbp-html-preview-workspace-addin.c
b/src/plugins/html-preview/gbp-html-preview-workspace-addin.c
new file mode 100644
index 000000000..2cd2b095f
--- /dev/null
+++ b/src/plugins/html-preview/gbp-html-preview-workspace-addin.c
@@ -0,0 +1,245 @@
+/* gbp-html-preview-workspace-addin.c
+ *
+ * Copyright 2022 Christian Hergert <chergert redhat com>
+ *
+ * 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
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program 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 General Public License for more details.
+ *
+ * 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
+ */
+
+#define G_LOG_DOMAIN "gbp-html-preview-workspace-addin"
+
+#include "config.h"
+
+#include <libide-code.h>
+#include <libide-editor.h>
+#include <libide-gui.h>
+#include <libide-webkit.h>
+
+#include "gbp-html-preview-workspace-addin.h"
+
+struct _GbpHtmlPreviewWorkspaceAddin
+{
+ GObject parent_instance;
+ IdeWorkspace *workspace;
+ GSignalGroup *buffer_signals;
+ IdeEditorPage *editor_page;
+};
+
+static GHashTable *known_languages;
+
+static void live_preview_action (GbpHtmlPreviewWorkspaceAddin *self,
+ GVariant *params);
+
+IDE_DEFINE_ACTION_GROUP (GbpHtmlPreviewWorkspaceAddin, gbp_html_preview_workspace_addin, {
+ { "html-preview", live_preview_action },
+})
+
+static void
+gbp_html_preview_workspace_addin_set_language (GbpHtmlPreviewWorkspaceAddin *self,
+ const char *language_id)
+{
+ gboolean enabled;
+
+ g_assert (GBP_IS_HTML_PREVIEW_WORKSPACE_ADDIN (self));
+
+ IDE_TRACE_MSG ("Switching language-id to %s", language_id ? language_id : "NULL");
+
+ enabled = language_id != NULL &&
+ g_hash_table_contains (known_languages, language_id);
+
+ gbp_html_preview_workspace_addin_set_action_enabled (self, "html-preview", enabled);
+}
+
+static void
+gbp_html_preview_workspace_addin_page_changed (IdeWorkspaceAddin *addin,
+ IdePage *page)
+{
+ GbpHtmlPreviewWorkspaceAddin *self = (GbpHtmlPreviewWorkspaceAddin *)addin;
+ IdeBuffer *buffer = NULL;
+
+ IDE_ENTRY;
+
+ g_assert (GBP_IS_HTML_PREVIEW_WORKSPACE_ADDIN (self));
+ g_assert (!page || IDE_IS_PAGE (page));
+
+ self->editor_page = NULL;
+
+ if (IDE_IS_EDITOR_PAGE (page))
+ {
+ self->editor_page = IDE_EDITOR_PAGE (page);
+ buffer = ide_editor_page_get_buffer (self->editor_page);
+ }
+
+ g_signal_group_set_target (self->buffer_signals, buffer);
+
+ IDE_EXIT;
+}
+
+static void
+gbp_html_preview_workspace_addin_notify_language_id (GbpHtmlPreviewWorkspaceAddin *self,
+ GParamSpec *pspec,
+ IdeBuffer *buffer)
+{
+ g_assert (GBP_IS_HTML_PREVIEW_WORKSPACE_ADDIN (self));
+ g_assert (IDE_IS_BUFFER (buffer));
+
+ gbp_html_preview_workspace_addin_set_language (self, ide_buffer_get_language_id (buffer));
+}
+
+static void
+gbp_html_preview_workspace_addin_bind (GbpHtmlPreviewWorkspaceAddin *self,
+ IdeBuffer *buffer,
+ GSignalGroup *signal_group)
+{
+ g_assert (GBP_IS_HTML_PREVIEW_WORKSPACE_ADDIN (self));
+ g_assert (IDE_IS_BUFFER (buffer));
+ g_assert (G_IS_SIGNAL_GROUP (signal_group));
+
+ gbp_html_preview_workspace_addin_set_language (self, ide_buffer_get_language_id (buffer));
+}
+
+static void
+gbp_html_preview_workspace_addin_unbind (GbpHtmlPreviewWorkspaceAddin *self,
+ GSignalGroup *signal_group)
+{
+ g_assert (GBP_IS_HTML_PREVIEW_WORKSPACE_ADDIN (self));
+ g_assert (G_IS_SIGNAL_GROUP (signal_group));
+
+ gbp_html_preview_workspace_addin_set_language (self, NULL);
+}
+
+static void
+gbp_html_preview_workspace_addin_load (IdeWorkspaceAddin *addin,
+ IdeWorkspace *workspace)
+{
+ GbpHtmlPreviewWorkspaceAddin *self = (GbpHtmlPreviewWorkspaceAddin *)addin;
+
+ IDE_ENTRY;
+
+ g_assert (GBP_IS_HTML_PREVIEW_WORKSPACE_ADDIN (self));
+ g_assert (IDE_IS_WORKSPACE (workspace));
+
+ self->workspace = workspace;
+
+ gtk_widget_insert_action_group (GTK_WIDGET (workspace),
+ "html-preview",
+ G_ACTION_GROUP (self));
+
+ self->buffer_signals = g_signal_group_new (IDE_TYPE_BUFFER);
+ g_signal_connect_object (self->buffer_signals,
+ "bind",
+ G_CALLBACK (gbp_html_preview_workspace_addin_bind),
+ self,
+ G_CONNECT_SWAPPED);
+ g_signal_connect_object (self->buffer_signals,
+ "unbind",
+ G_CALLBACK (gbp_html_preview_workspace_addin_unbind),
+ self,
+ G_CONNECT_SWAPPED);
+ g_signal_group_connect_object (self->buffer_signals,
+ "notify::language-id",
+ G_CALLBACK (gbp_html_preview_workspace_addin_notify_language_id),
+ self,
+ G_CONNECT_SWAPPED);
+
+ IDE_EXIT;
+}
+
+static void
+gbp_html_preview_workspace_addin_unload (IdeWorkspaceAddin *addin,
+ IdeWorkspace *workspace)
+{
+ GbpHtmlPreviewWorkspaceAddin *self = (GbpHtmlPreviewWorkspaceAddin *)addin;
+
+ IDE_ENTRY;
+
+ g_assert (IDE_IS_MAIN_THREAD ());
+ g_assert (GBP_IS_HTML_PREVIEW_WORKSPACE_ADDIN (self));
+ g_assert (IDE_IS_WORKSPACE (workspace));
+
+ gtk_widget_insert_action_group (GTK_WIDGET (workspace), "html-preview", NULL);
+
+ g_clear_object (&self->buffer_signals);
+
+ self->editor_page = NULL;
+ self->workspace = NULL;
+
+ IDE_EXIT;
+}
+
+static void
+workspace_addin_iface_init (IdeWorkspaceAddinInterface *iface)
+{
+ iface->load = gbp_html_preview_workspace_addin_load;
+ iface->unload = gbp_html_preview_workspace_addin_unload;
+ iface->page_changed = gbp_html_preview_workspace_addin_page_changed;
+}
+
+G_DEFINE_FINAL_TYPE_WITH_CODE (GbpHtmlPreviewWorkspaceAddin, gbp_html_preview_workspace_addin, G_TYPE_OBJECT,
+ G_IMPLEMENT_INTERFACE (IDE_TYPE_WORKSPACE_ADDIN, workspace_addin_iface_init)
+ G_IMPLEMENT_INTERFACE (G_TYPE_ACTION_GROUP,
gbp_html_preview_workspace_addin_init_action_group))
+
+static void
+gbp_html_preview_workspace_addin_class_init (GbpHtmlPreviewWorkspaceAddinClass *klass)
+{
+ known_languages = g_hash_table_new (g_str_hash, g_str_equal);
+#define ADD_LANGUAGE(name) \
+ g_hash_table_insert (known_languages, \
+ (char *)g_intern_static_string (name), \
+ NULL)
+ ADD_LANGUAGE ("erb-html");
+ ADD_LANGUAGE ("html");
+#undef ADD_LANGUAGE
+}
+
+static void
+gbp_html_preview_workspace_addin_init (GbpHtmlPreviewWorkspaceAddin *self)
+{
+ gbp_html_preview_workspace_addin_set_action_enabled (self, "html-preview", FALSE);
+}
+
+static void
+live_preview_action (GbpHtmlPreviewWorkspaceAddin *self,
+ GVariant *params)
+{
+ g_autoptr(IdeHtmlGenerator) generator = NULL;
+ g_autoptr(IdePanelPosition) position = NULL;
+ g_autoptr(IdeBuffer) buffer = NULL;
+ IdeWebkitPage *page;
+ guint column = 0;
+
+ IDE_ENTRY;
+
+ g_assert (IDE_IS_MAIN_THREAD ());
+ g_assert (GBP_IS_HTML_PREVIEW_WORKSPACE_ADDIN (self));
+ g_assert (IDE_IS_WORKSPACE (self->workspace));
+ g_assert (IDE_IS_EDITOR_PAGE (self->editor_page));
+
+ buffer = g_signal_group_dup_target (self->buffer_signals);
+ generator = ide_html_generator_new_for_buffer (GTK_TEXT_BUFFER (buffer));
+ page = ide_webkit_page_new_for_generator (generator);
+ position = ide_page_get_position (IDE_PAGE (self->editor_page));
+
+ if (!ide_panel_position_get_column (position, &column))
+ column = 0;
+
+ ide_panel_position_set_column (position, column + 1);
+ ide_panel_position_set_depth (position, 0);
+
+ ide_workspace_add_page (self->workspace, IDE_PAGE (page), position);
+ panel_widget_raise (PANEL_WIDGET (page));
+
+ IDE_EXIT;
+}
diff --git a/src/plugins/html-preview/gbp-html-preview-workspace-addin.h
b/src/plugins/html-preview/gbp-html-preview-workspace-addin.h
new file mode 100644
index 000000000..0ff8a5914
--- /dev/null
+++ b/src/plugins/html-preview/gbp-html-preview-workspace-addin.h
@@ -0,0 +1,31 @@
+/* gbp-html-preview-workspace-addin.h
+ *
+ * Copyright 2022 Christian Hergert <chergert redhat com>
+ *
+ * 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
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program 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 General Public License for more details.
+ *
+ * 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
+ */
+
+#pragma once
+
+#include <glib-object.h>
+
+G_BEGIN_DECLS
+
+#define GBP_TYPE_HTML_PREVIEW_WORKSPACE_ADDIN (gbp_html_preview_workspace_addin_get_type())
+
+G_DECLARE_FINAL_TYPE (GbpHtmlPreviewWorkspaceAddin, gbp_html_preview_workspace_addin, GBP,
HTML_PREVIEW_WORKSPACE_ADDIN, GObject)
+
+G_END_DECLS
diff --git a/src/plugins/html-preview/gtk/menus.ui b/src/plugins/html-preview/gtk/menus.ui
index 729b91e0d..2c2e89e1a 100644
--- a/src/plugins/html-preview/gtk/menus.ui
+++ b/src/plugins/html-preview/gtk/menus.ui
@@ -1,11 +1,12 @@
<?xml version="1.0"?>
<interface>
- <menu id="ide-editor-page-document-menu">
- <section id="editor-document-section">
+ <menu id="ide-editor-page-menu">
+ <section id="ide-editor-page-preview-section">
<item>
- <attribute name="after">editor-document-open-in-new-frame</attribute>
- <attribute name="label" translatable="yes">Open Preview</attribute>
- <attribute name="action">editor-page.preview-as-html</attribute>
+ <attribute name="id">html-preview-item</attribute>
+ <attribute name="label" translatable="yes">Open Preview…</attribute>
+ <attribute name="action">html-preview.html-preview</attribute>
+ <attribute name="hidden-when">action-disabled</attribute>
</item>
</section>
</menu>
diff --git a/src/plugins/html-preview/html-preview-plugin.c b/src/plugins/html-preview/html-preview-plugin.c
new file mode 100644
index 000000000..3b8ed1632
--- /dev/null
+++ b/src/plugins/html-preview/html-preview-plugin.c
@@ -0,0 +1,37 @@
+/* html-preview-plugin.c
+ *
+ * Copyright 2022 Christian Hergert <chergert redhat com>
+ *
+ * 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
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program 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 General Public License for more details.
+ *
+ * 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
+ */
+
+#define G_LOG_DOMAIN "html-preview-plugin"
+
+#include "config.h"
+
+#include <libpeas/peas.h>
+
+#include <libide-gui.h>
+
+#include "gbp-html-preview-workspace-addin.h"
+
+_IDE_EXTERN void
+_gbp_html_preview_register_types (PeasObjectModule *module)
+{
+ peas_object_module_register_extension_type (module,
+ IDE_TYPE_WORKSPACE_ADDIN,
+ GBP_TYPE_HTML_PREVIEW_WORKSPACE_ADDIN);
+}
diff --git a/src/plugins/html-preview/html-preview.gresource.xml
b/src/plugins/html-preview/html-preview.gresource.xml
index 0fb4f242c..ba92b0a55 100644
--- a/src/plugins/html-preview/html-preview.gresource.xml
+++ b/src/plugins/html-preview/html-preview.gresource.xml
@@ -1,9 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<gresources>
- <gresource prefix="/plugins/html_preview">
- <file>js/markdown-view.js</file>
- <file>js/marked.js</file>
- <file>css/markdown.css</file>
- <file>gtk/menus.ui</file>
+ <gresource prefix="/plugins/html-preview">
+ <file>html-preview.plugin</file>
+ <file preprocess="xml-stripblanks">gtk/menus.ui</file>
</gresource>
</gresources>
diff --git a/src/plugins/html-preview/html-preview.plugin b/src/plugins/html-preview/html-preview.plugin
index 6a1e9c106..0a26c739a 100644
--- a/src/plugins/html-preview/html-preview.plugin
+++ b/src/plugins/html-preview/html-preview.plugin
@@ -1,12 +1,10 @@
[Plugin]
Authors=Christian Hergert <christian hergert me>
Builtin=true
-Copyright=Copyright © 2015 Christian Hergert
-Depends=webkit;
-Description=Live preview of HTML, reStructuredText and Markdown documents.
-Loader=python3
-Module=html_preview
-Name=HTML, reStructuredText and Markdown Preview
-X-Editor-View-Languages=*
-X-Builder-ABI=@PACKAGE_ABI@
-X-Has-Resources=true
+Copyright=Copyright © 2015-2022 Christian Hergert
+Depends=editorui;webkit;
+Description=Live preview of HTML documents
+Embedded=_gbp_html_preview_register_types
+Module=html-preview
+Name=HTML Preview
+X-Workspace-Kind=primary;editor;
diff --git a/src/plugins/html-preview/meson.build b/src/plugins/html-preview/meson.build
index 4810516d0..4d2f3b9f4 100644
--- a/src/plugins/html-preview/meson.build
+++ b/src/plugins/html-preview/meson.build
@@ -1,21 +1,20 @@
if get_option('plugin_html_preview')
-html_preview_resources = gnome.compile_resources(
- 'html_preview',
- 'html-preview.gresource.xml',
- gresource_bundle: true,
- install: true,
- install_dir: plugindir,
-)
+if not get_option('webkit').enabled()
+ error('-Dwebkit=enabled is required for html-preview plugin')
+endif
-install_data('html_preview.py', install_dir: plugindir)
+plugins_sources += files([
+ 'html-preview-plugin.c',
+ 'gbp-html-preview-workspace-addin.c',
+])
-configure_file(
- input: 'html-preview.plugin',
- output: 'html-preview.plugin',
- configuration: config_h,
- install: true,
- install_dir: plugindir,
+plugin_html_preview_resources = gnome.compile_resources(
+ 'html-preview-resources',
+ 'html-preview.gresource.xml',
+ c_name: 'gbp_html_preview'
)
+plugins_sources += plugin_html_preview_resources
+
endif
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]