def on_reload(self, *args): # Guard clause: it will not load documents which are not supported self.file_format = self.recognize_format() if self.file_format == 'error' or not self.preview_bar.props.visible: # if self.file_format == 'error' or not self.panel.props.visible: return html_content = '' doc = self.parent_plugin.window.get_active_document() start, end = doc.get_bounds() unsaved_text = doc.get_text(start, end, True) if self.file_format == 'html': html_content = self.get_html_from_html(unsaved_text) elif self.file_format == 'tex': html_content = self.get_html_from_tex() elif self.file_format == 'md': if self._settings.get_string('backend') == 'python': html_content = self.get_html_from_md_python(unsaved_text) else: html_content = self.get_html_from_md_pandoc(unsaved_text) else: return # The html code is converted into bytes my_string = GLib.String() my_string.append(html_content) bytes_content = my_string.free_to_bytes() # This uri will be used as a reference for links and images using relative paths dummy_uri = self.get_dummy_uri() self._webview.load_bytes(bytes_content, 'text/html', 'UTF-8', dummy_uri)
def _on_reload_unsafe(self): """Must be called ONLY by `on_reload` which checks pre-conditions, or by `_unlock_reload` which is called only by `on_reload` itself.""" html_content = '' doc = self.parent_plugin.window.get_active_document() if doc is None: print("The window doesn't have any active document to preview") return if self._file_format == None: # The clause guard has to be duplicated because the document might # change during the delay between `on_reload` & `_on_reload_unsafe` return start, end = doc.get_bounds() unsaved_text = doc.get_text(start, end, True) unsaved_text = unsaved_text.encode('utf-8').decode() if self._file_format == 'html': html_content = self.get_html_from_html(unsaved_text) elif self._active_backend == 'python': html_content = self.get_html_from_p3md(unsaved_text) else: html_content = self.get_html_from_pandoc(unsaved_text) # The html code is converted into bytes my_string = GLib.String() my_string.append(html_content) bytes_content = my_string.free_to_bytes() # This uri will be used as a reference for links and images using # relative paths. It might not be related to the current file. dummy_uri = self.get_dummy_uri() self._webview_manager.load_bytes_for_uri(bytes_content, dummy_uri)
def export_p3md(self): file_chooser = self.launch_file_chooser(self.output_extension) if file_chooser is None: return False md_extensions = [] for plugin_id in self._backend.plugins: if self._backend.plugins[plugin_id].get_active(): md_extensions.append(plugin_id) doc = self.gedit_window.get_active_document() start, end = doc.get_bounds() unsaved_text = doc.get_text(start, end, True) html_content = markdown.markdown(unsaved_text, extensions=md_extensions) if self.css_manager.is_active(): pre_string = '<html><head><meta charset="utf-8" />' + \ '<link rel="stylesheet" href="' + self.css_manager.css_uri + \ '" /></head><body>' post_string = '</body></html>' html_content = pre_string + html_content + post_string if self.output_extension == '.pdf': my_string = GLib.String() my_string.append(html_content) bytes_content = my_string.free_to_bytes() source_uri = self._preview_container.get_dummy_uri() webview_manager = self._preview_container._webview_manager webview_manager.load_bytes_for_uri(bytes_content, source_uri) webview_manager.print_webview(file_chooser.get_uri()) else: # if self.output_extension == '.html': with open(file_chooser.get_filename(), 'w') as f: f.write(html_content) file_chooser.destroy() return True
def on_reload(self, *args): # Guard clause: it will not load documents which are not supported self.file_format = self.recognize_format() if self.file_format == 'error' or not self.preview_bar.props.visible: return elif self.panel.get_visible_child() != self.preview_bar: return if self.parent_plugin._auto_position: self.auto_change_panel() css_uri = '' # TODO virer ça if self._settings.get_boolean('use-style'): css_uri = self._settings.get_string('style') html_content = '' doc = self.parent_plugin.window.get_active_document() start, end = doc.get_bounds() unsaved_text = doc.get_text(start, end, True) if self.file_format == 'html': html_content = self.get_html_from_html(unsaved_text) elif self._settings.get_string('backend') == 'python' \ and self.file_format == 'md': html_content = self.get_html_from_p3md(unsaved_text, css_uri) else: is_tex = self.file_format != 'tex' html_content = self.get_html_from_pandoc(unsaved_text, is_tex) # The html code is converted into bytes my_string = GLib.String() my_string.append(html_content) bytes_content = my_string.free_to_bytes() # This uri will be used as a reference for links and images using relative paths dummy_uri = self.get_dummy_uri() self._webview.load_bytes(bytes_content, 'text/html', 'UTF-8', dummy_uri)
def on_reload(self, *args): # Guard clause: it will not load documents which are not .md if self.recognize_format() is 'error': if len(self.panel.get_children()) is 1: # FIXME 1 pour bottom mais 2 pour side self.panel.hide() return elif self.recognize_format() is 'html': self.panel.show() doc = self.window.get_active_document() start, end = doc.get_bounds() html_string = doc.get_text(start, end, True) pre_string = '<html><head><meta charset="utf-8" /></head><body>' post_string = '</body></html>' html_string = self.current_page(html_string) html_content = pre_string + html_string + post_string elif self.recognize_format() is 'tex': self.panel.show() doc = self.window.get_active_document() file_path = doc.get_location().get_path() # It uses pandoc to produce the html code pre_string = '<html><head><meta charset="utf-8" /><link rel="stylesheet" href="' + \ self._settings.get_string('style') + '" /></head><body>' post_string = '</body></html>' result = subprocess.run(['pandoc', file_path], stdout=subprocess.PIPE) html_string = result.stdout.decode('utf-8') html_string = self.current_page(html_string) html_content = pre_string + html_string + post_string else: self.panel.show() # Get the current document, or the temporary document if requested doc = self.window.get_active_document() if self.auto_reload: start, end = doc.get_bounds() unsaved_text = doc.get_text(start, end, True) f = open(BASE_TEMP_NAME + '.md', 'w') f.write(unsaved_text) f.close() file_path = self.temp_file_md.get_path() else: file_path = doc.get_location().get_path() # It uses pandoc to produce the html code pre_string = '<html><head><meta charset="utf-8" /><link rel="stylesheet" href="' + \ self._settings.get_string('style') + '" /></head><body>' post_string = '</body></html>' result = subprocess.run(['pandoc', file_path], stdout=subprocess.PIPE) html_string = result.stdout.decode('utf-8') html_string = self.current_page(html_string) html_content = pre_string + html_string + post_string # The html code is converted into bytes my_string = GLib.String() my_string.append(html_content) bytes_content = my_string.free_to_bytes() # This uri will be used as a reference for links and images using relative paths dummy_uri = self.get_dummy_uri() # The content is loaded self._webview.load_bytes(bytes_content, 'text/html', 'UTF-8', dummy_uri) self.window.lookup_action('md_prev_export_doc').set_enabled(True) self.window.lookup_action('md_prev_print_doc').set_enabled(True) self.window.lookup_action('md_prev_insert_picture').set_enabled(True)
def __init__(self): HTMLParser.__init__(self) self.s = GLib.String() self.stack = [] self.to_apply = []