[gnome-builder/wip/gtk4-port: 1390/1774] plugins/sphinx-preview: add preview for reStructuredText
- From: Christian Hergert <chergert src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-builder/wip/gtk4-port: 1390/1774] plugins/sphinx-preview: add preview for reStructuredText
- Date: Mon, 11 Jul 2022 22:31:44 +0000 (UTC)
commit d51718c5c3225c91b7f47278a3fc892d8b821cb3
Author: Christian Hergert <chergert redhat com>
Date: Tue Jun 7 11:45:10 2022 -0700
plugins/sphinx-preview: add preview for reStructuredText
When using .rst without a conf.py, we will assume it is reStructuredText
without Sphinx. This uses a subprocess to parse the rst and convert it to
HTML by sending a Python script to standard input and the buffer contents
in a memfd (or anonymous fd) to the script running under python3.
While it requires spawning a process, I do prefer this to running Python's
docutils.core.publish_file() in process as it had some overrides of
builtins.open() which are undesireable. Additionally, it keeps the memory
fragmentation in the subprocess which can be torn down quickly.
.../sphinx-preview/gbp-rst-html-generator.c | 267 +++++++++++++++++++++
.../sphinx-preview/gbp-rst-html-generator.h | 31 +++
.../gbp-sphinx-preview-workspace-addin.c | 6 +-
src/plugins/sphinx-preview/meson.build | 1 +
src/plugins/sphinx-preview/rst2html.py | 49 ++++
.../sphinx-preview/sphinx-preview.gresource.xml | 1 +
6 files changed, 353 insertions(+), 2 deletions(-)
---
diff --git a/src/plugins/sphinx-preview/gbp-rst-html-generator.c
b/src/plugins/sphinx-preview/gbp-rst-html-generator.c
new file mode 100644
index 000000000..bade840b9
--- /dev/null
+++ b/src/plugins/sphinx-preview/gbp-rst-html-generator.c
@@ -0,0 +1,267 @@
+/* gbp-rst-html-generator.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-rst-html-generator"
+
+#include "config.h"
+
+#include <errno.h>
+
+#include <libide-code.h>
+#include <libide-foundry.h>
+#include <libide-threading.h>
+
+#include "gbp-rst-html-generator.h"
+
+struct _GbpRstHtmlGenerator
+{
+ IdeHtmlGenerator parent_instance;
+ GSignalGroup *buffer_signals;
+};
+
+enum {
+ PROP_0,
+ PROP_BUFFER,
+ N_PROPS
+};
+
+G_DEFINE_FINAL_TYPE (GbpRstHtmlGenerator, gbp_rst_html_generator, IDE_TYPE_HTML_GENERATOR)
+
+static GParamSpec *properties [N_PROPS];
+static GBytes *rst2html;
+
+static void
+gbp_rst_html_generator_communicate_cb (GObject *object,
+ GAsyncResult *result,
+ gpointer user_data)
+{
+ IdeSubprocess *subprocess = (IdeSubprocess *)object;
+ g_autoptr(IdeTask) task = user_data;
+ g_autoptr(GBytes) bytes = NULL;
+ g_autoptr(GError) error = NULL;
+ g_autofree char *stdout_buf = NULL;
+ gsize len;
+
+ g_assert (IDE_IS_SUBPROCESS (subprocess));
+ g_assert (G_IS_ASYNC_RESULT (result));
+ g_assert (IDE_IS_TASK (task));
+
+ if (!ide_subprocess_communicate_utf8_finish (subprocess, result, &stdout_buf, NULL, &error))
+ {
+ ide_task_return_error (task, g_steal_pointer (&error));
+ return;
+ }
+
+ len = strlen (stdout_buf);
+ bytes = g_bytes_new_take (g_steal_pointer (&stdout_buf), len);
+
+ ide_task_return_pointer (task,
+ g_steal_pointer (&bytes),
+ g_bytes_unref);
+}
+
+static void
+gbp_rst_html_generator_generate_async (IdeHtmlGenerator *generator,
+ GCancellable *cancellable,
+ GAsyncReadyCallback callback,
+ gpointer user_data)
+{
+ GbpRstHtmlGenerator *self = (GbpRstHtmlGenerator *)generator;
+ g_autoptr(IdeSubprocessLauncher) launcher = NULL;
+ g_autoptr(IdeSubprocess) subprocess = NULL;
+ g_autoptr(IdeBuffer) buffer = NULL;
+ g_autoptr(IdeTask) task = NULL;
+ g_autoptr(GError) error = NULL;
+ g_autoptr(GBytes) content = NULL;
+ const char *source_path;
+ GFile *file;
+ int fd;
+
+ g_assert (GBP_IS_RST_HTML_GENERATOR (self));
+ g_assert (!cancellable || G_IS_CANCELLABLE (cancellable));
+
+ task = ide_task_new (self, cancellable, callback, user_data);
+ ide_task_set_source_tag (task, gbp_rst_html_generator_generate_async);
+
+ if (!(buffer = g_signal_group_dup_target (self->buffer_signals)))
+ {
+ ide_task_return_new_error (task,
+ G_IO_ERROR,
+ G_IO_ERROR_CANCELLED,
+ "Operation was cancelled");
+ return;
+ }
+
+ content = ide_buffer_dup_content (buffer);
+ file = ide_buffer_get_file (buffer);
+ source_path = g_file_peek_path (file);
+
+ if (-1 == (fd = ide_foundry_bytes_to_memfd (content, "rst2html-input")))
+ {
+ int errsv = errno;
+ ide_task_return_new_error (task,
+ G_IO_ERROR,
+ g_io_error_from_errno (errsv),
+ "%s",
+ g_strerror (errsv));
+ return;
+ }
+
+ launcher = ide_subprocess_launcher_new (G_SUBPROCESS_FLAGS_STDOUT_PIPE |
+ G_SUBPROCESS_FLAGS_STDERR_SILENCE |
+ G_SUBPROCESS_FLAGS_STDIN_PIPE);
+ ide_subprocess_launcher_push_args (launcher, IDE_STRV_INIT ("python3", "-", source_path));
+ ide_subprocess_launcher_take_fd (launcher, fd, 3);
+
+ if (!(subprocess = ide_subprocess_launcher_spawn (launcher, cancellable, &error)))
+ {
+ ide_task_return_error (task, g_steal_pointer (&error));
+ return;
+ }
+
+ ide_subprocess_communicate_utf8_async (subprocess,
+ (const char *)g_bytes_get_data (rst2html, NULL),
+ cancellable,
+ gbp_rst_html_generator_communicate_cb,
+ g_steal_pointer (&task));
+}
+
+static GBytes *
+gbp_rst_html_generator_generate_finish (IdeHtmlGenerator *generator,
+ GAsyncResult *result,
+ GError **error)
+{
+ g_assert (GBP_IS_RST_HTML_GENERATOR (generator));
+ g_assert (IDE_IS_TASK (result));
+
+ return ide_task_propagate_pointer (IDE_TASK (result), error);
+}
+
+static gboolean
+file_to_base_uri (GBinding *binding,
+ const GValue *from,
+ GValue *to,
+ gpointer user_data)
+{
+ g_value_set_string (to, g_file_get_uri (g_value_get_object (from)));
+ return TRUE;
+}
+
+static void
+gbp_rst_html_generator_set_buffer (GbpRstHtmlGenerator *self,
+ IdeBuffer *buffer)
+{
+ g_assert (GBP_IS_RST_HTML_GENERATOR (self));
+ g_assert (!buffer || IDE_IS_BUFFER (buffer));
+
+ g_signal_group_set_target (self->buffer_signals, buffer);
+
+ if (IDE_IS_BUFFER (buffer))
+ g_object_bind_property_full (buffer, "file",
+ self, "base-uri",
+ G_BINDING_SYNC_CREATE,
+ file_to_base_uri,
+ NULL, NULL, NULL);
+}
+
+static void
+gbp_rst_html_generator_dispose (GObject *object)
+{
+ GbpRstHtmlGenerator *self = (GbpRstHtmlGenerator *)object;
+
+ g_clear_object (&self->buffer_signals);
+
+ G_OBJECT_CLASS (gbp_rst_html_generator_parent_class)->dispose (object);
+}
+
+static void
+gbp_rst_html_generator_get_property (GObject *object,
+ guint prop_id,
+ GValue *value,
+ GParamSpec *pspec)
+{
+ GbpRstHtmlGenerator *self = GBP_RST_HTML_GENERATOR (object);
+
+ switch (prop_id)
+ {
+ case PROP_BUFFER:
+ g_value_take_object (value, g_signal_group_dup_target (self->buffer_signals));
+ break;
+
+ default:
+ G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+ }
+}
+
+static void
+gbp_rst_html_generator_set_property (GObject *object,
+ guint prop_id,
+ const GValue *value,
+ GParamSpec *pspec)
+{
+ GbpRstHtmlGenerator *self = GBP_RST_HTML_GENERATOR (object);
+
+ switch (prop_id)
+ {
+ case PROP_BUFFER:
+ gbp_rst_html_generator_set_buffer (self, g_value_get_object (value));
+ break;
+
+ default:
+ G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+ }
+}
+
+static void
+gbp_rst_html_generator_class_init (GbpRstHtmlGeneratorClass *klass)
+{
+ GObjectClass *object_class = G_OBJECT_CLASS (klass);
+ IdeHtmlGeneratorClass *generator_class = IDE_HTML_GENERATOR_CLASS (klass);
+
+ object_class->dispose = gbp_rst_html_generator_dispose;
+ object_class->get_property = gbp_rst_html_generator_get_property;
+ object_class->set_property = gbp_rst_html_generator_set_property;
+
+ generator_class->generate_async = gbp_rst_html_generator_generate_async;
+ generator_class->generate_finish = gbp_rst_html_generator_generate_finish;
+
+ properties [PROP_BUFFER] =
+ g_param_spec_object ("buffer", NULL, NULL,
+ IDE_TYPE_BUFFER,
+ (G_PARAM_READWRITE |
+ G_PARAM_CONSTRUCT_ONLY |
+ G_PARAM_STATIC_STRINGS));
+
+ g_object_class_install_properties (object_class, N_PROPS, properties);
+
+ rst2html = g_resources_lookup_data ("/plugins/sphinx-preview/rst2html.py", 0, NULL);
+}
+
+static void
+gbp_rst_html_generator_init (GbpRstHtmlGenerator *self)
+{
+ self->buffer_signals = g_signal_group_new (IDE_TYPE_BUFFER);
+
+ g_signal_group_connect_object (self->buffer_signals,
+ "changed",
+ G_CALLBACK (ide_html_generator_invalidate),
+ self,
+ G_CONNECT_SWAPPED);
+}
diff --git a/src/plugins/sphinx-preview/gbp-rst-html-generator.h
b/src/plugins/sphinx-preview/gbp-rst-html-generator.h
new file mode 100644
index 000000000..c7924c522
--- /dev/null
+++ b/src/plugins/sphinx-preview/gbp-rst-html-generator.h
@@ -0,0 +1,31 @@
+/* gbp-rst-html-generator.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 <libide-webkit.h>
+
+G_BEGIN_DECLS
+
+#define GBP_TYPE_RST_HTML_GENERATOR (gbp_rst_html_generator_get_type())
+
+G_DECLARE_FINAL_TYPE (GbpRstHtmlGenerator, gbp_rst_html_generator, GBP, RST_HTML_GENERATOR, IdeHtmlGenerator)
+
+G_END_DECLS
diff --git a/src/plugins/sphinx-preview/gbp-sphinx-preview-workspace-addin.c
b/src/plugins/sphinx-preview/gbp-sphinx-preview-workspace-addin.c
index 803df3208..6d9006a01 100644
--- a/src/plugins/sphinx-preview/gbp-sphinx-preview-workspace-addin.c
+++ b/src/plugins/sphinx-preview/gbp-sphinx-preview-workspace-addin.c
@@ -27,6 +27,7 @@
#include <libide-gui.h>
#include <libide-webkit.h>
+#include "gbp-rst-html-generator.h"
#include "gbp-sphinx-compiler.h"
#include "gbp-sphinx-html-generator.h"
#include "gbp-sphinx-preview-workspace-addin.h"
@@ -259,8 +260,9 @@ open_rst_preview (GbpSphinxPreviewWorkspaceAddin *self,
g_assert (GBP_IS_SPHINX_PREVIEW_WORKSPACE_ADDIN (self));
g_assert (IDE_IS_BUFFER (buffer));
- /* TODO: docutils translation with python in subprocess */
- generator = ide_html_generator_new_for_buffer (GTK_TEXT_BUFFER (buffer));
+ generator = g_object_new (GBP_TYPE_RST_HTML_GENERATOR,
+ "buffer", buffer,
+ NULL);
page = ide_webkit_page_new_for_generator (generator);
IDE_RETURN (IDE_PAGE (page));
diff --git a/src/plugins/sphinx-preview/meson.build b/src/plugins/sphinx-preview/meson.build
index ccd02f713..b71c5be02 100644
--- a/src/plugins/sphinx-preview/meson.build
+++ b/src/plugins/sphinx-preview/meson.build
@@ -6,6 +6,7 @@ endif
plugins_sources += files([
'sphinx-preview-plugin.c',
+ 'gbp-rst-html-generator.c',
'gbp-sphinx-compiler.c',
'gbp-sphinx-html-generator.c',
'gbp-sphinx-preview-workspace-addin.c',
diff --git a/src/plugins/sphinx-preview/rst2html.py b/src/plugins/sphinx-preview/rst2html.py
new file mode 100755
index 000000000..82e9899e8
--- /dev/null
+++ b/src/plugins/sphinx-preview/rst2html.py
@@ -0,0 +1,49 @@
+#!/usr/bin/env python3
+
+# 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
+
+import docutils.core
+import docutils.io
+import os
+import os.path
+import sys
+
+# This script is intended to be run as standard input to Python with
+# the source content provided as file-descriptor 3. The result is
+# written to standard output.
+
+source = os.fdopen(3)
+source_path = sys.argv[1]
+dest_path = os.path.splitext(source_path)[0] + '.html'
+docutils.core.publish_programmatically(docutils.io.FileInput, # source_class
+ source, # source
+ source_path, # source_path
+ docutils.io.FileOutput, # destination_class
+ sys.stdout, # destination
+ dest_path, # destination_path
+ None, # reader
+ 'standalone', # reader_name
+ None, # parser
+ 'restructuredtext', # parser_name
+ None, # writer
+ 'html', # writer_name
+ None, # settings
+ None, # settings_spec
+ None, # settings_overrides
+ None, # config_section
+ True) # enable_exit_status
diff --git a/src/plugins/sphinx-preview/sphinx-preview.gresource.xml
b/src/plugins/sphinx-preview/sphinx-preview.gresource.xml
index cb615b98c..c819d77a5 100644
--- a/src/plugins/sphinx-preview/sphinx-preview.gresource.xml
+++ b/src/plugins/sphinx-preview/sphinx-preview.gresource.xml
@@ -3,5 +3,6 @@
<gresource prefix="/plugins/sphinx-preview">
<file>sphinx-preview.plugin</file>
<file preprocess="xml-stripblanks">gtk/menus.ui</file>
+ <file>rst2html.py</file>
</gresource>
</gresources>
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]