2023-10-28 19:53:42 +00:00
|
|
|
# Python imports
|
|
|
|
import os
|
|
|
|
|
|
|
|
# Lib imports
|
|
|
|
import gi
|
|
|
|
gi.require_version('Gtk', '3.0')
|
2023-11-01 03:04:08 +00:00
|
|
|
gi.require_version('Gdk', '3.0')
|
2023-10-28 19:53:42 +00:00
|
|
|
gi.require_version('WebKit2', '4.0')
|
|
|
|
from gi.repository import Gtk
|
2023-11-01 03:04:08 +00:00
|
|
|
from gi.repository import Gdk
|
2023-10-28 19:53:42 +00:00
|
|
|
from gi.repository import WebKit2
|
|
|
|
|
|
|
|
# Application imports
|
2023-11-05 05:38:15 +00:00
|
|
|
from . import markdown
|
2023-11-01 03:04:08 +00:00
|
|
|
from .markdown_template_mixin import MarkdownTemplateMixin
|
2023-10-28 19:53:42 +00:00
|
|
|
from plugins.plugin_base import PluginBase
|
|
|
|
|
|
|
|
|
|
|
|
|
2023-11-01 03:04:08 +00:00
|
|
|
class Plugin(MarkdownTemplateMixin, PluginBase):
|
2023-10-28 19:53:42 +00:00
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
|
|
|
|
|
|
|
self.name = "Markdown Preview" # NOTE: Need to remove after establishing private bidirectional 1-1 message bus
|
|
|
|
# where self.name should not be needed for message comms
|
|
|
|
self.path = os.path.dirname(os.path.realpath(__file__))
|
|
|
|
self._GLADE_FILE = f"{self.path}/markdown_preview.glade"
|
|
|
|
|
2023-11-01 03:04:08 +00:00
|
|
|
self.is_preview_paused = False
|
2023-11-04 19:45:54 +00:00
|
|
|
self.is_md_file = False
|
2023-10-28 19:53:42 +00:00
|
|
|
|
|
|
|
|
|
|
|
def run(self):
|
2023-11-01 03:04:08 +00:00
|
|
|
WebKit2.WebView() # Need one initialized for webview to work from glade file
|
|
|
|
|
2023-10-28 19:53:42 +00:00
|
|
|
self._builder = Gtk.Builder()
|
|
|
|
self._builder.add_from_file(self._GLADE_FILE)
|
|
|
|
self._connect_builder_signals(self, self._builder)
|
|
|
|
|
|
|
|
separator_right = self._ui_objects[0]
|
|
|
|
self._markdown_dialog = self._builder.get_object("markdown_preview_dialog")
|
2023-11-01 03:04:08 +00:00
|
|
|
self._markdown_view = self._builder.get_object("markdown_view")
|
2023-10-28 19:53:42 +00:00
|
|
|
self._web_view_settings = self._builder.get_object("web_view_settings")
|
|
|
|
|
2023-11-01 03:04:08 +00:00
|
|
|
self._markdown_dialog.set_relative_to(separator_right)
|
|
|
|
self._markdown_view.set_settings(self._web_view_settings)
|
|
|
|
self._markdown_view.set_background_color(Gdk.RGBA(0, 0, 0, 0.0))
|
|
|
|
|
2023-10-28 19:53:42 +00:00
|
|
|
|
|
|
|
def generate_reference_ui_element(self):
|
|
|
|
...
|
|
|
|
|
|
|
|
def subscribe_to_events(self):
|
|
|
|
self._event_system.subscribe("tggle_markdown_preview", self._tggle_markdown_preview)
|
2023-11-01 03:04:08 +00:00
|
|
|
self._event_system.subscribe("set_active_src_view", self._set_active_src_view)
|
|
|
|
self._event_system.subscribe("buffer_changed", self._do_markdown_translate)
|
2023-10-28 19:53:42 +00:00
|
|
|
|
2023-11-04 19:45:54 +00:00
|
|
|
def _buffer_changed_first_load(self, buffer):
|
|
|
|
self._buffer = buffer
|
|
|
|
|
|
|
|
self._do_markdown_translate(buffer)
|
|
|
|
|
2023-10-28 19:53:42 +00:00
|
|
|
def _set_active_src_view(self, source_view):
|
|
|
|
self._active_src_view = source_view
|
|
|
|
self._buffer = self._active_src_view.get_buffer()
|
|
|
|
|
2023-11-04 19:45:54 +00:00
|
|
|
self._do_markdown_translate(self._buffer)
|
2023-10-28 19:53:42 +00:00
|
|
|
|
2023-11-04 19:45:54 +00:00
|
|
|
def _handle_settings(self, widget = None, eve = None):
|
|
|
|
...
|
2023-11-01 03:04:08 +00:00
|
|
|
|
|
|
|
def _tggle_preview_updates(self, widget = None, eve = None):
|
|
|
|
self.is_preview_paused = not self.is_preview_paused
|
|
|
|
widget.set_active(self.is_preview_paused)
|
|
|
|
|
2023-11-04 19:45:54 +00:00
|
|
|
if not self.is_preview_paused:
|
2023-11-01 03:04:08 +00:00
|
|
|
self._do_markdown_translate(self._buffer)
|
2023-10-28 19:53:42 +00:00
|
|
|
|
|
|
|
def _tggle_markdown_preview(self, widget = None, eve = None):
|
2023-11-01 03:04:08 +00:00
|
|
|
if not self._active_src_view: return
|
|
|
|
|
2023-10-28 19:53:42 +00:00
|
|
|
is_visible = self._markdown_dialog.is_visible()
|
|
|
|
buffer = self._active_src_view.get_buffer()
|
|
|
|
data = None
|
|
|
|
|
|
|
|
if not is_visible:
|
|
|
|
self._markdown_dialog.popup();
|
2023-11-01 03:04:08 +00:00
|
|
|
self._do_markdown_translate(buffer)
|
2023-10-28 19:53:42 +00:00
|
|
|
elif not data and is_visible:
|
2023-11-01 03:04:08 +00:00
|
|
|
self._markdown_dialog.popdown()
|
|
|
|
|
|
|
|
def _do_markdown_translate(self, buffer):
|
|
|
|
if self.is_preview_paused: return
|
|
|
|
|
2023-11-04 19:45:54 +00:00
|
|
|
self.is_markdown_check()
|
2023-11-01 03:04:08 +00:00
|
|
|
is_visible = self._markdown_dialog.is_visible()
|
2023-11-04 19:45:54 +00:00
|
|
|
if not is_visible or not self.is_md_file: return
|
|
|
|
self.render_markdown(buffer)
|
2023-11-01 03:04:08 +00:00
|
|
|
|
2023-11-04 19:45:54 +00:00
|
|
|
def render_markdown(self, buffer):
|
2023-11-01 03:04:08 +00:00
|
|
|
start_iter = buffer.get_start_iter()
|
|
|
|
end_iter = buffer.get_end_iter()
|
|
|
|
text = buffer.get_text(start_iter, end_iter, include_hidden_chars = False)
|
|
|
|
html = markdown.markdown(text)
|
|
|
|
|
2023-11-12 22:02:04 +00:00
|
|
|
path = self._active_src_view.get_current_file().get_parent().get_path()
|
2023-11-01 03:04:08 +00:00
|
|
|
data = self.wrap_html_to_body(html)
|
2023-11-04 19:45:54 +00:00
|
|
|
self._markdown_view.load_html(content = data, base_uri = f"file://{path}/")
|
|
|
|
|
|
|
|
def is_markdown_check(self):
|
|
|
|
self.is_md_file = self._active_src_view.get_filetype() == "markdown"
|
|
|
|
if not self.is_md_file:
|
|
|
|
data = self.wrap_html_to_body("<h1>Not a Markdown file...</h1>")
|
|
|
|
self._markdown_view.load_html(content = data, base_uri = None)
|