[gnome-builder/wip/gtk4-port] plugins/html-preview: rewrite html-preview plugin in C



commit 17f0b511871715dd272eb5ffdde62f805b914ba6
Author: Christian Hergert <chergert redhat com>
Date:   Mon Jun 6 12:05:00 2022 -0700

    plugins/html-preview: rewrite html-preview plugin in C
    
    And also drop the markdown/sphinx support from this plugin. They will land
    as new plugins later on.

 src/plugins/html-preview/css/markdown.css          |  961 --------
 .../gbp-html-preview-workspace-addin.c             |  241 ++
 .../gbp-html-preview-workspace-addin.h             |   31 +
 src/plugins/html-preview/gtk/menus.ui              |   14 +-
 src/plugins/html-preview/html-preview-plugin.c     |   37 +
 .../html-preview/html-preview.gresource.xml        |    8 +-
 src/plugins/html-preview/html-preview.plugin       |   14 +-
 src/plugins/html-preview/html_preview.py           |  563 -----
 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, 337 insertions(+), 3917 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..aad1c796b
--- /dev/null
+++ b/src/plugins/html-preview/gbp-html-preview-workspace-addin.c
@@ -0,0 +1,241 @@
+/* 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, {
+  { "live-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, "live-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)
+{
+}
+
+static void
+live_preview_action (GbpHtmlPreviewWorkspaceAddin *self,
+                     GVariant                     *params)
+{
+  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);
+  page = ide_webkit_page_new_for_buffer (GTK_TEXT_BUFFER (buffer));
+  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);
+
+  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 6f4483abe..8b34fb69b 100644
--- a/src/plugins/html-preview/gtk/menus.ui
+++ b/src/plugins/html-preview/gtk/menus.ui
@@ -1,12 +1,10 @@
 <?xml version="1.0"?>
 <interface>
-  <menu id="ide-editor-page-document-menu">
-    <section id="editor-document-section">
-      <item>
-        <attribute name="after">editor-document-open-in-new-frame</attribute>
-        <attribute name="label" translatable="yes">Open Preview</attribute>
-        <attribute name="action">html-preview.preview-as-html</attribute>
-      </item>
-    </section>
+  <menu id="ide-editor-page-preview-section">
+    <item>
+      <attribute name="label" translatable="yes">Open Preview…</attribute>
+      <attribute name="action">html-preview.live-preview</attribute>
+      <attribute name="hidden-when">action-disabled</attribute>
+    </item>
   </menu>
 </interface>
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..1c57072c3 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
+Copyright=Copyright © 2015-2022 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
+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]