[gnome-builder] html preview: add reStruccturedText support



commit 25f3c1ec98b405adf1c7c427ab49e794e8b1c6e1
Author: Sebastien Lafargue <slafargue gnome org>
Date:   Tue Mar 14 22:13:17 2017 +0100

    html preview: add reStruccturedText support
    
    To make it work, you need to have
    python3-docutils installed

 plugins/html-preview/html-preview.plugin           |    4 +-
 .../html-preview/html_preview_plugin/__init__.py   |   28 +++++++++++++++++--
 2 files changed, 27 insertions(+), 5 deletions(-)
---
diff --git a/plugins/html-preview/html-preview.plugin b/plugins/html-preview/html-preview.plugin
index 6b4bfd4..8f33d45 100644
--- a/plugins/html-preview/html-preview.plugin
+++ b/plugins/html-preview/html-preview.plugin
@@ -1,8 +1,8 @@
 [Plugin]
 Module=html_preview_plugin
 Loader=python3
-Name=HTML and Markdown Preview
-Description=Live preview of HTML and Markdown documents.
+Name=HTML, reStructuredText and Markdown Preview
+Description=Live preview of HTML, reStructuredText and Markdown documents.
 Authors=Christian Hergert <christian hergert me>
 Copyright=Copyright © 2015 Christian Hergert
 Builtin=true
diff --git a/plugins/html-preview/html_preview_plugin/__init__.py 
b/plugins/html-preview/html_preview_plugin/__init__.py
index e9ad2f2..b4ac668 100644
--- a/plugins/html-preview/html_preview_plugin/__init__.py
+++ b/plugins/html-preview/html_preview_plugin/__init__.py
@@ -34,6 +34,13 @@ from gi.repository import Ide
 from gi.repository import WebKit2
 from gi.repository import Peas
 
+can_preview_rst = True
+
+try:
+    from docutils.core import publish_string
+except ImportError:
+    can_preview_rst = False
+
 _ = Ide.gettext
 
 class HtmlPreviewData(GObject.Object, Ide.ApplicationAddin):
@@ -67,17 +74,23 @@ class HtmlPreviewAddin(GObject.Object, Ide.EditorViewAddin):
         actions.remove_action('preview-as-html')
 
     def do_language_changed(self, language_id):
-        enabled = (language_id in ('html', 'markdown'))
+        enabled = (language_id in ('html', 'markdown', 'rst'))
         self.action.set_enabled(enabled)
 
     def preview_activated(self, editor):
         document = editor.get_document()
+        language = document.get_language()
+        if language and language.get_id() == 'rst' and not can_preview_rst:
+            return
+
         view = HtmlPreviewView(document, visible=True)
+
         stack = editor.get_ancestor(Ide.LayoutStack)
         stack.add(view)
 
 class HtmlPreviewView(Ide.LayoutView):
     markdown = False
+    rst = False
 
     def __init__(self, document, *args, **kwargs):
         super().__init__(*args, **kwargs)
@@ -90,8 +103,12 @@ class HtmlPreviewView(Ide.LayoutView):
         settings.enable_html5_local_storage = False
 
         language = document.get_language()
-        if language and language.get_id() == 'markdown':
-            self.markdown = True
+        if language:
+            id = language.get_id()
+            if id == 'markdown':
+                self.markdown = True
+            elif id == 'rst':
+                self.rst = True
 
         document.connect('changed', self.on_changed)
         self.on_changed(document)
@@ -125,6 +142,9 @@ class HtmlPreviewView(Ide.LayoutView):
 </html>
 """ % params
 
+    def get_rst(self, text):
+        return publish_string(text, writer_name='html5')
+
     def reload(self):
         base_uri = self.document.get_file().get_file().get_uri()
 
@@ -133,6 +153,8 @@ class HtmlPreviewView(Ide.LayoutView):
 
         if self.markdown:
             text = self.get_markdown(text)
+        elif self.rst:
+            text = self.get_rst(text).decode("utf-8")
 
         self.webview.load_html(text, base_uri)
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]