Exemplo n.º 1
0
    def show_preview(self, loaded=False):
        if loaded:
            self.scrolled_window.remove(self.scrolled_window.get_child())
            self.scrolled_window.add(self.preview_webview)
            self.preview_webview.show()
            self.queue_draw()
        else:
            # Insert a tag with ID to scroll to
            # self.TextBuffer.insert_at_cursor('<span id="scroll_mark"></span>')
            # TODO
            # Find a way to find the next header, scroll to the next header.
            # TODO: provide a local version of mathjax

            # We need to convert relative routes to absolute ones
            # For that first we need to know if the file is saved:
            if self.filename:
                base_path = os.path.dirname(self.filename)
            else:
                base_path = ''
            os.environ['PANDOC_PREFIX'] = base_path + '/'

            args = ['pandoc',
                    '-s',
                    '--from=markdown',
                    '--to=html5',
                    '--mathjax',
                    '--css=' + Theme.get_current().web_css_path,
                    '--quiet',
                    '--lua-filter=' + helpers.get_script_path('relative_to_absolute.lua'),
                    '--lua-filter=' + helpers.get_script_path('task-list.lua')]

            # TODO: find a way to pass something like this instead of the quiet arg        
            #'--metadata pagetitle="test"',
            
            proc = subprocess.Popen(
                args, stdin=subprocess.PIPE, stdout=subprocess.PIPE)

            text = bytes(self.get_text(), "utf-8")
            output = proc.communicate(text)[0]

            if self.preview_webview is None:
                self.preview_webview = WebKit.WebView()
                self.preview_webview.get_settings().set_allow_universal_access_from_file_urls(True)

                # Delete the cursor-scroll mark again
                # cursor_iter = self.TextBuffer.get_iter_at_mark(self.TextBuffer.get_insert())
                # begin_del = cursor_iter.copy()
                # begin_del.backward_chars(30)
                # self.TextBuffer.delete(begin_del, cursor_iter)

                # Show preview once the load is finished
                self.preview_webview.connect("load-changed", self.on_preview_load_change)

                # This saying that all links will be opened in default browser, \
                # but local files are opened in appropriate apps:
                self.preview_webview.connect("decide-policy", self.on_click_link)

            self.preview_webview.load_html(output.decode("utf-8"), 'file://localhost/')
    def export(self, text=""):
        """Export to pdf, html or odt the given text

        Keyword Arguments:
            text {str} -- Text to export (default: {""})
        """

        export_format = self.stack.get_visible_child_name()

        if export_format == "advanced":
            self.advanced_export(text)
        else:
            filename = self.filechoosers[export_format].get_filename()
            if filename.endswith("." + export_format):
                filename = filename[:-len(export_format) - 1]

            output_dir = os.path.abspath(os.path.join(filename,
                                                      os.path.pardir))
            basename = os.path.basename(filename)

            args = ['pandoc', '--from=markdown', '-s']

            if export_format == "pdf":
                args.append("-o%s.pdf" % basename)

            elif export_format == "odt":
                args.append("-o%s.odt" % basename)

            elif export_format == "html":
                css = Theme.ADWAITA.get_gtk_css_file()
                relativize = helpers.get_script_path(
                    'relative_to_absolute.lua')
                task_list = helpers.get_script_path('task-list.lua')
                args.append("-c%s" % css)
                args.append("-o%s.html" % basename)
                args.append("--mathjax")
                args.append("--lua-filter=" + relativize)
                args.append("--lua-filter=" + task_list)

            proc = subprocess.Popen(args,
                                    stdin=subprocess.PIPE,
                                    stdout=subprocess.PIPE,
                                    cwd=output_dir)
            _ = proc.communicate(text)[0]
Exemplo n.º 3
0
    def toggle_preview(self, state):

        if state.get_boolean():

            # Insert a tag with ID to scroll to
            # self.TextBuffer.insert_at_cursor('<span id="scroll_mark"></span>')
            # TODO
            # Find a way to find the next header, scroll to the next header.
            # TODO: provide a local version of mathjax

            # We need to convert relative routes to absolute ones
            # For that first we need to know if the file is saved:
            if self.filename:
                base_path = os.path.dirname(self.filename)
            else:
                base_path = ''
            os.environ['PANDOC_PREFIX'] = base_path + '/'

            # Set the styles according the color theme
            if self.settings.get_value("dark-mode"):
                stylesheet = helpers.get_media_path('uberwriter_dark.css')
            else:
                stylesheet = helpers.get_media_path('uberwriter.css')

            args = [
                'pandoc', '-s', '--from=markdown', '--to=html5', '--mathjax',
                '--css=' + stylesheet, '--lua-filter=' +
                helpers.get_script_path('relative_to_absolute.lua'),
                '--lua-filter=' + helpers.get_script_path('task-list.lua')
            ]

            p = subprocess.Popen(args,
                                 stdin=subprocess.PIPE,
                                 stdout=subprocess.PIPE)

            text = bytes(self.get_text(), "utf-8")
            output = p.communicate(text)[0]

            # Load in Webview and scroll to #ID
            self.webview = WebKit.WebView()
            self.webview_settings = self.webview.get_settings()
            self.webview_settings.set_allow_universal_access_from_file_urls(
                True)
            self.webview.load_html(output.decode("utf-8"), 'file://localhost/')

            # Delete the cursor-scroll mark again
            # cursor_iter = self.TextBuffer.get_iter_at_mark(self.TextBuffer.get_insert())
            # begin_del = cursor_iter.copy()
            # begin_del.backward_chars(30)
            # self.TextBuffer.delete(begin_del, cursor_iter)

            self.ScrolledWindow.remove(self.TextEditor)
            self.ScrolledWindow.add(self.webview)
            self.webview.show()

            # This saying that all links will be opened in default browser, \
            # but local files are opened in appropriate apps:
            self.webview.connect("decide-policy", self.on_click_link)
        else:
            self.ScrolledWindow.remove(self.webview)
            self.webview.destroy()
            self.ScrolledWindow.add(self.TextEditor)
            self.TextEditor.show()

        self.queue_draw()
        return True