Added markdown check guard to plugin

This commit is contained in:
itdominator 2023-11-04 14:45:54 -05:00
parent 81f53e8d22
commit 685f75d76b

View File

@ -27,6 +27,7 @@ class Plugin(MarkdownTemplateMixin, PluginBase):
self._GLADE_FILE = f"{self.path}/markdown_preview.glade" self._GLADE_FILE = f"{self.path}/markdown_preview.glade"
self.is_preview_paused = False self.is_preview_paused = False
self.is_md_file = False
def run(self): def run(self):
@ -54,23 +55,25 @@ class Plugin(MarkdownTemplateMixin, PluginBase):
self._event_system.subscribe("set_active_src_view", self._set_active_src_view) self._event_system.subscribe("set_active_src_view", self._set_active_src_view)
self._event_system.subscribe("buffer_changed", self._do_markdown_translate) self._event_system.subscribe("buffer_changed", self._do_markdown_translate)
def _buffer_changed_first_load(self, buffer):
self._buffer = buffer
self._do_markdown_translate(buffer)
def _set_active_src_view(self, source_view): def _set_active_src_view(self, source_view):
self._active_src_view = source_view self._active_src_view = source_view
self._buffer = self._active_src_view.get_buffer() self._buffer = self._active_src_view.get_buffer()
self._do_markdown_translate(self._buffer)
def _handle_settings(self, widget = None, eve = None): def _handle_settings(self, widget = None, eve = None):
... ...
def _buffer_changed_first_load(self, buffer):
self._buffer = buffer
self._do_markdown_translate(buffer)
def _tggle_preview_updates(self, widget = None, eve = None): def _tggle_preview_updates(self, widget = None, eve = None):
self.is_preview_paused = not self.is_preview_paused self.is_preview_paused = not self.is_preview_paused
widget.set_active(self.is_preview_paused) widget.set_active(self.is_preview_paused)
if notself.is_preview_paused: if not self.is_preview_paused:
self._do_markdown_translate(self._buffer) self._do_markdown_translate(self._buffer)
def _tggle_markdown_preview(self, widget = None, eve = None): def _tggle_markdown_preview(self, widget = None, eve = None):
@ -89,9 +92,12 @@ class Plugin(MarkdownTemplateMixin, PluginBase):
def _do_markdown_translate(self, buffer): def _do_markdown_translate(self, buffer):
if self.is_preview_paused: return if self.is_preview_paused: return
self.is_markdown_check()
is_visible = self._markdown_dialog.is_visible() is_visible = self._markdown_dialog.is_visible()
if not is_visible: return if not is_visible or not self.is_md_file: return
self.render_markdown(buffer)
def render_markdown(self, buffer):
start_iter = buffer.get_start_iter() start_iter = buffer.get_start_iter()
end_iter = buffer.get_end_iter() end_iter = buffer.get_end_iter()
text = buffer.get_text(start_iter, end_iter, include_hidden_chars = False) text = buffer.get_text(start_iter, end_iter, include_hidden_chars = False)
@ -100,3 +106,9 @@ class Plugin(MarkdownTemplateMixin, PluginBase):
path = self._active_src_view.get_current_filepath().get_parent().get_path() path = self._active_src_view.get_current_filepath().get_parent().get_path()
data = self.wrap_html_to_body(html) data = self.wrap_html_to_body(html)
self._markdown_view.load_html(content = data, base_uri = f"file://{path}/") 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)