2018-07-17 10:46:43 +00:00
|
|
|
# -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-
|
|
|
|
### BEGIN LICENSE
|
2019-07-05 18:47:49 +00:00
|
|
|
# Copyright (C) 2019, Wolf Vollprecht <w.vollprecht@gmail.com>
|
2018-07-17 18:25:12 +00:00
|
|
|
# This program is free software: you can redistribute it and/or modify it
|
|
|
|
# under the terms of the GNU General Public License version 3, as published
|
2018-07-17 10:46:43 +00:00
|
|
|
# by the Free Software Foundation.
|
2018-07-17 18:25:12 +00:00
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful, but
|
|
|
|
# WITHOUT ANY WARRANTY; without even the implied warranties of
|
|
|
|
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
|
2018-07-17 10:46:43 +00:00
|
|
|
# PURPOSE. See the GNU General Public License for more details.
|
2018-07-17 18:25:12 +00:00
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License along
|
2018-07-17 10:46:43 +00:00
|
|
|
# with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
### END LICENSE
|
2018-07-17 18:25:12 +00:00
|
|
|
"""Manages all the export operations and dialogs
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
import logging
|
2019-04-14 00:58:45 +00:00
|
|
|
import os
|
2018-07-17 10:46:43 +00:00
|
|
|
from gettext import gettext as _
|
2019-03-26 14:18:19 +00:00
|
|
|
|
2019-04-14 00:58:45 +00:00
|
|
|
import gi
|
2019-03-26 14:18:19 +00:00
|
|
|
|
2019-01-27 19:20:56 +00:00
|
|
|
gi.require_version('Gtk', '3.0')
|
2018-07-17 18:25:12 +00:00
|
|
|
from gi.repository import Gtk
|
2018-07-17 10:46:43 +00:00
|
|
|
|
2019-04-01 16:00:17 +00:00
|
|
|
from uberwriter import helpers
|
2019-04-01 20:40:59 +00:00
|
|
|
from uberwriter.theme import Theme
|
2018-07-17 10:46:43 +00:00
|
|
|
|
2018-07-17 18:25:12 +00:00
|
|
|
LOGGER = logging.getLogger('uberwriter')
|
|
|
|
|
2019-03-31 02:16:18 +00:00
|
|
|
|
|
|
|
class Export:
|
2018-07-17 18:25:12 +00:00
|
|
|
"""
|
|
|
|
Manages all the export operations and dialogs
|
|
|
|
"""
|
|
|
|
|
2019-04-01 20:40:59 +00:00
|
|
|
__gtype_name__ = "export_dialog"
|
2018-07-17 10:46:43 +00:00
|
|
|
|
2019-04-14 00:58:45 +00:00
|
|
|
formats = [
|
|
|
|
{
|
2019-04-14 02:03:44 +00:00
|
|
|
"name": "LaTeX (pdf)",
|
|
|
|
"ext": "pdf",
|
|
|
|
"to": "pdf"
|
2018-07-17 10:46:43 +00:00
|
|
|
},
|
2019-04-14 00:58:45 +00:00
|
|
|
{
|
2019-04-14 02:03:44 +00:00
|
|
|
"name": "LaTeX Beamer Slideshow (pdf)",
|
2018-07-17 10:46:43 +00:00
|
|
|
"ext": "pdf",
|
2019-04-14 02:03:44 +00:00
|
|
|
"to": "beamer"
|
2018-07-17 10:46:43 +00:00
|
|
|
},
|
2019-04-14 00:58:45 +00:00
|
|
|
{
|
2019-04-14 02:03:44 +00:00
|
|
|
"name": "LaTeX (tex)",
|
2018-07-17 10:46:43 +00:00
|
|
|
"ext": "tex",
|
2019-04-14 02:03:44 +00:00
|
|
|
"to": "latex"
|
2018-07-17 10:46:43 +00:00
|
|
|
},
|
2019-04-14 00:58:45 +00:00
|
|
|
{
|
2019-04-14 02:03:44 +00:00
|
|
|
"name": "LaTeX Beamer Slideshow (tex)",
|
|
|
|
"ext": "tex",
|
2018-07-17 10:46:43 +00:00
|
|
|
"to": "beamer"
|
|
|
|
},
|
2019-04-14 02:03:44 +00:00
|
|
|
{
|
|
|
|
"name": "ConTeXt",
|
|
|
|
"ext": "tex",
|
|
|
|
"to": "context"
|
|
|
|
},
|
2019-04-14 00:58:45 +00:00
|
|
|
{
|
2018-07-17 10:46:43 +00:00
|
|
|
"name": "HTML",
|
|
|
|
"ext": "html",
|
2019-04-14 02:03:44 +00:00
|
|
|
"to": "html"
|
2018-07-17 10:46:43 +00:00
|
|
|
},
|
2019-04-14 00:58:45 +00:00
|
|
|
{
|
2019-04-14 02:03:44 +00:00
|
|
|
"name": "HTML and JavaScript Slideshow (Slidy)",
|
|
|
|
"ext": "html",
|
|
|
|
"to": "slidy"
|
2018-07-17 10:46:43 +00:00
|
|
|
},
|
2019-04-14 00:58:45 +00:00
|
|
|
{
|
2019-04-14 02:03:44 +00:00
|
|
|
"name": "HTML and JavaScript Slideshow (Slideous)",
|
|
|
|
"ext": "html",
|
|
|
|
"to": "slideous"
|
2018-07-17 10:46:43 +00:00
|
|
|
},
|
2019-04-14 00:58:45 +00:00
|
|
|
{
|
2019-04-14 02:03:44 +00:00
|
|
|
"name": "HTML5 and JavaScript Slideshow (DZSlides)",
|
|
|
|
"ext": "html",
|
|
|
|
"to": "dzslides"
|
2018-07-17 10:46:43 +00:00
|
|
|
},
|
2019-04-14 00:58:45 +00:00
|
|
|
{
|
2019-04-14 02:03:44 +00:00
|
|
|
"name": "HTML5 and JavaScript Slideshow (reveal.js)",
|
|
|
|
"ext": "html",
|
|
|
|
"to": "revealjs"
|
2018-07-17 10:46:43 +00:00
|
|
|
},
|
2019-04-14 00:58:45 +00:00
|
|
|
{
|
2019-04-14 02:03:44 +00:00
|
|
|
"name": "HTML and JavaScript Slideshow (S5)",
|
|
|
|
"ext": "html",
|
|
|
|
"to": "s5"
|
2018-07-17 10:46:43 +00:00
|
|
|
},
|
2019-04-14 00:58:45 +00:00
|
|
|
{
|
2019-04-14 02:03:44 +00:00
|
|
|
"name": "Textile",
|
|
|
|
"ext": "txt",
|
|
|
|
"to": "textile"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "reStructuredText",
|
|
|
|
"ext": "txt",
|
|
|
|
"to": "rst"
|
2018-07-17 10:46:43 +00:00
|
|
|
},
|
2019-04-14 00:58:45 +00:00
|
|
|
{
|
2019-04-14 02:03:44 +00:00
|
|
|
"name": "MediaWiki Markup",
|
2018-07-17 10:46:43 +00:00
|
|
|
"ext": "txt",
|
|
|
|
"to": "mediawiki"
|
|
|
|
},
|
2019-04-14 00:58:45 +00:00
|
|
|
{
|
2019-04-14 02:03:44 +00:00
|
|
|
"name": "OpenDocument (xml)",
|
2018-07-17 10:46:43 +00:00
|
|
|
"ext": "xml",
|
2018-07-17 18:25:12 +00:00
|
|
|
"to": "opendocument"
|
2018-07-17 10:46:43 +00:00
|
|
|
},
|
2019-04-14 00:58:45 +00:00
|
|
|
{
|
2019-04-14 02:03:44 +00:00
|
|
|
"name": "OpenDocument (texi)",
|
2018-07-17 10:46:43 +00:00
|
|
|
"ext": "texi",
|
2018-07-17 18:25:12 +00:00
|
|
|
"to": "texinfo"
|
2018-07-17 10:46:43 +00:00
|
|
|
},
|
2019-04-14 00:58:45 +00:00
|
|
|
{
|
2019-11-29 12:29:19 +00:00
|
|
|
"name": "LibreOffice Text Document",
|
2019-04-14 02:03:44 +00:00
|
|
|
"ext": "odt",
|
|
|
|
"to": "odt"
|
2018-07-17 10:46:43 +00:00
|
|
|
},
|
2019-04-14 00:58:45 +00:00
|
|
|
{
|
2019-04-14 02:03:44 +00:00
|
|
|
"name": "Microsoft Word (docx)",
|
|
|
|
"ext": "docx",
|
|
|
|
"to": "docx"
|
2018-07-17 10:46:43 +00:00
|
|
|
},
|
2019-04-14 00:58:45 +00:00
|
|
|
{
|
2019-04-14 02:03:44 +00:00
|
|
|
"name": "Rich Text Format",
|
|
|
|
"ext": "rtf",
|
|
|
|
"to": "rtf"
|
2018-07-17 10:46:43 +00:00
|
|
|
},
|
2019-04-14 00:58:45 +00:00
|
|
|
{
|
2019-04-14 02:03:44 +00:00
|
|
|
"name": "Groff Man",
|
|
|
|
"ext": "man",
|
|
|
|
"to": "man"
|
2018-07-17 10:46:43 +00:00
|
|
|
},
|
2019-04-14 00:58:45 +00:00
|
|
|
{
|
2019-04-14 02:03:44 +00:00
|
|
|
"name": "EPUB v3",
|
2018-07-17 10:46:43 +00:00
|
|
|
"ext": "epub",
|
|
|
|
"to": "epub"
|
|
|
|
}
|
2019-04-14 00:58:45 +00:00
|
|
|
]
|
2018-07-17 10:46:43 +00:00
|
|
|
|
2019-04-14 00:58:45 +00:00
|
|
|
def __init__(self, filename):
|
|
|
|
"""Set up the about dialog"""
|
2019-11-04 22:05:03 +00:00
|
|
|
self.builder = Gtk.Builder()
|
|
|
|
self.builder.add_from_resource(
|
|
|
|
"/de/wolfvollprecht/UberWriter/ui/Export.ui")
|
2019-04-14 00:58:45 +00:00
|
|
|
self.dialog = self.builder.get_object("Export")
|
|
|
|
self.stack = self.builder.get_object("export_stack")
|
|
|
|
self.stack_switcher = self.builder.get_object("format_switcher")
|
2019-12-01 03:09:51 +00:00
|
|
|
self.paper_size = self.builder.get_object("combobox_paper_size")
|
2018-07-17 10:46:43 +00:00
|
|
|
|
2019-04-14 00:58:45 +00:00
|
|
|
stack_pdf_disabled = self.builder.get_object("pdf_disabled")
|
|
|
|
filename = filename or _("Untitled document.md")
|
2018-07-17 10:46:43 +00:00
|
|
|
|
2019-04-14 00:58:45 +00:00
|
|
|
self.filechoosers = {export_format: self.stack.get_child_by_name(export_format)
|
2019-04-17 01:10:19 +00:00
|
|
|
for export_format in ["pdf", "html", "advanced"]}
|
2019-04-14 00:58:45 +00:00
|
|
|
for export_format, filechooser in self.filechoosers.items():
|
|
|
|
filechooser.set_do_overwrite_confirmation(True)
|
|
|
|
filechooser.set_current_folder(os.path.dirname(filename))
|
|
|
|
if export_format == "advanced":
|
|
|
|
self.adv_export_name = self.builder.get_object("advanced_export_name")
|
|
|
|
self.adv_export_name.set_text(os.path.basename(filename)[:-3])
|
|
|
|
else:
|
|
|
|
filechooser.set_current_name(os.path.basename(filename)[:-2] + export_format)
|
2018-07-17 10:46:43 +00:00
|
|
|
|
2019-04-14 00:58:45 +00:00
|
|
|
# Disable pdf if Texlive not installed
|
|
|
|
texlive_installed = helpers.exist_executable("pdftex")
|
2018-07-17 10:46:43 +00:00
|
|
|
|
2019-04-14 00:58:45 +00:00
|
|
|
if not texlive_installed:
|
|
|
|
self.filechoosers["pdf"].set_visible(False)
|
|
|
|
stack_pdf_disabled.set_visible(True)
|
|
|
|
stack_pdf_disabled.set_text(disabled_text())
|
|
|
|
stack_pdf_disabled.set_justify(Gtk.Justification.CENTER)
|
|
|
|
self.stack.connect('notify', self.allow_export, 'visible_child_name')
|
2018-07-17 10:46:43 +00:00
|
|
|
|
2019-04-14 00:58:45 +00:00
|
|
|
self.builder.get_object("highlight_style").set_active(0)
|
2018-07-17 10:46:43 +00:00
|
|
|
|
2019-04-14 00:58:45 +00:00
|
|
|
self.builder.get_object("css_filechooser").set_uri(
|
|
|
|
helpers.path_to_file(Theme.get_current().web_css_path))
|
2018-07-17 10:46:43 +00:00
|
|
|
|
2019-04-14 00:58:45 +00:00
|
|
|
format_store = Gtk.ListStore(int, str)
|
|
|
|
for i, fmt in enumerate(self.formats):
|
|
|
|
format_store.append([i, fmt["name"]])
|
|
|
|
self.format_field = self.builder.get_object('choose_format')
|
|
|
|
self.format_field.set_model(format_store)
|
2018-07-17 10:46:43 +00:00
|
|
|
|
2019-04-14 00:58:45 +00:00
|
|
|
format_renderer = Gtk.CellRendererText()
|
|
|
|
self.format_field.pack_start(format_renderer, True)
|
|
|
|
self.format_field.add_attribute(format_renderer, "text", 1)
|
|
|
|
self.format_field.set_active(0)
|
2018-07-17 18:25:12 +00:00
|
|
|
|
2019-04-14 00:58:45 +00:00
|
|
|
def export(self, text=""):
|
|
|
|
"""Export the given text using the specified format.
|
|
|
|
For advanced export, this includes special flags for the enabled options.
|
2018-07-17 18:25:12 +00:00
|
|
|
|
|
|
|
Keyword Arguments:
|
2019-04-14 00:58:45 +00:00
|
|
|
text {str} -- Text to export (default: {""})
|
2018-07-17 18:25:12 +00:00
|
|
|
"""
|
|
|
|
|
2019-04-14 00:58:45 +00:00
|
|
|
export_type = self.stack.get_visible_child_name()
|
2019-12-01 03:09:51 +00:00
|
|
|
args = []
|
2019-04-14 00:58:45 +00:00
|
|
|
if export_type == "advanced":
|
|
|
|
filename = self.adv_export_name.get_text()
|
|
|
|
output_dir = os.path.abspath(self.filechoosers["advanced"].get_current_folder())
|
|
|
|
basename = os.path.basename(filename)
|
|
|
|
|
|
|
|
fmt = self.formats[self.format_field.get_active()]
|
|
|
|
to = fmt["to"]
|
|
|
|
ext = fmt["ext"]
|
2018-07-17 18:25:12 +00:00
|
|
|
|
2019-04-14 00:58:45 +00:00
|
|
|
if self.builder.get_object("html5").get_active() and to == "html":
|
|
|
|
to = "html5"
|
|
|
|
if self.builder.get_object("smart").get_active():
|
|
|
|
to += "+smart"
|
2018-07-17 18:25:12 +00:00
|
|
|
|
2019-12-15 02:26:50 +00:00
|
|
|
args.extend(self.get_advanced_arguments(to, ext))
|
2018-07-17 18:25:12 +00:00
|
|
|
|
2019-04-14 00:58:45 +00:00
|
|
|
else:
|
2019-12-01 03:09:51 +00:00
|
|
|
args = [
|
|
|
|
"--variable=papersize:a4"
|
|
|
|
]
|
2019-04-14 00:58:45 +00:00
|
|
|
filename = self.filechoosers[export_type].get_filename()
|
|
|
|
if filename.endswith("." + export_type):
|
|
|
|
filename = filename[:-len(export_type)-1]
|
|
|
|
output_dir = os.path.abspath(os.path.join(filename, os.path.pardir))
|
|
|
|
basename = os.path.basename(filename)
|
|
|
|
|
|
|
|
to = export_type
|
|
|
|
ext = export_type
|
|
|
|
|
|
|
|
if export_type == "html":
|
|
|
|
to = "html5"
|
|
|
|
args.append("--standalone")
|
|
|
|
args.append("--css=%s" % Theme.get_current().web_css_path)
|
|
|
|
args.append("--mathjax")
|
|
|
|
args.append("--lua-filter=%s" % helpers.get_script_path('relative_to_absolute.lua'))
|
|
|
|
args.append("--lua-filter=%s" % helpers.get_script_path('task-list.lua'))
|
|
|
|
|
2019-12-15 02:26:50 +00:00
|
|
|
|
2019-04-14 00:58:45 +00:00
|
|
|
helpers.pandoc_convert(
|
|
|
|
text, to=to, args=args,
|
|
|
|
outputfile="%s/%s.%s" % (output_dir, basename, ext))
|
|
|
|
|
2019-12-15 02:26:50 +00:00
|
|
|
def get_advanced_arguments(self, to_fmt, ext_fmt):
|
2019-04-14 00:58:45 +00:00
|
|
|
"""Retrieve a list of the selected advanced arguments
|
2018-07-17 18:25:12 +00:00
|
|
|
|
|
|
|
For most of the advanced option checkboxes, returns a list
|
|
|
|
of the related pandoc flags
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
basename {str} -- the name of the file
|
2019-12-15 02:26:50 +00:00
|
|
|
to_fmt {str} -- the format of the export
|
|
|
|
ext_fmt {str} -- the extension of the export
|
2018-07-17 18:25:12 +00:00
|
|
|
|
|
|
|
Returns:
|
|
|
|
list of {str} -- related pandoc flags
|
|
|
|
"""
|
|
|
|
|
|
|
|
highlight_style = self.builder.get_object("highlight_style").get_active_text()
|
|
|
|
|
2019-04-14 00:58:45 +00:00
|
|
|
conditions = [
|
2019-12-01 03:09:51 +00:00
|
|
|
{
|
2019-12-15 02:26:50 +00:00
|
|
|
"condition": to_fmt == "pdf",
|
2019-12-01 03:09:51 +00:00
|
|
|
"yes": "--variable=papersize:" + self.get_paper_size(),
|
|
|
|
"no": None
|
|
|
|
},
|
2019-12-15 02:26:50 +00:00
|
|
|
{
|
|
|
|
"condition": self.get_paper_size() == "a4" and (to_fmt == "odt" or to_fmt == "docx"),
|
|
|
|
"yes": "--reference-doc " + helpers.get_reference_files_path('reference-a4.'+to_fmt) + to_fmt,
|
|
|
|
"no": None
|
|
|
|
},
|
2019-04-14 00:58:45 +00:00
|
|
|
{
|
2018-07-17 18:25:12 +00:00
|
|
|
"condition": self.builder.get_object("toc").get_active(),
|
|
|
|
"yes": "--toc",
|
|
|
|
"no": None
|
|
|
|
},
|
2019-04-14 00:58:45 +00:00
|
|
|
{
|
2018-07-17 18:25:12 +00:00
|
|
|
"condition": self.builder.get_object("highlight").get_active(),
|
|
|
|
"yes": "--highlight-style=%s" % highlight_style,
|
|
|
|
"no": "--no-highlight"
|
|
|
|
},
|
2019-04-14 00:58:45 +00:00
|
|
|
{
|
2018-07-17 18:25:12 +00:00
|
|
|
"condition": self.builder.get_object("standalone").get_active(),
|
|
|
|
"yes": "--standalone",
|
|
|
|
"no": None
|
|
|
|
},
|
2019-04-14 00:58:45 +00:00
|
|
|
{
|
2018-07-17 18:25:12 +00:00
|
|
|
"condition": self.builder.get_object("number_sections").get_active(),
|
|
|
|
"yes": "--number-sections",
|
|
|
|
"no": None
|
|
|
|
},
|
2019-04-14 00:58:45 +00:00
|
|
|
{
|
2018-07-17 18:25:12 +00:00
|
|
|
"condition": self.builder.get_object("strict").get_active(),
|
|
|
|
"yes": "--strict",
|
|
|
|
"no": None
|
|
|
|
},
|
2019-04-14 00:58:45 +00:00
|
|
|
{
|
2018-07-17 18:25:12 +00:00
|
|
|
"condition": self.builder.get_object("incremental").get_active(),
|
|
|
|
"yes": "--incremental",
|
|
|
|
"no": None
|
|
|
|
},
|
2019-04-14 00:58:45 +00:00
|
|
|
{
|
2018-07-17 18:25:12 +00:00
|
|
|
"condition": self.builder.get_object("self_contained").get_active(),
|
|
|
|
"yes": "--self-contained",
|
|
|
|
"no": None
|
|
|
|
}
|
2019-04-14 00:58:45 +00:00
|
|
|
]
|
2018-07-17 10:46:43 +00:00
|
|
|
|
2019-04-14 00:58:45 +00:00
|
|
|
args = []
|
2018-07-17 10:46:43 +00:00
|
|
|
|
2019-04-14 00:58:45 +00:00
|
|
|
args.extend([c["yes"] if c["condition"] else c["no"] for c in conditions])
|
2018-07-17 10:46:43 +00:00
|
|
|
|
2019-04-14 00:58:45 +00:00
|
|
|
args = list(filter(lambda arg: arg is not None, args))
|
2018-07-17 10:46:43 +00:00
|
|
|
|
|
|
|
css_uri = self.builder.get_object("css_filechooser").get_uri()
|
|
|
|
if css_uri:
|
|
|
|
if css_uri.startswith("file://"):
|
|
|
|
css_uri = css_uri[7:]
|
|
|
|
args.append("--css=%s" % css_uri)
|
|
|
|
|
|
|
|
bib_uri = self.builder.get_object("bib_filechooser").get_uri()
|
|
|
|
if bib_uri:
|
|
|
|
if bib_uri.startswith("file://"):
|
|
|
|
bib_uri = bib_uri[7:]
|
|
|
|
args.append("--bibliography=%s" % bib_uri)
|
|
|
|
|
2018-07-17 18:25:12 +00:00
|
|
|
return args
|
2018-07-18 14:24:56 +00:00
|
|
|
|
2019-12-01 03:09:51 +00:00
|
|
|
def get_paper_size(self):
|
|
|
|
paper_size = self.paper_size.get_active_text()
|
|
|
|
|
|
|
|
paper_formats = {
|
|
|
|
"A4": "a4",
|
|
|
|
"US Letter": "letter"
|
|
|
|
}
|
|
|
|
|
|
|
|
return paper_formats[paper_size]
|
|
|
|
|
2018-07-18 14:24:56 +00:00
|
|
|
def allow_export(self, widget, data, signal):
|
|
|
|
"""Disable export button if the visible child is "pdf_disabled"
|
|
|
|
"""
|
|
|
|
|
|
|
|
del widget, data, signal
|
|
|
|
|
|
|
|
export_btn = self.builder.get_object("export_btn")
|
|
|
|
|
|
|
|
if self.stack.get_visible_child_name() == "pdf_disabled":
|
|
|
|
export_btn.set_sensitive(False)
|
|
|
|
else:
|
|
|
|
export_btn.set_sensitive(True)
|
|
|
|
|
|
|
|
def disabled_text():
|
|
|
|
"""Return the TexLive installation instructions
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
{str} -- [TexLive installation instructions]
|
|
|
|
"""
|
|
|
|
|
|
|
|
if os.path.isfile("/.flatpak-info"):
|
|
|
|
text = _("Please, install the TexLive extension from Gnome Software or running\n")\
|
|
|
|
+ ("flatpak install flathub de.wolfvollprecht.UberWriter.Plugin.TexLive")
|
|
|
|
else:
|
|
|
|
text = _("Please, install TexLive from your distribuiton repositories")
|
|
|
|
return text
|