Compare commits

..

4 Commits

9 changed files with 32 additions and 16 deletions

View File

Before

Width:  |  Height:  |  Size: 5.8 KiB

After

Width:  |  Height:  |  Size: 5.8 KiB

1
404.txt Normal file

File diff suppressed because one or more lines are too long

View File

@ -15,7 +15,12 @@ __folder__ = os.path.dirname(__file__)
STYLE_FILE = os.path.join(os.path.dirname(__folder__), 'User',
'MarkdownLivePreview.css')
DEFAULT_STYLE_FILE = os.path.join(__folder__, 'default.css')
def plugin_loaded():
global DEFAULT_STYLE_FILE
DEFAULT_STYLE_FILE = sublime.load_resource('Packages/MarkdownLivePreview/'
'default.css')
def get_preview_name(md_view):
name = md_view.name() \
@ -94,13 +99,7 @@ def hide_preview(view):
sublime.set_timeout(preview.close(), 250)
def get_style():
if os.path.exists(STYLE_FILE):
with open(STYLE_FILE) as fp:
return fp.read()
with open(DEFAULT_STYLE_FILE) as fp:
content = fp.read()
content = ''.join([line.strip() for line in content.splitlines()])
content = ''.join([line.strip() for line in DEFAULT_STYLE_FILE.splitlines()])
return content + "pre code .space {color: var(--light-bg)}"
def show_html(md_view, preview):
@ -132,6 +131,8 @@ def show_html(md_view, preview):
# set viewport position
# sublime.set_clipboard(html)
return
# 0 < y < 1
y = md_view.text_to_layout(md_view.sel()[0].begin())[1] / md_view.layout_extent()[1]

View File

@ -19,7 +19,7 @@ class MarkdownLivePreviewListener(sublime_plugin.EventListener):
preview = get_view_from_id(window, id)
if id is None or preview is None:
preview = create_preview(view)
sublime.set_timeout(lambda: show_html(view, preview), 1000)
sublime.set_timeout_async(lambda: show_html(view, preview), 1000)
else:
show_html(view, preview)

View File

@ -3,11 +3,13 @@ Fast:
☐ sync scroll @needsUpdate(because of images)
☐ cache image in object when used, so that it's faster @needsTest
☐ call settings listener on_new too - might be too heavy
☐ add clear cache command
Medium:
☐ auto refresh preview if loading images
☐ use alt attribute for 404 error
☐ use MarkdownLivePreview syntax, so we can use syntax's settings
☐ listen for settings to change
Long:
☐ fix #4 @high

View File

@ -5,7 +5,10 @@ import sublime
import re
from .image_manager import ImageManager
file404 = os.path.join(os.path.dirname(__file__), '404.png')
def plugin_loaded():
global error404, loading
loading = sublime.load_resource('Packages/MarkdownLivePreview/loading.txt')
error404 = sublime.load_resource('Packages/MarkdownLivePreview/404.txt')
def replace_img_src_base64(html):
@ -23,7 +26,7 @@ def replace_img_src_base64(html):
if ''.join(path).startswith(tuple(get_settings().get('load_from_internet'
'_when_starts'))):
image = ImageManager.get(''.join(path))
image = image or to_base64('loading.png')
image = image or loading
else:
# local image
@ -37,13 +40,13 @@ def is_markdown_view(view):
def to_base64(path=None, content=None):
if path is None and content is None:
return to_base64(file404)
return error404
elif content is None and path is not None:
try:
with open(path, 'rb') as fp:
content = fp.read()
except (FileNotFoundError, OSError):
return to_base64(file404)
return error404
return 'data:image/png;base64,' + ''.join([chr(el) for el in list(base64.standard_b64encode(content))])

View File

@ -5,8 +5,10 @@ from threading import Thread
import urllib.request, urllib.error
import sublime
from .functions import *
import tempfile
CACHE_FILE = os.path.join(os.path.dirname(__file__), 'cache.txt')
CACHE_FILE = os.path.join(tempfile.gettempdir(),
'MarkdownLivePreviewCache.txt')
TIMEOUT = 20 # seconds
SEPARATOR = '---%cache%--'
@ -22,7 +24,7 @@ def get_base64_saver(loading, url):
content.reason.strerror == 'getaddrinfo failed'):
loading[url] = 404
return
return sublime.error_message('An unexpected error has occured: ' +
return sublime.error_message('An unexpected error has occured: ' +
str(content))
loading[url] = to_base64(content=content)

1
loading.txt Normal file
View File

@ -0,0 +1 @@
data:image/png;base64,R0lGODlhZABkAPU+AAAAAAwNDRweHyYpKzg8Pzo+QUBFSERJTEdMT05UV1NYXFVbX1hfY1lfZGFobWJpbmhvdGxzeHF5fnJ6gHV9g3Z+hHqDiXuEin+IjoCIjoKLkYKMkoSNk4eQl4iSmIqTmouUm42XnY+ZoJKco5OdpJOepZSeppahqJeiqZmjqpumrZ6psJ+qsqOutqSvt6SwuKezu6i0vKm1vay4wK66wq+7w6+8xLK+xrK/x7TAybXCy7bDy7jEzbjFzrzJ0gAAACH5BAUAAD4AIf8LTkVUU0NBUEUyLjADAQAAACwAAAAAZABkAAAG/kCfcEgsGo/IpHLJbDqf0Kh0Sq1ar9isdsvter/gsHhMLpvP6LR6zW673/C4fE6v2+/4vH7P7/v/gIGCg4SFhoeIiW8IAAAUilUpjQABkEsmMUchkwBIOTOQBQICGUabk0ctFhYdiSajAgOZRKeNRjkYqxaghyuwAgxFtZ1FJBe6NokHvya0nEUzuhYgijG/B86oRCDSOZAPv6VCw0SquiiWNwOwAzfjz0I8uasYPIMvDQ0kRhm/Ee/afKiQ1sIIDBAgkuUxQKDhA29ERMHK9GJSJR85pLUiwkOELgx6Goo0sG/IK1gVhCig9MjHimOreAmBMU+XngciRTrAMSQB/qxmR6KtEjGko7Shey7kbGgA6A0GBz4oOUjCno8YNXWp6NOCwVICD6UYPQqiBiANDHNOkILiqIYVg2Y0yPlAikddICASQtuwJJQY9OAimqFCZpRPei0pPnKjg4fHkB936JBYyg4VmDNrVlH5zYMFoEOLZgDBSoejqDfQEc1atBXUsOl8bi26bpUNsKWpnlPjg+PIj32brZJjs/HOi5PjiMFzCo4ZyAWpqCBhwgspMFa9NZRjg4TvEjZCEQFzWvQ9KiiA/+73SVtpGAT7mcFh/XcPVqH0uCsNhDs+J9gnAQXX+cADCSDMggRVVtGE2lZ6fCAgfkPcdYFhRAhlAVHxxfCnC4d42EdghtII1hYGLgjxki6GOSiNHtR990F+QpymizcZ0SNEjquI1+FHetDHQYFEuCANhBpaMMRAuqRYxEEJDSLPR1YlWVRN9Vjy3ioFCWHlEC6Uh44iOcB0gQck2kSEB90o4sEFx1yY5irQ9JdIDdIANcSXRBiDzGAfVcbnELiwmEgHx3Q5p5JGmOPjIdAF9eIRnyRnhA1AWvqEn4pq6umnoIYq6qiklmrqqaimquqqrLbq6quwxirrrLTWauuttwYBADs=

View File

@ -1,7 +1,13 @@
# DuckDuckGo - The Search engine you'll fall in love with
# DuckDuckGo - The Search engine you'll fall in love with this is a test.
This is a test, and this is pretty cool!
![image](http://afterishtar.pl/images/100x100.gif)
Hope you'll enjoy using MarkdownLivePreview!
![image](https://forum.sublimetext.com/uploads/st-forum-wide.png)
![image](http://local.dev/tests/php/img/image-php.php)
this is a tets