Abstract the path base of open files

gh-pages
somas95 2018-05-06 20:00:14 +02:00
parent 132b3d6022
commit e0c4854613
3 changed files with 30 additions and 7 deletions

View File

@ -13,6 +13,13 @@
asked to install them manually.
</description>
</key>
<key name='open-file-path' type='s'>
<default>"/tmp"</default>
<summary>Open file base path</summary>
<description>
Open file paths of the current session
</description>
</key>
</schema>

View File

@ -14,6 +14,7 @@
# with this program. If not, see <http://www.gnu.org/licenses/>.
### END LICENSE
import os
import re
import http.client
import urllib
@ -28,6 +29,7 @@ from pprint import pprint
from gi.repository import Gtk, Gdk, GdkPixbuf, GObject
from uberwriter_lib import LatexToPNG
from .Settings import Settings
from .FixTable import FixTable
@ -276,6 +278,7 @@ class UberwriterInlinePreview():
self.TextView.connect_after('popup-menu', self.move_popup)
self.TextView.connect('button-press-event', self.click_move_button)
self.popover = None
self.settings = Settings.new()
def open_popover_with_widget(self, widget):
a = self.TextBuffer.create_child_anchor(self.TextBuffer.get_iter_at_mark(self.ClickMark))
@ -363,7 +366,7 @@ class UberwriterInlinePreview():
link = MarkupBuffer.regex["LINK"]
footnote = re.compile('\[\^([^\s]+?)\]')
image = re.compile("!\[(.+?)\]\((.+?)\)")
image = re.compile("!\[(.*?)\]\((.+?)\)")
buf = self.TextBuffer
context_offset = 0
@ -457,6 +460,11 @@ class UberwriterInlinePreview():
path = match.group(2)
if path.startswith("file://"):
path = path[7:]
elif not path.startswith("/"):
# then the path is relative
base_path = self.settings.get_value("open-file-path").get_string()
path = base_path + "/" + path
logger.info(path)
pb = GdkPixbuf.Pixbuf.new_from_file_at_size(path, 400, 300)
image = Gtk.Image.new_from_pixbuf(pb)

View File

@ -378,7 +378,7 @@ class UberwriterWindow(Window):
f.write(self.get_text())
f.close()
self.filename = filename
self.set_filename(filename)
self.set_headerbar_title(os.path.basename(filename) + self.title_end)
self.did_change = False
@ -416,7 +416,7 @@ class UberwriterWindow(Window):
f.write(self.get_text())
f.close()
self.filename = filename
self.set_filename(filename)
self.set_headerbar_title(os.path.basename(filename) + self.title_end)
try:
@ -594,7 +594,7 @@ class UberwriterWindow(Window):
self.TextEditor.redos = []
self.did_change = False
self.filename = None
self.set_filename()
self.set_headerbar_title("New File" + self.title_end)
def menu_activate_focusmode(self, widget=None):
@ -709,7 +709,7 @@ class UberwriterWindow(Window):
os.environ['PANDOC_PREFIX'] = base_path + '/'
# Set the styles according the color theme
if self.settings.get_value("dark-mode"):
if Settings.get_value("dark-mode"):
stylesheet = helpers.get_media_path('uberwriter_dark.css')
else:
stylesheet = helpers.get_media_path('uberwriter.css')
@ -806,7 +806,7 @@ class UberwriterWindow(Window):
self.set_headerbar_title(os.path.basename(filename) + self.title_end)
self.TextEditor.undo_stack = []
self.TextEditor.redo_stack = []
self.filename = filename
self.set_filename(filename)
except Exception as e:
logger.warning("Error Reading File: %r" % e)
@ -1108,7 +1108,7 @@ class UberwriterWindow(Window):
self.text_change_event = self.TextBuffer.connect('changed', self.text_changed)
# Init file name with None
self.filename = None
self.set_filename()
self.generate_recent_files_menu(self.builder.get_object('recent'))
@ -1241,6 +1241,14 @@ class UberwriterWindow(Window):
self.hb.props.title = title
self.set_title(title)
def set_filename(self, filename=None):
if filename:
self.filename = filename
base_path = os.path.dirname(self.filename)
else:
self.filename = None
base_path = "/"
self.settings.set_value("open-file-path", GLib.Variant("s", base_path))
def save_settings(self):
if not os.path.exists(CONFIG_PATH):
try: