Compare commits

..

1 Commits

Author SHA1 Message Date
8eb0172eb4 improve replace_img_src_base64: use bs4 2017-01-26 18:32:08 +11:00
4 changed files with 25 additions and 30 deletions

View File

@ -63,15 +63,17 @@ def markdown2html(md, basepath):
# I personaly edited the file (markdown2.py:1743)
html = strip_html_comments(html)
# Beautiful uses the <br/> but the sublime phantoms do not support them...
# exception, again, because <pre> aren't supported by the phantoms
# so, because this is monosaped font, I just replace it with a '.' and make transparent ;)
html = html.replace('&nbsp;', '<i class="space">.</i>')
# Phantoms have problem with images size when they're loaded from an url/path
# So, the solution is to convert them to base64
html = replace_img_src_base64(html, basepath=os.path.dirname(basepath))
# BeautifulSoup uses the <br/> but the sublime phantoms do not support them...
html = html.replace('<br/>', '<br />')
html = html.replace('&nbsp;', '&nbspespace;') # save where are the spaces
# exception, again, because <pre> aren't supported by the phantoms
html = html.replace('&nbspespace;', '<i class="space">.</i>')
html = replace_img_src_base64(html, basepath=os.path.dirname(basepath))
sublime.set_clipboard(html)
return html
def show_html(md_view, preview):

View File

@ -1,5 +1,6 @@
Fast:
☐ cache image in object when used, so that it's faster @needsTest
☐ add settings to keep md view open #13
Medium:
☐ auto refresh preview if loading images

View File

@ -19,29 +19,21 @@ def strip_html_comments(html):
def replace_img_src_base64(html, basepath):
"""Really messy, but it works (should be updated)"""
index = -1
tag_start = '<img src="'
shtml, html = html, list(html)
while True:
index = shtml.find(tag_start, index + 1)
if index == -1:
break
path, end = get_content_till(html, '"', start=index + len(tag_start))
if ''.join(path).startswith('data:image/'):
continue
if ''.join(path).startswith(tuple(get_settings().get('load_from_internet' + \
'_when_starts', []))):
image = ImageManager.get(''.join(path))
image = image or loading
else:
# local image
path = ''.join(path)
path = os.path.join(basepath, path)
image = to_base64(path)
html[index+len(tag_start):end] = image
shtml = ''.join(html)
return ''.join(html)
soup = BeautifulSoup(html)
load_from_internet_starters = get_settings().get('load_from_internet_when_starts')
for img in soup.find_all('img'):
if img['src'].startswith('data:image/'):
continue
elif img['src'].startswith(tuple(load_from_internet_starters)):
image = ImageManager.get(img['src']) or loading
else: # this is a local image
image = to_base64(os.path.join(basepath, src))
img['src'] = image
return str(soup)
def is_markdown_view(view):
return 'markdown' in view.scope_name(0)

View File

@ -26,7 +26,7 @@ if you is moods.curious:
| 45 | John |
| `<table>` | `><` |
[Sublime Text Logo](https://upload.wikimedia.org/wikipedia/en/4/4c/Sublime_Text_Logo.png)
![Sublime Text Logo](https://upload.wikimedia.org/wikipedia/en/4/4c/Sublime_Text_Logo.png)
Some plugin I just *need*: