Skip to content

Commit

Permalink
fix loading 404 from resource
Browse files Browse the repository at this point in the history
  • Loading branch information
math2001 committed Jan 6, 2017
1 parent 76d56de commit 057f770
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 10 deletions.
File renamed without changes
1 change: 1 addition & 0 deletions 404.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAGQAAAArCAYAAACO7C3tAAAABGdBTUEAALGOfPtRkwAAACBjSFJNAACHDgAAjBIAAPxoAACFkgAAeeQAAO3tAAA7IgAAIN+/WeXLAAAKqGlDQ1BJQ0MgUHJvZmlsZQAASMetl2dQFOkWhk/35EQaGAEJQ06C5Cg5DqAgGUwMM4QhjOPAkEyoLCq4oqiIgAldJCi4BkDWgIhiWhQT5gVZVNTrYkBUVG4Dl+Huj/1xq+6pOtVPnf76/U73nK/qHQD6Za5IlIrKAaQJM8Qhvh7sqOgYNukpEEEDlMAO9Lm8dJF7cHAg/GN8ugfI+PW26bgW/G8hz49P5wEgwRjH8dN5aRifwLKBJxJnAOD4WF0nK0M0zhswVhRjDWJcOc6Jk3x0nOMmuWNiTViIJ8b3Ach0LlecCED7E6uzM3mJmA4dj7G5kC8QYmyNsQsviYvtQ8fuway0tKXjvA9jw7j/0kn8m2acVJPLTZTy5LtMBNlLkC5K5ebA/zvSUiVTe2hhSU8S+4VgVwXsm1WmLA2QsjBuXtAUC/gT6yc4SeIXPsW8dM+YKeZzvQKmWJIS7j7FXPH0s4IMTtgUi5eGSPWFqfMCpfrxHCnHp3uHTnGCwIczxblJYZFTnCmImDfF6SmhAdNrPKV1sSRE2nOC2Ef6jmnp073xuNN7ZSSF+UnfK97LW9qPMFy6RpThIdURpQZP95zqK62nZ4ZKn83AhmqKk7n+wdM6wdJvAp4gACHEQxpwgQ1+4AWQEZ89PlfguVSUIxYkJmWw3bFTEs/mCHlms9iW5hbYBI6fucmf9MP9ibOEsMjTtdwUALfLAKj3dC0Sm926WgCW2nRN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H2VB8wmo2tZ6PGMsc0qrjdDjCaPBS38GlrZiHomwzC4MGDIyYV/xYSlUAAZ9JEPQhFBwzFKVNPhhXbjKMmeiIFQeECAhczuBOFTyNcNhODgD4416lfv75Vk3gQiFNHW2ECGow2KqCpHL9yjOkG5HlgOmczen8KmDTsP2BfocwRNluvRxCmMBgraaJp06bZHrYFCe4mcKtT3f1VID/HUTI/+zOR6FYIYJPIOTvRFgw0dYa9BIJS0RiCoADqeEcb00zRF8LjIC6hheEViVIggN9+8ENPzmgIE+2iOBiu0g6qmCuCb/iW0JPkJeGv3x8UJSQSrUAAB2OcZ3MLBmD7MYNOQmna0Fb5In5MRLaXC36JGYEIhMmjgZybqJ+VUXhGu/2Cmy6YMK73s9HET3BTBZpES2wcKfxNHe8QBtc6uUnIj3xIp7OC/iTUOO34EilKDGynzqYNbdQnjaCw59zNCgrQYLPHxbanT5/K824KY+AiHBfrOOvG5OXKlSvwI1Y3gOkcHzA23ZTi7/jFmX7+8xtC/AOF/82HDx1fAgAAAABJRU5ErkJggg==
7 changes: 3 additions & 4 deletions MLPApi.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
STYLE_FILE = os.path.join(os.path.dirname(__folder__), 'User',
'MarkdownLivePreview.css')


def plugin_loaded():
global DEFAULT_STYLE_FILE
DEFAULT_STYLE_FILE = sublime.load_resource('Packages/MarkdownLivePreview/'
Expand Down Expand Up @@ -98,10 +99,6 @@ 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()

content = ''.join([line.strip() for line in DEFAULT_STYLE_FILE.splitlines()])
return content + "pre code .space {color: var(--light-bg)}"

Expand Down Expand Up @@ -134,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]
Expand Down
1 change: 1 addition & 0 deletions MarkdownLivePreview.tasks
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ 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
Expand Down
8 changes: 5 additions & 3 deletions functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
import re
from .image_manager import ImageManager

file404 = os.path.join(os.path.dirname(__file__), '404.png')
def plugin_loaded():
global error404
error404 = sublime.load_resource('Packages/MarkdownLivePreview/404.txt')


def replace_img_src_base64(html):
Expand Down Expand Up @@ -37,13 +39,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))])

Expand Down
6 changes: 4 additions & 2 deletions image_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
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%--'
Expand All @@ -22,7 +24,7 @@ def callback(content):
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)

Expand Down
10 changes: 9 additions & 1 deletion sample.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
# 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](C:/users/math/desktop/hello.png)

hello world

This is a test dfsdf hello world, this is pretty cool.

0 comments on commit 057f770

Please sign in to comment.